Selaa lähdekoodia

fix : 导量逻辑修改

bilingfeng 1 vuosi sitten
vanhempi
commit
301686b17e

+ 1 - 1
game-module/game-module-manage/src/main/java/com/zanxiang/game/module/manage/ManageApplication.java

@@ -23,7 +23,7 @@ public class ManageApplication {
 
     public static void main(String[] args) {
         SpringApplication.run(ManageApplication.class, args);
-        System.out.println("赞象Manage服务启动成功 <小程序监听修改上线2> ( ´・・)ノ(._.`) \n" +
+        System.out.println("赞象Manage服务启动成功 <导量逻辑> ( ´・・)ノ(._.`) \n" +
                 "___  ___  ___   _   _   ___  _____  _____ \n" +
                 "|  \\/  | / _ \\ | \\ | | / _ \\|  __ \\|  ___|\n" +
                 "| .  . |/ /_\\ \\|  \\| |/ /_\\ \\ |  \\/| |__  \n" +

+ 6 - 0
game-module/game-module-manage/src/main/java/com/zanxiang/game/module/manage/constant/RedisKeyConstant.java

@@ -22,4 +22,10 @@ public class RedisKeyConstant {
      */
     public static final String PAY_APP_CHECK_LOCK = RedisKeyConstant.REDIS_PREFIX + "payApplicationCheck_lock";
 
+    /**
+     * 小程序失败计数器
+     */
+    public static final String APPLET_ERROR_COUNT = RedisKeyConstant.REDIS_PREFIX + "APPLET_ERROR_COUNT_";
+
+
 }

+ 70 - 0
game-module/game-module-manage/src/main/java/com/zanxiang/game/module/manage/enums/ExpireTimeEnum.java

@@ -0,0 +1,70 @@
+package com.zanxiang.game.module.manage.enums;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+
+/**
+ * @author : lingfeng
+ * @time : 2021-11-16
+ * @description : 时间常量
+ */
+@Getter
+@AllArgsConstructor
+public enum ExpireTimeEnum {
+
+    /**
+     * 无固定期限
+     */
+    NONE(0, "无固定期限"),
+
+    /**
+     * 1分钟
+     */
+    ONE_MIN(60, "1分钟"),
+
+    /**
+     * 5分钟
+     */
+    FIVE_MIN(5 * 60, "5分钟"),
+
+    /**
+     * 30分钟
+     */
+    HALF_HOUR(30 * 60, "30分钟"),
+
+    /**
+     * 1小时
+     */
+    ONE_HOUR(60 * 60, "1小时"),
+
+    /**
+     * 1天
+     */
+    ONE_DAY(24 * 60 * 60, "1天"),
+
+    /**
+     * 一周
+     */
+    ONE_WEEK(24 * 60 * 60 * 7, "一周"),
+
+    /**
+     * 1个月
+     */
+    ONE_MON(30 * 24 * 60 * 60, "1个月"),
+
+    /**
+     * 1年
+     */
+    ONE_YEAR(365 * 24 * 60 * 60, "1年");
+
+    /**
+     * 时间
+     */
+    private final long time;
+
+    /**
+     * 描述
+     */
+    private final String desc;
+
+}  

+ 26 - 0
game-module/game-module-manage/src/main/java/com/zanxiang/game/module/manage/service/impl/AppletCheckServiceImpl.java

@@ -12,7 +12,9 @@ import com.zanxiang.game.module.base.pojo.enums.PayApplicationTypeEnum;
 import com.zanxiang.game.module.base.pojo.enums.StatusEnum;
 import com.zanxiang.game.module.base.rpc.IWxApiServiceRpc;
 import com.zanxiang.game.module.manage.constant.RedisKeyConstant;
+import com.zanxiang.game.module.manage.enums.ExpireTimeEnum;
 import com.zanxiang.game.module.manage.service.*;
+import com.zanxiang.game.module.manage.utils.RedisUtil;
 import com.zanxiang.game.module.mybatis.entity.GamePayWay;
 import com.zanxiang.game.module.mybatis.entity.ListenCall;
 import com.zanxiang.game.module.mybatis.entity.PayApplication;
@@ -54,6 +56,9 @@ public class AppletCheckServiceImpl implements IAppletCheckService {
     @DubboReference(providedBy = ServerInfo.SERVER_SDK_DUBBO_NAME)
     private IWxApiServiceRpc wxApiServiceRpc;
 
+    @Autowired
+    private RedisUtil<Integer> redisUtil;
+
     @Autowired
     private IPayBoxService payBoxService;
 
@@ -93,6 +98,11 @@ public class AppletCheckServiceImpl implements IAppletCheckService {
 
     @Override
     public void payApplicationCheck(PayApplication payApplication) {
+        //异常计数器, 返回false的时候过滤
+        if (!this.countUpdate(payApplication.getAppId())) {
+            return;
+        }
+        //连续三次报警, 进行通知
         try {
             //更新游戏支付
             this.gamePayUpdate(payApplication);
@@ -113,6 +123,22 @@ public class AppletCheckServiceImpl implements IAppletCheckService {
         }
     }
 
+    private boolean countUpdate(String appId) {
+        String key = RedisKeyConstant.APPLET_ERROR_COUNT + appId;
+        Integer count = redisUtil.getCache(key);
+        if (count != null && count >= 2) {
+            redisUtil.deleteCache(key);
+            return Boolean.TRUE;
+        }
+        if (count == null) {
+            count = 1;
+        } else {
+            count += 1;
+        }
+        redisUtil.setCache(key, count, ExpireTimeEnum.HALF_HOUR.getTime());
+        return Boolean.FALSE;
+    }
+
     private void gamePayUpdate(PayApplication payApplication) {
         //修改支付应用状态
         log.error("支付应用异常 - 修改支付应用状态, appName : {}", payApplication.getAppName());

+ 11 - 4
game-module/game-module-manage/src/main/java/com/zanxiang/game/module/manage/service/impl/UserServiceImpl.java

@@ -108,12 +108,19 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IU
         if (gameDTO == null) {
             throw new BaseException("参数错误, 关联导量游戏信息不存在");
         }
+        //判断该玩家是否已经导过该游戏
+        if (super.count(new LambdaQueryWrapper<User>()
+                .eq(User::getGameId, gameDTO.getId())
+                .eq(User::getRelationUserId, userId)
+        ) > 0) {
+            throw new BaseException("玩家已经导到该游戏端, 请不要重复操作");
+        }
         //判断手机号是否被该游戏其他用户绑定
-        int count = super.count(new LambdaQueryWrapper<User>()
+        if (super.count(new LambdaQueryWrapper<User>()
                 .eq(User::getGameId, gameDTO.getId())
-                .eq(User::getMobile, mobile));
-        if (count > 0) {
-            throw new BaseException("参数错误, 该手机号已被该游戏其他玩家信息绑定");
+                .eq(User::getMobile, mobile)
+        ) > 0) {
+            throw new BaseException("手机号已被游戏其他玩家绑定");
         }
         //复制用户信息
         User h5User = BeanUtil.copy(user, User.class);

+ 272 - 0
game-module/game-module-manage/src/main/java/com/zanxiang/game/module/manage/utils/RedisUtil.java

@@ -0,0 +1,272 @@
+package com.zanxiang.game.module.manage.utils;
+
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.*;
+import org.springframework.stereotype.Component;
+
+import java.util.Set;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * @author : lingfeng
+ * @time : 2021-11-23
+ * @description : redis缓存通用前缀
+ */
+@Component
+@Slf4j
+public class RedisUtil<T> {
+
+    @Autowired
+    private RedisTemplate redisTemplate;
+
+    /**
+     * 设置缓存
+     *
+     * @param key  : 缓存key
+     * @param t    : 缓存对象
+     * @param time : 过期时间
+     * @return : 返回是否设置成功
+     */
+    public boolean setCache(String key, T t, long time) {
+        try {
+            ValueOperations<String, T> operations = redisTemplate.opsForValue();
+            operations.set(key, t, time, TimeUnit.SECONDS);
+            return true;
+        } catch (Exception e) {
+            log.error("缓存添加异常, key : {}, value : {}, e : {}", key, t, e.getMessage());
+        }
+        return false;
+    }
+
+    /**
+     * 获取缓存方法
+     *
+     * @param key : 缓存key
+     * @return : 返回缓存对象
+     */
+    public T getCache(String key) {
+        try {
+            if (redisTemplate.hasKey(key)) {
+                ValueOperations<String, T> operations = redisTemplate.opsForValue();
+                return operations.get(key);
+            }
+        } catch (Exception e) {
+            log.error("获取缓存异常, key : {}, e : {}", key, e.getMessage());
+        }
+        return null;
+    }
+
+    /**
+     * redis 事物锁设置方法
+     *
+     * @param key  : 缓存key
+     * @param t    : 缓存对象
+     * @param time : 过期时间
+     * @return : 返回是否设置成功
+     */
+    public boolean setIfAbsent(String key, T t, long time) {
+        try {
+            ValueOperations<String, T> operations = redisTemplate.opsForValue();
+            return operations.setIfAbsent(key, t, time, TimeUnit.SECONDS);
+        } catch (Exception e) {
+            log.error("缓存添加异常, key : {}, value : {}, e : {}", key, t, e.getMessage());
+        }
+        return false;
+    }
+
+    /**
+     * 删除缓存的方法
+     *
+     * @param key : 缓存key
+     * @return : 返回缓存对象
+     */
+    public boolean deleteCache(String key) {
+        try {
+            if (redisTemplate.hasKey(key)) {
+                return redisTemplate.delete(key);
+            }
+        } catch (Exception e) {
+            log.error("删除缓存异常, key : {}, e : {}", key, e.getMessage());
+        }
+        return false;
+    }
+
+    /**
+     * set集合添加元素
+     *
+     * @param key : 缓存key
+     * @param t   : 需要添加的元素
+     * @return : 返回是否添加成功
+     */
+    public boolean addToSet(String key, T... t) {
+        try {
+            SetOperations<String, T> setOperations = redisTemplate.opsForSet();
+            Long add = setOperations.add(key, t);
+            return add != null && add >= 1;
+        } catch (Exception e) {
+            log.error("Set添加失败,key : {}, e : {}", key, e.getMessage());
+        }
+        return false;
+    }
+
+    /**
+     * set集合删除元素
+     *
+     * @param key : 缓存key
+     * @param t   : 需要删除的元素
+     * @return : 返回是否删除成功
+     */
+    public boolean removeOfSet(String key, T... t) {
+        try {
+            SetOperations<String, T> setOperations = redisTemplate.opsForSet();
+            Long remove = setOperations.remove(key, t);
+            return remove != null && remove >= 1;
+        } catch (Exception e) {
+            log.error("Set删除失败,key : {}, e : {}", key, e.getMessage());
+        }
+        return false;
+    }
+
+    /**
+     * 判断元素是否存在于set集合
+     *
+     * @param key : 缓存key
+     * @param t   : 需要判断的元素
+     * @return : 判断元素是否存在
+     */
+    public boolean isMemberInSet(String key, T t) {
+        try {
+            SetOperations<String, T> setOperations = redisTemplate.opsForSet();
+            return setOperations.isMember(key, t);
+        } catch (Exception e) {
+            log.error("Set元素是否存在判断异常, key : {}, e : {}", key, e.getMessage());
+        }
+        return false;
+    }
+
+    /**
+     * 添加单个缓存到list
+     *
+     * @param key   : 缓存key
+     * @param t     : 缓存元素
+     * @param isTop : isTop :  真:前 假:后
+     * @return : 返回添加结果
+     */
+    public boolean setListCache(String key, T t, boolean isTop) {
+        try {
+            ListOperations<String, T> listOperations = redisTemplate.opsForList();
+            if (isTop) {
+                listOperations.leftPush(key, t);
+            } else {
+                listOperations.rightPush(key, t);
+            }
+            return true;
+        } catch (Exception e) {
+            log.error("redis添加队列元素失败,key : {}, e : {}", key, e.getMessage());
+        }
+        return false;
+    }
+
+    /**
+     * 获取列表中的前or后的值
+     *
+     * @param key   : 缓存key
+     * @param isTop :  真:前 假:后
+     * @return : 返回缓存数据
+     */
+    public T getListOneCache(String key, boolean isTop) {
+        try {
+            if (redisTemplate.hasKey(key)) {
+                ListOperations<String, T> listOperations = redisTemplate.opsForList();
+                T t = null;
+                if (isTop) {
+                    t = listOperations.leftPop(key);
+                } else {
+                    t = listOperations.rightPop(key);
+                }
+                return t;
+            }
+
+        } catch (Exception e) {
+            log.error("redis获取队列元素失败,key : {}, e : {}", key, e.getMessage());
+        }
+        return null;
+    }
+
+    /**
+     * 添加单个元素进入有序集合
+     *
+     * @param key   : 缓存key
+     * @param t     : 缓存值
+     * @param score : 分数
+     * @return : 返回是否添加成功
+     */
+    public boolean addZSet(String key, T t, double score) {
+        try {
+            ZSetOperations zSet = redisTemplate.opsForZSet();
+            Boolean add = zSet.add(key, t, score);
+            return add == null ? false : add;
+        } catch (Exception e) {
+            log.error("ZSet添加失败元素失败, key : {}, t : {}, score : {}, e : {}", key, t.toString(), score, e.getMessage());
+        }
+        return false;
+    }
+
+    /**
+     * 获取分数区间
+     *
+     * @param key : 缓存key
+     * @param min : 最小值
+     * @param max : 最大值
+     * @return {@link Set}<{@link T}>
+     */
+    public Set<T> getZSetByScore(String key, Double min, Double max) {
+        try {
+            ZSetOperations<String, T> zSet = redisTemplate.opsForZSet();
+            Set<T> set = zSet.rangeByScore(key, min, max);
+            return set;
+        } catch (Exception e) {
+            log.error("ZSet获取元素失败, key : {}, min : {}, max : {}, e : {}", key, min, max, e.getMessage());
+        }
+        return null;
+    }
+
+
+    /**
+     * 按照索引区间获取(按分数正序排序)
+     * (0, -1)表示获取全部
+     *
+     * @param key   : 缓存key
+     * @param start : 开始
+     * @param end   : 结束
+     * @return {@link Set}<{@link T}>
+     */
+    public Set<T> rangeZSet(String key, long start, long end) {
+        try {
+            ZSetOperations<String, T> zSet = redisTemplate.opsForZSet();
+            return zSet.range(key, start, end);
+        } catch (Exception e) {
+            log.error("ZSet获取元素失败, key : {}, start : {}, end : {}, e : {}", key, start, end, e.getMessage());
+        }
+        return null;
+    }
+
+    /**
+     * 按照缓存值删除
+     *
+     * @param key : 缓存key
+     * @param t   : 缓存值
+     * @return boolean : 删除结果
+     */
+    public boolean delZSetValue(String key, T t) {
+        try {
+            ZSetOperations<String, T> zSet = redisTemplate.opsForZSet();
+            Long remove = zSet.remove(key, t);
+            return remove != null && remove == 1;
+        } catch (Exception e) {
+            log.error("ZSet删除元素失败, key : {}, t : {}, , e : {}", key, t, e.getMessage());
+        }
+        return false;
+    }
+}