Просмотр исходного кода

新增mp访问token从临时服务器获取逻辑

codingliang 1 год назад
Родитель
Сommit
5ceb781f94

+ 7 - 1
src/main/java/com/sqx/modules/pay/controller/app/ApiWeiXinPayController.java

@@ -1,5 +1,6 @@
 package com.sqx.modules.pay.controller.app;
 
+import com.sqx.common.utils.RedisUtils;
 import com.sqx.common.utils.Result;
 import com.sqx.modules.app.annotation.Login;
 import com.sqx.modules.pay.dto.PayOrderDTO;
@@ -9,6 +10,8 @@ import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestAttribute;
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -113,7 +116,10 @@ public class ApiWeiXinPayController {
         return notifyShop(2,request);
     }
 
-
+    @GetMapping("/temp/token/{type}")
+    public String getToken(@PathVariable String type) {
+        return wxService.getAccessToken(type);
+    }
 
     public String notify(Integer type,HttpServletRequest request){
         String resXml = "";

+ 6 - 0
src/main/java/com/sqx/modules/pay/service/WxService.java

@@ -47,4 +47,10 @@ public interface WxService {
 
     void notifyRider(PayDetails payDetails);
 
+    /**
+     * 获取token(临时方法)
+     * @param type 1用户端、2商家端、3骑手端
+     * @return token
+     */
+    String getAccessToken(String type);
 }

+ 18 - 0
src/main/java/com/sqx/modules/pay/service/impl/WxServiceImpl.java

@@ -6,6 +6,7 @@ import com.github.wxpay.sdk.WXPay;
 import com.github.wxpay.sdk.WXPayConstants;
 import com.github.wxpay.sdk.WXPayUtil;
 import com.sqx.common.constant.RedisKey;
+import com.sqx.common.utils.RedisUtils;
 import com.sqx.common.utils.Result;
 import com.sqx.modules.app.dao.UserDao;
 import com.sqx.modules.app.dao.UserMoneyDetailsDao;
@@ -33,6 +34,7 @@ import com.sqx.modules.pay.entity.PayDetails;
 import com.sqx.modules.pay.service.WxService;
 import com.sqx.modules.utils.AmountCalUtils;
 import com.sqx.modules.utils.MD5Util;
+import com.sqx.modules.utils.SenInfoCheckUtil;
 import com.sqx.modules.utils.WXConfigUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.redisson.api.RLock;
@@ -718,6 +720,22 @@ public class WxServiceImpl implements WxService {
         }
     }
 
