浏览代码

代码优化

dsx 2 年之前
父节点
当前提交
c9597d9044

+ 7 - 0
pom.xml

@@ -168,6 +168,13 @@
             <version>3.0.0-SNAPSHOT</version>
         </dependency>
 
+        <!--htt请求工具-->
+        <dependency>
+            <groupId>com.dtflys.forest</groupId>
+            <artifactId>forest-spring-boot-starter</artifactId>
+            <version>1.5.24</version>
+        </dependency>
+
 
     </dependencies>
     <dependencyManagement>

+ 9 - 8
src/main/java/com/fdkankan/openApi/component/ValidateApiAOP.java

@@ -68,14 +68,15 @@ public class ValidateApiAOP {
                 throw new ApiBusinessException(HttpStatus.COUNT_OVER, "");
             }
         }
-        if (counting) {
-            //预减次数
-            long decrStock = redisUtil.decr(String.format(RedisKey.API_METHOD_COUNT, authorization, method), 1);
-            log.info("前置减次数后,{}", decrStock);
-            if (decrStock < 1) {
-                localOverCache.setTimedCache(authorization, true);
-            }
-        }
+        // TODO: 2023/4/17 次数校验,先注释掉,现阶段测试接口是否可用,需要测试次数时再放开,次数校验按照用户维度还是接口维度待定 method
+//        if (counting) {
+//            //预减次数
+//            long decrStock = redisUtil.decr(String.format(RedisKey.API_METHOD_COUNT, authorization, method), 1);
+//            log.info("前置减次数后,{}", decrStock);
+//            if (decrStock < 1) {
+//                localOverCache.setTimedCache(authorization, true);
+//            }
+//        }
 
     }
 

+ 64 - 0
src/main/java/com/fdkankan/openApi/controller/system/SsoUserController.java

@@ -0,0 +1,64 @@
+package com.fdkankan.openApi.controller.system;
+
+
+import cn.dev33.satoken.annotation.SaIgnore;
+import cn.dev33.satoken.stp.SaLoginModel;
+import cn.dev33.satoken.stp.StpUtil;
+import com.fdkankan.common.util.PasswordUtils;
+import com.fdkankan.openApi.controller.BaseController;
+import com.fdkankan.openApi.entity.system.Users;
+import com.fdkankan.openApi.httpclient.client.FdKKClient;
+import com.fdkankan.openApi.service.system.IUsersService;
+import com.fdkankan.openApi.vo.system.LoginParam;
+import com.fdkankan.openApi.vo.www.FdkkLoginVo;
+import com.fdkankan.web.response.Result;
+import com.fdkankan.web.response.ResultData;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+/**
+ * <p>
+ *  前端控制器
+ * </p>
+ *
+ * @author 
+ * @since 2023-02-15
+ */
+@RestController
+@RequestMapping("/sso/user")
+public class SsoUserController extends BaseController {
+
+    @Autowired
+    private FdKKClient fdKKClient;
+    @Autowired
+    private IUsersService usersService;
+
+    /**
+     * 登录
+     * @return
+     */
+    @SaIgnore
+    @PostMapping("/login")
+    public ResultData getScenesByUsername(@RequestBody LoginParam param) {
+        String password = PasswordUtils.decycptPasswordWeb(param.getPassword());
+        param.setPassword(password);
+        Result<FdkkLoginVo> fdkkLoginVo = fdKKClient.fdkkLogin(param);
+        SaLoginModel saLoginModel = new SaLoginModel();
+        saLoginModel
+                .setIsLastingCookie(false)
+                .setToken(fdkkLoginVo.getData().getToken());
+        FdkkLoginVo data = fdkkLoginVo.getData();
+        Users users = usersService.findByUserName(data.getUser().getUserName(), data.getUser().getId(), data.getUser().getHead());
+        StpUtil.login(users.getUserName(), saLoginModel);
+        StpUtil.getSession().set("user", users);
+        data.getUser().setFdkkId(data.getUser().getId());
+        data.getUser().setId(users.getId());
+        return ResultData.ok(data);
+    }
+
+
+}
+

