From 19914b812675255e35bf7060189418390129d4fb Mon Sep 17 00:00:00 2001 From: laishajiang <12977614+laishajiang@user.noreply.gitee.com> Date: Thu, 18 Apr 2024 09:59:49 +0800 Subject: [PATCH] =?UTF-8?q?pc=E7=AB=AF=E7=99=BB=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mailbox-outer/pom.xml | 6 ++++ .../outer/config/InterceptorConfig.java | 4 ++- .../outer/controller/AuthController.java | 29 ++++++++++++++--- .../outer/controller/SmsController.java | 14 +++++++- .../com/biutag/outer/service/UserService.java | 13 +++++++- .../outer/validate/UserLoginsValidate.java | 32 +++++++++++++++++++ 6 files changed, 91 insertions(+), 7 deletions(-) create mode 100644 mailbox-outer/src/main/java/com/biutag/outer/validate/UserLoginsValidate.java diff --git a/mailbox-outer/pom.xml b/mailbox-outer/pom.xml index 1d1b855..403608e 100644 --- a/mailbox-outer/pom.xml +++ b/mailbox-outer/pom.xml @@ -78,6 +78,12 @@ alibabacloud-dysmsapi20170525 2.0.24 + + cn.dev33 + sa-token-core + 1.37.0 + compile + diff --git a/mailbox-outer/src/main/java/com/biutag/outer/config/InterceptorConfig.java b/mailbox-outer/src/main/java/com/biutag/outer/config/InterceptorConfig.java index 8a5a29b..31114c5 100644 --- a/mailbox-outer/src/main/java/com/biutag/outer/config/InterceptorConfig.java +++ b/mailbox-outer/src/main/java/com/biutag/outer/config/InterceptorConfig.java @@ -25,7 +25,9 @@ public class InterceptorConfig implements WebMvcConfigurer { "/auth/openid", "/auth/wx/faceAuth", "/file/stream/**", - "/api/**"); + "/api/**", + "/sms/sendLogin", + "/auth/login"); } static class AuthInterceptor implements HandlerInterceptor { diff --git a/mailbox-outer/src/main/java/com/biutag/outer/controller/AuthController.java b/mailbox-outer/src/main/java/com/biutag/outer/controller/AuthController.java index c3ab15d..d2683ce 100644 --- a/mailbox-outer/src/main/java/com/biutag/outer/controller/AuthController.java +++ b/mailbox-outer/src/main/java/com/biutag/outer/controller/AuthController.java @@ -4,18 +4,19 @@ import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONObject; +import com.biutag.aop.NotLogin; import com.biutag.core.AjaxResult; import com.biutag.enums.ErrorEnum; import com.biutag.exception.AuthException; +import com.biutag.exception.BusinessException; import com.biutag.outer.domain.User; import com.biutag.outer.domain.bo.AuthUser; import com.biutag.outer.domain.bo.FaceAuthBo; import com.biutag.outer.service.FaceAuthService; import com.biutag.outer.service.UserService; -import com.biutag.outer.util.TokenUtil; -import com.biutag.outer.util.UserHelper; -import com.biutag.outer.util.Weixin; -import com.biutag.outer.util.Weixin2; +import com.biutag.outer.util.*; +import com.biutag.outer.validate.UserLoginsValidate; +import io.swagger.annotations.ApiOperation; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.validation.annotation.Validated; @@ -97,4 +98,24 @@ public class AuthController { return AjaxResult.success(faceAuthService.save(faceAuth)); } + + + @NotLogin + @PostMapping("/login") + @ApiOperation(value="登录系统") + public AjaxResult login(@Validated() @RequestBody UserLoginsValidate loginsValidate) { + if (!loginsValidate.getCode().equals(CodeUtil.get(loginsValidate.getSmsRequestId()))) { + throw new BusinessException("验证码错误"); + } + User user = userService.getByPhone(loginsValidate.getPhone()); + if (user == null) { + user = new User(); + user.setPhone(loginsValidate.getPhone()); + user.setCreateTime(LocalDateTime.now()); + userService.save(user); + } + AuthUser authUser = AuthUser.of(user); + return AjaxResult.success(JSONObject.of("token", TokenUtil.set(authUser))); + } + } diff --git a/mailbox-outer/src/main/java/com/biutag/outer/controller/SmsController.java b/mailbox-outer/src/main/java/com/biutag/outer/controller/SmsController.java index 4b8819c..416d466 100644 --- a/mailbox-outer/src/main/java/com/biutag/outer/controller/SmsController.java +++ b/mailbox-outer/src/main/java/com/biutag/outer/controller/SmsController.java @@ -1,6 +1,7 @@ package com.biutag.outer.controller; import com.alibaba.fastjson2.JSONObject; +import com.biutag.aop.NotLogin; import com.biutag.core.AjaxResult; import com.biutag.outer.util.CodeUtil; import com.biutag.outer.util.Sms; @@ -34,5 +35,16 @@ public class SmsController { // 设置缓存 return AjaxResult.success(JSONObject.of("requestId", CodeUtil.set(code))); } - + @NotLogin + @PostMapping("sendLogin") + public AjaxResult sendLogin(@RequestParam String phone) { + String code = String.valueOf(1000 + random.nextInt(9000)); + if (active.equals("prod")) { + SmsSend.sendCode(phone,code); + } else { + Sms.send(phone, code); + } + // 设置缓存 + return AjaxResult.success(JSONObject.of("requestId", CodeUtil.set(code))); + } } diff --git a/mailbox-outer/src/main/java/com/biutag/outer/service/UserService.java b/mailbox-outer/src/main/java/com/biutag/outer/service/UserService.java index 27bee85..3ad141b 100644 --- a/mailbox-outer/src/main/java/com/biutag/outer/service/UserService.java +++ b/mailbox-outer/src/main/java/com/biutag/outer/service/UserService.java @@ -1,13 +1,14 @@ package com.biutag.outer.service; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; -import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.biutag.outer.domain.User; import com.biutag.outer.domain.bo.AuthUser; import com.biutag.outer.mapper.UserMapper; import com.biutag.outer.util.UserHelper; +import com.biutag.outer.validate.UserLoginsValidate; import org.springframework.stereotype.Service; @Service @@ -28,4 +29,14 @@ public class UserService extends ServiceImpl { return updated; } + public User getUser(UserLoginsValidate loginsValidate) { + User user = baseMapper.selectOne(new QueryWrapper() + .eq("phone", loginsValidate.getPhone()) + .last("limit 1")); + return user; + } + + public User getByPhone(String phone) { + return getOne(new LambdaQueryWrapper().eq(User::getPhone, phone)); + } } diff --git a/mailbox-outer/src/main/java/com/biutag/outer/validate/UserLoginsValidate.java b/mailbox-outer/src/main/java/com/biutag/outer/validate/UserLoginsValidate.java new file mode 100644 index 0000000..c0a71a4 --- /dev/null +++ b/mailbox-outer/src/main/java/com/biutag/outer/validate/UserLoginsValidate.java @@ -0,0 +1,32 @@ +package com.biutag.outer.validate; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotEmpty; +import lombok.Data; +import org.hibernate.validator.constraints.Length; + +import java.io.Serializable; + +@Data +@ApiModel("系统登录参数") +public class UserLoginsValidate implements Serializable { + + private static final long serialVersionUID = 1L; + + @NotEmpty(message = "账号不能为空") + @Length(min = 11, max = 11, message = "账号或验证码错误") + @ApiModelProperty(value = "登录账号", required = true) + private String phone; + + @NotBlank(message = "短信验证码不能为空") + @Length(min = 4, max = 4, message = "短信验证码错误") + private String code; + + @NotBlank(message = "短信验证码错误或已失效") + private String smsRequestId; + + + +}