package com.xzx.gc.common.utils;
|
|
import cn.hutool.core.codec.Base64;
|
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.convert.Convert;
|
import cn.hutool.core.date.DateTime;
|
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.lang.Dict;
|
import cn.hutool.core.map.MapUtil;
|
import cn.hutool.core.text.StrBuilder;
|
import cn.hutool.core.util.NumberUtil;
|
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.extra.emoji.EmojiUtil;
|
import cn.hutool.json.JSONUtil;
|
import com.xzx.gc.common.constant.CommonEnum;
|
import com.xzx.gc.common.constant.Constants;
|
import com.xzx.gc.common.constant.QueueEnum;
|
import com.xzx.gc.common.constant.RedisKeyConstant;
|
import com.xzx.gc.common.dto.log.ConsoleContentDto;
|
import com.xzx.gc.common.dto.log.ConsoleDto;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Component;
|
|
import javax.servlet.http.HttpServletRequest;
|
import java.math.BigDecimal;
|
import java.math.RoundingMode;
|
import java.util.*;
|
|
@Component
|
@Slf4j
|
public class BusinessUtil {
|
|
@Autowired
|
private RedisUtil redisUtil;
|
|
private static final int REPEAT_COUNT=30;
|
|
/**
|
* 保存登录信息到redis
|
* @param ip
|
* @param userId
|
*/
|
public void saveLoginInfoToRedis(String ip,String userId){
|
//记录最后1次登录时间
|
String format = DateUtil.format(new Date(), DateUtils.DATE_FORMAT_YMDHMS);
|
Dict set = Dict.create().set("time", format).set("ip", ip);
|
String s = JSONUtil.toJsonStr(set);
|
if(!redisUtil.hexists(RedisKeyConstant.USER_LOGIN_INFO,userId)) {
|
redisUtil.hset(RedisKeyConstant.USER_LOGIN_INFO, userId, s);
|
}
|
}
|
|
|
/**
|
* 手机号脱敏
|
* @param mobile
|
* @return
|
*/
|
public String changeMobile(String mobile){
|
if(StrUtil.isNotBlank(mobile)&&mobile.length()==11){
|
String replace = StrUtil.replace(mobile, 3, 7, Convert.toChar("*"));
|
return replace;
|
}
|
return mobile;
|
}
|
|
|
/**
|
* 获取真实版本号
|
* @param version
|
* @return
|
*/
|
public Double getVersion(String version){
|
if(StrUtil.isNotBlank(version)) {
|
return Convert.toDouble(StrUtil.removePrefix(version, "v"));
|
}
|
return null;
|
}
|
/**
|
* 是否要加密
|
* @param version 版本号
|
* @return
|
*/
|
public boolean isAuth(String version){
|
// if(StrUtil.isNotBlank(version)&&version.startsWith("v")) {
|
// double versionDouble = 0;
|
// try {
|
// versionDouble = Convert.toDouble(StrUtil.removePrefix(version,"v"));
|
// } catch (Exception e) {
|
// return false;
|
// }
|
// if (NumberUtil.compare(versionDouble, Convert.toDouble("1.2")) > 0) {
|
// return true;
|
// }
|
// }
|
// return false;
|
return true;
|
}
|
|
|
/**
|
* 是否验证的url地址
|
* @param url
|
* @return
|
*/
|
public boolean isAuthUrl(String url){
|
List<String> noAuthUrls = Constants.NO_AUTH_URLS;
|
for (String noAuthUrl : noAuthUrls) {
|
if(url.contains(noAuthUrl)){
|
return false;
|
}
|
}
|
return true;
|
}
|
|
/**
|
* 是否验证的方法名称
|
* @param url
|
* @return
|
*/
|
public boolean isAuthMethod(String url){
|
List<String> noAuthUrls = Constants.NO_AUTH_METHODS;
|
for (String noAuthUrl : noAuthUrls) {
|
if(url.equals(noAuthUrl)){
|
return false;
|
}
|
}
|
return true;
|
}
|
|
/**
|
* 格式化输入请求参数
|
* @param getArgs
|
* @return
|
*/
|
public String prettyInParam( Object[] getArgs){
|
List<Object> objects = new ArrayList<>(Arrays.asList(getArgs));
|
String param="{}";
|
if(CollUtil.isNotEmpty(objects)) {
|
objects.removeIf(next -> next instanceof HttpServletRequest);
|
if(objects.size()==0){
|
return param;
|
}else if(objects.size()==1){
|
if(objects.get(0)==null){
|
return param;
|
}else {
|
if (objects.get(0).toString().startsWith("<xml>") && (objects.get(0).toString().endsWith("</xml>"))) {
|
return objects.get(0).toString();
|
} else {
|
return JSONS.format(objects.get(0), -1, -1);
|
}
|
}
|
}else{
|
return JSONS.format(objects,-1,-1);
|
}
|
}
|
return param;
|
|
}
|
|
/**
|
* 格式化输出输出参数
|
* @param result
|
* @return
|
*/
|
public String prettyOut( Object result){
|
if(result!=null) {
|
try {
|
return JSONS.format(result,-1,-1);
|
} catch (Exception e) {
|
return result.toString();
|
}
|
}
|
return "";
|
}
|
|
|
public String changeArea(String townName){
|
if("宁乡县".equals(townName)){
|
return "宁乡市";
|
}
|
return townName;
|
}
|
|
/**
|
* 格式化头部
|
* @return
|
*/
|
public String prettyHeader( Map<String, String> headerMaps){
|
Dict dict=Dict.create();
|
//键全变成了小写
|
if(MapUtil.isNotEmpty(headerMaps)){
|
// dict.set("token",headerMaps.get("token")).set("timestamp",headerMaps.get("timestamp")).set("nonce",headerMaps.get("nonce")).set("sign",headerMaps.get("sign"))
|
// .set("userId",headerMaps.get("userId")).set("authKey",headerMaps.get("authkey")).set("version",headerMaps.get("version"));
|
dict.set("userId",headerMaps.get("userId")).set("version",headerMaps.get("version"));
|
}
|
return JSONS.format(dict,-1,-1);
|
}
|
|
|
/**
|
* 获取访问路径
|
* @return
|
*/
|
public String getViewUrl(){
|
String urlPrefix=null;
|
if(SpringUtil.isProdOrCloud()){
|
urlPrefix=Constants.IMG_VIEW_PATH;
|
}else if(SpringUtil.isTest()){
|
urlPrefix="http://192.168.0.100/resource/static/upload";
|
}else if(SpringUtil.isCheck()){
|
urlPrefix="https://wxtest.cnxzx.com/resource/static/upload";
|
} else{
|
// urlPrefix="http://192.168.0.18/static/upload";
|
urlPrefix="http://120.27.238.55:8002/static/upload";
|
}
|
return urlPrefix;
|
}
|
|
|
/**
|
* 处理金额 保留2位小数
|
* @param s
|
* @return
|
*/
|
public String changeMoney(String s){
|
if(StrUtil.isBlank(s)){
|
s=Convert.toStr(Constants.MONEY_INIT);
|
}
|
return NumberUtil.roundStr(s, 2, RoundingMode.DOWN);
|
}
|
|
public String changeMoney(BigDecimal s){
|
if(s==null){
|
s= Constants.MONEY_INIT;
|
}
|
String money=s.toString();
|
return NumberUtil.roundStr(money, 2, RoundingMode.DOWN);
|
}
|
|
/**
|
* 处理重量 保留3位小数
|
* @param s
|
* @return
|
*/
|
public String changeWeight(String s){
|
if(StrUtil.isBlank(s)){
|
s=Convert.toStr(Constants.WEIGHT_INIT);
|
}
|
return NumberUtil.roundStr(s, 3, RoundingMode.DOWN);
|
}
|
|
|
|
|
/**
|
* 打印
|
* @param
|
* @return
|
*/
|
public String console(ConsoleDto consoleDto){
|
String laughing = EmojiUtil.toUnicode(":"+consoleDto.getEmjio()+":");
|
StrBuilder strBuilder = StrBuilder.create(" "+consoleDto.getHeader()+":\n\n");
|
strBuilder.append("┏"+StrUtil.repeat("━",REPEAT_COUNT)+" "+laughing+consoleDto.getTitle()+"请求开始"+laughing+" "+StrUtil.repeat("━",REPEAT_COUNT)+"\n");
|
for (ConsoleContentDto consoleContentDto : consoleDto.getList()) {
|
strBuilder.append("┣ "+consoleContentDto.getName()+": "+consoleContentDto.getValue()+" \n");
|
}
|
strBuilder.append("┗"+StrUtil.repeat("━",REPEAT_COUNT)+" "+laughing+consoleDto.getTitle()+"请求结束"+laughing+" "+StrUtil.repeat("━",REPEAT_COUNT)+"\n");
|
return strBuilder.toString();
|
}
|
|
public String checkProject(String url){
|
String s = StrUtil.removePrefix(url, "/");
|
String s1 = StrUtil.removePrefix(s, "/");
|
if(s1.startsWith("gc-user")){
|
return "用户服务";
|
}else if(s1.startsWith("gc-pay")){
|
return "支付服务";
|
}else if(s1.startsWith("gc-order")){
|
return "订单服务";
|
}else if(s1.startsWith("gc-sys")){
|
return "消息服务";
|
}else if(s1.startsWith("gc-role")){
|
return "权限服务";
|
}else {
|
return "服务";
|
}
|
}
|
|
|
|
|
/**
|
* 获取设备类型名称
|
* @param clientType
|
* @return
|
*/
|
public String getClientTypeName(String clientType){
|
if(StringUtils.isNotBlank(clientType)){
|
if("1".equals(clientType)){
|
clientType="苹果";
|
}else if("2".equals(clientType)) {
|
clientType = "安卓";
|
} else if("3".equals(clientType)){
|
clientType="WEB";
|
}
|
}else{
|
clientType="未知";
|
}
|
return clientType;
|
}
|
|
/**
|
* 是否是web请求
|
* @param clientType 客户端类型
|
* @return
|
*/
|
public boolean isWeb(String clientType){
|
if(CommonEnum.WEB.getValue().equals(clientType)){
|
return true;
|
}else {
|
return false;
|
}
|
}
|
|
/**
|
* 是否是App请求
|
* @param clientType 客户端类型
|
* @return
|
*/
|
public boolean isApp(String clientType){
|
if(CommonEnum.安卓.getValue().equals(clientType)||CommonEnum.苹果.getValue().equals(clientType)){
|
return true;
|
}else {
|
return false;
|
}
|
}
|
|
/**
|
* 输出
|
* @param title
|
* @return
|
*/
|
public String repeatService(String title){
|
return StrUtil.repeat("*",30)+title+StrUtil.repeat("*",30);
|
}
|
|
|
/**
|
* 队列输出
|
* @param queueName
|
* @param uuid
|
* @param errMsg
|
*/
|
public void queueConsole(String queueName,String uuid,String errMsg){
|
ConsoleDto consoleDto=new ConsoleDto();
|
consoleDto.setEmjio("snake");
|
consoleDto.setTitle("队列发送");
|
consoleDto.setHeader("您有新的队列");
|
List<ConsoleContentDto> list=CollUtil.newArrayList();
|
|
ConsoleContentDto consoleContentDto=new ConsoleContentDto();
|
consoleContentDto.setName("队列标识");
|
consoleContentDto.setValue(uuid);
|
list.add(consoleContentDto);
|
|
consoleContentDto=new ConsoleContentDto();
|
consoleContentDto.setName("队列名称");
|
consoleContentDto.setValue(queueName);
|
list.add(consoleContentDto);
|
|
consoleContentDto=new ConsoleContentDto();
|
consoleContentDto.setName("队列描述");
|
consoleContentDto.setValue(QueueEnum.getDescByName(queueName));
|
list.add(consoleContentDto);
|
|
if(StrUtil.isNotBlank(errMsg)) {
|
consoleContentDto = new ConsoleContentDto();
|
consoleContentDto.setName("异常信息");
|
consoleContentDto.setValue(errMsg);
|
list.add(consoleContentDto);
|
}
|
|
consoleContentDto = new ConsoleContentDto();
|
consoleContentDto.setName("发送状态");
|
if(StrUtil.isBlank(errMsg)){
|
consoleContentDto.setValue("发送成功!");
|
}else{
|
consoleContentDto.setValue("发送失败!");
|
}
|
list.add(consoleContentDto);
|
|
consoleDto.setList(list);
|
// String console = console(consoleDto);
|
// log.debug(console);
|
}
|
|
|
|
public BigDecimal changeMul(BigDecimal stepMulForReceiver){
|
if(stepMulForReceiver==null){
|
stepMulForReceiver=BigDecimal.ZERO;
|
}else{
|
stepMulForReceiver=Convert.toBigDecimal(changeMoney(Convert.toStr(stepMulForReceiver,"0")));
|
}
|
return stepMulForReceiver;
|
}
|
|
public BigDecimal changeMul(String stepMulForReceiver){
|
BigDecimal mul=StrUtil.isNotBlank(stepMulForReceiver)?Convert.toBigDecimal(stepMulForReceiver):Constants.MONEY_INIT;
|
return NumberUtil.round(mul, 2, RoundingMode.DOWN);
|
}
|
|
|
/**
|
* 是否是后台请求
|
* @param url
|
* @return
|
*/
|
public boolean isAdmin(String url){
|
return url.contains("/admin");
|
}
|
|
|
/**
|
* 计算日环比
|
* @return
|
*/
|
public String getRatio(BigDecimal yesterday,BigDecimal today){
|
//回收日环比计算方法: (本日回收金额/昨日回收金额 — 1 )× 100% 正值为增长率 负值为下降率
|
String recyleRatio="";
|
if (yesterday.compareTo(BigDecimal.ZERO) != 0) {
|
BigDecimal mul = NumberUtil.mul(NumberUtil.sub(NumberUtil.div(today, yesterday), 1), 100);
|
if(mul.compareTo(BigDecimal.ZERO)>=0){
|
recyleRatio = recyleRatio+"+";
|
}
|
recyleRatio =recyleRatio+ changeMoney(mul) + "%";
|
} else {
|
recyleRatio = "+100.00%";
|
}
|
return recyleRatio;
|
}
|
|
|
|
/**
|
* 是否是特别的区域 主要指1个区多合伙人
|
* @param townShipId
|
* @return
|
*/
|
public boolean isSpecialArea(String townShipId){
|
if(Constants.YUELU_CODE.equals(townShipId)){
|
return true;
|
}else if(Constants.YUHUA_CODE.equals(townShipId)){
|
return true;
|
}
|
return false;
|
}
|
|
/**
|
* 否是特别的区域 根据adoce查找。主要指1个区多合伙人
|
* @param adcode
|
* @return
|
*/
|
public boolean isSpecialAreaByAdcode(String adcode){
|
if(Constants.YUELU_ADCODE.equals(adcode)){
|
return true;
|
}else if(Constants.YUHUA_ADCODE.equals(adcode)){
|
return true;
|
}
|
return false;
|
}
|
|
/**
|
* 检查验证码
|
* @param code
|
* @param phone
|
* @return
|
*/
|
public boolean checkSmsCode(String code,String phone){
|
String s = redisUtil.get(RedisKeyConstant.SMS_CODE + phone);
|
if(StrUtil.isNotBlank(s)&&StrUtil.isNotBlank(code)&&s.equals(code)){
|
return true;
|
}
|
return false;
|
}
|
|
|
public static void main(String[] args) {
|
}
|
|
}
|