ソースを参照

app登录相关

lyhzzz 3 年 前
コミット
29125edc22

+ 3 - 0
src/main/java/com/fdkankan/ucenter/constant/LoginConstant.java

@@ -112,4 +112,7 @@ public class LoginConstant {
     public static final int FAILURE_CODE_3034 = 3034;
     public static final String FAILURE_MSG_3034 = "服务器繁忙,请重试!";
 
+    public static final int FAILURE_CODE_3035 = 3035;
+    public static final String FAILURE_MSG_3035 = "登录二维码已失效,请重新获取";
+
 }

+ 84 - 5
src/main/java/com/fdkankan/ucenter/controller/AppController.java

@@ -1,13 +1,16 @@
 package com.fdkankan.ucenter.controller;
 
+import com.alibaba.fastjson.JSONObject;
+import com.fdkankan.common.util.JwtUtil;
 import com.fdkankan.ucenter.common.Result;
 import com.fdkankan.ucenter.service.impl.AppService;
+import com.fdkankan.ucenter.service.impl.LoginService;
+import com.fdkankan.ucenter.vo.request.AppLoginParam;
 import com.fdkankan.ucenter.vo.request.LoginParam;
+import com.fdkankan.ucenter.vo.request.RegisterParam;
+import org.apache.commons.lang3.StringUtils;
 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;
+import org.springframework.web.bind.annotation.*;
 
 @RestController
 @RequestMapping("/api/sso/app")
