Эх сурвалжийг харах

fix 手机号登录验证

zoujiajian 3 жил өмнө
parent
commit
9815d89758

+ 82 - 14
netflix-web/src/main/java/com/cyksj/web/controller/manage/AdminController.java

@@ -1,7 +1,10 @@
 package com.cyksj.web.controller.manage;
 
 import cn.dev33.satoken.stp.StpUtil;
+import cn.hutool.core.date.DateTime;
 import cn.hutool.core.date.DateUtil;
+import cn.hutool.core.lang.Validator;
+import cn.hutool.core.util.StrUtil;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
@@ -12,13 +15,15 @@ import com.cyksj.common.util.Jsons;
 import com.cyksj.dto.Result;
 import com.cyksj.enums.GatewayResponse;
 import com.cyksj.mapper.CustomerServiceMapper;
+import com.cyksj.mapper.PhoneCodeMapper;
 import com.cyksj.model.dto.WxMpTemplateData;
 import com.cyksj.model.dto.WxMpTemplateMessage;
-import com.cyksj.model.entity.CustomerService;
+import com.cyksj.model.entity.PhoneCode;
 import com.cyksj.model.manage.dto.AdminInfo;
 import com.cyksj.model.manage.entity.Admin;
 import com.cyksj.service.mange.AdminService;
 import com.cyksj.service.wechat.WeChatService;
+import com.cyksj.web.util.StpUserUtil;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
@@ -52,10 +57,12 @@ public class AdminController {
 
     private final CustomerServiceMapper customerServiceMapper;
 
+    private final PhoneCodeMapper phoneCodeMapper;
+
     private static final List<String> SEND_LIST = List.of("o_i5g6V5z5uDpx_SBhJ4ePek8g8k","o_i5g6X99wu2OmC4DcQKXKNmJexA");
 
     @PostMapping(value = "/login")
-    public Result<AdminInfo> login(@RequestBody Admin adminRequest) throws Exception {
+    public Result<Long> login(@RequestBody Admin adminRequest) throws Exception {
         Admin admin = adminService.getOne(
                 new QueryWrapper<Admin>().lambda().eq(Admin::getUserName,adminRequest.getUserName()));
 
@@ -73,18 +80,7 @@ public class AdminController {
             throw BusinessRuntimeException.getInstance("密码有误.");
         }
 
-        // 生成token
-
-        StpUtil.login(admin.getId());
-
-        String token = StpUtil.getTokenValue();
-
-        admin.setPassWord("");
-
-        AdminInfo info = new AdminInfo();
-        info.setInfo(admin);
-        info.setToken(token);
-        return GatewayResponse.SUCCESS.newBuilder().toResult(info);
+        return GatewayResponse.SUCCESS.newBuilder().toResult(admin.getId());
     }
 
     @PostMapping("/post")
@@ -104,6 +100,78 @@ public class AdminController {
         return GatewayResponse.SUCCESS.newBuilder().toResult();
     }
 
+    /**
+     * 编辑管理员
+     */
+    @PutMapping("/put")
+    public Result<String> editAdmin(@RequestBody Admin admin) {
+        Long adminId = StpUserUtil.getLoginIdAsLong();
+        Admin exist = adminService.getById(adminId);
+        if (admin == null) {
+            throw BusinessRuntimeException.getInstance("账号不存在,请重新登录");
+        }
+        if (exist.getCategory() != Admin.Category.superoot) {
+            throw BusinessRuntimeException.getInstance("无超级管理员权限");
+        }
+        if (StrUtil.isNotEmpty(admin.getPhone())) {
+            boolean mobile = Validator.isMobile(admin.getPhone());
+            if (!mobile) {
+                throw BusinessRuntimeException.getInstance("请输入正确的手机号");
+            }
+        }
+        if (admin.getId() == null || adminService.getById(admin.getId()) == null) {
+            throw BusinessRuntimeException.getInstance("账号不存在");
+        }
+        adminService.saveOrUpdate(admin);
+        StpUserUtil.logoutByLoginId(admin.getId());
+
+        return GatewayResponse.SUCCESS.newBuilder().toResult("编辑成功");
+    }
+
+
+    /**
+     * 校验手机号获取登录信息
+     */
+    @GetMapping("/get/loginInfo")
+    public Result<AdminInfo> getLoginInfo(String phone, String code, Long adminId) {
+        Admin admin = adminService.getById(adminId);
+        if (admin == null) {
+            throw BusinessRuntimeException.getInstance("账号不存在");
+        }
+        if (StrUtil.hasEmpty(phone, code)) {
+            throw BusinessRuntimeException.getInstance("请输入对应信息");
+        }
+        if (!phone.equals(admin.getPhone())) {
+            throw BusinessRuntimeException.getInstance("输入手机号与账号配置登录手机号不一致");
+        }
+        DateTime now = DateTime.now();
+        PhoneCode phoneCode = phoneCodeMapper.selectOne(Wrappers.lambdaQuery(PhoneCode.class).eq(PhoneCode::getPhone, phone).last("limit 1"));
+        if (phoneCode == null || StrUtil.isEmpty(phoneCode.getCode())) {
+            throw BusinessRuntimeException.getInstance("请重新获取手机验证码");
+        }
+        //3分钟失效
+        Date updateTime = phoneCode.getUpdateTime();
+        DateTime afterFiveMinutes = DateUtil.offsetMinute(updateTime, 3);
+        if (now.isAfter(afterFiveMinutes)) {
+            throw BusinessRuntimeException.getInstance("手机验证码已失效,请重新获取");
+        }
+        if (!StrUtil.equals(code, phoneCode.getCode())) {
+            throw BusinessRuntimeException.getInstance("手机验证码错误,请重新输入");
+        }
+        // 生成token
+
+        StpUtil.login(admin.getId());
+
+        String token = StpUtil.getTokenValue();
+
+        admin.setPassWord("");
+
+        AdminInfo info = new AdminInfo();
+        info.setInfo(admin);
+        info.setToken(token);
+        return GatewayResponse.SUCCESS.newBuilder().toResult(info);
+    }
+
     @GetMapping("/get/list")
     public Result<List<Admin>> list(){
         List<Admin> list = adminService.list(new LambdaQueryWrapper<Admin>().eq(Admin::getStatus,true));