浏览代码

用户登录验证及用户信息获取注解封装

xufeng 2 年之前
父节点
当前提交
4bc4b6fbc9

+ 0 - 9
game-module/game-common/src/main/java/com/zanxiang/common/Test.java

@@ -1,9 +0,0 @@
-package com.zanxiang.common;
-
-import lombok.Data;
-
-//@Entity(name="test")
-@Data
-public class Test {
-    private Long Id;
-}

+ 11 - 1
game-module/game-common/src/main/java/com/zanxiang/common/enums/HttpStatusEnum.java

@@ -27,6 +27,11 @@ public enum HttpStatusEnum {
      */
     UNKNOWN_ERROR(-9999, "未知错误"),
 
+    /**
+     * 请先登录
+     */
+    USER_NO_LOGIN(40400, "请登录"),
+
     /**
      * 用户名为空
      */
@@ -315,7 +320,12 @@ public enum HttpStatusEnum {
     /**
      * 玩家充值未达标
      */
-    MEM_NOT_REACH_STAND(41407, "玩家充值未达标");
+    MEM_NOT_REACH_STAND(41407, "玩家充值未达标"),
+
+    /**
+     * 玩家充值未达标
+     */
+    ORDER_AMOUNT_IS_NULL(41500, "订单金额为空");
 
     /**
      * 回传类型

+ 7 - 0
game-module/game-common/src/main/java/com/zanxiang/common/exception/CustomException.java

@@ -1,5 +1,7 @@
 package com.zanxiang.common.exception;
 
+import com.zanxiang.common.enums.HttpStatusEnum;
+
 /**
  * 自定义异常
  */
@@ -19,6 +21,11 @@ public class CustomException extends RuntimeException {
         this.code = code;
     }
 
+    public CustomException(HttpStatusEnum httpStatusEnum) {
+        this.message = httpStatusEnum.getMsg();
+        this.code = httpStatusEnum.getCode();
+    }
+
     public CustomException(String message, Throwable e) {
         super(message, e);
         this.message = message;

+ 1 - 1
game-module/game-common/src/main/java/com/zanxiang/common/handler/GlobalExceptionHandler.java

@@ -37,7 +37,7 @@ public class GlobalExceptionHandler {
         if (StringUtils.isNull(e.getCode())) {
             return ResultVo.fail(e.getMessage());
         }
-        return ResultVo.fail(e.getMessage());
+        return new ResultVo<>(e.getCode(), e.getMessage());
     }
 
     @ExceptionHandler(Exception.class)

+ 5 - 0
game-module/game-manage/pom.xml

@@ -16,6 +16,11 @@
             <groupId>com.zanxiang.game</groupId>
             <version>0.0.1-SNAPSHOT</version>
         </dependency>
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>fastjson</artifactId>
+            <version>${alibaba.fastjson.version}</version>
+        </dependency>
     </dependencies>
 
 </project>

+ 13 - 0
game-module/game-sdk/src/main/java/com/zanxiang/sdk/common/annotation/ValidLogin.java

@@ -0,0 +1,13 @@
+package com.zanxiang.sdk.common.annotation;
+
+import java.lang.annotation.*;
+
+/**
+ * @author xufeng
+ * @date 2022/6/21 11:24
+ */
+@Target({ElementType.PARAMETER})
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+public @interface ValidLogin {
+}

+ 64 - 0
game-module/game-sdk/src/main/java/com/zanxiang/sdk/common/resolver/TokenArgumentResolver.java

@@ -0,0 +1,64 @@
+package com.zanxiang.sdk.common.resolver;
+
+import com.zanxiang.common.enums.HttpStatusEnum;
+import com.zanxiang.common.exception.CustomException;
+import com.zanxiang.common.utils.StringUtils;
+import com.zanxiang.common.utils.bean.BeanUtils;
+import com.zanxiang.sdk.common.annotation.ValidLogin;
+import com.zanxiang.sdk.domain.dto.UserTokenDTO;
+import com.zanxiang.sdk.domain.params.UserData;
+import com.zanxiang.sdk.service.IUserTokenService;
+import org.springframework.core.MethodParameter;
+import org.springframework.stereotype.Component;
+import org.springframework.stereotype.Service;
+import org.springframework.web.bind.support.WebDataBinderFactory;
+import org.springframework.web.context.request.NativeWebRequest;
+import org.springframework.web.method.support.HandlerMethodArgumentResolver;
+import org.springframework.web.method.support.ModelAndViewContainer;
+
+import javax.annotation.Resource;
+import javax.servlet.http.HttpServletRequest;
+
+/**
+ * token转用户信息解析器
+ *
+ * @author xufeng
+ * @date 2022/6/20 09:59
+ */
+@Component
+@Service
+public class TokenArgumentResolver implements HandlerMethodArgumentResolver {
+
+    @Resource
+    private IUserTokenService iUserTokenService;
+
+    @Override
+    public boolean supportsParameter(MethodParameter parameter) {
+        return parameter.getParameterType().equals(UserData.class);
+    }
+
+    @Override
+    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
+        HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);
+        if (request == null) {
+            throw new CustomException(HttpStatusEnum.UNKNOWN_ERROR);
+        }
+        boolean b = parameter.hasParameterAnnotation(ValidLogin.class);
+        String token = request.getHeader("token");
+        if (b && StringUtils.isEmpty(token)) {
+            throw new CustomException(HttpStatusEnum.USER_NO_LOGIN);
+        }
+        String deviceType = "";
+        if (StringUtils.isEmpty(token)) {
+            return null;
+        }
+        UserTokenDTO tokenInfoByTokenDevice = iUserTokenService.getTokenInfoByTokenDevice(token, deviceType);
+        if (tokenInfoByTokenDevice == null) {
+            return null;
+        }
+        if (b && tokenInfoByTokenDevice.getUserId() == null) {
+            throw new CustomException(HttpStatusEnum.USER_NO_LOGIN);
+        }
+        return BeanUtils.copy(tokenInfoByTokenDevice, UserData.class);
+    }
+}

+ 23 - 2
game-module/game-sdk/src/main/java/com/zanxiang/sdk/controller/PayController.java

@@ -2,7 +2,11 @@ package com.zanxiang.sdk.controller;
 
 import com.alibaba.nacos.common.utils.IPUtil;
 import com.zanxiang.common.domain.ResultMap;
+import com.zanxiang.common.domain.ResultVo;
+import com.zanxiang.common.enums.HttpStatusEnum;
 import com.zanxiang.common.enums.PayWayEnum;
+import com.zanxiang.common.utils.StringUtils;
+import com.zanxiang.sdk.domain.bo.PlatformOrderBO;
 import com.zanxiang.sdk.domain.bo.ProductPayParamBO;
 import com.zanxiang.sdk.domain.dto.PlatformOrderDTO;
 import com.zanxiang.sdk.domain.params.ProductPayParam;
@@ -12,12 +16,15 @@ import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import org.jdom.JDOMException;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.*;
 
 import javax.annotation.Resource;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
+import java.math.BigDecimal;
+import java.util.Objects;
 
 /**
  * 支付公共入口
@@ -37,10 +44,24 @@ public class PayController {
 
     @ApiOperation(value = "支付参数生成")
     @GetMapping(value = "/create")
-    public ResultMap create(@RequestBody ProductPayParam product) {
+    public ResultMap create(@Validated @RequestBody ProductPayParam product) {
+        String userId = "1";
+        if (StringUtils.isEmpty(product.getOrderId())) {
+            //虚拟充值情况,直接传金额实时生成订单
+            if (product.getAmount() == null || Objects.equals(product.getAmount(), BigDecimal.ZERO)) {
+                return ResultMap.error(HttpStatusEnum.ORDER_AMOUNT_IS_NULL.getMsg());
+            }
+            PlatformOrderBO orderBO = new PlatformOrderBO();
+            orderBO.setAmount(product.getAmount());
+            orderBO.setProductName("充值" + product.getAmount());
+            orderBO.setProductId("ptb_charge");
+            orderBO.setMgUserId(product.getMgUserId());
+            orderBO.setUserId(userId);
+            String orderId = platformOrderService.create(orderBO);
+            product.setOrderId(orderId);
+        }
         PlatformOrderDTO info = platformOrderService.info("1", "1");
         System.out.println(info);
-        String userId = "1";
         ProductPayParamBO bo = new ProductPayParamBO();
         bo.setUserId(userId);
         bo.setPayDevice(product.getPayDevice());

+ 4 - 0
game-module/game-sdk/src/main/java/com/zanxiang/sdk/domain/params/CommonParam.java

@@ -14,6 +14,10 @@ public class CommonParam {
     @JsonAlias("app_id")
     private String gameId;
 
+    @ApiModelProperty("游戏玩家id")
+    @JsonAlias("mg_user_id")
+    private String mgUserId;
+
     //==============device 相关===============
     @ApiModelProperty("设备id")
     @JsonAlias("device-device_id")

+ 6 - 2
game-module/game-sdk/src/main/java/com/zanxiang/sdk/domain/params/ProductPayParam.java

@@ -5,6 +5,8 @@ import com.sun.istack.NotNull;
 import io.swagger.annotations.ApiModelProperty;
 import lombok.Data;
 
+import java.math.BigDecimal;
+
 /**
  * 支付调起生成相关参数
  *
@@ -17,16 +19,18 @@ public class ProductPayParam extends CommonParam {
 
     @ApiModelProperty("订单id")
     @JsonAlias("device-orderId")
-    @NotNull
     private String orderId;
 
+    @ApiModelProperty("金额")
+    private BigDecimal amount;
+
     @ApiModelProperty("支付类型(1:支付宝 2:微信 3:银联)")
     @JsonAlias("payway")
     @NotNull
     private Integer payWay;
 
     @ApiModelProperty("支付方式 (1:PC,平板 2:Wap 3:App )")
-    @NotNull
+//    @NotNull
     private Integer payDevice;
 
 

+ 5 - 5
game-module/game-sdk/src/main/java/com/zanxiang/sdk/service/Impl/PlatformOrderServiceImpl.java

@@ -108,8 +108,8 @@ public class PlatformOrderServiceImpl extends ServiceImpl<OrderMapper, Order> im
 
     @Override
     public PlatformOrderDTO info(String id, String userId) {
-        Order order = orderMapper.selectByPrimaryKey(Long.valueOf(id));
-//        Order order = getOne(new LambdaQueryWrapper<Order>().eq(Order::getId, id).eq(Order::getUserId, userId));
+//        Order order = orderMapper.selectByPrimaryKey(Long.valueOf(id));
+        Order order = getOne(new LambdaQueryWrapper<Order>().eq(Order::getId, id).eq(Order::getUserId, userId));
         return BeanUtils.copy(order, PlatformOrderDTO.class);
     }
 
@@ -137,9 +137,9 @@ public class PlatformOrderServiceImpl extends ServiceImpl<OrderMapper, Order> im
                 if (StringUtils.isEmpty(platformOrderBO.getProductName())) {
                     throw new NullPointerException("ProductName");
                 }
-                if (platformOrderBO.getProductCnt() == null) {
-                    throw new NullPointerException("ProductCnt");
-                }
+//                if (platformOrderBO.getProductCnt() == null) {
+//                    throw new NullPointerException("ProductCnt");
+//                }
                 if (platformOrderBO.getAmount() == null) {
                     throw new NullPointerException("Amount");
                 }

+ 2 - 0
game-module/game-sdk/src/main/resources/META-INF/spring.factories

@@ -0,0 +1,2 @@
+org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
+  com.zanxiang.common.handler.GlobalExceptionHandler