+    @Resource
+    private RedisUtils redisUtils;
+
+    @Override
+    public String getAccessToken(String type) {
+        String token = null;
+        if (StrUtil.equals(type, "1")) {
+            token = SenInfoCheckUtil.getMpToken();
+        } else if (StrUtil.equals(type, "2")) {
+            token = SenInfoCheckUtil.getShopMpToken();
+        } else if (StrUtil.equals(type, "3")) {
+            token = SenInfoCheckUtil.getRiderMpToken();
+        }
+        return token;
+    }
+
     @Transactional
     @Override
     public void notfiy(PayDetails payDetails){

+ 40 - 11
src/main/java/com/sqx/modules/utils/SenInfoCheckUtil.java

@@ -1,6 +1,7 @@
 package com.sqx.modules.utils;
 
 import cn.hutool.core.util.StrUtil;
+import cn.hutool.http.HttpUtil;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.google.common.collect.Maps;
@@ -8,12 +9,16 @@ import com.sqx.common.constant.RedisKey;
 import com.sqx.common.utils.RedisUtils;
 import com.sqx.modules.common.entity.CommonInfo;
 import com.sqx.modules.common.service.CommonInfoService;
+import icu.xuyijie.secureapi.cipher.CipherAlgorithmEnum;
+import icu.xuyijie.secureapi.cipher.CipherUtils;
+import icu.xuyijie.secureapi.model.SecureApiPropertiesConfig;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Component;
 import org.springframework.web.client.RestTemplate;
@@ -42,25 +47,33 @@ public class SenInfoCheckUtil {
 
     private static RedisUtils redisUtils;
 
+    private static boolean GET_MP_TOKEN_FROM_TEMP;
+    private static SecureApiPropertiesConfig SECUREAPIPROPERTIESCONFIG;
+
     // 注入的时候,给类的 service 注入
     @Autowired
-    public void setWxChatContentService(CommonInfoService commonInfoService, RedisUtils redisUtils) {
+    public void setWxChatContentService(CommonInfoService commonInfoService, RedisUtils redisUtils, @Value("${mp.temp:false}") boolean isTemp, SecureApiPropertiesConfig secureApiPropertiesConfig) {
         SenInfoCheckUtil.commonInfoService = commonInfoService;
         SenInfoCheckUtil.redisUtils = redisUtils;
+        SenInfoCheckUtil.GET_MP_TOKEN_FROM_TEMP = isTemp;
+        SenInfoCheckUtil.SECUREAPIPROPERTIESCONFIG = secureApiPropertiesConfig;
     }
 
 
     /**
      * 获取Token  小程序
-     * @param
-     * @param
      * @return AccessToken
      */
     public static String getMpToken(){
         String token = (String) redisUtils.get(RedisKey.MP_TOKEN_CACHE_KEY);
         if (StrUtil.isBlank(token)) {
-            token = getMpAccessToken();
-
+            if (!GET_MP_TOKEN_FROM_TEMP) {
+                token = getMpAccessToken();
+            } else {
+                try {
+                    token = doDecrypt(HttpUtil.get("https://mxys.chuanghai-tech.com/sqx_fast/app/wxPay/temp/token/1"));
+                } catch (Exception e) {}
+            }
             redisUtils.set(RedisKey.MP_TOKEN_CACHE_KEY, token, RedisUtils.HOUR_ONE_EXPIRE);
         }
         return token;
@@ -68,30 +81,46 @@ public class SenInfoCheckUtil {
 
     /**
      * 获取Token  小程序
-     * @param
-     * @param
      * @return AccessToken
      */
-    public static String getRiderMpToken(){
+    public static String getRiderMpToken() {
         String token = (String) redisUtils.get(RedisKey.MP_OF_RIDER_TOKEN_CACHE_KEY);
         if (StrUtil.isBlank(token)) {
-            token = getRiderMpAccessToken();
+            if (!GET_MP_TOKEN_FROM_TEMP) {
+                token = getRiderMpAccessToken();
+            } else {
+                try {
+                    token = doDecrypt(HttpUtil.get("https://mxys.chuanghai-tech.com/sqx_fast/app/wxPay/temp/token/1"));
+                } catch (Exception e) {}
+            }
 
             redisUtils.set(RedisKey.MP_OF_RIDER_TOKEN_CACHE_KEY, token, RedisUtils.HOUR_ONE_EXPIRE);
         }
         return token;
     }
 
-    public static String getShopMpToken(){
+    public static String getShopMpToken() {
         String token = (String) redisUtils.get(RedisKey.MP_OF_SHOP_TOKEN_CACHE_KEY);
         if (StrUtil.isBlank(token)) {
-            token = getShopMpAccessToken();
+            if (!GET_MP_TOKEN_FROM_TEMP) {
+                token = getShopMpAccessToken();
+            } else {
+                try {
+                    token = doDecrypt(HttpUtil.get("https://mxys.chuanghai-tech.com/sqx_fast/app/wxPay/temp/token/1"));
+                } catch (Exception e) {}
+            }
 
             redisUtils.set(RedisKey.MP_OF_SHOP_TOKEN_CACHE_KEY, token, RedisUtils.HOUR_ONE_EXPIRE);
         }
         return token;
     }
 
+    private static String doDecrypt(String text) {
+        CipherUtils cipherUtils = new CipherUtils(CipherAlgorithmEnum.AES_CBC_PKCS5);
+        String key = SECUREAPIPROPERTIESCONFIG.getKey();
+        String iv = SECUREAPIPROPERTIESCONFIG.getIv();
+        return cipherUtils.decrypt(text, key, iv);
+    }
 
     public static void getImg(String relation,String goodsId,String type, String page,HttpServletResponse response){
         String mpToken = getMpToken();

+ 2 - 0
src/main/resources/application-dev.yml

@@ -106,3 +106,5 @@ secure-api:
     # LocalDateTime类型日期格式化
     local-date-time-format: yyyy-MM-dd HH:mm:ss
 
+mp:
+    temp: true

+ 3 - 0
src/main/resources/application-prod.yml

@@ -106,3 +106,6 @@ secure-api:
     date-format: yyyy-MM-dd HH:mm:ss
     # LocalDateTime类型日期格式化
     local-date-time-format: yyyy-MM-dd HH:mm:ss
+
+mp:
+    temp: false