Browse Source

fix : 壳包屏蔽省份

bilingfeng 1 year ago
parent
commit
d55ae83b10

+ 5 - 0
game-module/game-module-mybatis/src/main/java/com/zanxiang/game/module/mybatis/entity/GameAppletShell.java

@@ -99,4 +99,9 @@ public class GameAppletShell implements Serializable {
      */
     private String testVersion;
 
+    /**
+     * 省份屏蔽
+     */
+    private String provShield;
+
 }

+ 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" +

+ 4 - 1
game-module/game-module-sdk/src/main/java/com/zanxiang/game/module/sdk/controller/InitController.java

@@ -14,6 +14,8 @@ import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
+import javax.servlet.http.HttpServletRequest;
+
 /**
  * @author : lingfeng
  * @time : 2023-01-03
@@ -43,7 +45,8 @@ public class InitController {
     @ApiOperation(value = "微信小游戏壳包控制开关")
     @GetMapping("/shell/switch")
     @ApiResponses(value = {@ApiResponse(code = 200, message = "成功", response = Boolean.class)})
-    public ResultVO<Integer> getShellSwitch(@RequestParam String appId, @RequestParam String version) {
+    public ResultVO<Integer> getShellSwitch(@RequestParam String appId, @RequestParam String version,
+                                            HttpServletRequest request) {
         Integer shellSwitch = gameAppletShellService.getShellSwitch(appId, version);
         log.error("客保控制开关返回值, shellSwitch : {}", shellSwitch);
         return ResultVO.ok(shellSwitch);

+ 134 - 0
game-module/game-module-sdk/src/main/java/com/zanxiang/game/module/sdk/pojo/result/IpCheckResult.java

@@ -0,0 +1,134 @@
+package com.zanxiang.game.module.sdk.pojo.result;
+
+import lombok.Data;
+
+/**
+ * @author : lingfeng
+ * @time : 2023-10-13
+ * @description : ip检测
+ */
+@Data
+public class IpCheckResult {
+
+    /**
+     * 消息
+     */
+    private String msg;
+
+    /**
+     * 是否成功
+     */
+    private boolean success;
+
+    /**
+     * 错误码
+     */
+    private Integer code;
+
+    /**
+     * 数据
+     */
+    private DataBean data;
+
+    @Data
+    public static class DataBean {
+
+        /**
+         * 订单号
+         */
+        private String orderNo;
+
+        /**
+         * 结果
+         */
+        private IpCheckResult.ResultBean result;
+    }
+
+    @Data
+    public static class ResultBean {
+
+        /**
+         * 大洲
+         */
+        private String continent;
+
+        /**
+         * 所属机构
+         */
+        private String owner;
+
+        /**
+         * 国家
+         */
+        private String country;
+
+        /**
+         * 经度
+         */
+        private String lng;
+
+        /**
+         * 行政编码
+         */
+        private String adcode;
+
+        /**
+         * 城市
+         */
+        private String city;
+
+        /**
+         * 时区
+         */
+        private String timezone;
+
+        /**
+         * 运营商
+         */
+        private String isp;
+
+        /**
+         * 精度
+         */
+        private String accuracy;
+
+        /**
+         * 采集方式
+         */
+        private String source;
+
+        /**
+         * 自治域编码
+         */
+        private String asnumber;
+
+        /**
+         * 国家编码
+         */
+        private String areacode;
+
+        /**
+         * 邮编
+         */
+        private String zipcode;
+
+        /**
+         * 定位半径
+         */
+        private String radius;
+
+        /**
+         * 省份
+         */
+        private String prov;
+
+        /**
+         * 纬度
+         */
+        private String lat;
+    }
+
+    public Boolean isFail() {
+        return this.code != 200;
+    }
+}

+ 4 - 1
game-module/game-module-sdk/src/main/java/com/zanxiang/game/module/sdk/service/IGameAppletShellService.java

@@ -4,6 +4,8 @@ import com.baomidou.mybatisplus.extension.service.IService;
 import com.zanxiang.game.module.mybatis.entity.GameAppletShell;
 import com.zanxiang.game.module.mybatis.entity.User;
 
+import javax.servlet.http.HttpServletRequest;
+
 /**
  * @author : lingfeng
  * @time : 2023-07-06
@@ -16,9 +18,10 @@ public interface IGameAppletShellService extends IService<GameAppletShell> {
      *
      * @param appId   应用程序id
      * @param version 版本
+     * @param request 网络请求
      * @return {@link Integer}
      */
-    Integer getShellSwitch(String appId, String version);
+    Integer getShellSwitch(String appId, String version, HttpServletRequest request);
 
     /**
      * 获取用户壳开关

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

@@ -4,6 +4,7 @@ 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;
 
 /**
@@ -21,4 +22,12 @@ 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);
 }

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

@@ -10,16 +10,20 @@ import com.zanxiang.game.module.mybatis.entity.GameUserRole;
 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.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;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.logging.log4j.util.Strings;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import javax.servlet.http.HttpServletRequest;
 import java.util.Comparator;
 import java.util.List;
 import java.util.Objects;
@@ -39,8 +43,11 @@ public class GameAppletShellServiceImpl extends ServiceImpl<GameAppletShellMappe
     @Autowired
     private IGameUserRoleService gameUserRoleService;
 
+    @Autowired
+    private IUserCardService userCardService;
+
     @Override
-    public Integer getShellSwitch(String appId, String version) {
+    public Integer getShellSwitch(String appId, String version, HttpServletRequest request) {
         log.error("壳包接口收到请求, appId : {}, version : {}", appId, version);
         GameExt gameExt = gameExtService.getByGameAppId(appId);
         if (gameExt == null) {
@@ -55,6 +62,16 @@ public class GameAppletShellServiceImpl extends ServiceImpl<GameAppletShellMappe
         if (Objects.equals(gameAppletShell.getArraignVersion(), version)) {
             return ShellSwitchEnum.SHELL_SWITCH_OPEN.getShellSwitch();
         }
+        //省份屏蔽
+        if (Strings.isNotBlank(gameAppletShell.getProvShield())) {
+            try {
+                IpCheckResult result = userCardService.userIpCheck(IpUtil.getRealIp(request));
+                if (result.getData().getResult().getProv().contains(gameAppletShell.getProvShield())) {
+                    return ShellSwitchEnum.SHELL_SWITCH_OPEN.getShellSwitch();
+                }
+            } catch (Exception ignored) {
+            }
+        }
         //线上版本
         if (Objects.equals(gameAppletShell.getProVersion(), version)) {
             //申诉中, 返回申诉状态

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

@@ -11,6 +11,7 @@ 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.IUserCardService;
 import com.zanxiang.game.module.sdk.service.IUserService;
 import com.zanxiang.module.util.JsonUtil;
@@ -20,15 +21,13 @@ 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.HttpEntity;
-import org.springframework.http.HttpHeaders;
-import org.springframework.http.HttpMethod;
-import org.springframework.http.ResponseEntity;
+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;
 
@@ -227,4 +226,32 @@ public class UserCardServiceImpl extends ServiceImpl<UserCardMapper, UserCard> i
         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;
+    }
 }