From 4fee2b0002f103bb0ec54a2b7db52c22ff7dcbbe Mon Sep 17 00:00:00 2001 From: wangshuai Date: Wed, 3 Jul 2024 11:18:55 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E5=8A=A8utils=20=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E9=A6=96=E9=A1=B5=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ruoyi-admin/pom.xml | 21 - ruoyi-common/pom.xml | 23 + .../ruoyi/common/utils/HttpClientUtilT.java | 468 ++++++++++++++++++ .../ruoyi/business/service/IElemoService.java | 4 + .../business/service/IMeituanService.java | 5 + .../service/impl/ElemoServiceImpl.java | 8 + .../service/impl/MeituanServiceImpl.java | 48 ++ ruoyi-ui/src/router/index.js | 3 +- 8 files changed, 558 insertions(+), 22 deletions(-) create mode 100644 ruoyi-common/src/main/java/com/ruoyi/common/utils/HttpClientUtilT.java create mode 100644 ruoyi-system/src/main/java/com/ruoyi/business/service/IElemoService.java create mode 100644 ruoyi-system/src/main/java/com/ruoyi/business/service/IMeituanService.java create mode 100644 ruoyi-system/src/main/java/com/ruoyi/business/service/impl/ElemoServiceImpl.java create mode 100644 ruoyi-system/src/main/java/com/ruoyi/business/service/impl/MeituanServiceImpl.java diff --git a/ruoyi-admin/pom.xml b/ruoyi-admin/pom.xml index 565acc2..cbc6826 100644 --- a/ruoyi-admin/pom.xml +++ b/ruoyi-admin/pom.xml @@ -66,27 +66,6 @@ ruoyi-generator - - org.apache.httpcomponents - httpclient - 4.5.1 - - - org.apache.httpcomponents - httpcore - 4.4.10 - - - org.apache.httpcomponents - fluent-hc - 4.5.1 - - - org.apache.httpcomponents - httpmime - 4.5.1 - - diff --git a/ruoyi-common/pom.xml b/ruoyi-common/pom.xml index 46663f6..c82405a 100644 --- a/ruoyi-common/pom.xml +++ b/ruoyi-common/pom.xml @@ -128,6 +128,29 @@ org.projectlombok lombok + + + + org.apache.httpcomponents + httpclient + 4.5.1 + + + org.apache.httpcomponents + httpcore + 4.4.10 + + + org.apache.httpcomponents + fluent-hc + 4.5.1 + + + org.apache.httpcomponents + httpmime + 4.5.1 + + \ No newline at end of file diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/HttpClientUtilT.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/HttpClientUtilT.java new file mode 100644 index 0000000..ab2b42d --- /dev/null +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/HttpClientUtilT.java @@ -0,0 +1,468 @@ +package com.ruoyi.common.utils; + +import org.apache.commons.io.IOUtils; +import org.apache.http.Header; +import org.apache.http.HttpEntity; +import org.apache.http.HttpHeaders; +import org.apache.http.HttpResponse; +import org.apache.http.client.ServiceUnavailableRetryStrategy; +import org.apache.http.client.config.RequestConfig; +import org.apache.http.client.utils.URIBuilder; +import org.apache.http.entity.ByteArrayEntity; +import org.apache.http.entity.ContentType; +import org.apache.http.entity.StringEntity; +import org.apache.http.entity.mime.HttpMultipartMode; +import org.apache.http.entity.mime.MultipartEntityBuilder; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.DefaultHttpRequestRetryHandler; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; +import org.apache.http.protocol.HttpContext; +import org.apache.http.util.EntityUtils; +import org.springframework.web.multipart.MultipartFile; +import org.springframework.web.multipart.MultipartRequest; + +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.OutputStream; +import java.io.UnsupportedEncodingException; +import java.net.URISyntaxException; +import java.nio.charset.Charset; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import java.util.concurrent.TimeUnit; + +/** + * @author lixiaohua + * @since 2019-03-18 + */ +public class HttpClientUtilT { + + /** + * 编码格式。发送编码格式统一用UTF-8 + */ + private static final String ENCODING = "UTF-8"; + + /** + * 设置连接超时时间,单位毫秒。 + */ + private static final int CONNECT_TIMEOUT = 500000; + + /** + * 请求获取数据的超时时间(即响应时间),单位毫秒。 + */ + private static final int SOCKET_TIMEOUT = 500000; + + private static final int REQ_TIMEOUT = 100000; + + private static PoolingHttpClientConnectionManager cm = null; + + static { + cm = new PoolingHttpClientConnectionManager(); + cm.setMaxTotal(10);//多线程调用注意配置,根据线程数设定 + cm.setDefaultMaxPerRoute(50); + } + + public static CloseableHttpClient getHttpClient() { +// CloseableHttpClient httpClient = HttpClients.createDefault(); + ServiceUnavailableRetryStrategy serviceUnavailableRetryStrategy = new ServiceUnavailableRetryStrategy() { + /** + * retry逻辑 + */ + @Override + public boolean retryRequest(HttpResponse response, int executionCount, HttpContext context) { + if (executionCount <= 3) + return true; + else + return false; + } + + /** + * retry间隔时间 + */ + @Override + public long getRetryInterval() { + return 20000; + } + }; + + /** + * evictExpiredConnections: 定期回收过期链接 + * setConnectTimeout:设置连接超时时间,单位毫秒。 + * setConnectionRequestTimeout:设置从connect Manager(连接池)获取Connection + * 超时时间,单位毫秒。这个属性是新加的属性,因为目前版本是可以共享连接池的。 + * setSocketTimeout:请求获取数据的超时时间(即响应时间),单位毫秒。 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用。 + */ + CloseableHttpClient httpClient = HttpClients.custom() + .setRetryHandler(new DefaultHttpRequestRetryHandler()) + .setConnectionManager(cm) + .evictExpiredConnections() + .evictIdleConnections(1, TimeUnit.SECONDS) + .setConnectionTimeToLive(1, TimeUnit.SECONDS) + .setDefaultRequestConfig(RequestConfig + .custom() + .setSocketTimeout(SOCKET_TIMEOUT) + .setConnectionRequestTimeout(REQ_TIMEOUT) + .setConnectTimeout(CONNECT_TIMEOUT) + .build()) + .build(); + return httpClient; + } + + /** + * 发送get请求;带请求头和请求参数 + * + * @param url 请求地址 + * @param headers 请求头集合 + * @param params 请求参数集合 + * @return + * @throws Exception + */ + public static String doGet(String url, Map headers, Map params) { + + // 创建httpClient对象 + CloseableHttpClient httpClient = getHttpClient(); + + // 创建httpResponse对象 + CloseableHttpResponse httpResponse = null; + + // 创建访问的地址 + URIBuilder uriBuilder = null; + // 创建http对象 + HttpGet httpGet = null; + try { + uriBuilder = new URIBuilder(url); + if (params != null && params.size() > 0) { + Set> entrySet = params.entrySet(); + for (Entry entry : entrySet) { + uriBuilder.setParameter(entry.getKey(), entry.getValue().toString()); + } + } + httpGet = new HttpGet(uriBuilder.build()); + } catch (URISyntaxException e) { + e.printStackTrace(); + } + // 设置请求头 + packageHeader(headers, httpGet); + + // 执行请求并获得响应结果 + return getHttpClientResult(httpResponse, httpClient, httpGet); + } + + /** + * 发送get请求;带请求头和请求参数 + * + * @param url 请求地址 + * @param headers 请求头集合 + * @param params 请求参数集合 + * @return + * @throws Exception + */ + public static void doGet(String url, Map headers + , Map params, HttpServletResponse servletResponse) { + + // 创建httpClient对象 + CloseableHttpClient httpClient = getHttpClient(); + + // 创建httpResponse对象 + CloseableHttpResponse httpResponse = null; + + // 创建访问的地址 + URIBuilder uriBuilder = null; + // 创建http对象 + HttpGet httpGet = null; + try { + uriBuilder = new URIBuilder(url); + if (params != null && params.size() > 0) { + Set> entrySet = params.entrySet(); + for (Entry entry : entrySet) { + uriBuilder.setParameter(entry.getKey(), entry.getValue().toString()); + } + } + httpGet = new HttpGet(uriBuilder.build()); + } catch (URISyntaxException e) { + e.printStackTrace(); + } + // 设置请求头 + packageHeader(headers, httpGet); + + // 执行请求并获得响应结果 + getHttpClientResult(httpResponse, httpClient, httpGet, servletResponse); + } + + /** + * post json + * + * @param url + * @param json + * @return + */ + public static String doPostByte(String url, Map headers, byte[] json) { + // 创建httpClient对象 + CloseableHttpClient httpClient = getHttpClient(); + + // 创建http对象 + HttpPost httpPost = new HttpPost(url); + + // 设置请求头 + packageHeader(headers, httpPost); + + // 创建请求内容 + ByteArrayEntity entity = new ByteArrayEntity(json); + httpPost.setEntity(entity); + + // 创建httpResponse对象 + CloseableHttpResponse httpResponse = null; + + // 执行请求并获得响应结果 + return getHttpClientResult(httpResponse, httpClient, httpPost); + } + + /** + * post headers json + * + * @param url + * @param headers + * @param json + * @return + */ + public static void doPostByte(String url, Map headers + , byte[] json, HttpServletResponse servletResponse) { + // 创建httpClient对象 + CloseableHttpClient httpClient = getHttpClient(); + + // 创建http对象 + HttpPost httpPost = new HttpPost(url); + + // 设置请求头 + packageHeader(headers, httpPost); + + // 创建请求内容 + ByteArrayEntity entity = new ByteArrayEntity(json); + httpPost.setEntity(entity); + + // 创建httpResponse对象 + CloseableHttpResponse httpResponse = null; + + // 执行请求并获得响应结果 + getHttpClientResult(httpResponse, httpClient, httpPost, servletResponse); + } + + /** + * post json + * + * @param url + * @param json + * @return + */ + public static String doPostJson(String url, Map headers, String json) { + // 创建httpClient对象 + CloseableHttpClient httpClient = getHttpClient(); + + // 创建http对象 + HttpPost httpPost = new HttpPost(url); + + // 设置请求头 + packageHeader(headers, httpPost); + + // 创建请求内容 + StringEntity entity = new StringEntity(json, "utf-8"); + httpPost.setEntity(entity); + + // 创建httpResponse对象 + CloseableHttpResponse httpResponse = null; + + // 执行请求并获得响应结果 + return getHttpClientResult(httpResponse, httpClient, httpPost); + } + + /** + * post json + * + * @param url + * @param json + * @return + */ + public static void doPostJson(String url, Map headers, String json, HttpServletResponse servletResponse) { + // 创建httpClient对象 + CloseableHttpClient httpClient = getHttpClient(); + + // 创建http对象 + HttpPost httpPost = new HttpPost(url); + + // 设置请求头 + packageHeader(headers, httpPost); + + // 创建请求内容 + StringEntity entity = new StringEntity(json, "utf-8"); + httpPost.setEntity(entity); + + // 创建httpResponse对象 + CloseableHttpResponse httpResponse = null; + + // 执行请求并获得响应结果 + getHttpClientResult(httpResponse, httpClient, httpPost, servletResponse); + } + + /** + * post form 上传 file + * + * @param url + * @param headers + * @param params + * @param multipartRequest + * @return + */ + public static void doPostToFile(String url, Map headers + , Map params, MultipartRequest multipartRequest, HttpServletResponse servletResponse) { + // 创建httpClient对象 + CloseableHttpClient httpClient = getHttpClient(); + + // 创建http对象 + HttpPost httpPost = new HttpPost(url); + + // 设置请求头 + packageHeader(headers, httpPost); + httpPost.removeHeaders(HttpHeaders.CONTENT_TYPE); + + packageParam(multipartRequest, params, httpPost); + + // 创建httpResponse对象 + CloseableHttpResponse httpResponse = null; + + // 执行请求并获得响应结果 + getHttpClientResult(httpResponse, httpClient, httpPost, servletResponse); + } + + /** + * Description: 获得响应结果 + * + * @param httpResponse + * @param httpClient + * @param httpMethod + * @return + * @throws Exception + */ + public static String getHttpClientResult(CloseableHttpResponse httpResponse + , CloseableHttpClient httpClient, HttpRequestBase httpMethod) { + // 获取返回结果 + String content = ""; + // 执行请求 + try { + httpResponse = httpClient.execute(httpMethod); + if (httpResponse != null && httpResponse.getStatusLine() != null) { + if (httpResponse.getEntity() != null) { + content = EntityUtils.toString(httpResponse.getEntity(), ENCODING); + } + } + return content; + } catch (IOException e) { + e.printStackTrace(); + throw new RuntimeException("内部服务异常!"); + } finally { + IOUtils.closeQuietly(httpResponse); + httpMethod.releaseConnection(); + } + } + + public static void getHttpClientResult(CloseableHttpResponse httpResponse + , CloseableHttpClient httpClient, HttpRequestBase httpMethod, HttpServletResponse servletResponse) { + // 执行请求 + try { + httpResponse = httpClient.execute(httpMethod); + + setHeader(httpResponse, servletResponse); + + HttpEntity entity = httpResponse.getEntity(); + OutputStream out = servletResponse.getOutputStream(); + entity.writeTo(out); + out.flush(); + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException("内部服务异常!"); + } finally { + IOUtils.closeQuietly(httpResponse); + httpMethod.releaseConnection(); + } + } + + /** + * Description: 封装请求头 + * + * @param header + * @param httpMethod + */ + public static void packageHeader(Map header, HttpRequestBase httpMethod) { + // 封装请求头 + if (header != null && header.size() > 0) { + Set> entrySet = header.entrySet(); + for (Entry entry : entrySet) { + // 设置到请求头到HttpRequestBase对象中 + // 有中文时: httpMethod.setHeader(entry.getKey(), URLEncoder.encode(entry.getValue(), "utf-8")); + httpMethod.setHeader(entry.getKey(), entry.getValue()); + } + } + httpMethod.removeHeaders(HttpHeaders.CONTENT_LENGTH); + } + + /** + * 将CloseableHttpResponse的响应头,设置到HttpServletResponse上 + * + * @param response + * @param servletResponse + */ + private static void setHeader(CloseableHttpResponse response, HttpServletResponse servletResponse) { + Header[] headers = response.getAllHeaders(); + for (Header header : headers) { + System.out.println(header.getName()); + if (header.getName().contains("Content-Type") + || header.getName().equalsIgnoreCase("content-disposition") + || header.getName().contains("Content-Length")) { + servletResponse.setHeader(header.getName(), header.getValue()); + } + } + } + + /** + * Description: 封装请求参数 + * + * @param params + * @param httpMethod + * @throws UnsupportedEncodingException + */ + public static void packageParam(MultipartRequest multipartRequest + , Map params, HttpEntityEnclosingRequestBase httpMethod) { + try { + // 封装请求参数 + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); + builder.setCharset(Charset.forName("utf-8")); + //加上此行代码解决返回中文乱码问题 + builder.setMode(HttpMultipartMode.RFC6532); + String fileName; + for (Entry entry : multipartRequest.getFileMap().entrySet()) { + MultipartFile multipartFile = entry.getValue(); + fileName = multipartFile.getOriginalFilename(); + System.out.println("文件表单上传======" + fileName + "====="); + // 文件流 + builder.addBinaryBody(entry.getKey(), multipartFile.getInputStream(), ContentType.MULTIPART_FORM_DATA, fileName); + } + // 封装参数 + if (params != null) { + for (String key : params.keySet()) { + System.out.println("文件表单上传======" + key + "=====" + params.get(key)); + builder.addTextBody(key, params.get(key).toString(), ContentType.MULTIPART_FORM_DATA); + } + } + HttpEntity entity = builder.build(); + httpMethod.setEntity(entity); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + throw new RuntimeException("转码异常!"); + } catch (IOException e) { + e.printStackTrace(); + throw new RuntimeException("文件转码异常!"); + } + } + +} diff --git a/ruoyi-system/src/main/java/com/ruoyi/business/service/IElemoService.java b/ruoyi-system/src/main/java/com/ruoyi/business/service/IElemoService.java new file mode 100644 index 0000000..afe3e1f --- /dev/null +++ b/ruoyi-system/src/main/java/com/ruoyi/business/service/IElemoService.java @@ -0,0 +1,4 @@ +package com.ruoyi.business.service; + +public interface IElemoService { +} diff --git a/ruoyi-system/src/main/java/com/ruoyi/business/service/IMeituanService.java b/ruoyi-system/src/main/java/com/ruoyi/business/service/IMeituanService.java new file mode 100644 index 0000000..9fcb216 --- /dev/null +++ b/ruoyi-system/src/main/java/com/ruoyi/business/service/IMeituanService.java @@ -0,0 +1,5 @@ +package com.ruoyi.business.service; + +public interface IMeituanService { + String getComments(); +} diff --git a/ruoyi-system/src/main/java/com/ruoyi/business/service/impl/ElemoServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/business/service/impl/ElemoServiceImpl.java new file mode 100644 index 0000000..a1907f1 --- /dev/null +++ b/ruoyi-system/src/main/java/com/ruoyi/business/service/impl/ElemoServiceImpl.java @@ -0,0 +1,8 @@ +package com.ruoyi.business.service.impl; + +import com.ruoyi.business.service.IElemoService; +import org.springframework.stereotype.Service; + +@Service +public class ElemoServiceImpl implements IElemoService { +} diff --git a/ruoyi-system/src/main/java/com/ruoyi/business/service/impl/MeituanServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/business/service/impl/MeituanServiceImpl.java new file mode 100644 index 0000000..fe102de --- /dev/null +++ b/ruoyi-system/src/main/java/com/ruoyi/business/service/impl/MeituanServiceImpl.java @@ -0,0 +1,48 @@ +package com.ruoyi.business.service.impl; + +import com.ruoyi.business.domain.BusStoreInfo; +import com.ruoyi.business.mapper.BusStoreInfoMapper; +import com.ruoyi.business.service.IMeituanService; +import com.ruoyi.common.utils.HttpClientUtilT; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@Service +public class MeituanServiceImpl implements IMeituanService { + + @Autowired + private BusStoreInfoMapper busStoreInfoMapper; + + @Override + public String getComments() { + BusStoreInfo busStoreInfo = new BusStoreInfo(); + List busStoreInfoList = busStoreInfoMapper.selectBusStoreInfoList(busStoreInfo); + for(BusStoreInfo store: busStoreInfoList){ + + } + //commScore=1 好评;commScore=0 全部(即订单量);五星好评取返回结果orderCommentScore=5 + String url = "https://waimaieapp.meituan.com/gw/customer/comment/list"; + Map params = new HashMap<>(); + params.put("ignoreSetRouterProxy",true); + params.put("acctId","196975850"); + params.put("wmPoiId","18277065"); + params.put("token","0G5G-OOR2Q2APHeiU6dvlB6W2TBBirC_1c6zb_K2HG8I*"); + params.put("appType",3); + params.put("commScore",1); + params.put("commType",-1); + params.put("hasContent",-1); + params.put("periodType",4); + params.put("beginTime",1719676800); + params.put("endTime",1719676800); + params.put("onlyAuditNotPass",0); + params.put("pageNum",1); + params.put("pageSize",10); + params.put("source",1); + String result = HttpClientUtilT.doGet(url,null,params); + return result; + } +} diff --git a/ruoyi-ui/src/router/index.js b/ruoyi-ui/src/router/index.js index 71907b6..332019a 100644 --- a/ruoyi-ui/src/router/index.js +++ b/ruoyi-ui/src/router/index.js @@ -68,7 +68,8 @@ export const constantRoutes = [ children: [ { path: 'index', - component: () => import('@/views/index'), + // component: () => import('@/views/index'), + component: () => import('@/views/market/whole'), name: 'Index', meta: { title: '首页', icon: 'dashboard', affix: true } }