钱包导出时间不对、第三方扣款

This commit is contained in:
dzc committed 2025-03-05 21:12:39 +08:00
1 parent 8e6d2c6f01
commit 31fe4d3bb4
24 files changed
+369 -121

No files matched your search

@@ -1,10 +1,10 @@
package com.bsd.pjcloud.thirdparty.vo.zy;
import cn.hutool.core.util.ObjectUtil;
import lombok.AllArgsConstructor;
import lombok.Builder;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Map;
@Data
public class ZyBaseVo {
@@ -54,4 +54,9 @@ public class ZyBaseVo {
return ObjectUtil.equal(code, ZyBaseVo.successCode);
}
@JsonIgnore
public Map<String, Object> getErrorMap() {
return Map.of("code", code, "msg", msg);
}
}
@@ -27,7 +27,7 @@ public class ThirdZySelfController {
@Inner(value = false)
@GetMapping("/checkSystemStatus")
@Operation(summary = "第三方url健康探测", description = "第三方url健康探测")
@Idempotent(expireTime = 10, info = "请勿重复调用")
@Idempotent(expireTime = 3, info = "请勿重复调用")
public Map<String, Object> checkSystemStatus() {
return zySelfService.checkSystemStatus();
}
@@ -35,7 +35,7 @@ public class ThirdZySelfController {
@Inner(value = false)
@GetMapping("/getToken")
@Operation(summary = "第三方提供获取token", description = "第三方提供获取token")
@Idempotent(expireTime = 10, info = "请勿重复调用")
@Idempotent(expireTime = 3, info = "请勿重复调用")
public ZySelfGetTokenVo getToken(ZySelfGetTokenDto params) {
return zySelfService.getToken(params);
}
@@ -43,15 +43,14 @@ public class ThirdZySelfController {
@Inner(value = false)
@PostMapping("/correct")
@Operation(summary = "第三方一卡通系统提供冲正接口", description = "第三方一卡通系统提供冲正接口")
public ZySelfCorrectVo correct(ZySelfCorrectDto params, @RequestParam(value = "access_token", required = false) String accessToken) {
public Map<String, Object> correct(ZySelfCorrectDto params, @RequestParam(value = "access_token", required = false) String accessToken) {
return zySelfService.correct(params, accessToken);
}
@Inner(value = false)
@PostMapping("/pay")
@Operation(summary = "第三方一卡通系统提供扣费接口", description = "第三方一卡通系统提供扣费接口")
@Idempotent(expireTime = 10, info = "请勿重复调用")
public ZySelfPayVo pay(ZySelfPayDto params, @RequestParam(value = "access_token", required = false) String accessToken) {
public Map<String, Object> pay(ZySelfPayDto params, @RequestParam(value = "access_token", required = false) String accessToken) {
return zySelfService.pay(params, accessToken);
}
@@ -28,4 +28,12 @@ public interface ThirdZyIdDictMapper extends PjcloudBaseMapper<ThirdZyIdDictEnti
return selectList(wrapper);
}
default ThirdZyIdDictEntity getByZyIds(ThirdZyBizEnumType type, String zyId) {
if (ObjectUtil.isEmpty(type)) return null;
LambdaQueryWrapper<ThirdZyIdDictEntity> wrapper = new LambdaQueryWrapper<ThirdZyIdDictEntity>()
.eq(ObjectUtil.isNotEmpty(zyId), ThirdZyIdDictEntity::getZyId, zyId)
.eq(ThirdZyIdDictEntity::getBizType, type.getType());
return selectOne(wrapper);
}
}
@@ -24,10 +24,10 @@ public interface ThirdZySelfService {
/**
* 第三方一卡通系统提供冲正接口
*/
ZySelfCorrectVo correct(ZySelfCorrectDto params, String accessToken);
Map<String, Object> correct(ZySelfCorrectDto params, String accessToken);
/**
* 第三方一卡通系统提供扣费接口
*/
ZySelfPayVo pay(ZySelfPayDto params, String accessToken);
Map<String, Object> pay(ZySelfPayDto params, String accessToken);
}
@@ -23,9 +23,7 @@ import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@Service
@Validated
@@ -66,7 +64,7 @@ public class ThirdZyForeignServiceImpl implements ThirdZyForeignService {
Map<String, Object> encryptionBody = BeanUtil.beanToMap(dto);
entity.setApiUrl(url);
entity.setOrgReq(JSONUtil.toJsonStr(encryptionBody));
Map<String, Object> body = zyApiTool.getEncryptionMap(encryptionBody);
Map<String, Object> body = zyApiTool.getZyEncryptionMap(encryptionBody);
entity.setEncipherReq(JSONUtil.toJsonStr(body));
String res = HttpRequest.post(url).form(body).execute().body();
if (ObjectUtil.isEmpty(res)) {
@@ -55,21 +55,18 @@ public class ThirdZyMerServiceImpl implements ThirdZyMerService {
// 转换商户id
List<String> mchList = dto.stream().map(ZyMerSyncDealerInfoDto::getDealerKeyObj).distinct().toList();
Map<String, String> mchIdDict = zyApiTool.generateZyId(bizTYpe, mchList);
List<Map<String, Object>> listMap = new ArrayList<>();
for (ZyMerSyncDealerInfoDto v : dto) {
v.setEPID(deptDdDict.get(v.getEPID()));
v.setDealerKeyObj(mchIdDict.get(v.getDealerKeyObj()));
Map<String, Object> map = zyApiTool.removeEmptyMap(BeanUtil.beanToMap(v));
if (ObjectUtil.isNotEmpty(map)) listMap.add(map);
}
String url = zyConfig.getBaseUrl() + ZyApi.Mer.SYNC_DEALER_INFO;
try {
url = String.format(url, zyApiTool.getToken());
Map<String, Object> encryptionBody = new HashMap<>();
encryptionBody.put("dealers", listMap);
encryptionBody.put("dealers", dto);
entity.setApiUrl(url);
entity.setOrgReq(JSONUtil.toJsonStr(encryptionBody));
Map<String, Object> body = zyApiTool.getEncryptionMap(encryptionBody);
Map<String, Object> body = zyApiTool.getZyEncryptionMap(encryptionBody);
entity.setEncipherReq(JSONUtil.toJsonStr(body));
String res = HttpRequest.post(url).form(body).execute().body();
if (ObjectUtil.isEmpty(res)) {
@@ -103,23 +100,20 @@ public class ThirdZyMerServiceImpl implements ThirdZyMerService {
ThirdZyLogEntity entity = new ThirdZyLogEntity();
entity.setBizType(bizType.getType());
long startTime = System.currentTimeMillis();
List<Map<String, Object>> listMap = new ArrayList<>();
// 转换zyId
List<String> list = dto.stream().map(ZyMerSyncEnterpriseInfoDto::getEPID).distinct().toList();
Map<String, String> idDict = zyApiTool.generateZyId(bizType, list);
for (ZyMerSyncEnterpriseInfoDto v : dto) {
v.setEPID(idDict.get(v.getEPID()));
Map<String, Object> map = zyApiTool.removeEmptyMap(BeanUtil.beanToMap(v));
if (ObjectUtil.isNotEmpty(map)) listMap.add(map);
}
String url = zyConfig.getBaseUrl() + ZyApi.Mer.SYNC_ENTERPRISE_INFO;
try {
url = String.format(url, zyApiTool.getToken());
Map<String, Object> encryptionBody = new HashMap<>();
encryptionBody.put("enterprises", listMap);
encryptionBody.put("enterprises", dto);
entity.setApiUrl(url);
entity.setOrgReq(JSONUtil.toJsonStr(encryptionBody));
Map<String, Object> body = zyApiTool.getEncryptionMap(encryptionBody);
Map<String, Object> body = zyApiTool.getZyEncryptionMap(encryptionBody);
entity.setEncipherReq(JSONUtil.toJsonStr(body));
String res = HttpRequest.post(url).form(body).execute().body();
if (ObjectUtil.isEmpty(res)) {
@@ -164,6 +158,7 @@ public class ThirdZyMerServiceImpl implements ThirdZyMerService {
Map<String, String> roleIdDict = zyApiTool.generateZyId(bizType, roleList);
for (ZyMerSyncAccountInfoDto v : dto) {
v.setEPID(deptIdDict.get(v.getEPID()));
v.setPerCode(userIdDict.get(v.getPerCode()));
v.setAccNum(userIdDict.get(v.getAccNum()));
v.setAccClassID(roleIdDict.get(v.getAccClassID()));
}
@@ -179,7 +174,7 @@ public class ThirdZyMerServiceImpl implements ThirdZyMerService {
encryptionBody.put("currentMaxFixId", String.valueOf(maxFixId.getAsLong()));
entity.setApiUrl(url);
entity.setOrgReq(JSONUtil.toJsonStr(encryptionBody));
Map<String, Object> body = zyApiTool.getEncryptionMap(encryptionBody, "data", "currentMaxFixId");
Map<String, Object> body = zyApiTool.getZyEncryptionMap(encryptionBody, "data", "currentMaxFixId");
entity.setEncipherReq(JSONUtil.toJsonStr(body));
String res = HttpRequest.post(url).form(body).execute().body();
if (ObjectUtil.isEmpty(res)) {
@@ -219,21 +214,18 @@ public class ThirdZyMerServiceImpl implements ThirdZyMerService {
long startTime = System.currentTimeMillis();
String url = zyConfig.getBaseUrl() + ZyApi.Mer.SYNC_ACC_CLASS_INFO;
try {
List<Map<String, Object>> listMap = new ArrayList<>();
// 转换zyId
List<String> roleList = dto.stream().map(ZyMerSyncAccClassInfoDto::getEPID).distinct().toList();
Map<String, String> roleIdDict = zyApiTool.generateZyId(bizType, roleList);
for (ZyMerSyncAccClassInfoDto v : dto) {
v.setEPID(roleIdDict.get(v.getEPID()));
Map<String, Object> map = zyApiTool.removeEmptyMap(BeanUtil.beanToMap(v));
if (ObjectUtil.isNotEmpty(map)) listMap.add(map);
}
url = String.format(url, zyApiTool.getToken());
Map<String, Object> encryptionBody = new HashMap<>();
encryptionBody.put("accClasses", listMap);
encryptionBody.put("accClasses", dto);
entity.setApiUrl(url);
entity.setOrgReq(JSONUtil.toJsonStr(encryptionBody));
Map<String, Object> body = zyApiTool.getEncryptionMap(encryptionBody);
Map<String, Object> body = zyApiTool.getZyEncryptionMap(encryptionBody);
entity.setEncipherReq(JSONUtil.toJsonStr(body));
String res = HttpRequest.post(url).form(body).execute().body();
if (ObjectUtil.isEmpty(res)) {
@@ -1,21 +1,31 @@
package com.bsd.pjcloud.thirdparty.service.impl.zy;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.bsd.pjcloud.common.core.constant.CacheConstants;
import com.bsd.pjcloud.common.core.util.R;
import com.bsd.pjcloud.hard.api.dto.third.HardCallbackPayDto;
import com.bsd.pjcloud.hard.api.feign.RemoteHardThirdSelfZyService;
import com.bsd.pjcloud.hard.api.vo.third.HardCallbackPayVo;
import com.bsd.pjcloud.thirdparty.config.ZyConfig;
import com.bsd.pjcloud.thirdparty.constant.ThirdZyBizEnumType;
import com.bsd.pjcloud.thirdparty.dto.zy.self.ZySelfCorrectDto;
import com.bsd.pjcloud.thirdparty.dto.zy.self.ZySelfGetTokenDto;
import com.bsd.pjcloud.thirdparty.dto.zy.self.ZySelfPayDto;
import com.bsd.pjcloud.thirdparty.dto.zy.self.ZySelfTokenDto;
import com.bsd.pjcloud.thirdparty.entity.zy.ThirdZyIdDictEntity;
import com.bsd.pjcloud.thirdparty.entity.zy.ThirdZyLogEntity;
import com.bsd.pjcloud.thirdparty.mapper.zy.ThirdZyIdDictMapper;
import com.bsd.pjcloud.thirdparty.service.impl.ThirdZySelfService;
import com.bsd.pjcloud.thirdparty.utils.zy.api.ZyApi;
import com.bsd.pjcloud.thirdparty.utils.zy.api.ZyApiTool;
import com.bsd.pjcloud.thirdparty.vo.zy.ZyBaseVo;
import com.bsd.pjcloud.thirdparty.vo.zy.self.ZySelfCorrectVo;
import com.bsd.pjcloud.thirdparty.vo.zy.self.ZySelfGetTokenVo;
@@ -29,6 +39,8 @@ import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.TimeUnit;
@@ -45,9 +57,12 @@ public class ThirdZySelfServiceImpl implements ThirdZySelfService {
private final StringRedisTemplate redisTemplate;
private final ThirdZyLogServiceImpl thirdZyLogService;
private final ThirdZyIdDictMapper thirdZyIdDictMapper;
private final RemoteHardThirdSelfZyService hardCallback;
private final ZyApiTool zyApiTool;
/**
* 第三方url健康探测
*/
@@ -79,13 +94,13 @@ public class ThirdZySelfServiceImpl implements ThirdZySelfService {
* 第三方一卡通系统提供冲正接口
*/
@Override
public ZySelfCorrectVo correct(ZySelfCorrectDto params, String accessToken) {
public Map<String, Object> correct(ZySelfCorrectDto params, String accessToken) {
ThirdZyBizEnumType bizType = ThirdZyBizEnumType.SELF_CORRECT;
ThirdZyLogEntity entity = new ThirdZyLogEntity();
entity.setBizType(bizType.getType());
long startTime = System.currentTimeMillis();
ZySelfCorrectVo result = new ZySelfCorrectVo();
try {
ZySelfCorrectVo result = new ZySelfCorrectVo();
result.setCode(ZyBaseVo.errorCode);
boolean isToken = checkIsToken(accessToken);
// 判断token
@@ -96,19 +111,19 @@ public class ThirdZySelfServiceImpl implements ThirdZySelfService {
throw new RuntimeException(msg);
}
// 校验加密参数
boolean isSign = checkSign(params);
if (!isSign) {
String resJson = checkSign(params);
if (ObjectUtil.isEmpty(resJson)) {
String msg = "数据加密认证失败!sign值验证失败";
result.setCode(ZyBaseVo.signError);
result.setMsg(msg);
throw new RuntimeException(msg);
}
return null;
return BeanUtil.beanToMap(result);
} catch (Exception e) {
String error = String.format("|%s||correct|意外的错误error|%s|stack|%s|", TAG, e.getMessage(), Arrays.toString(e.getStackTrace()));
log.error(error);
entity.setError(error);
return null;
return result.getErrorMap();
} finally {
long elapsedTime = System.currentTimeMillis() - startTime;
entity.setTimeConsuming(Long.toString(elapsedTime));
@@ -120,7 +135,7 @@ public class ThirdZySelfServiceImpl implements ThirdZySelfService {
* 第三方一卡通系统提供扣费接口
*/
@Override
public ZySelfPayVo pay(ZySelfPayDto params, String accessToken) {
public Map<String, Object> pay(ZySelfPayDto params, String accessToken) {
ThirdZyBizEnumType bizType = ThirdZyBizEnumType.SELF_PAY;
ThirdZyLogEntity entity = new ThirdZyLogEntity();
entity.setBizType(bizType.getType());
@@ -137,19 +152,57 @@ public class ThirdZySelfServiceImpl implements ThirdZySelfService {
throw new RuntimeException(msg);
}
// 校验加密参数
boolean isSign = checkSign(params);
if (!isSign) {
String jsonRes = checkSign(params);
if (ObjectUtil.isEmpty(jsonRes)) {
String msg = "数据加密认证失败!sign值验证失败";
result.setCode(ZyBaseVo.signError);
result.setMsg(msg);
throw new RuntimeException(msg);
}
return result;
result.setCode(ZyBaseVo.errorCode);
result.setMsg(ZyBaseVo.defaultErrorMsg);
ZySelfPayDto dto = JSONUtil.toBean(jsonRes, ZySelfPayDto.class);
String accNum = dto.getAccNum();
ThirdZyIdDictEntity userIdDict = thirdZyIdDictMapper.getByZyIds(bizType, accNum);
if (ObjectUtil.isEmpty(userIdDict)) {
throw new RuntimeException("用户id不存在");
}
String amount = dto.getAmount();
if (ObjectUtil.isEmpty(amount)) {
throw new RuntimeException("金额转换失败");
}
int payPrice = new BigDecimal(amount).multiply(BigDecimal.valueOf(100)).intValue();
String dealTime = dto.getDealTime();
LocalDateTime payTime = LocalDateTimeUtil.parse(dealTime, DatePattern.NORM_DATETIME_PATTERN);
// 调用硬件接口
HardCallbackPayDto payDto = new HardCallbackPayDto();
payDto.setUserId(Long.valueOf(userIdDict.getSelfId()));
payDto.setPayPrice(payPrice);
payDto.setPayTime(payTime);
payDto.setOrderNo(dto.getSerialNum());
HardCallbackPayVo payRes = hardCallback.pay(payDto).getData();
result.setCode(payRes.getCode().getType());
switch (payRes.getCode()) {
case SUCCESS -> {
result.setMsg(ZyBaseVo.selfSuccessMsg);
result.setAccNum(payRes.getUserId().toString());
result.setAccName(payRes.getUsername());
result.setPerCode(payRes.getUserId().toString());
result.setPlatformSerialNum(dto.getSerialNum());
result.setSerialNum(dto.getSerialNum());
result.setAmount(amount);
result.setRecDate(LocalDateTimeUtil.format(payRes.getPayTime(), DatePattern.NORM_DATETIME_PATTERN));
return zyApiTool.getSelfEncryptionMap(BeanUtil.beanToMap(result));
}
default -> {
return result.getErrorMap();
}
}
} catch (Exception e) {
String error = String.format("|%s||pay|意外的错误error|%s|stack|%s|", TAG, e.getMessage(), Arrays.toString(e.getStackTrace()));
log.error(error);
entity.setError(error);
return result;
return result.getErrorMap();
} finally {
long elapsedTime = System.currentTimeMillis() - startTime;
entity.setTimeConsuming(Long.toString(elapsedTime));
@@ -210,46 +263,62 @@ public class ThirdZySelfServiceImpl implements ThirdZySelfService {
log.info("|{}|checkIsToken校验错误,未传入token|{}|", TAG, string);
return false;
}
Claims claims = Jwts.parser()
.setSigningKey(zyConfig.getSelfAppSecret())
.parseClaimsJws(string)
.getBody();
String token = (String) claims.get(zyConfig.getSelfTokenKey());
String redisKey = CacheConstants.THIRDPARTY_ZY_SELF_TOKEN + token;
String json = redisTemplate.opsForValue().get(redisKey);
if (ObjectUtil.isEmpty(json)) {
log.info("|{}|checkIsToken校验错误token|{}|", TAG, string);
try {
Claims claims = Jwts.parser()
.setSigningKey(zyConfig.getSelfTokenSecret())
.parseClaimsJws(string)
.getBody();
String token = (String) claims.get(zyConfig.getSelfTokenKey());
String redisKey = CacheConstants.THIRDPARTY_ZY_SELF_TOKEN + token;
String json = redisTemplate.opsForValue().get(redisKey);
if (ObjectUtil.isEmpty(json)) {
log.error("|{}|checkIsToken校验错误token|{}|", TAG, string);
return false;
}
log.info("|{}|checkIsToken校验成功info|{}|", TAG, json);
return true;
} catch (Exception e) {
log.error("|{}|checkIsToken意外的错误token|{}|", TAG, string);
return false;
}
log.info("|{}|checkIsToken校验成功info|{}|", TAG, json);
return true;
}
/**
* 校验参数
*/
private boolean checkSign(Object obj) {
if (ObjectUtil.isEmpty(obj)) return false;
private String checkSign(Object obj) {
if (ObjectUtil.isEmpty(obj)) return null;
try {
Map<String, Object> map = BeanUtil.beanToMap(obj);
if (ObjectUtil.isEmpty(map)) return false;
if (ObjectUtil.isEmpty(map)) return null;
Map<String, Object> paramsMap = new TreeMap<>(map);
paramsMap.remove("sign");
String sign = map.getOrDefault("sign", "").toString();
if (ObjectUtil.isEmpty(sign)) {
log.error("|{}|checkSign意外的错误,sign为空", sign);
return false;
return null;
}
if (ObjectUtil.isEmpty(paramsMap)) {
log.error("|{}|checkSign意外的错误,paramsMap为空", paramsMap);
return false;
return null;
}
for (Map.Entry<String, Object> entry : paramsMap.entrySet()) {
Object value = entry.getValue();
if (value instanceof String) {
entry.setValue(Base64.decodeStr((String) value));
} else {
entry.setValue(Base64.decodeStr(value.toString()));
}
}
String string = zyApiTool.getResultSign(paramsMap);
if (ObjectUtil.equals(sign, string)) {
return JSONUtil.toJsonStr(paramsMap);
}
return true;
} catch (Exception e) {
log.error("|{}|checkSign意外的错误,加密参数|{}|", TAG, JSONUtil.toJsonStr(obj), e);
return false;
return null;
}
return null;
}
// 生成token
@@ -51,7 +51,7 @@ public class ThirdZyStrideServiceImpl implements ThirdZyStrideService {
encryptionBody.put("dealers", dto);
entity.setApiUrl(url);
entity.setOrgReq(JSONUtil.toJsonStr(encryptionBody));
Map<String, Object> body = zyApiTool.getEncryptionMap(encryptionBody);
Map<String, Object> body = zyApiTool.getZyEncryptionMap(encryptionBody);
entity.setEncipherReq(JSONUtil.toJsonStr(body));
String res = HttpRequest.post(url).form(body).execute().body();
if (ObjectUtil.isEmpty(res)) {
@@ -90,7 +90,7 @@ public class ThirdZyStrideServiceImpl implements ThirdZyStrideService {
encryptionBody.put("dealers", dto);
entity.setApiUrl(url);
entity.setOrgReq(JSONUtil.toJsonStr(encryptionBody));
Map<String, Object> body = zyApiTool.getEncryptionMap(encryptionBody);
Map<String, Object> body = zyApiTool.getZyEncryptionMap(encryptionBody);
entity.setEncipherReq(JSONUtil.toJsonStr(body));
String res = HttpRequest.post(url).form(body).execute().body();
if (ObjectUtil.isEmpty(res)) {
@@ -129,7 +129,7 @@ public class ThirdZyStrideServiceImpl implements ThirdZyStrideService {
encryptionBody.put("dealers", dto);
entity.setApiUrl(url);
entity.setOrgReq(JSONUtil.toJsonStr(encryptionBody));
Map<String, Object> body = zyApiTool.getEncryptionMap(encryptionBody);
Map<String, Object> body = zyApiTool.getZyEncryptionMap(encryptionBody);
entity.setEncipherReq(JSONUtil.toJsonStr(body));
String res = HttpRequest.post(url).form(body).execute().body();
if (ObjectUtil.isEmpty(res)) {
@@ -6,15 +6,12 @@ import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.bsd.pjcloud.common.core.constant.CacheConstants;
import com.bsd.pjcloud.thirdparty.config.ZyConfig;
import com.bsd.pjcloud.thirdparty.constant.ThirdZyBizEnumType;
import com.bsd.pjcloud.thirdparty.entity.zy.ThirdZyIdDictEntity;
import com.bsd.pjcloud.thirdparty.mapper.zy.ThirdZyIdDictMapper;
import com.bsd.pjcloud.thirdparty.vo.zy.mer.ZyMerGetTokenVo;
import io.undertow.security.idm.DigestAlgorithm;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
@@ -23,8 +20,6 @@ import org.springframework.validation.annotation.Validated;
import org.apache.commons.codec.digest.DigestUtils;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.*;
import java.util.concurrent.TimeUnit;
@@ -135,35 +130,59 @@ public class ZyApiTool {
return result;
}
public Map<String, Object> getEncryptionMap(Map<String, Object> params, String... str) {
/**
* 获取正元传输的数据
*/
public Map<String, Object> getZyEncryptionMap(Map<String, Object> params, String... str) {
Map<String, Object> body = new LinkedHashMap<>();
if (ObjectUtil.isEmpty(str)) return body;
for (String v : str) {
body.put(v, params.getOrDefault(v, null));
}
prePost(body);
prePost(body, true);
return body;
}
public Map<String, Object> getEncryptionMap(Map<String, Object> params) {
public Map<String, Object> getZyEncryptionMap(Map<String, Object> params) {
Map<String, Object> body = new HashMap<>();
body.put("data", params);
prePost(body);
prePost(body, true);
return body;
}
public Map<String, Object> removeEmptyMap(Map<String, Object> params) {
if (ObjectUtil.isEmpty(params)) return null;
for (Map.Entry<String, Object> entry : params.entrySet()) {
Object value = entry.getValue();
if (value == null) {
params.remove(entry.getKey());
}
/**
* 获取第三方传输的数据
*/
public Map<String, Object> getSelfEncryptionMap(Map<String, Object> params, String... str) {
Map<String, Object> body = new LinkedHashMap<>();
if (ObjectUtil.isEmpty(str)) return body;
for (String v : str) {
body.put(v, params.getOrDefault(v, null));
}
return params;
prePost(body, false);
return body;
}
private void prePost(Map<String, Object> params) {
public Map<String, Object> getSelfEncryptionMap(Map<String, Object> params) {
Map<String, Object> body = new HashMap<>();
body.put("data", params);
prePost(body, false);
return body;
}
public String getResultSign(Map<String, Object> params) {
for (Map.Entry<String, Object> entry : params.entrySet()) {
Object value = entry.getValue();
if (value instanceof String) {
entry.setValue(Base64.encode((String) value));
} else {
entry.setValue(Base64.encode(value.toString()));
}
}
return generateSign(params, false);
}
private void prePost(Map<String, Object> params, boolean isZy) {
for (Map.Entry<String, Object> entry : params.entrySet()) {
Object value = entry.getValue();
if (value instanceof String) {
@@ -173,7 +192,7 @@ public class ZyApiTool {
entry.setValue(json);
}
}
String sign = generateSign(params);
String sign = generateSign(params, isZy);
for (Map.Entry<String, Object> entry : params.entrySet()) {
Object value = entry.getValue();
if (value instanceof String) {
@@ -185,7 +204,7 @@ public class ZyApiTool {
params.put("sign", sign);
}
private String generateSign(Map<String, Object> params) {
private String generateSign(Map<String, Object> params, boolean isZy) {
// 将参数以参数名的字典升序排序
Map<String, Object> sortParams = new TreeMap<String, Object>(params);
Set<Map.Entry<String, Object>> entrys = sortParams.entrySet();
@@ -193,7 +212,7 @@ public class ZyApiTool {
for (Map.Entry<String, Object> entry : entrys) {
baseString.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
}
baseString.append("key=").append(zyConfig.getAppId());
baseString.append("key=").append(isZy ? zyConfig.getAppId() : zyConfig.getSelfAppId());
String signStr = baseString.toString();
String digestHex = DigestUtils.md5Hex(signStr);
log.info("|{}|加密前的参数为|{}|加密后的参数为|{}|", TAG, signStr, digestHex);