瀏覽代碼

fix : SDK优化阿里云功能接口

bilingfeng 1 年之前
父節點
當前提交
6c5a2c2f96

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

@@ -23,7 +23,7 @@ public class SDKApplication {
 
     public static void main(String[] args) {
         SpringApplication.run(SDKApplication.class, args);
-        System.out.println("赞象SDK服务启动成功 <兼容客服支付, 且发送指定客服消息> ( ´・・)ノ(._.`) \n" +
+        System.out.println("赞象SDK服务启动成功 <兼容客服支付, 且发送指定客服消息, 优化阿里云市场接口> ( ´・・)ノ(._.`) \n" +
                 " ___________ _   __\n" +
                 "/  ___|  _  \\ | / /\n" +
                 "\\ `--.| | | | |/ / \n" +

+ 38 - 0
game-module/game-module-sdk/src/main/java/com/zanxiang/game/module/sdk/service/IAliApiService.java

@@ -0,0 +1,38 @@
+package com.zanxiang.game.module.sdk.service;
+
+import com.zanxiang.game.module.sdk.pojo.result.IpCheckResult;
+import reactor.util.function.Tuple2;
+
+/**
+ * @author : lingfeng
+ * @time : 2023-12-12
+ * @description : 阿里云市场api
+ */
+public interface IAliApiService {
+
+    /**
+     * 阿里实名认证, 仅用在测试环境
+     *
+     * @param cardName : 名字
+     * @param cardId   : 身份证
+     * @return : 返回检测结果
+     */
+    Tuple2<Boolean, String> authenticationCheck(String cardName, String cardId);
+
+    /**
+     * 阿里实名认证, 包含归属地, 用在生产环境
+     *
+     * @param cardName : 名字
+     * @param cardId   : 身份证
+     * @return : 返回检测结果
+     */
+    Tuple2<Boolean, String> userCardCheck(String cardName, String cardId);
+
+    /**
+     * 阿里IP检测
+     *
+     * @param ip : 网络地址
+     * @return : 检测结果
+     */
+    IpCheckResult userIpCheck(String ip);
+}

+ 0 - 9
game-module/game-module-sdk/src/main/java/com/zanxiang/game/module/sdk/service/IUserCardService.java

@@ -4,7 +4,6 @@ import com.baomidou.mybatisplus.extension.service.IService;
 import com.zanxiang.game.module.mybatis.entity.UserCard;
 import com.zanxiang.game.module.sdk.pojo.param.UserCardUpdateParam;
 import com.zanxiang.game.module.sdk.pojo.param.UserData;
-import com.zanxiang.game.module.sdk.pojo.result.IpCheckResult;
 import com.zanxiang.module.util.pojo.ResultVO;
 
 /**
@@ -22,12 +21,4 @@ public interface IUserCardService extends IService<UserCard> {
      * @return {@link ResultVO}<{@link Boolean}>
      */
     ResultVO<Boolean> userAuthentication(UserCardUpdateParam param, UserData userData);
-
-    /**
-     * ip监测
-     *
-     * @param ip : 网络地址
-     * @return {@link IpCheckResult}
-     */
-    IpCheckResult userIpCheck(String ip);
 }

+ 117 - 0
game-module/game-module-sdk/src/main/java/com/zanxiang/game/module/sdk/service/impl/AliApiServiceImpl.java

@@ -0,0 +1,117 @@
+package com.zanxiang.game.module.sdk.service.impl;
+
+import com.zanxiang.game.module.sdk.pojo.result.CardCheckResult;
+import com.zanxiang.game.module.sdk.pojo.result.IpCheckResult;
+import com.zanxiang.game.module.sdk.service.IAliApiService;
+import com.zanxiang.module.util.JsonUtil;
+import com.zanxiang.module.util.exception.BaseException;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.logging.log4j.util.Strings;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.*;
+import org.springframework.stereotype.Service;
+import org.springframework.util.LinkedMultiValueMap;
+import org.springframework.util.MultiValueMap;
+import org.springframework.web.client.RestTemplate;
+import org.springframework.web.util.UriComponentsBuilder;
+import reactor.util.function.Tuple2;
+import reactor.util.function.Tuples;
+
+import java.util.UUID;
+
+/**
+ * @author : lingfeng
+ * @time : 2023-12-12
+ * @description : 阿里云市场api
+ */
+@Slf4j
+@Service
+public class AliApiServiceImpl implements IAliApiService {
+
+    @Autowired
+    private RestTemplate restTemplate;
+
+    /**
+     * 云市场应用标识
+     */
+    private final String APP_CODE = "f395b1587fc04a49a975f908660fb1e9";
+
+
+
+    @Override
+    public Tuple2<Boolean, String> authenticationCheck(String cardName, String cardId) {
+        String host = "https://dskj.market.alicloudapi.com/platform/check/verified";
+        String url = host + "?certCode=" + cardId + "&realName=" + cardName;
+        HttpHeaders headers = new HttpHeaders();
+        headers.set("Authorization", "APPCODE " + this.APP_CODE);
+        headers.set("X-Ca-Nonce", UUID.randomUUID().toString());
+        HttpEntity httpEntity = new HttpEntity<>(headers);
+        CardCheckResult result;
+        try {
+            String resultStr = restTemplate.postForObject(url, httpEntity, String.class);
+            result = JsonUtil.toObj(resultStr, CardCheckResult.class);
+        } catch (Exception e) {
+            log.error("请求阿里实名认证接口异常(OLD), cardName : {}, cardId : {}, e : {}", cardName, cardId, e.getMessage());
+            throw new BaseException("请求阿里实名认证接口异常");
+        }
+        if (result == null || !result.isSuccess()) {
+            log.error("请求阿里实名认证接口返回值为空(OLD), cardName : {}, cardId : {}, result : {}", cardName, cardId, JsonUtil.toString(result));
+            throw new BaseException("实名认证失败");
+        }
+        return Tuples.of(result.isPast(), Strings.EMPTY);
+    }
+
+    @Override
+    public Tuple2<Boolean, String> userCardCheck(String cardName, String cardId) {
+        String url = "https://eid.shumaidata.com/eid/check";
+        //请求头
+        HttpHeaders headers = new HttpHeaders();
+        headers.add("Authorization", "APPCODE " + this.APP_CODE);
+        MultiValueMap<String, String> requestParams = new LinkedMultiValueMap<>();
+        requestParams.add("name", cardName);
+        requestParams.add("idcard", cardId);
+        CardCheckResult result;
+        try {
+            ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST,
+                    new HttpEntity<>(requestParams, headers), String.class);
+            String body = responseEntity.getBody();
+            result = JsonUtil.toObj(body, CardCheckResult.class);
+        } catch (Exception e) {
+            log.error("请求阿里实名认证接口异常(NEW), cardName : {}, cardId : {}, e : {}", cardName, cardId, e.getMessage());
+            throw new BaseException("请求阿里实名认证接口异常");
+        }
+        if (result == null || result.isFail()) {
+            log.error("请求阿里实名认证接口返回值为空(NEW),cardName : {},cardId : {}, result : {}", cardName, cardId, JsonUtil.toString(result));
+            throw new BaseException("实名认证失败");
+        }
+        log.error("实名认证结果, result : {}", JsonUtil.toString(result));
+        return Tuples.of(result.past(), result.getAddress());
+    }
+
+    @Override
+    public IpCheckResult userIpCheck(String ip) {
+        String url = "https://ipcity.market.alicloudapi.com/ip/city/query";
+        //构造请求参数
+        UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromHttpUrl(url).queryParam("ip", ip);
+        //请求头
+        HttpHeaders headers = new HttpHeaders();
+        headers.set("Authorization", "APPCODE " + this.APP_CODE);
+        RequestEntity<Object> requestEntity = new RequestEntity<>(headers, HttpMethod.GET, uriBuilder.build().toUri());
+        //请求
+        IpCheckResult result;
+        try {
+            ResponseEntity<String> responseEntity = restTemplate.exchange(requestEntity, String.class);
+            String body = responseEntity.getBody();
+            result = JsonUtil.toObj(body, IpCheckResult.class);
+        } catch (Exception e) {
+            log.error("请求阿里ip检测接口异常, ip : {}, e : {}", ip, e.getMessage());
+            throw new BaseException("请求阿里ip监测接口异常");
+        }
+        if (result == null || result.isFail()) {
+            log.error("请求阿里ip检测接口返回值为空, ip : {}, result : {}", ip, JsonUtil.toString(result));
+            throw new BaseException("请求阿里ip检测接口返回值为空");
+        }
+        log.error("ip检测结果, result : {}", JsonUtil.toString(result));
+        return result;
+    }
+}

+ 3 - 3
game-module/game-module-sdk/src/main/java/com/zanxiang/game/module/sdk/service/impl/GameAppletShellServiceImpl.java

@@ -11,10 +11,10 @@ import com.zanxiang.game.module.mybatis.entity.User;
 import com.zanxiang.game.module.mybatis.mapper.GameAppletShellMapper;
 import com.zanxiang.game.module.sdk.enums.ShellSwitchEnum;
 import com.zanxiang.game.module.sdk.pojo.result.IpCheckResult;
+import com.zanxiang.game.module.sdk.service.IAliApiService;
 import com.zanxiang.game.module.sdk.service.IGameAppletShellService;
 import com.zanxiang.game.module.sdk.service.IGameExtService;
 import com.zanxiang.game.module.sdk.service.IGameUserRoleService;
-import com.zanxiang.game.module.sdk.service.IUserCardService;
 import com.zanxiang.module.util.JsonUtil;
 import com.zanxiang.module.util.exception.BaseException;
 import com.zanxiang.module.web.util.IpUtil;
@@ -44,7 +44,7 @@ public class GameAppletShellServiceImpl extends ServiceImpl<GameAppletShellMappe
     private IGameUserRoleService gameUserRoleService;
 
     @Autowired
-    private IUserCardService userCardService;
+    private IAliApiService aliApiService;
 
     @Override
     public Integer getShellSwitch(String appId, String version) {
@@ -138,7 +138,7 @@ public class GameAppletShellServiceImpl extends ServiceImpl<GameAppletShellMappe
             }
             String prov = null;
             try {
-                IpCheckResult result = userCardService.userIpCheck(IpUtil.getRealIp(request));
+                IpCheckResult result = aliApiService.userIpCheck(IpUtil.getRealIp(request));
                 prov = result.getData().getResult().getProv();
             } catch (Exception ignored) {
             }

+ 4 - 91
game-module/game-module-sdk/src/main/java/com/zanxiang/game/module/sdk/service/impl/UserCardServiceImpl.java

@@ -10,26 +10,17 @@ import com.zanxiang.game.module.mybatis.mapper.UserCardMapper;
 import com.zanxiang.game.module.sdk.constant.RegexConstant;
 import com.zanxiang.game.module.sdk.pojo.param.UserCardUpdateParam;
 import com.zanxiang.game.module.sdk.pojo.param.UserData;
-import com.zanxiang.game.module.sdk.pojo.result.CardCheckResult;
-import com.zanxiang.game.module.sdk.pojo.result.IpCheckResult;
+import com.zanxiang.game.module.sdk.service.IAliApiService;
 import com.zanxiang.game.module.sdk.service.IUserCardService;
 import com.zanxiang.game.module.sdk.service.IUserService;
-import com.zanxiang.module.util.JsonUtil;
-import com.zanxiang.module.util.exception.BaseException;
 import com.zanxiang.module.util.pojo.ResultVO;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.logging.log4j.util.Strings;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
-import org.springframework.http.*;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
-import org.springframework.util.LinkedMultiValueMap;
-import org.springframework.util.MultiValueMap;
-import org.springframework.web.client.RestTemplate;
-import org.springframework.web.util.UriComponentsBuilder;
 import reactor.util.function.Tuple2;
-import reactor.util.function.Tuples;
 
 import java.text.SimpleDateFormat;
 import java.time.LocalDate;
@@ -37,7 +28,6 @@ import java.time.LocalDateTime;
 import java.util.Calendar;
 import java.util.Date;
 import java.util.GregorianCalendar;
-import java.util.UUID;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -54,7 +44,7 @@ public class UserCardServiceImpl extends ServiceImpl<UserCardMapper, UserCard> i
     private IUserService userService;
 
     @Autowired
-    private RestTemplate restTemplate;
+    private IAliApiService aliApiService;
 
     /**
      * 服务器域名
@@ -89,9 +79,9 @@ public class UserCardServiceImpl extends ServiceImpl<UserCardMapper, UserCard> i
         //阿里实名认证
         Tuple2<Boolean, String> tuple2;
         if (this.serverUrl.contains("test")) {
-            tuple2 = this.authenticationCheck(cardName, cardId);
+            tuple2 = aliApiService.authenticationCheck(cardName, cardId);
         } else {
-            tuple2 = this.userCardCheck(cardName, cardId);
+            tuple2 = aliApiService.userCardCheck(cardName, cardId);
         }
         //实名认证失败
         if (!tuple2.getT1()) {
@@ -177,81 +167,4 @@ public class UserCardServiceImpl extends ServiceImpl<UserCardMapper, UserCard> i
         return sex;
     }
 
-    private Tuple2<Boolean, String> authenticationCheck(String cardName, String cardId) {
-        String host = "https://dskj.market.alicloudapi.com/platform/check/verified";
-        String appCode = "f395b1587fc04a49a975f908660fb1e9";
-        String url = host + "?certCode=" + cardId + "&realName=" + cardName;
-        HttpHeaders headers = new HttpHeaders();
-        headers.set("Authorization", "APPCODE " + appCode);
-        headers.set("X-Ca-Nonce", UUID.randomUUID().toString());
-        HttpEntity httpEntity = new HttpEntity<>(headers);
-        CardCheckResult result;
-        try {
-            String resultStr = restTemplate.postForObject(url, httpEntity, String.class);
-            result = JsonUtil.toObj(resultStr, CardCheckResult.class);
-        } catch (Exception e) {
-            log.error("请求阿里实名认证接口异常(OLD), cardName : {}, cardId : {}, e : {}", cardName, cardId, e.getMessage());
-            throw new BaseException("请求阿里实名认证接口异常");
-        }
-        if (result == null || !result.isSuccess()) {
-            log.error("请求阿里实名认证接口返回值为空(OLD), cardName : {}, cardId : {}, result : {}", cardName, cardId, JsonUtil.toString(result));
-            throw new BaseException("实名认证失败");
-        }
-        return Tuples.of(result.isPast(), Strings.EMPTY);
-    }
-
-    private Tuple2<Boolean, String> userCardCheck(String cardName, String cardId) {
-        String url = "https://eid.shumaidata.com/eid/check";
-        String appCode = "f395b1587fc04a49a975f908660fb1e9";
-        //请求头
-        HttpHeaders headers = new HttpHeaders();
-        headers.add("Authorization", "APPCODE " + appCode);
-        MultiValueMap<String, String> requestParams = new LinkedMultiValueMap<>();
-        requestParams.add("name", cardName);
-        requestParams.add("idcard", cardId);
-        CardCheckResult result;
-        try {
-            ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST,
-                    new HttpEntity<>(requestParams, headers), String.class);
-            String body = responseEntity.getBody();
-            result = JsonUtil.toObj(body, CardCheckResult.class);
-        } catch (Exception e) {
-            log.error("请求阿里实名认证接口异常(NEW), cardName : {}, cardId : {}, e : {}", cardName, cardId, e.getMessage());
-            throw new BaseException("请求阿里实名认证接口异常");
-        }
-        if (result == null || result.isFail()) {
-            log.error("请求阿里实名认证接口返回值为空(NEW),cardName : {},cardId : {}, result : {}", cardName, cardId, JsonUtil.toString(result));
-            throw new BaseException("实名认证失败");
-        }
-        log.error("实名认证结果, result : {}", JsonUtil.toString(result));
-        return Tuples.of(result.past(), result.getAddress());
-    }
-
-    @Override
-    public IpCheckResult userIpCheck(String ip) {
-        String url = "https://ipcity.market.alicloudapi.com/ip/city/query";
-        String appCode = "f395b1587fc04a49a975f908660fb1e9";
-        //构造请求参数
-        UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromHttpUrl(url).queryParam("ip", ip);
-        //请求头
-        HttpHeaders headers = new HttpHeaders();
-        headers.set("Authorization", "APPCODE " + appCode);
-        RequestEntity<Object> requestEntity = new RequestEntity<>(headers, HttpMethod.GET, uriBuilder.build().toUri());
-        //请求
-        IpCheckResult result;
-        try {
-            ResponseEntity<String> responseEntity = restTemplate.exchange(requestEntity, String.class);
-            String body = responseEntity.getBody();
-            result = JsonUtil.toObj(body, IpCheckResult.class);
-        } catch (Exception e) {
-            log.error("请求阿里ip检测接口异常, ip : {}, e : {}", ip, e.getMessage());
-            throw new BaseException("请求阿里ip监测接口异常");
-        }
-        if (result == null || result.isFail()) {
-            log.error("请求阿里ip检测接口返回值为空, ip : {}, result : {}", ip, JsonUtil.toString(result));
-            throw new BaseException("请求阿里ip检测接口返回值为空");
-        }
-        log.error("ip检测结果, result : {}", JsonUtil.toString(result));
-        return result;
-    }
 }