From 3540e22313e81dcc121b6973a7cf73ebf7390926 Mon Sep 17 00:00:00 2001 From: Lenovo Date: Tue, 2 Jul 2024 09:08:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/MeituanController.java | 140 ++++++ .../ruoyi/business/utils/HttpClientUtilT.java | 469 ++++++++++++++++++ 2 files changed, 609 insertions(+) create mode 100644 ruoyi-admin/src/main/java/com/ruoyi/business/controller/MeituanController.java create mode 100644 ruoyi-admin/src/main/java/com/ruoyi/business/utils/HttpClientUtilT.java diff --git a/ruoyi-admin/src/main/java/com/ruoyi/business/controller/MeituanController.java b/ruoyi-admin/src/main/java/com/ruoyi/business/controller/MeituanController.java new file mode 100644 index 0000000..514cbf7 --- /dev/null +++ b/ruoyi-admin/src/main/java/com/ruoyi/business/controller/MeituanController.java @@ -0,0 +1,140 @@ +package com.ruoyi.business.controller; + +import com.alibaba.fastjson2.JSON; +import com.ruoyi.business.utils.HttpClientUtilT; +import com.ruoyi.common.core.domain.R; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.mime.MultipartEntityBuilder; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.util.EntityUtils; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; + +@RestController +@RequestMapping("/mt") +public class MeituanController { + + /** + * 获取订单 + * @return + */ + @GetMapping("/getOrders") + public R orders() { + String url = "https://e.waimai.meituan.com/gw/api/order/mix/history/list/common"; + Map params = new LinkedHashMap<>(); + params.put("region_id","1000510100"); + params.put("region_version","1717637457"); + params.put("yodaReady","h5"); + params.put("csecplatform",4); + params.put("csecversion","2.4.0"); + String param = "?"; + for (Map.Entry stringObjectEntry : params.entrySet()) { + param = param + stringObjectEntry.getKey() + "=" + stringObjectEntry.getValue()+"&"; + } + param = param.substring(0, param.length()-1); + url=url+param; + Map headers = new HashMap<>(); + headers.put("Cookie", "WEBDFPID=81x675y2y0x156y20z8194xvv5622uv180938x0x565979585zu21vx7-2034987530564-1719627530564QGCGCKGfd79fef3d01d5e9aadc18ccd4d0c95072413; _lxsdk_cuid=19061b46258c8-0a575a85246371-d373666-1fa400-19061b4625873; _lxsdk=19061b46258c8-0a575a85246371-d373666-1fa400-19061b4625873; device_uuid=!f681d2ae-d8e3-44ae-abed-9319cef9b9a1; uuid_update=true; shopCategory=food; JSESSIONID=1uja6towq16yyjlh5bsieyjib; acctId=196975850; token=08Y-B6R3RJh0vWW3sunjIls8g5ks282V0XDqw0_8uK2I*; wmPoiId=21737990; isOfflineSelfOpen=0; city_id=510100; isChain=0; ignore_set_router_proxy=false; region_id=1000510100; region_version=1717637457; bsid=owwHans2BfoYdNrDqfvWbv2A2saE_AOD_UtRrtJNH0nhZDvBSKHje0DOtzsvg4RQ1sA0nGCkn25eIU2f8BViXA; city_location_id=510100; location_id=510107; has_not_waimai_poi=0; cityId=210100; provinceId=210000; set_info=%7B%22wmPoiId%22%3A%2221737990%22%2C%22region_id%22%3A%221000510100%22%2C%22region_version%22%3A1717637457%7D; wpush_server_url=wss://wpush.meituan.com; pushToken=08Y-B6R3RJh0vWW3sunjIls8g5ks282V0XDqw0_8uK2I*; setPrivacyTime=1_20240701; logan_session_token=9i1ql2qeonn9g01klg1m; _lxsdk_s=1906ce6f1b5-623-dde-89d%7C%7C75"); + Map body = new HashMap<>(); + body.put("tag","complete"); + body.put("tartDate","2024-06-28"); + body.put("endDate","2024-06-28"); + body.put("pageNum",1); + body.put("pageSize",10); + body.put("pageGray",1); + String bodyString = JSON.toJSONString(body); + String result = HttpClientUtilT.doPostJson(url,headers,bodyString); + return R.ok(result); + } + + + /** + * 获取号码 + * @return + * @throws Exception + */ + @GetMapping("/getPriPhone") + public R priPhone() throws Exception { + String url = "https://e.waimai.meituan.com/v2/order/history/r/search/ajax"; + Map params = new LinkedHashMap<>(); + params.put("searchItem","2001141341103646099"); + params.put("region_id","1000440100"); + params.put("region_version","1687236854"); + params.put("yodaReady","h5"); + params.put("csecplatform",4); + params.put("csecversion","2.4.0"); + params.put("mtgsig","%7B%22a1%22%3A%221.1%22%2C%22a2%22%3A1719559430105%2C%22a3%22%3A%221719555287467YYKUWIAfd79fef3d01d5e9aadc18ccd4d0c95077155%22%2C%22a5%22%3A%22RysVtjJl6dwe7UCBGvbf%22%2C%22a6%22%3A%22hs1.4aOG4x69iuIGtADfqn9IKcTw1%2FkgMmU2b7SKfkduAyV5JdsMP7K%2FyaI4LzRA7LmuUUNuW8gACTFDnxuSagQ8j8Q%3D%3D%22%2C%22x0%22%3A4%2C%22d1%22%3A%224ddede8d030621664d0ad9194ed8ffee%22%7D"); + String param = "?"; + for (Map.Entry stringObjectEntry : params.entrySet()) { + param = param + stringObjectEntry.getKey() + "=" + stringObjectEntry.getValue()+"&"; + } + param = param.substring(0, param.length()-1); + url = url + param; + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); + builder.addTextBody("searchItem","2001141341103646099"); + HttpClient client = HttpClientBuilder.create().build(); + HttpPost httpPost = new HttpPost(url); + HttpEntity entity = builder.build(); + httpPost.setEntity(entity); + httpPost.setHeader("Cookie","WEBDFPID=81x675y2y0x156y20z8194xvv5622uv180938x0x565979585zu21vx7-2034987530564-1719627530564QGCGCKGfd79fef3d01d5e9aadc18ccd4d0c95072413; _lxsdk_cuid=19061b46258c8-0a575a85246371-d373666-1fa400-19061b4625873; _lxsdk=19061b46258c8-0a575a85246371-d373666-1fa400-19061b4625873; device_uuid=!f681d2ae-d8e3-44ae-abed-9319cef9b9a1; uuid_update=true; shopCategory=food; JSESSIONID=1uja6towq16yyjlh5bsieyjib; acctId=196975850; token=08Y-B6R3RJh0vWW3sunjIls8g5ks282V0XDqw0_8uK2I*; wmPoiId=21737990; isOfflineSelfOpen=0; city_id=510100; isChain=0; ignore_set_router_proxy=false; region_id=1000510100; region_version=1717637457; bsid=owwHans2BfoYdNrDqfvWbv2A2saE_AOD_UtRrtJNH0nhZDvBSKHje0DOtzsvg4RQ1sA0nGCkn25eIU2f8BViXA; city_location_id=510100; location_id=510107; has_not_waimai_poi=0; cityId=210100; provinceId=210000; set_info=%7B%22wmPoiId%22%3A%2221737990%22%2C%22region_id%22%3A%221000510100%22%2C%22region_version%22%3A1717637457%7D; wpush_server_url=wss://wpush.meituan.com; pushToken=08Y-B6R3RJh0vWW3sunjIls8g5ks282V0XDqw0_8uK2I*; setPrivacyTime=1_20240701; logan_session_token=9i1ql2qeonn9g01klg1m; _lxsdk_s=1906ce6f1b5-623-dde-89d%7C%7C75"); + HttpResponse response = client.execute(httpPost); + HttpEntity responseEntity = response.getEntity(); + String resp = EntityUtils.toString(responseEntity); + return R.ok(resp); + } + + /** + * 获取订单数和好评(4,5星)数 + * @return + */ + @GetMapping("/getComments") + public R comments() { + //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","21737990"); + params.put("token","08Y-B6R3RJh0vWW3sunjIls8g5ks282V0XDqw0_8uK2I*"); + 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 R.ok(result); + } + + /** + * 获取评分 + * @return + */ + @GetMapping("/getScore") + public R score() { + String url = "https://waimaieapp.meituan.com/gw/customer/comment/scores"; + Map params = new HashMap<>(); + params.put("ignoreSetRouterProxy",true); + params.put("acctId","196975850"); + params.put("wmPoiId","21737990"); + params.put("token","08Y-B6R3RJh0vWW3sunjIls8g5ks282V0XDqw0_8uK2I*"); + params.put("appType",3); + params.put("source",1); + String result = HttpClientUtilT.doGet(url,null,params); + return R.ok(result); + } + +} diff --git a/ruoyi-admin/src/main/java/com/ruoyi/business/utils/HttpClientUtilT.java b/ruoyi-admin/src/main/java/com/ruoyi/business/utils/HttpClientUtilT.java new file mode 100644 index 0000000..d439dc1 --- /dev/null +++ b/ruoyi-admin/src/main/java/com/ruoyi/business/utils/HttpClientUtilT.java @@ -0,0 +1,469 @@ +package com.ruoyi.business.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.methods.*; +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("文件转码异常!"); + } + } + +}