|
@@ -2,31 +2,55 @@ package fcb.project.manager.base.utils;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import fdage.back.sdk.base.entity.Result;
|
|
|
+import lombok.extern.log4j.Log4j2;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
+import java.time.Duration;
|
|
|
import java.time.Instant;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/**
|
|
|
* 2 * @Author: Abner
|
|
|
* 3 * @Date: 2021/1/18 9:11
|
|
|
* 4
|
|
|
*/
|
|
|
+@Service
|
|
|
+@Log4j2
|
|
|
public class FcbUtils {
|
|
|
|
|
|
|
|
|
- public static Result<Object> getFcbSign(Map<String , Object> params
|
|
|
+ @Autowired
|
|
|
+ private RedisTemplate redisTemplate;
|
|
|
+
|
|
|
+
|
|
|
+ public Result<Object> getFcbSign(Map<String , Object> params
|
|
|
, String fcbClientSecret , String fcbClientCode){
|
|
|
|
|
|
if(CollectionUtils.isEmpty(params)){
|
|
|
return Result.failure("入参不能为空");
|
|
|
}
|
|
|
+ long timeStamp = Instant.now().getEpochSecond();
|
|
|
+ Instant stepOneStart = Instant.now();
|
|
|
+ String redisKey = JSON.toJSONString(params) + timeStamp;
|
|
|
+ if(redisTemplate.hasKey(redisKey)){
|
|
|
+ log.info("缓存中存在,直接返回");
|
|
|
+ Map<String , Object> resultMp = (Map<String, Object>) redisTemplate.opsForValue().get(redisKey);
|
|
|
+ return Result.success(resultMp);
|
|
|
+
|
|
|
+ }
|
|
|
+ Instant stepOneEnd = Instant.now();
|
|
|
+ long stepOneDuration = Duration.between(stepOneStart , stepOneEnd).toMillis();
|
|
|
+ log.info("步骤1耗时:{}" , stepOneDuration);
|
|
|
+ Instant stepTwoStart = Instant.now();
|
|
|
for (Map.Entry entry : params.entrySet()) {
|
|
|
//1、先剔除value值为null的键值对
|
|
|
if(null == entry.getValue()){
|
|
@@ -34,22 +58,29 @@ public class FcbUtils {
|
|
|
}
|
|
|
}
|
|
|
//2、新增房车宝的client_secret作为噪点
|
|
|
- long timeStamp = Instant.now().getEpochSecond();
|
|
|
params.put("client_secret" , fcbClientSecret);
|
|
|
params.put("client_code" , fcbClientCode);
|
|
|
params.put("req_time" , timeStamp);
|
|
|
params = DataUtils.sortMapByKey(params);
|
|
|
- StringBuilder stringBuilder = new StringBuilder();
|
|
|
+ Instant stepTwoEnd = Instant.now();
|
|
|
+ long stepTwoDuration = Duration.between(stepTwoStart , stepTwoEnd).toMillis();
|
|
|
+ log.info("步骤2耗时:{}" , stepTwoDuration);
|
|
|
|
|
|
+ Instant stepThreeStart = Instant.now();
|
|
|
+ StringBuilder stringBuilder = new StringBuilder();
|
|
|
for ( Map.Entry entry : params.entrySet()) {
|
|
|
stringBuilder.append(entry.getValue());
|
|
|
}
|
|
|
-
|
|
|
//3、生成MD5密文,然后转成小写字母
|
|
|
String md5EncryptionStr = DataUtils.md5Encryption(stringBuilder.toString());
|
|
|
Map<String , Object> resultMap = new HashMap<>();
|
|
|
resultMap.put("authcode" , md5EncryptionStr);
|
|
|
resultMap.put("timeStamp" , timeStamp);
|
|
|
+ //缓存一秒
|
|
|
+ redisTemplate.opsForValue().set(redisKey , JSON.toJSONString(resultMap) , 1000 , TimeUnit.MILLISECONDS);
|
|
|
+ Instant stepThreeEnd = Instant.now();
|
|
|
+ long stepThreeDuration = Duration.between(stepThreeStart , stepThreeEnd).toMillis();
|
|
|
+ log.info("步骤3耗时:{}" , stepThreeDuration);
|
|
|
return Result.success(resultMap);
|
|
|
|
|
|
}
|