Browse Source

pc端登录

master
laishajiang 2 years ago
parent
commit
19914b8126
  1. 6
      mailbox-outer/pom.xml
  2. 4
      mailbox-outer/src/main/java/com/biutag/outer/config/InterceptorConfig.java
  3. 29
      mailbox-outer/src/main/java/com/biutag/outer/controller/AuthController.java
  4. 14
      mailbox-outer/src/main/java/com/biutag/outer/controller/SmsController.java
  5. 13
      mailbox-outer/src/main/java/com/biutag/outer/service/UserService.java
  6. 32
      mailbox-outer/src/main/java/com/biutag/outer/validate/UserLoginsValidate.java

6
mailbox-outer/pom.xml

@ -78,6 +78,12 @@
<artifactId>alibabacloud-dysmsapi20170525</artifactId>
<version>2.0.24</version>
</dependency>
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-core</artifactId>
<version>1.37.0</version>
<scope>compile</scope>
</dependency>
</dependencies>

4
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 {

29
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<JSONObject> 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)));
}
}

14
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<JSONObject> 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)));
}
}

13
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<UserMapper, User> {
return updated;
}
public User getUser(UserLoginsValidate loginsValidate) {
User user = baseMapper.selectOne(new QueryWrapper<User>()
.eq("phone", loginsValidate.getPhone())
.last("limit 1"));
return user;
}
public User getByPhone(String phone) {
return getOne(new LambdaQueryWrapper<User>().eq(User::getPhone, phone));
}
}

32
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;
}
Loading…
Cancel
Save