Ver Fonte

fix : 新增接口获取小程序的accessToken

lingfeng há 2 dias atrás
pai
commit
9c5fb6aef4

+ 1 - 1
game-module/game-module-sdk/src/main/java/com/zanxiang/game/module/sdk/SDKApplication.java

@@ -25,7 +25,7 @@ public class SDKApplication {
 
     public static void main(String[] args) {
         SpringApplication.run(SDKApplication.class, args);
-        System.out.println("赞象SDK服务启动成功 <腾讯平台SDK新增前端回调事件类型> ( ´・・)ノ(._.`) \n" +
+        System.out.println("赞象SDK服务启动成功 <新增接口获取小程序的accessToken> ( ´・・)ノ(._.`) \n" +
                 " ___________ _   __\n" +
                 "/  ___|  _  \\ | / /\n" +
                 "\\ `--.| | | | |/ / \n" +

+ 3 - 2
game-module/game-module-sdk/src/main/java/com/zanxiang/game/module/sdk/controller/AppletController.java

@@ -42,8 +42,9 @@ public class AppletController {
     @UnSignCheck
     @PushDataCheck
     @GetMapping("/get/applet/accessToken")
-    public ResultVO<Map<String, String>> getAppletToken(@RequestParam String gameId) {
-        return ResultVO.ok(gameAppletService.getAppletToken(gameId));
+    public ResultVO<Map<String, String>> getAppletToken(@RequestParam String gameId, @RequestParam Long signTime,
+                                                        @RequestParam String sign) {
+        return ResultVO.ok(gameAppletService.getAppletToken(gameId, signTime, sign));
     }
 
     @UnSignCheck

+ 4 - 2
game-module/game-module-sdk/src/main/java/com/zanxiang/game/module/sdk/service/IGameAppletService.java

@@ -18,10 +18,12 @@ public interface IGameAppletService extends IService<GameApplet> {
     /**
      * 根据游戏外部唯一标志获取微信小游戏token
      *
-     * @param gameAppId : CP游戏包唯一标识
+     * @param gameId   : CP游戏包唯一标识
+     * @param signTime : 请求时间
+     * @param sign     : 签名
      * @return : 微信小游戏 appId, token
      */
-    Map<String, String> getAppletToken(String gameAppId);
+    Map<String, String> getAppletToken(String gameId, Long signTime, String sign);
 
     /**
      * applet味精检查

+ 32 - 2
game-module/game-module-sdk/src/main/java/com/zanxiang/game/module/sdk/service/impl/GameAppletServiceImpl.java

@@ -9,6 +9,7 @@ import com.zanxiang.game.module.base.rpc.IKfMsgRpc;
 import com.zanxiang.game.module.mybatis.entity.Game;
 import com.zanxiang.game.module.mybatis.entity.GameApplet;
 import com.zanxiang.game.module.mybatis.entity.GameExt;
+import com.zanxiang.game.module.mybatis.entity.GameSupper;
 import com.zanxiang.game.module.mybatis.mapper.GameAppletMapper;
 import com.zanxiang.game.module.sdk.pojo.dto.AppletMsgDTO;
 import com.zanxiang.game.module.sdk.pojo.dto.GameAppletDTO;
@@ -17,6 +18,7 @@ import com.zanxiang.game.module.sdk.pojo.vo.GameInitVO;
 import com.zanxiang.game.module.sdk.service.IGameAppletService;
 import com.zanxiang.game.module.sdk.service.IGameExtService;
 import com.zanxiang.game.module.sdk.service.IGameService;
+import com.zanxiang.game.module.sdk.service.IGameSupperService;
 import com.zanxiang.game.module.sdk.service.api.WxApiService;
 import com.zanxiang.game.module.sdk.service.pay.MiPayService;
 import com.zanxiang.game.module.sdk.util.SignUtil;
@@ -65,12 +67,26 @@ public class GameAppletServiceImpl extends ServiceImpl<GameAppletMapper, GameApp
     @Autowired
     private IGameAppletService gameAppletService;
 
+    @Autowired
+    private IGameSupperService gameSupperService;
+
     @Override
-    public Map<String, String> getAppletToken(String gameAppId) {
-        GameExt gameExt = gameExtService.getByGameAppId(gameAppId);
+    public Map<String, String> getAppletToken(String gameId, Long signTime, String sign) {
+        GameExt gameExt = gameExtService.getByGameAppId(gameId);
         if (gameExt == null) {
             throw new BaseException("参数错误, 游戏配置信息不存在");
         }
+        Game game = gameService.getById(gameExt.getGameId());
+        if (game == null) {
+            throw new BaseException("参数错误, 游戏信息不存在");
+        }
+        GameSupper gameSupper = gameSupperService.getById(game.getSuperGameId());
+        if (gameSupper == null) {
+            throw new BaseException("参数错误, 超父游戏信息不存在");
+        }
+        //签名验证
+        this.signCheck(gameSupper.getCpServerKey(), gameId, signTime, sign);
+        //获取小程序 accessToken
         GameAppletDTO gameAppletDTO = gameAppletService.getByGameId(gameExt.getGameId());
         if (gameAppletDTO == null) {
             throw new BaseException("参数错误, 微信小游戏配置信息不存在");
@@ -82,6 +98,20 @@ public class GameAppletServiceImpl extends ServiceImpl<GameAppletMapper, GameApp
         return resultMap;
     }
 
+    private void signCheck(String cpServerKey, String gameId, Long signTime, String sign) {
+        String signStr = "cpServerKey=" + cpServerKey + "gameId=" + gameId + "signTime=" + signTime;
+        String mySign;
+        try {
+            mySign = SignUtil.MD5(signStr);
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+        if (!Objects.equals(mySign.toUpperCase(), sign.toUpperCase())) {
+            log.error("加密验证失败, sign : {}, mySign : {}", sign, mySign);
+            throw new BaseException("加密标识错误");
+        }
+    }
+
     @Override
     public String appletMsgCheck(String appId, String signature, String timestamp, String nonce, String echoStr) throws Exception {
         log.error("appId : {}, signature : {}, timestamp : {}, nonce : {}, echoStr : {}", appId, signature, timestamp, nonce, echoStr);