@@ -15,13 +18,89 @@ public class AppController {
 
     @Autowired
     private AppService appService;
+    @Autowired
+    private LoginService loginService;
     /**
      * 登录
      * phoneNum 用户名
      * password 密码
      */
     @PostMapping("/userLogin")
-    public Result login(@RequestBody LoginParam param){
+    public Result userLogin(@RequestBody LoginParam param){
         return Result.success(appService.login(param));
     }
+
+    /**
+     * app扫码登录
+     * appUserName  相机设备嘛
+     * appPassword  相机密码
+     * uuid
+     */
+    @PostMapping("/login")
+    public Result login(@RequestBody AppLoginParam param){
+        appService.appLogin(param);
+        return Result.success();
+    }
+    /**
+     * app登录
+     * appUserName  相机设备嘛
+     * appPassword  相机密码
+     */
+    @PostMapping("/login2")
+    public Result login2(@RequestBody AppLoginParam param){
+        return Result.success(appService.login2(param));
+    }
+
+    @PostMapping("/quickLogin")
+    public Result quickLogin(@RequestBody LoginParam param){
+        return  Result.success(appService.quickLogin(param));
+    }
+
+    /**
+     * 注册
+     * @param param
+     * @return
+     */
+    @PostMapping("/register")
+    public Result register(RegisterParam param){
+        param.setConfirmPwd(param.getPassword());
+        loginService.register(param);
+        return Result.success();
+    }
+
+    /**
+     * 登出
+     */
+    @RequestMapping(value = "/logout", method = RequestMethod.POST)
+    public Result logout(@RequestHeader String token) {
+        appService.logout(token);
+        return Result.success();
+    }
+
+    /**
+     * app 获取随机昵称
+     */
+    @PostMapping("/getNickName")
+    public Result getNickName(){
+        return Result.success(appService.getNickName());
+    }
+
+    /**
+     * 检测手机号码是否注册
+     */
+    @PostMapping("/checkUserName")
+    public Result checkUserName(@RequestBody AppLoginParam param){
+        loginService.checkUser(param.getAppUserName(),false);
+        return Result.success();
+    }
+
+    /**
+     * 重置密码
+     */
+    @PostMapping("/resetPassword")
+    public Result resetPassword(@RequestBody RegisterParam param){
+        param.setConfirmPwd(param.getPassword());
+        loginService.changePassword(param);
+        return Result.success();
+    }
 }

+ 1 - 1
src/main/java/com/fdkankan/ucenter/controller/LoginController.java

@@ -60,7 +60,7 @@ public class LoginController {
      */
     @PostMapping("/checkUser")
     public Result checkUser(@RequestBody LoginParam param){
-        loginService.checkUser(param.getPhoneNum());
+        loginService.checkUser(param.getPhoneNum(),true);
         return Result.success();
     }
     /**

+ 1 - 0
src/main/java/com/fdkankan/ucenter/service/ICameraService.java

@@ -36,4 +36,5 @@ public interface ICameraService extends IService<Camera> {
 
     void deleteCooperationUser(Long cameraId);
 
+    Camera getBySnCodeAndPassword(String appUserName, String appPassword);
 }

+ 2 - 0
src/main/java/com/fdkankan/ucenter/service/IUserService.java

@@ -48,4 +48,6 @@ public interface IUserService extends IService<User> {
     void updateUserDetail(UserParam param, String userName);
 
     HashMap<Long, User> getByIds(List<Long> userIds);
+
+    Long getCountByNickName(String nickName);
 }

+ 86 - 8
src/main/java/com/fdkankan/ucenter/service/impl/AppService.java

@@ -1,18 +1,21 @@
 package com.fdkankan.ucenter.service.impl;
 
 import com.alibaba.fastjson.JSONObject;
+import com.fdkankan.common.constant.AppConstant;
+import com.fdkankan.common.constant.CameraConstant;
 import com.fdkankan.common.exception.BusinessException;
 import com.fdkankan.common.util.Base64Converter;
 import com.fdkankan.common.util.JwtUtil;
+import com.fdkankan.common.util.RandomUtil;
 import com.fdkankan.common.util.SecurityUtil;
 import com.fdkankan.redis.util.RedisUtil;
 import com.fdkankan.ucenter.common.RedisKeyUtil;
 import com.fdkankan.ucenter.constant.LoginConstant;
+import com.fdkankan.ucenter.entity.Camera;
+import com.fdkankan.ucenter.entity.CameraDetail;
 import com.fdkankan.ucenter.entity.User;
-import com.fdkankan.ucenter.service.ICameraDetailService;
-import com.fdkankan.ucenter.service.IScenePlusService;
-import com.fdkankan.ucenter.service.ISceneProService;
-import com.fdkankan.ucenter.service.IUserService;
+import com.fdkankan.ucenter.service.*;
+import com.fdkankan.ucenter.vo.request.AppLoginParam;
 import com.fdkankan.ucenter.vo.request.LoginParam;
 import com.fdkankan.ucenter.vo.response.LoginVo;
 import com.fdkankan.ucenter.vo.response.UserVo;
@@ -39,6 +42,11 @@ public class AppService {
     ISceneProService sceneProService;
     @Autowired
     IScenePlusService scenePlusService;
+    @Autowired
+    ICameraService cameraService;
+    @Autowired
+    LoginService loginService;
+
 
     public LoginVo login(LoginParam param) {
         if (StringUtils.isEmpty(param.getPassword()) || StringUtils.isEmpty(param.getPhoneNum())){
@@ -53,8 +61,13 @@ public class AppService {
         if(!user.getPassword().equals(passwordCode)){
             throw new BusinessException(LoginConstant.FAILURE_CODE_3014, LoginConstant.FAILURE_MSG_3014);
         }
-        String token = this.redisLogin(user.getUserName(), JSONObject.toJSONString(user));
+        String token = this.redisLogin(user.getUserName(), 21800L);
+
+        return commonLogin(user,param,token);
+    }
 
+
+    private LoginVo commonLogin(User user,LoginParam param,String token){
         Long count = cameraDetailService.getCountByUserId(user.getId());
         if(param.getCameraType() == null){
             param.setCameraType(  4);
@@ -77,11 +90,76 @@ public class AppService {
         return vo;
     }
 
-
-    public String redisLogin(String userName,String value){
+    public String redisLogin(String userName,Long time){
         String token = JwtUtil.createJWT(-1,userName,"app");
         String redisKey = RedisKeyUtil.PREFIX_CACHE_CAMERA+ userName;
-        redisUtil.set(redisKey, token,21600);
+        redisUtil.set(redisKey, token,time);
         return token;
     }
+
+    public void appLogin(AppLoginParam param) {
+        if(StringUtils.isEmpty(param.getAppUserName()) || StringUtils.isEmpty(param.getAppPassword())
+                || StringUtils.isEmpty(param.getUuid())){
+            throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
+        }
+        commonCheckCamera(param);
+        redisUtil.set(param.getUuid(),param.getAppUserName(),60 * 5);
+    }
+
+    private void commonCheckCamera(AppLoginParam param){
+        Camera camera = cameraService.getBySnCodeAndPassword(param.getAppUserName(),param.getAppPassword());
+        if(camera == null){
+            throw new BusinessException(CameraConstant.FAILURE_6003);
+        }
+        CameraDetail detail = cameraDetailService.getByCameraId(camera.getId());
+        if (detail == null ){
+            throw new BusinessException(AppConstant.FAILURE_CODE_4012, AppConstant.FAILURE_MSG_4012);
+        }
+    }
+
+    public JSONObject login2(AppLoginParam param) {
+        if(StringUtils.isEmpty(param.getAppUserName()) || StringUtils.isEmpty(param.getAppPassword())){
+            throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
+        }
+        commonCheckCamera(param);
+        String token = redisLogin(param.getAppUserName(), 604800L);
+        JSONObject obj = new JSONObject();
+        obj.put("token", token);
+        return obj;
+    }
+
+    public LoginVo quickLogin(LoginParam param) {
+        if(StringUtils.isEmpty(param.getPhoneNum()) || StringUtils.isEmpty(param.getMsgAuthCode())){
+            throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
+        }
+        loginService.checkSms(param.getMsgAuthCode(),param.getPhoneNum(),true);
+
+        User user = userService.getByUserName(param.getPhoneNum());
+        if(user == null){
+            throw new BusinessException(LoginConstant.FAILURE_CODE_3015, LoginConstant.FAILURE_MSG_3015);
+        }
+        String token = redisLogin(param.getPhoneNum(), 21600L);
+        return commonLogin(user,param,token);
+    }
+
+    public void logout(String token) {
+        String username = JwtUtil.getUsername(token);
+        String redisKey = RedisKeyUtil.PREFIX_CACHE_CAMERA+ username;
+        if(redisUtil.hasKey(redisKey)){
+            redisUtil.del(redisKey);
+        }
+    }
+
+    public JSONObject getNickName() {
+        String nickName = null;
+        Long count = 0L;
+        do {
+            nickName = "mob" + RandomUtil.generateShortUuid();
+            count = userService.getCountByNickName(nickName);
+        } while (count > 0);
+        JSONObject jsonObject = new JSONObject();
+        jsonObject.put("nickName",nickName);
+        return jsonObject;
+    }
+
 }

+ 12 - 0
src/main/java/com/fdkankan/ucenter/service/impl/CameraServiceImpl.java

@@ -283,4 +283,16 @@ public class CameraServiceImpl extends ServiceImpl<ICameraMapper, Camera> implem
         fdkkLaserService.disableCooperation(detailMap, cameraMap);
 
     }
+
+    @Override
+    public Camera getBySnCodeAndPassword(String appUserName, String appPassword) {
+        LambdaQueryWrapper<Camera> wrapper = new LambdaQueryWrapper<>();
+        wrapper.eq(Camera::getChildName,appUserName)
+                .eq(Camera::getChildPassword,appPassword);
+        List<Camera> list = this.list(wrapper);
+        if(list != null && list.size() >0){
+            return list.get(0);
+        }
+        return null;
+    }
 }

+ 12 - 5
src/main/java/com/fdkankan/ucenter/service/impl/LoginService.java

@@ -72,16 +72,21 @@ public class LoginService {
 
     public void logout(String token) {
         String redisKey = String.format(RedisKey.TOKEN_V3,token);
-        redisUtil.del(redisKey);
+        if(redisUtil.hasKey(redisKey)){
+            redisUtil.del(redisKey);
+        }
     }
 
-    public void checkUser(String phoneNum) {
+    public void checkUser(String phoneNum,Boolean flg) {
         if(StringUtils.isNotBlank(phoneNum)){
             throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
         }
         User user = userService.getByUserName(phoneNum);
-        if(user == null){
-            throw new BusinessException(LoginConstant.FAILURE_CODE_3014, LoginConstant.FAILURE_MSG_3014);
+        if(user == null && flg){
+            throw new BusinessException(LoginConstant.FAILURE_CODE_3015, LoginConstant.FAILURE_MSG_3015);
+        }
+        if(user != null && !flg){
+            throw new BusinessException(LoginConstant.FAILURE_CODE_3008, LoginConstant.FAILURE_MSG_3008);
         }
     }
 
@@ -155,8 +160,10 @@ public class LoginService {
         if (StringUtils.isEmpty(uuid)){
             throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
         }
+        //二维码失效,清除本地文件二维码
         if(!redisUtil.hasKey(uuid)){
-            throw new BusinessException(LoginConstant.FAILURE_CODE_3004, LoginConstant.FAILURE_MSG_3004);
+            FileUtil.delFile(ConstantFilePath.LOGIN_QR_CODE_PATH + uuid + ".png");
+            throw new BusinessException(LoginConstant.FAILURE_CODE_3035, LoginConstant.FAILURE_MSG_3035);
         }
         String childName = redisUtil.get(uuid);
         Camera camera = cameraService.getBySnCode(childName);

+ 7 - 0
src/main/java/com/fdkankan/ucenter/service/impl/UserServiceImpl.java

@@ -242,4 +242,11 @@ public class UserServiceImpl extends ServiceImpl<IUserMapper, User> implements I
         list.forEach(entity -> map.put(entity.getId(),entity));
         return map;
     }
+
+    @Override
+    public Long getCountByNickName(String nickName) {
+        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
+        wrapper.eq(User::getNickName,nickName);
+        return this.count(wrapper);
+    }
 }

+ 45 - 0
src/main/java/com/fdkankan/ucenter/task/TaskService.java

@@ -0,0 +1,45 @@
+package com.fdkankan.ucenter.task;
+
+import com.fdkankan.common.constant.ConstantFilePath;
+import com.fdkankan.common.util.DateUtil;
+import com.fdkankan.common.util.FileUtil;
+import com.fdkankan.redis.util.RedisUtil;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+
+import java.io.File;
+import java.util.Date;
+
+@Component
+@Slf4j
+public class TaskService {
+
+    @Autowired
+    RedisUtil redisUtil;
+
+    @Scheduled(cron = "0 */10 * * * ?")
+    public void delQrCode(){
+        long startTime = new Date().getTime();
+        log.info("delQrCode:开始定时清理本地登录二维码" );
+        String qrCodePath = ConstantFilePath.LOGIN_QR_CODE_PATH;
+        File file = new File(qrCodePath);
+        File[] files = file.listFiles();
+        if(files  == null || files.length<=0){
+            return;
+        }
+        for (File f : files) {
+            if(f==null || !f.getName().contains(".")){
+                continue;
+            }
+            String name = f.getName().substring(0,f.getName().lastIndexOf("."));
+            if(!redisUtil.hasKey(name)){
+                log.info("删除文件:{}",f.getPath());
+                FileUtil.delFile(f.getPath());
+            }
+        }
+        log.info("delQrCode:结束定时清理本地登录二维码:耗时{}秒",(new Date().getTime() - startTime)/1000 );
+    }
+
+}

+ 10 - 0
src/main/java/com/fdkankan/ucenter/vo/request/AppLoginParam.java

@@ -0,0 +1,10 @@
+package com.fdkankan.ucenter.vo.request;
+
+import lombok.Data;
+
+@Data
+public class AppLoginParam {
+    private String appUserName;
+    private String appPassword;
+    private String uuid;
+}

+ 1 - 0
src/main/java/com/fdkankan/ucenter/vo/request/LoginParam.java

@@ -8,4 +8,5 @@ public class LoginParam {
     private String phoneNum;        //用户名
     private String password;        //密码
     private Integer cameraType;
+    private String msgAuthCode;
 }