diff --git a/module-common/src/main/java/org/jeecg/api/service/impl/AppletIndexServiceImpl.java b/module-common/src/main/java/org/jeecg/api/service/impl/AppletIndexServiceImpl.java index 2e06984..b1165ab 100644 --- a/module-common/src/main/java/org/jeecg/api/service/impl/AppletIndexServiceImpl.java +++ b/module-common/src/main/java/org/jeecg/api/service/impl/AppletIndexServiceImpl.java @@ -16,8 +16,8 @@ import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.jeecg.api.bean.FansPageBean; import org.jeecg.api.bean.HttpClientUtil; import org.jeecg.api.bean.PageBean; -import org.jeecg.api.bean.WxQrCodeVo; import org.jeecg.api.service.AppletIndexService; +import org.jeecg.api.wxUtils.WxQrCodeVo; import org.jeecg.common.api.vo.Result; import org.jeecg.common.util.RedisUtil; import org.jeecg.config.shiro.ShiroRealm; diff --git a/module-common/src/main/java/org/jeecg/api/wxUtils/CreateOrderReq.java b/module-common/src/main/java/org/jeecg/api/wxUtils/CreateOrderReq.java new file mode 100644 index 0000000..ee6b59d --- /dev/null +++ b/module-common/src/main/java/org/jeecg/api/wxUtils/CreateOrderReq.java @@ -0,0 +1,8 @@ +package org.jeecg.api.wxUtils; + +public class CreateOrderReq { + /** + * 微信用户openid(前端不用传参) + */ + private String wxOpenId; +} diff --git a/module-common/src/main/java/org/jeecg/api/wxUtils/HttpClientUtil.java b/module-common/src/main/java/org/jeecg/api/wxUtils/HttpClientUtil.java new file mode 100644 index 0000000..62721c4 --- /dev/null +++ b/module-common/src/main/java/org/jeecg/api/wxUtils/HttpClientUtil.java @@ -0,0 +1,1120 @@ +package org.jeecg.api.wxUtils; + + +import com.alibaba.fastjson.JSON; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.HttpStatus; +import org.apache.http.NameValuePair; +import org.apache.http.client.CookieStore; +import org.apache.http.client.entity.UrlEncodedFormEntity; +import org.apache.http.client.methods.*; +import org.apache.http.client.utils.HttpClientUtils; +import org.apache.http.client.utils.URIBuilder; +import org.apache.http.conn.ssl.SSLConnectionSocketFactory; +import org.apache.http.conn.ssl.SSLContextBuilder; +import org.apache.http.conn.ssl.TrustStrategy; +import org.apache.http.entity.ContentType; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.BasicCookieStore; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.message.BasicNameValuePair; +import org.apache.http.util.EntityUtils; + +import javax.net.ssl.SSLContext; +import java.io.IOException; +import java.net.URI; +import java.nio.charset.Charset; +import java.security.KeyManagementException; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; +import java.security.cert.CertificateException; +import java.security.cert.X509Certificate; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * 通过url获取数据 + * + * @author zc + * + */ +public class HttpClientUtil { + + /** + * 设置可访问https + * @return + */ + public static CloseableHttpClient createSSLClientDefault() { + try { + SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() { + //信任所有 + public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { + return true; + } + }).build(); + SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); + + return HttpClients.custom().setSSLSocketFactory(sslsf).build(); + } catch (KeyManagementException e) { + e.printStackTrace(); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } catch (KeyStoreException e) { + e.printStackTrace(); + } + return HttpClients.createDefault(); + } +/*************************************Get**********************************************/ + /** + * 一般查询用doget + * @param url + * @param param + * @param + * @return + */ + public static String doGet(String url, Map param) { + // 创建Httpclient对象 + CloseableHttpClient httpclient = createSSLClientDefault();//调用createSSLClientDefault + String resultString = ""; + CloseableHttpResponse response = null; + try { + + // 创建uri + URIBuilder builder = new URIBuilder(url); + if (param != null) { + for (String key : param.keySet()) { + builder.addParameter(key, param.get(key)); + } + } + URI uri = builder.build(); + CookieStore cookieStore = new BasicCookieStore(); + httpclient = HttpClients.custom().setDefaultCookieStore(cookieStore).build(); + // 创建http GET请求 + HttpGet httpGet = new HttpGet(uri); + //设置请求头 +// if (parameter != null) { +// //签名 +// httpGet.addHeader("Sign",parameter.getSign()); +// //用户ID +// httpGet.addHeader("User_ID",parameter.getUserId()+""); +// //用户角色ID +// httpGet.addHeader("User_RID",parameter.getRId()); +// //用户单位ID +// httpGet.addHeader("Dept_ID",parameter.getDeptId()+""); +// //用户科室ID +// httpGet.addHeader("Unit_ID",parameter.getUnitid()+""); +// httpGet.addHeader("CacheKey",parameter.getCacheKey()); +// httpGet.addHeader("Org_id", parameter.getOrg_id() + ""); +// httpGet.addHeader("Product_id", parameter.getProduct_id() + ""); +// httpGet.addHeader("Timestamp", parameter.getTimestamp()); +// } + + // 执行请求 + response = httpclient.execute(httpGet); + // 判断返回状态是否为200 + if (response.getStatusLine().getStatusCode() == 200) { + resultString = EntityUtils.toString(response.getEntity(), + "UTF-8"); + } + + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + if (response != null) { + response.close(); + } + httpclient.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + return resultString; + } + + public static String doGet(String url) { + return doGet(url, null); + } + + public static String doGet3(String url, Map param) { + // 创建Httpclient对象 + //调用createSSLClientDefault + CloseableHttpClient httpclient = createSSLClientDefault(); + String resultString = ""; + CloseableHttpResponse response = null; + try { + + // 创建uri + URIBuilder builder = new URIBuilder(url); + if (param != null) { + for (String key : param.keySet()) { + builder.addParameter(key, param.get(key)); + } + } + URI uri = builder.build(); + + // 创建http GET请求 + HttpGet httpGet = new HttpGet(uri); + //设置请求头 +// if (parameter != null) { +// //签名 +// httpGet.addHeader("Sign",parameter.getSign()); +// //用户ID +// httpGet.addHeader("User_ID",parameter.getUserId()+""); +// //用户角色ID +// httpGet.addHeader("User_RID",parameter.getRId()); +// //用户单位ID +// httpGet.addHeader("Dept_ID",parameter.getDeptId()+""); +// //用户科室ID +// httpGet.addHeader("Unit_ID",parameter.getUnitid()+""); +// httpGet.addHeader("CacheKey",parameter.getCacheKey()); +// httpGet.addHeader("Org_id", parameter.getOrg_id() + ""); +// httpGet.addHeader("Product_id", parameter.getProduct_id() + ""); +// httpGet.addHeader("Timestamp", parameter.getTimestamp()); +// } + + // 执行请求 + response = httpclient.execute(httpGet); + // 判断返回状态是否为200 + if (response.getStatusLine().getStatusCode() == 200) { + resultString = EntityUtils.toString(response.getEntity(), + "UTF-8"); + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + if (response != null) { + response.close(); + } + httpclient.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + return resultString; + } + + /** + * 上传文件的doGet + * @param url + * @param param + * @param + * @return + */ + public static String doGet2(String url, Map param) { + + // 创建Httpclient对象 + CloseableHttpClient httpclient = createSSLClientDefault(); + String resultString = ""; + CloseableHttpResponse response = null; + try { + + // 创建uri + URIBuilder builder = new URIBuilder(url); + if (param != null) { + for (String key : param.keySet()) { + builder.addParameter(key, param.get(key)); + } + } + URI uri = builder.build(); + // 创建http GET请求 + HttpGet httpGet = new HttpGet(uri); +// if (parameter != null) { +// //用户ID +// httpGet.addHeader("User_ID",parameter.getUserId()+""); +// //用户角色ID +// httpGet.addHeader("User_RID",parameter.getRId()); +// //用户单位ID +// httpGet.addHeader("Dept_ID",parameter.getDeptId()+""); +// //用户科室ID +// httpGet.addHeader("Unit_ID",parameter.getUnitid()+""); +// httpGet.addHeader("CacheKey",parameter.getCacheKey()); +// httpGet.addHeader("Org_id", parameter.getOrg_id()); +// httpGet.addHeader("Product_id", parameter.getProduct_id()); +// httpGet.addHeader("Sign", parameter.getSign()); +// httpGet.addHeader("requestCode", parameter.getRequestCode()); +// httpGet.addHeader("Content-Type", "application/json;charset=UTF-8"); +// httpGet.addHeader("Timestamp", parameter.getTimestamp()); +// } + // 执行请求 + response = httpclient.execute(httpGet); + if (response.getStatusLine().getStatusCode() == 200) { + resultString = EntityUtils.toString(response.getEntity(), + "UTF-8"); + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + if (response != null) { + response.close(); + } + httpclient.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + return resultString; + } + + public static String doGet2(String url) { + return doGet2(url, null); + + } + + public static String doGet4(String url, Map param) { + // 创建Httpclient对象 + //调用createSSLClientDefault + CloseableHttpClient httpclient = createSSLClientDefault(); + String resultString = ""; + CloseableHttpResponse response = null; + try { + + // 创建uri + URIBuilder builder = new URIBuilder(url); + if (param != null) { + for (String key : param.keySet()) { + builder.addParameter(key, param.get(key)); + } + } + URI uri = builder.build(); + + // 创建http GET请求 + HttpGet httpGet = new HttpGet(uri); + //设置请求头 +// httpGet.addHeader(""); +// if (parameter != null) { +// //签名 +// httpGet.addHeader("Sign",parameter.getSign()); +// //用户ID +// httpGet.addHeader("User_ID",parameter.getUserId()+""); +// //用户角色ID +// httpGet.addHeader("User_RID",parameter.getRId()); +// //用户单位ID +// httpGet.addHeader("Dept_ID",parameter.getDeptId()+""); +// //用户科室ID +// httpGet.addHeader("Unit_ID",parameter.getUnitid()+""); +// httpGet.addHeader("CacheKey",parameter.getCacheKey()); +// httpGet.addHeader("Org_id", parameter.getOrg_id() + ""); +// httpGet.addHeader("Product_id", parameter.getProduct_id() + ""); +// httpGet.addHeader("Timestamp", parameter.getTimestamp()); +// } + + // 执行请求 + response = httpclient.execute(httpGet); + // 判断返回状态是否为200 + if (response.getStatusLine().getStatusCode() == 200) { + resultString = EntityUtils.toString(response.getEntity(), + "UTF-8"); + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + if (response != null) { + response.close(); + } + httpclient.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + return resultString; + } + + public static String doGet5(String url, Map param) { + // 创建Httpclient对象 + //调用createSSLClientDefault + CloseableHttpClient httpclient = createSSLClientDefault(); + String resultString = ""; + CloseableHttpResponse response = null; + try { + + // 创建uri + URIBuilder builder = new URIBuilder(url); + if (param != null) { + for (String key : param.keySet()) { + builder.addParameter(key, param.get(key)); + } + } + URI uri = builder.build(); + + // 创建http GET请求 + HttpGet httpGet = new HttpGet(uri); + //设置请求头 +// httpGet.addHeader(""); +// if (parameter != null) { +// httpGet.addHeader("Accept","application/json, text/javascript, */*; q=0.01"); +// httpGet.addHeader("Accept-Encoding","gzip, deflate, br"); +// httpGet.addHeader("Accept-Language","zh-CN,zh;q=0.9"); +// httpGet.addHeader("Connection","keep-alive"); + httpGet.addHeader("Cookie","PHPSESSID=b1epvn4gilmvlqd63gb71sgb4m"); +// httpGet.addHeader("Host","www.5577yc.com"); +// httpGet.addHeader("Referer","https://www.5577yc.com/pc/member/index.html"); +// httpGet.addHeader("Sec-Fetch-Mode","cors"); +// httpGet.addHeader("Sec-Fetch-Site","same-origin"); +// httpGet.addHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36"); + httpGet.addHeader("X-Requested-With","XMLHttpRequest"); +// httpGet.addHeader("Org_id", parameter.getOrg_id() + ""); +// httpGet.addHeader("Product_id", parameter.getProduct_id() + ""); +// httpGet.addHeader("Timestamp", parameter.getTimestamp()); +// } + + // 执行请求 + response = httpclient.execute(httpGet); + // 判断返回状态是否为200 + if (response.getStatusLine().getStatusCode() == 200) { + resultString = EntityUtils.toString(response.getEntity(), + "UTF-8"); + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + if (response != null) { + response.close(); + } + httpclient.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + return resultString; + } + + public static String doGet5(String url) { + return doGet5(url, null); + + } + /*************************************Get**********************************************/ + + /*************************************Post**********************************************/ + /** + * 原始doPost 基本不用 + * @param url + * @param param + * @param + * @return + */ + public static String doPost(String url, Map param) { + // 创建Httpclient对象 + CloseableHttpClient httpClient = createSSLClientDefault(); + CloseableHttpResponse response = null; + String resultString = ""; + + try { + // 创建Http Post请求 + HttpPost httpPost = new HttpPost(url); + + // 创建参数列表 + if (param != null) { + List paramList = new ArrayList<>(); + for (String key : param.keySet()) { + paramList.add(new BasicNameValuePair(key, param.get(key))); + } + // 模拟表单 + UrlEncodedFormEntity entity = new UrlEncodedFormEntity( + paramList); + httpPost.setEntity(entity); + } + // 执行http请求 + response = httpClient.execute(httpPost); + + + + + resultString = EntityUtils.toString(response.getEntity(), "utf-8"); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + response.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + return resultString; + } + + /** + * 项目经常用的doPost2 + * @param url + * @param param + * @param + * @return + */ + public static String doPost2(String url, Map param){ + + // 创建Httpclient对象 + CloseableHttpClient httpClient = createSSLClientDefault(); + CloseableHttpResponse response = null; + String resultString = ""; + + try { + // 创建uri + URIBuilder builder = new URIBuilder(url); + if (param != null) { + for (String key : param.keySet()) { + builder.addParameter(key, param.get(key)); + } + } + URI uri = builder.build(); + // 创建Http Post请求 + HttpPost httpPost = new HttpPost(uri); + httpPost.setHeader("Content-type", "application/x-www-form-urlencoded"); +// httpPost.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); + // 创建参数列表 + if (param != null) { + List paramList = new ArrayList<>(); + for (String key : param.keySet()) { + paramList.add(new BasicNameValuePair(key, param.get(key))); + } + + // 模拟表单 + UrlEncodedFormEntity entity = new UrlEncodedFormEntity( + paramList,"UTF-8"); + entity.setContentEncoding("UTF-8"); + entity.setContentType("application/x-www-form-urlencoded"); + httpPost.setEntity(entity); + + } + // 执行http请求 + response = httpClient.execute(httpPost); + resultString = EntityUtils.toString(response.getEntity(), "utf-8"); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + response.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + return resultString; + } + + /** + * .net登录post + * @param url + * @param param + * @param + * @return + */ + public static String doPost3(String url, Map param){ + + // 创建Httpclient对象 + CloseableHttpClient httpClient = createSSLClientDefault(); + CloseableHttpResponse response = null; + String resultString = ""; + + try { + // 创建uri + URIBuilder builder = new URIBuilder(url); + if (param != null) { + for (String key : param.keySet()) { + builder.addParameter(key, param.get(key)); + } + } + URI uri = builder.build(); + // 创建Http Post请求 + HttpPost httpPost = new HttpPost(uri); + // 创建参数列表 + if (param != null) { + List paramList = new ArrayList<>(); + for (String key : param.keySet()) { + paramList.add(new BasicNameValuePair(key, param.get(key))); + } + // 模拟表单 + UrlEncodedFormEntity entity = new UrlEncodedFormEntity( + paramList, Charset.forName("UTF-8")); + entity.setContentEncoding("UTF-8"); + httpPost.setEntity(entity); + } + // 执行http请求 + response = httpClient.execute(httpPost); + resultString = EntityUtils.toString(response.getEntity(), "utf-8"); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + response.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + return resultString; + } + + + + /** + * 微信文件上传 + * @param url + * @param content + * @param fileName + * @return + */ + public static String doPost6(String url, byte[] content,String fileName){ + + String resultString = null; + //创建HttpClient + CloseableHttpClient httpClient = HttpClients.createDefault(); + HttpPost httpPost = new HttpPost(url); + org.apache.http.entity.mime.MultipartEntityBuilder builder = org.apache.http.entity.mime.MultipartEntityBuilder.create(); + /*绑定文件参数,传入文件流和contenttype,此处也可以继续添加其他formdata参数*/ + builder.addBinaryBody("file",content, ContentType.MULTIPART_FORM_DATA,fileName); + HttpEntity entity = builder.build(); + httpPost.setEntity(entity); + HttpResponse response = null; + //执行提交 + try{ + response = httpClient.execute(httpPost); + resultString = EntityUtils.toString(response.getEntity(), "utf-8"); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + HttpClientUtils.closeQuietly(httpClient); + HttpClientUtils.closeQuietly(response); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + return resultString; + } + + + /** + * 微信公众号文件上传 + * @param url + * @param content + * @param fileName + * @return + */ + public static String doPost7(String url, byte[] content,String fileName){ + + String resultString = null; + //创建HttpClient + CloseableHttpClient httpClient = HttpClients.createDefault(); + HttpPost httpPost = new HttpPost(url); + org.apache.http.entity.mime.MultipartEntityBuilder builder = org.apache.http.entity.mime.MultipartEntityBuilder.create(); + /*绑定文件参数,传入文件流和contenttype,此处也可以继续添加其他formdata参数*/ + builder.addBinaryBody("media",content, ContentType.MULTIPART_FORM_DATA,fileName); + HttpEntity entity = builder.build(); + httpPost.setEntity(entity); + HttpResponse response = null; + //执行提交 + try{ + response = httpClient.execute(httpPost); + resultString = EntityUtils.toString(response.getEntity(), "utf-8"); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + HttpClientUtils.closeQuietly(httpClient); + HttpClientUtils.closeQuietly(response); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + return resultString; + } + + /** + * 项目经常用的doPost4 + * @param url + * @param param + * @return + */ + public static String doPost4(String url, Map param){ + + // 创建Httpclient对象 + CloseableHttpClient httpClient = createSSLClientDefault(); + CloseableHttpResponse response = null; + String resultString = ""; + StringBuffer cookie = new StringBuffer(); + Map map = new HashMap<>(); + try { + // 创建uri + URIBuilder builder = new URIBuilder(url); + if (param != null) { + for (String key : param.keySet()) { + builder.addParameter(key, param.get(key)); + } + } + URI uri = builder.build(); + // 创建Http Post请求 + HttpPost httpPost = new HttpPost(uri); + // 创建参数列表 + if (param != null) { + List paramList = new ArrayList<>(); + for (String key : param.keySet()) { + paramList.add(new BasicNameValuePair(key, param.get(key))); + } + httpPost.addHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8"); + // 模拟表单 + UrlEncodedFormEntity entity = new UrlEncodedFormEntity( + paramList, Charset.forName("UTF-8")); + httpPost.setEntity(entity); + } + // 执行http请求 + response = httpClient.execute(httpPost); + resultString = EntityUtils.toString(response.getEntity(), "utf-8"); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + response.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + return resultString; + } + + + /** + * 项目经常用的doPost4 + * @param url + * @param param + * @param parameter + * @return + */ + public static String doPost5(String url, Map param,String parameter){ + + // 创建Httpclient对象 + CloseableHttpClient httpClient = createSSLClientDefault(); + CloseableHttpResponse response = null; + String resultString = ""; + StringBuffer cookie = new StringBuffer(); + Map map = new HashMap<>(); + try { + // 创建uri + CookieStore cookieStore = new BasicCookieStore(); + httpClient = HttpClients.custom().setDefaultCookieStore(cookieStore).build(); + URIBuilder builder = new URIBuilder(url); + if (param != null) { + for (String key : param.keySet()) { + builder.addParameter(key, param.get(key)); + } + } + URI uri = builder.build(); + // 创建Http Post请求 + HttpPost httpPost = new HttpPost(uri); + // 创建参数列表 + if (param != null) { + List paramList = new ArrayList<>(); + for (String key : param.keySet()) { + paramList.add(new BasicNameValuePair(key, param.get(key))); + } + httpPost.addHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8"); + httpPost.addHeader("Host","www.www7945.com"); + httpPost.addHeader("Origin","https://www.www7945.com"); + httpPost.addHeader("Referer","https://www.www7945.com/mobile/"); + httpPost.addHeader("User-Agent:","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"); + httpPost.addHeader("Connection","keep-alive"); + // 模拟表单 + UrlEncodedFormEntity entity = new UrlEncodedFormEntity( + paramList, Charset.forName("UTF-8")); + entity.setContentEncoding("UTF-8"); + httpPost.setEntity(entity); + } + if(parameter!=null){ + httpPost.addHeader("Cookie",parameter); + } + // 执行http请求 + response = httpClient.execute(httpPost); + String tokenStr = null; + + if (response != null) { + int statusCode = response.getStatusLine().getStatusCode(); + if (statusCode == HttpStatus.SC_OK) { + // 获得Cookies + List cookies = cookieStore.getCookies(); + for (org.apache.http.cookie.Cookie c : cookies) { + cookie.append(c.getName()).append("=").append(c.getValue()).append(";"); + if (c.getName().contains("token")) { + tokenStr = c.getValue(); + } + } + } + } + map.put("token",tokenStr); + map.put("cookie",cookie.toString()); + resultString = EntityUtils.toString(response.getEntity(), "utf-8"); + map.put("resultString",JSON.parse(resultString)); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + response.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + if(parameter!=null){ + return resultString; + } + return JSON.toJSONString(map); + } + + + + public static String doPost5(String url) { + return doPost5(url, null,null); + } + + public static String doPost4(String url) { + return doPost4(url, null); + } + + public static String doPost2(String url) { + return doPost2(url); + } + + + public static String doPost(String url) { + return doPost(url, null); + } + + /** + * 当数据需要以JSON格式传输 + * @param url + * @param json + * @param + * @return + */ + public static String doPostJson(String url, String json) { + // 创建Httpclient对象 + CloseableHttpClient httpClient = createSSLClientDefault(); + CloseableHttpResponse response = null; + String resultString = ""; + try { + // 创建Http Post请求 + HttpPost httpPost = new HttpPost(url); + // 创建请求内容 + StringEntity entity = new StringEntity(json, + ContentType.APPLICATION_JSON); + httpPost.setEntity(entity); + // 执行http请求 + response = httpClient.execute(httpPost); + resultString = EntityUtils.toString(response.getEntity(), "utf-8"); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + response.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + return resultString; + } + + + public static String doPostJson3(String url, String json) { + // 创建Httpclient对象 + CloseableHttpClient httpClient = createSSLClientDefault(); + CloseableHttpResponse response = null; + String resultString = ""; + try { + // 创建Http Post请求 + HttpPost httpPost = new HttpPost(url); + // 创建请求内容 + StringEntity entity = new StringEntity(json, + ContentType.APPLICATION_FORM_URLENCODED); + httpPost.setEntity(entity); + // 执行http请求 + response = httpClient.execute(httpPost); + resultString = EntityUtils.toString(response.getEntity(), "utf-8"); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + response.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + return resultString; + } + + /** + * 文件上传 + * @param url + * @param json + * @param + * @return + */ + public static String doPostJson2(String url, String json) { + // 创建Httpclient对象 + CloseableHttpClient httpClient = createSSLClientDefault(); + CloseableHttpResponse response = null; + String resultString = ""; + try { + // 创建Http Post请求 + HttpPost httpPost = new HttpPost(url); + // 创建请求内容 + StringEntity entity = new StringEntity(json, + ContentType.APPLICATION_JSON); + httpPost.setEntity(entity); + // 执行http请求 + response = httpClient.execute(httpPost); + resultString = EntityUtils.toString(response.getEntity(), "utf-8"); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + response.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + return resultString; + } + + /*************************************Post**********************************************/ + + + + + + + + + /*************************************Delete**********************************************/ + /** + * 原始删除 基本不怎么用 + * @param url + * @return + */ + public static String doDelete(String url) { + // 创建Httpclient对象 + CloseableHttpClient httpClient = createSSLClientDefault(); + CloseableHttpResponse response = null; + String resultString = ""; + + try { + // 创建Http Post请求 + HttpDelete httpPost = new HttpDelete(url); + // 执行http请求 + response = httpClient.execute(httpPost); + resultString = EntityUtils.toString(response.getEntity(), "utf-8"); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + response.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + return resultString; + } + + /** + * 常用删除 + * @param url + * @param param + * @param + * @return + */ + public static String doDelete2(String url,Map param) { + // 创建Httpclient对象 + CloseableHttpClient httpClient = createSSLClientDefault(); + CloseableHttpResponse response = null; + String resultString = ""; + + try { + // 创建uri + URIBuilder builder = new URIBuilder(url); + if (param != null) { + for (String key : param.keySet()) { + builder.addParameter(key, param.get(key)); + } + } + URI uri = builder.build(); + // 创建Http Delete请求 + HttpDelete httpDelete = new HttpDelete(uri); + // 执行http请求 + response = httpClient.execute(httpDelete); + resultString = EntityUtils.toString(response.getEntity(), "utf-8"); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + response.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + return resultString; + } + + /** + * 上传文件删除 + * @param url + * @param param + * @param + * @return + */ + public static String doDelete3(String url,Map param) { + // 创建Httpclient对象 + CloseableHttpClient httpClient = createSSLClientDefault(); + CloseableHttpResponse response = null; + String resultString = ""; + + try { + // 创建uri + URIBuilder builder = new URIBuilder(url); + if (param != null) { + for (String key : param.keySet()) { + builder.addParameter(key, param.get(key)); + } + } + URI uri = builder.build(); + // 创建Http Delete请求 + HttpDelete httpDelete = new HttpDelete(uri); + // 执行http请求 + response = httpClient.execute(httpDelete); + resultString = EntityUtils.toString(response.getEntity(), "utf-8"); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + response.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + return resultString; + } + + /*************************************Delete**********************************************/ + + + + + + + + + + /*************************************Put**********************************************/ + public static String doPut(String url) { + return doPut(url, null); + } + + /** + * 修改 + * @param url + * @param param + * @param + * @return + */ + public static String doPut(String url, Map param) { + // 创建Httpclient对象 + CloseableHttpClient httpClient = createSSLClientDefault(); + CloseableHttpResponse response = null; + String resultString = ""; + + try { + // 创建uri + URIBuilder builder = new URIBuilder(url); + if (param != null) { + for (String key : param.keySet()) { + builder.addParameter(key, param.get(key)); + } + } + URI uri = builder.build(); + // 创建Http Put请求 + HttpPut httpPut = new HttpPut(uri); + // 创建参数列表 + if (param != null) { + List paramList = new ArrayList<>(); + for (String key : param.keySet()) { + paramList.add(new BasicNameValuePair(key, param.get(key))); + } + // 模拟表单 + UrlEncodedFormEntity entity = new UrlEncodedFormEntity( + paramList, Charset.forName("UTF-8"));//Charset.forName("UTF-8")解决乱码 + entity.setContentEncoding("UTF-8"); + httpPut.setEntity(entity); + } + // 执行http请求 + response = httpClient.execute(httpPut); + resultString = EntityUtils.toString(response.getEntity(), "utf-8"); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + response.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + return resultString; + } + + /** + * 以JSON格式修改 + * @param url + * @param json + * @param parameter + * @return + */ + public static String doPutJson(String url, String json) { + // 创建Httpclient对象 + CloseableHttpClient httpClient = createSSLClientDefault(); + CloseableHttpResponse response = null; + String resultString = ""; + try { + // 创建Http Post请求 + HttpPut httpPut = new HttpPut(url); + // 创建请求内容 + StringEntity entity = new StringEntity(json, + ContentType.APPLICATION_JSON); + httpPut.setEntity(entity); + // 执行http请求 + response = httpClient.execute(httpPut); + resultString = EntityUtils.toString(response.getEntity(), "utf-8"); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + response.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + return resultString; + } + + + +} + diff --git a/module-common/src/main/java/org/jeecg/api/wxUtils/HttpServletUtils.java b/module-common/src/main/java/org/jeecg/api/wxUtils/HttpServletUtils.java new file mode 100644 index 0000000..28f8217 --- /dev/null +++ b/module-common/src/main/java/org/jeecg/api/wxUtils/HttpServletUtils.java @@ -0,0 +1,47 @@ +package org.jeecg.api.wxUtils; + +import javax.servlet.ServletInputStream; +import javax.servlet.http.HttpServletRequest; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; + +/** + *

+ * HttpServletRequest 获取请求体 + *

+ * + * @author songfayuan + * @date 2024/9/30 19:11 + */ +public class HttpServletUtils { + + /** + * 获取请求体 + * + * @param request + * @return + * @throws IOException + */ + public static String getRequestBody(HttpServletRequest request) throws IOException { + ServletInputStream stream = null; + BufferedReader reader = null; + StringBuffer sb = new StringBuffer(); + try { + stream = request.getInputStream(); + // 获取响应 + reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8)); + String line; + while ((line = reader.readLine()) != null) { + sb.append(line); + } + } catch (IOException e) { + throw new IOException("读取返回支付接口数据流出现异常!"); + } finally { + reader.close(); + } + return sb.toString(); + } + +} diff --git a/module-common/src/main/java/org/jeecg/api/wxUtils/PayOrderInfo.java b/module-common/src/main/java/org/jeecg/api/wxUtils/PayOrderInfo.java new file mode 100644 index 0000000..aae93ce --- /dev/null +++ b/module-common/src/main/java/org/jeecg/api/wxUtils/PayOrderInfo.java @@ -0,0 +1,31 @@ +package org.jeecg.api.wxUtils; + +import lombok.Data; + +import java.math.BigDecimal; + +/** + *

+ * 支付订单信息 + *

+ * + * @author songfayuan + * @date 2024/9/30 17:46 + */ +@Data +public class PayOrderInfo { + /** + * 订单标题 + */ + private String description; + /** + * 商户订单号 + * 只能是数字、大小写字母_-*且在同一个商户号下唯一。 + */ + private String outTradeNo; + /** + * 支付金额,单位:元 + */ + private BigDecimal amount; +} + diff --git a/module-common/src/main/java/org/jeecg/api/wxUtils/QueryOrderReq.java b/module-common/src/main/java/org/jeecg/api/wxUtils/QueryOrderReq.java new file mode 100644 index 0000000..ee2afc8 --- /dev/null +++ b/module-common/src/main/java/org/jeecg/api/wxUtils/QueryOrderReq.java @@ -0,0 +1,23 @@ +package org.jeecg.api.wxUtils; + +import lombok.Data; + +/** + *

+ * 订单查询请求 + *

+ * + * @author songfayuan + * @date 2024/9/30 19:19 + */ +@Data +public class QueryOrderReq { + /** + * 订单号:业务侧的订单号 + */ + private String transactionId; + /** + * 商户订单号 + */ + private String outTradeNo; +} diff --git a/module-common/src/main/java/org/jeecg/api/wxUtils/RefundOrderReq.java b/module-common/src/main/java/org/jeecg/api/wxUtils/RefundOrderReq.java new file mode 100644 index 0000000..9f63f4f --- /dev/null +++ b/module-common/src/main/java/org/jeecg/api/wxUtils/RefundOrderReq.java @@ -0,0 +1,36 @@ +package org.jeecg.api.wxUtils; + +import lombok.Data; + +import java.math.BigDecimal; + +/** + *

+ * 退款订单请求参数 + *

+ * + * @author songfayuan + * @date 2024/9/30 19:19 + */ +@Data +public class RefundOrderReq { + /** + * 订单号:业务侧的订单号 + */ + private String transactionId; + /** + * 商户订单号 + */ + private String outTradeNo; + + /** + * 原订单金额 说明:原支付交易的订单总金额,这里单位为元。 + */ + private BigDecimal totalAmount; + + /** + * 退款金额 说明:退款金额,这里单位为元,不能超过原订单支付金额。 + */ + private BigDecimal refundAmount; +} + diff --git a/module-common/src/main/java/org/jeecg/api/wxUtils/Response.java b/module-common/src/main/java/org/jeecg/api/wxUtils/Response.java new file mode 100644 index 0000000..4eff69e --- /dev/null +++ b/module-common/src/main/java/org/jeecg/api/wxUtils/Response.java @@ -0,0 +1,164 @@ +package org.jeecg.api.wxUtils; + +import lombok.Data; + +/** + * 返回参数封装类 + * + * @param + * @author songfayuan + */ + +@Data +public class Response { + /** + * 状态码 + */ + protected int code; + + /** + * 提示信息 + */ + protected String msg; + + /** + * 数据 + */ + protected T data; + + /** + * 成功时状态码 + */ + private static final int SUCCESS_CODE = 200; + + /** + * 成功时提示信息 + */ + private static final String SUCCESS_MSG = "success"; + + /** + * 异常、错误信息状态码 + */ + private static final int ERROR_CODE = 500; + + /** + * 异常、错误提示信息 + */ + private static final String ERROR_MSG = "服务器内部异常,请联系技术人员!"; + + /** + * 返回成功消息 + * + * @param + * @return + */ + public static Response success() { + Response resp = new Response(); + resp.code = (SUCCESS_CODE); + resp.msg = (SUCCESS_MSG); + return resp; + } + + /** + * 返回成功消息 + * + * @param + * @param msg + * @return + */ + public static Response successResponse(String msg) { + Response resp = new Response(); + resp.code = SUCCESS_CODE; + resp.msg = msg; + return resp; + } + + /** + * 返回错误消息 + * + * @param + * @return + */ + public static Response error() { + Response resp = new Response(); + resp.code = (ERROR_CODE); + resp.msg = (ERROR_MSG); + return resp; + } + + /** + * 返回错误消息 + * + * @param + * @param msg + * @return + */ + public static Response errorResponse(String msg) { + Response resp = new Response(); + resp.code = ERROR_CODE; + resp.msg = msg; + return resp; + } + + /** + * 自定义状态码、提示信息 + * + * @param + * @param code 状态码 + * @param msg 提示信息 + * @return + */ + public static Response response(int code, String msg) { + Response resp = new Response(); + resp.code = (code); + resp.msg = (msg); + return resp; + } + + /** + * 自定义状态码、提示信息、业务数据 + * + * @param + * @param code 状态码 + * @param msg 提示信息 + * @param data 业务数据 + * @return + */ + public static Response response(int code, String msg, T data) { + Response resp = new Response<>(); + resp.code = (code); + resp.msg = (msg); + resp.data = data; + return resp; + } + + /** + * 返回成功数据 + * + * @param + * @param data 业务数据 + * @return + */ + public static Response success(T data) { + Response resp = new Response<>(); + resp.code = (SUCCESS_CODE); + resp.msg = (SUCCESS_MSG); + resp.data = data; + return resp; + } + + /** + * 返回错误消息 + * + * @param + * @param data 业务数据 + * @return + */ + public static Response error(T data) { + Response resp = new Response<>(); + resp.code = (ERROR_CODE); + resp.msg = (ERROR_MSG); + resp.data = data; + return resp; + } +} diff --git a/module-common/src/main/java/org/jeecg/api/wxUtils/WxPayAutoCertificateConfig.java b/module-common/src/main/java/org/jeecg/api/wxUtils/WxPayAutoCertificateConfig.java new file mode 100644 index 0000000..b2c8015 --- /dev/null +++ b/module-common/src/main/java/org/jeecg/api/wxUtils/WxPayAutoCertificateConfig.java @@ -0,0 +1,86 @@ +package org.jeecg.api.wxUtils; + + +import lombok.extern.slf4j.Slf4j; +import org.springframework.context.annotation.Configuration; + +@Slf4j +@Configuration +public class WxPayAutoCertificateConfig { + +// /** +// * 微信小程序的 AppID +// */ +// @Value("${wx.miniapp.appid}") +// private String appid; +// +// /** +// * 微信小程序的密钥 +// */ +// @Value("${wx.miniapp.secret}") +// private String secret; +// +// /** +// * 商户号 +// */ +// @Value("${wx.miniapp.merchantId}") +// private String merchantId; +// +// /** +// * 商户API私钥路径 +// */ +// @Value("${wx.miniapp.privateKeyPath}") +// private String privateKeyPath; +// +// /** +// * 商户证书序列号 +// */ +// @Value("${wx.miniapp.merchantSerialNumber}") +// private String merchantSerialNumber; +// +// /** +// * 商户APIV3密钥 +// */ +// @Value("${wx.miniapp.apiV3Key}") +// private String apiV3Key; +// +// /** +// * 支付通知地址 +// */ +// @Value("${wx.miniapp.payNotifyUrl}") +// private String payNotifyUrl; +// +// /** +// * 退款通知地址 +// */ +// @Value("${wx.miniapp.refundNotifyUrl}") +// private String refundNotifyUrl; + +// /** +// * 初始化商户配置 +// * @return +// */ +// @Bean +// public Config rsaAutoCertificateConfig() { +// // 这里把Config作为配置Bean是为了避免多次创建资源,一般项目运行的时候这些东西都确定了 +// // 具体的参数改为申请的数据,可以通过读配置文件的形式获取 +//// Config config = new RSAAutoCertificateConfig.Builder() +//// .merchantId("1659066870") +//// .privateKeyFromPath("E:\\git_java\\api_java\\popularize-admin\\popularize-admin\\popularize-admin\\module-pay\\src\\main\\resources\\apiclient_key.pem") +//// .merchantSerialNumber("7BE56DC695B2B612BD1C6C710A7FBFA1AC46B10F") +//// .apiV3Key("vtribevtribevtribevtribevtribe12") +//// .build(); +// +// +//// Config config = new RSAAutoCertificateConfig.Builder() +//// .merchantId(merchantId) +//// .privateKeyFromPath(privateKeyPath) +//// .merchantSerialNumber(merchantSerialNumber) +//// .apiV3Key(apiV3Key) +//// .build(); +//// log.info("初始化微信支付商户配置完成..."); +// return config; +// } + + +} diff --git a/module-common/src/main/java/org/jeecg/api/wxUtils/WxPayConfig.java b/module-common/src/main/java/org/jeecg/api/wxUtils/WxPayConfig.java new file mode 100644 index 0000000..fa2f93d --- /dev/null +++ b/module-common/src/main/java/org/jeecg/api/wxUtils/WxPayConfig.java @@ -0,0 +1,15 @@ +package org.jeecg.api.wxUtils; +import lombok.Data; +import org.springframework.stereotype.Component; + + + + +@Data +@Component +public class WxPayConfig { + + + +} + diff --git a/module-common/src/main/java/org/jeecg/api/wxUtils/WxQrCodeVo.java b/module-common/src/main/java/org/jeecg/api/wxUtils/WxQrCodeVo.java new file mode 100644 index 0000000..48f37b3 --- /dev/null +++ b/module-common/src/main/java/org/jeecg/api/wxUtils/WxQrCodeVo.java @@ -0,0 +1,18 @@ +package org.jeecg.api.wxUtils; + +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +@Data +public class WxQrCodeVo { + /**图片地址*/ + @ApiModelProperty(value = "图片地址") + private String url; + + @ApiModelProperty(value = "二维码名称") + private String name; + + @ApiModelProperty(value = "二维码背景图") + private String bjImage; + +}