移动utils

登录首页调整
This commit is contained in:
wangshuai 2024-07-03 11:18:55 +08:00
parent 9b9bdd713c
commit 4fee2b0002
8 changed files with 558 additions and 22 deletions

View File

@ -66,27 +66,6 @@
<artifactId>ruoyi-generator</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.10</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>fluent-hc</artifactId>
<version>4.5.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.1</version>
</dependency>
</dependencies>
<build>

View File

@ -128,6 +128,29 @@
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- http -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.10</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>fluent-hc</artifactId>
<version>4.5.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.1</version>
</dependency>
</dependencies>
</project>

View File

@ -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<String, String> headers, Map<String, Object> 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<Entry<String, Object>> entrySet = params.entrySet();
for (Entry<String, Object> 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<String, String> headers
, Map<String, Object> 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<Entry<String, Object>> entrySet = params.entrySet();
for (Entry<String, Object> 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<String, String> 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<String, String> 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<String, String> 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<String, String> 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<String, String> headers
, Map<String, Object> 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<String, String> header, HttpRequestBase httpMethod) {
// 封装请求头
if (header != null && header.size() > 0) {
Set<Entry<String, String>> entrySet = header.entrySet();
for (Entry<String, String> 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<String, Object> params, HttpEntityEnclosingRequestBase httpMethod) {
try {
// 封装请求参数
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setCharset(Charset.forName("utf-8"));
//加上此行代码解决返回中文乱码问题
builder.setMode(HttpMultipartMode.RFC6532);
String fileName;
for (Entry<String, MultipartFile> 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("文件转码异常!");
}
}
}

View File

@ -0,0 +1,4 @@
package com.ruoyi.business.service;
public interface IElemoService {
}

View File

@ -0,0 +1,5 @@
package com.ruoyi.business.service;
public interface IMeituanService {
String getComments();
}

View File

@ -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 {
}

View File

@ -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<BusStoreInfo> 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<String,Object> 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;
}
}

View File

@ -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 }
}