package com.nuvole.util;
|
// @formatter:off
|
|
import cn.hutool.core.convert.Convert;
|
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.URLUtil;
|
import cn.hutool.crypto.SecureUtil;
|
import com.alibaba.fastjson.JSON;
|
import com.nuvole.constants.SystemConstants;
|
import jakarta.servlet.ServletInputStream;import jakarta.servlet.http.HttpServletRequest;import jakarta.servlet.http.HttpServletResponse;import org.apache.commons.text.StringEscapeUtils;
|
import org.springframework.web.context.request.RequestContextHolder;
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
import java.io.BufferedReader;
|
import java.io.IOException;
|
import java.io.InputStreamReader;
|
import java.util.HashMap;
|
import java.util.Iterator;
|
import java.util.List;
|
import java.util.Map;
|
|
/**
|
* .-~~~~~~~~~-._ _.-~~~~~~~~~-.
|
* __.' @Author ~. .~ 代码无Bug `.__
|
* .'// liu.q \./ (秘籍) \\`.
|
* .'// [916000612@qq.com] | 欲练神功 引刀自宫 \\`.
|
* .'// .-~"""""""~~~~-._ | _,-~~~~"""""""~-. \\`.
|
* .'//.-" 2019-04-09 `-. | .-' 10:48 "-.\\`.
|
* .'//______.============-.. \ | / ..-============.______\\`.
|
* .'______________________________\|/______________________________`.
|
*
|
* @Description :
|
*/
|
// @formatter:on
|
public class CommonUtil {
|
|
/**
|
* @Author : liuq
|
* @Date : Create in 2018/7/1 上午1:20
|
* @Description : 驼峰转下划线
|
*/
|
public static String camel2Underline(String line) {
|
return StrUtil.toUnderlineCase(line);
|
}
|
|
/*
|
* @Author : liu.q [916000612@qq.com]
|
*
|
* @Date : 2019-04-29 16:45
|
*
|
* @Description : 下划线转驼峰
|
*/
|
public static String Underline2camel(String para) {
|
return StrUtil.toCamelCase(para);
|
}
|
|
/**
|
* @Author : liu.q
|
* @Date : 2019-03-01 09:49
|
* @Description : 获取request
|
*/
|
public static HttpServletRequest getRequest() {
|
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
|
.getRequestAttributes();
|
HttpServletRequest request = requestAttributes.getRequest();
|
return request;
|
}
|
|
/**
|
* @Author : liu.q
|
* @Date : 2019-03-01 09:49
|
* @Description : response
|
*/
|
public static HttpServletResponse getResponse() {
|
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
|
.getRequestAttributes();
|
HttpServletResponse response = requestAttributes.getResponse();
|
return response;
|
}
|
|
/**
|
* @Author : liu.q
|
* @Date : 2019-03-01 09:51
|
* @Description : 获取访问地址
|
*/
|
public static String getHost() {
|
HttpServletRequest request = getRequest();
|
String scheme = request.getHeader("X-Forwarded-Scheme");
|
if (scheme == null) {
|
scheme = request.getScheme();
|
}
|
String port = request.getHeader("X-Forwarded-port");
|
if (port == null) {
|
port = request.getServerPort() + "";
|
}
|
if ("443".equals(port) || "80".equals(port)) {
|
return scheme + "://" + request.getServerName();
|
}
|
return scheme + "://" + request.getServerName() + ":" + port;// +request.getRequestURI();
|
}
|
|
/**
|
* 获取请求参数
|
*
|
* @return
|
*/
|
public static Map getParameterMap() {
|
// 参数Map
|
Map properties = CommonUtil.getRequest().getParameterMap();
|
// 返回值Map
|
Map returnMap = new HashMap();
|
Iterator entries = properties.entrySet().iterator();
|
Map.Entry entry;
|
String name = "";
|
String value = "";
|
while (entries.hasNext()) {
|
entry = (Map.Entry) entries.next();
|
name = (String) entry.getKey();
|
Object valueObj = entry.getValue();
|
if (null == valueObj) {
|
continue;
|
} else if (valueObj instanceof String[]) {
|
String[] values = (String[]) valueObj;
|
for (int i = 0; i < values.length; i++) {
|
value = values[i] + ",";
|
}
|
value = value.substring(0, value.length() - 1);
|
} else {
|
value = valueObj.toString();
|
// 当value为string empty为有效值 by zxc on 2020/5/11 15:00
|
if ("".equals(value)) {
|
continue;
|
}
|
}
|
returnMap.put(name, value);
|
}
|
return returnMap;
|
}
|
|
public static String getExportSign(String url, String timestamp) {
|
System.out.println("is url:" + URLUtil.getPath(url));
|
return Convert
|
.toStr(SecureUtil.md5(SecureUtil.md5(URLUtil.getPath(url) + timestamp) + SystemConstants.EXPORT_SIGN_K).hashCode());
|
}
|
|
public static String listToString(List<String> items) {
|
String result = "";
|
if (items != null && items.size() > 0) {
|
for (int i = 0; i < items.size(); i++) {
|
if (i == 0) {
|
result += items.get(i);
|
} else {
|
result += "," + items.get(i);
|
}
|
}
|
}
|
return result;
|
}
|
|
public static <T> T getObjFromReq(Class<T> clazz) {
|
String s = JSON.toJSONString(getParameterMap());
|
return JSON.parseObject(StringEscapeUtils.unescapeHtml4(s), clazz);
|
}
|
|
public static <T> T getObjFromReqBody(Class<T> clazz) {
|
String jsonStr = getRequestBody();
|
return JSON.parseObject(StringEscapeUtils.unescapeHtml4(jsonStr), clazz);
|
}
|
|
/**
|
* 获取request body值
|
*
|
* @param
|
* @return
|
*/
|
private static String getRequestBody() {
|
HttpServletRequest request = getRequest();
|
try {
|
return getStringFromInputStream(request.getInputStream());
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
public static String getStringFromInputStream(ServletInputStream stream) throws IOException {
|
StringBuilder sb = new StringBuilder();
|
BufferedReader reader = null;
|
try {
|
reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
|
char[] ch = new char[1024];
|
int bytesRead;
|
while ((bytesRead = reader.read(ch)) != -1) {
|
sb.append(ch, 0, bytesRead);
|
}
|
} finally {
|
if (reader != null) {
|
try {
|
reader.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
return sb.toString();
|
}
|
|
/**
|
* 获取用户标识
|
*
|
* @return
|
*/
|
public static String getUserAgent() {
|
HttpServletRequest request = getRequest();
|
return request.getHeader("User-Agent");
|
}
|
|
/**
|
* 获取客户端标识
|
*
|
* @return 1.支付宝-小程序, 2.支付宝-生活号, 3.微信-小程序, 4.微信-公众号
|
*/
|
public static String getClientType() {
|
HttpServletRequest request = getRequest();
|
return request.getHeader("CLIENT-TYPE");
|
}
|
|
/**
|
* 是否为微信小程序
|
*
|
* @return
|
*/
|
public static boolean isWxMin() {
|
String userAgent = getUserAgent();
|
return userAgent.contains("MiniProgram");
|
}
|
|
/**
|
* 是否为微信内置浏览器
|
*
|
* @return
|
*/
|
public static boolean isWxBro() {
|
String userAgent = getUserAgent();
|
return userAgent.contains("MicroMessenger");
|
}
|
|
/**
|
* 是否为支付宝内置浏览器
|
*
|
* @return
|
*/
|
public static boolean isAliBro() {
|
String userAgent = getUserAgent();
|
return userAgent.contains("Alipay");
|
}
|
|
/**
|
* 是否为支付宝小程序客户端
|
*
|
* @return
|
*/
|
public static boolean isAliMin() {
|
String userAgent = getUserAgent();
|
String clientType = getClientType();
|
return userAgent.contains("Alipay") && SystemConstants.APP_TYPE_ALI_MINI.equals(clientType);
|
}
|
|
/**
|
* 是否为翼支付
|
*
|
* @return
|
*/
|
public static boolean isBestPayBro() {
|
String userAgent = getUserAgent();
|
return userAgent.contains("Bestpay");
|
}
|
|
/**
|
* 是否为云闪付
|
*
|
* @return
|
*/
|
public static boolean isCloudPayBro() {
|
String userAgent = getUserAgent();
|
return userAgent.contains("CloudPay");
|
}
|
|
public static boolean isUnionPayBro() {
|
String userAgent = getUserAgent();
|
return userAgent.contains("UnionPay");
|
}
|
|
}
|