+ 5 - 0
src/main/java/com/fdkankan/openApi/controller/www/SceneController.java

@@ -5,6 +5,7 @@ import cn.dev33.satoken.annotation.SaIgnore;
 import com.fdkankan.common.constant.ErrorCode;
 import com.fdkankan.common.exception.BusinessException;
 import com.fdkankan.openApi.common.PageInfo;
+import com.fdkankan.openApi.component.ValidateApi;
 import com.fdkankan.openApi.controller.BaseController;
 import com.fdkankan.openApi.entity.www.ScenePlus;
 import com.fdkankan.openApi.service.laser.SceneService;
@@ -45,6 +46,7 @@ public class SceneController extends BaseController {
      */
     @SaIgnore
     @PostMapping("/getSceneList")
+    @ValidateApi(method = "scene:getSceneList")
     public ResultData getScenesByUsername(@RequestBody PageScenesParamVo param) {
         PageInfo pageInfo = scenePlusService.getScenesByUserId(this.getUserId(),param);
         return ResultData.ok(pageInfo);
@@ -57,6 +59,7 @@ public class SceneController extends BaseController {
      */
     @SaIgnore
     @PostMapping("/getScenePointInfo")
+    @ValidateApi(method = "scene:getScenePointInfo")
     public ResultData getScenePointInfo(@RequestBody @Validated BaseSceneParamVo param) {
         ScenePlus scenePlus = scenePlusService.getByNumAndUserId(this.getUserId(), param.getSceneCode());
         if(ObjectUtils.isEmpty(scenePlus)){
@@ -74,6 +77,7 @@ public class SceneController extends BaseController {
      */
     @SaIgnore
     @PostMapping("/getSceneObj")
+    @ValidateApi(method = "scene:getSceneObj")
     public ResultData getSceneObjFiles(@RequestBody @Validated BaseSceneParamVo param) {
         ScenePlus scenePlus = scenePlusService.getByNumAndUserId(this.getUserId(), param.getSceneCode());
         if(ObjectUtils.isEmpty(scenePlus)){
@@ -88,6 +92,7 @@ public class SceneController extends BaseController {
      */
     @SaIgnore
     @PostMapping("/getScenePanorama")
+    @ValidateApi(method = "scene:getScenePanorama")
     public ResultData getScenePanoramicImageFiles(@RequestBody @Validated BaseSceneParamVo param) {
         ScenePlus scenePlus = scenePlusService.getByNumAndUserId(this.getUserId(), param.getSceneCode());
         if (ObjectUtils.isEmpty(scenePlus)) {

+ 20 - 0
src/main/java/com/fdkankan/openApi/httpclient/address/FdkkAddressSource.java

@@ -0,0 +1,20 @@
+package com.fdkankan.openApi.httpclient.address;
+
+import com.dtflys.forest.callback.AddressSource;
+import com.dtflys.forest.http.ForestAddress;
+import com.dtflys.forest.http.ForestRequest;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+
+@Component
+public class FdkkAddressSource implements AddressSource {
+
+    @Value("${4dkk.fdService.basePath}")
+    private String basePath;
+
+
+    @Override
+    public ForestAddress getAddress(ForestRequest forestRequest) {
+        return new ForestAddress("","",null,basePath);
+    }
+}

+ 46 - 0
src/main/java/com/fdkankan/openApi/httpclient/callback/ErrorCallback.java

@@ -0,0 +1,46 @@
+package com.fdkankan.openApi.httpclient.callback;
+
+import cn.hutool.core.util.StrUtil;
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.dtflys.forest.callback.OnError;
+import com.dtflys.forest.exceptions.ForestRuntimeException;
+import com.dtflys.forest.http.ForestRequest;
+import com.dtflys.forest.http.ForestResponse;
+import com.fdkankan.common.exception.BusinessException;
+import lombok.extern.slf4j.Slf4j;
+
+import java.util.Objects;
+
+/**
+ * <p>
+ * TODO
+ * </p>
+ *
+ * @author dengsixing
+ * @since 2022/4/25
+ **/
+@Slf4j
+public class ErrorCallback implements OnError {
+
+    @Override
+    public void onError(ForestRuntimeException e, ForestRequest forestRequest,
+        ForestResponse forestResponse) {
+        JSONObject jsonObject = JSON.parseObject(forestResponse.getContent());
+        Integer status = jsonObject.getInteger("status");
+        if(status != null && status == 500){
+            log.error("接口报错,url:{},status:{},error:{}", forestRequest.getUrl(), status, jsonObject.getString("error"));
+            throw e;
+        }
+        JSONObject result = JSON.parseObject(forestResponse.getContent(), JSONObject.class);
+        Integer code = result.getInteger("code");
+        String message = result.getString("message");
+        if(StrUtil.isEmpty(message)){
+            message = result.getString("msg");
+        }
+        if(Objects.isNull(code) || code != 0){
+            throw new BusinessException(code, message);
+        }
+        throw new BusinessException(-1, "未知异常");
+    }
+}

+ 25 - 0
src/main/java/com/fdkankan/openApi/httpclient/callback/SuccessCallback.java

@@ -0,0 +1,25 @@
+package com.fdkankan.openApi.httpclient.callback;
+
+import com.alibaba.fastjson.JSONObject;
+import com.dtflys.forest.callback.OnSuccess;
+import com.dtflys.forest.http.ForestRequest;
+import com.dtflys.forest.http.ForestResponse;
+import lombok.extern.slf4j.Slf4j;
+
+/**
+ * <p>
+ * TODO
+ * </p>
+ *
+ * @author dengsixing
+ * @since 2022/4/25
+ **/
+@Slf4j
+public class SuccessCallback implements OnSuccess<JSONObject> {
+
+    @Override
+    public void onSuccess(JSONObject result, ForestRequest forestRequest,
+        ForestResponse forestResponse) {
+        log.info("请求成功,url:{},result:{}", forestRequest.getUrl(), forestResponse.getContent());
+    }
+}

+ 52 - 0
src/main/java/com/fdkankan/openApi/httpclient/callback/SuccessCondition.java

@@ -0,0 +1,52 @@
+package com.fdkankan.openApi.httpclient.callback;
+
+import cn.hutool.core.util.StrUtil;
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.dtflys.forest.callback.SuccessWhen;
+import com.dtflys.forest.http.ForestRequest;
+import com.dtflys.forest.http.ForestResponse;
+
+import java.util.Objects;
+
+/**
+ * <p>
+ *  自定义成功/失败条件实现类
+ *  需要实现 SuccessWhen 接口
+ * </p>
+ *
+ * @author dengsixing
+ * @since 2022/4/25
+ **/
+
+public class SuccessCondition implements SuccessWhen {
+
+    /**
+     * 请求成功条件
+     * @param req Forest请求对象
+     * @param res Forest响应对象
+     * @return 是否成功,true: 请求成功,false: 请求失败
+     */
+    @Override
+    public boolean successWhen(ForestRequest req, ForestResponse res) {
+        boolean reqStatus = res.noException() &&   // 请求过程没有异常
+            res.statusOk() &&     // 并且状态码在 100 ~ 399 范围内
+            res.statusIsNot(203);
+        if(!reqStatus){
+            return reqStatus;
+        }
+
+        String content = res.getContent();
+        if(StrUtil.isEmpty(content)){
+            reqStatus = false;
+            return reqStatus;
+        }
+        JSONObject result = JSON.parseObject(content, JSONObject.class);
+        Integer code = result.getInteger("code");
+        if(Objects.isNull(code) || code != 0){
+            reqStatus = false;
+            return reqStatus;
+        }
+        return true;
+    }
+}

+ 26 - 0
src/main/java/com/fdkankan/openApi/httpclient/client/FdKKClient.java

@@ -0,0 +1,26 @@
+package com.fdkankan.openApi.httpclient.client;
+
+import com.dtflys.forest.annotation.Address;
+import com.dtflys.forest.annotation.JSONBody;
+import com.dtflys.forest.annotation.Post;
+import com.dtflys.forest.annotation.Success;
+import com.fdkankan.openApi.httpclient.address.FdkkAddressSource;
+import com.fdkankan.openApi.httpclient.callback.SuccessCondition;
+import com.fdkankan.openApi.vo.www.FdkkLoginVo;
+import com.fdkankan.web.response.Result;
+
+/**
+ * 获取,调用4dkk服务
+ */
+@Address(source = FdkkAddressSource.class)
+@Success(condition = SuccessCondition.class)
+public interface FdKKClient {
+
+    /**
+     * 登录
+     */
+    @Post("/api/sso/user/login")
+    Result<FdkkLoginVo> fdkkLogin(@JSONBody Object param);
+
+
+}

+ 9 - 0
src/main/java/com/fdkankan/openApi/service/system/ISsoUserService.java

@@ -0,0 +1,9 @@
+package com.fdkankan.openApi.service.system;
+
+/**
+ * 登录
+ */
+public interface ISsoUserService{
+
+
+}

+ 8 - 0
src/main/java/com/fdkankan/openApi/service/system/impl/SsoUserServiceImpl.java

@@ -0,0 +1,8 @@
+package com.fdkankan.openApi.service.system.impl;
+
+import com.fdkankan.openApi.service.system.ISsoUserService;
+import org.springframework.stereotype.Service;
+
+@Service
+public class SsoUserServiceImpl implements ISsoUserService {
+}

+ 17 - 0
src/main/java/com/fdkankan/openApi/vo/system/LoginParam.java

@@ -0,0 +1,17 @@
+package com.fdkankan.openApi.vo.system;
+
+import lombok.Data;
+
+import javax.validation.constraints.NotBlank;
+
+@Data
+public class LoginParam {
+    private String areaNum ="86";
+    @NotBlank(message = "用户名不能为空")
+    private String phoneNum;        //用户名
+    @NotBlank(message = "密码不能为空")
+    private String password;        //密码
+    private Integer cameraType;
+    private String msgAuthCode;
+    private String clear;
+}

+ 9 - 0
src/main/java/com/fdkankan/openApi/vo/www/FdkkLoginVo.java

@@ -0,0 +1,9 @@
+package com.fdkankan.openApi.vo.www;
+
+import lombok.Data;
+
+@Data
+public class FdkkLoginVo {
+    private String token;
+    private FdkkUserVo user;
+}

+ 26 - 0
src/main/java/com/fdkankan/openApi/vo/www/FdkkUserVo.java

@@ -0,0 +1,26 @@
+package com.fdkankan.openApi.vo.www;
+
+import lombok.Data;
+
+@Data
+public class FdkkUserVo {
+    private Integer id ;
+    private Integer fdkkId ;
+    private String userName;                    //用户名
+    private String head;                        //头像
+    private String nickName;                    //昵称
+    private String email;                       //邮箱
+    private Integer availableSpace;             //可用空间
+    private Long cameraCount;                   //已绑相机
+    private Integer downloadNum;                //已下载场景总数
+    private Integer downloadNumTotal;           //总的下载场景总数
+    private Long incrementBindNum;              //已绑定的会员权益数量
+    private Long incrementNum;                  //会员权益数量
+
+    private Long cameraId;
+    private Integer cameraLogin;
+
+    private Long sceneCount;
+    private Long expansionTotal;
+
+}

+ 4 - 0
src/main/resources/bootstrap-prod.yml

@@ -29,6 +29,10 @@ spring:
             group: DEFAULT_GROUP
             refresh: true
 
+          - data-id: forest-config.yaml
+            group: DEFAULT_GROUP
+            refresh: true
+
       discovery:
         namespace: ${spring.cloud.nacos.namespace}
 

+ 4 - 0
src/main/resources/bootstrap-test.yml

@@ -29,6 +29,10 @@ spring:
             group: DEFAULT_GROUP
             refresh: true
 
+          - data-id: forest-config.yaml
+            group: DEFAULT_GROUP
+            refresh: true
+
       discovery:
         namespace: ${spring.cloud.nacos.namespace}