登入接口实现
This commit is contained in:
parent
374c549686
commit
4d23b57af2
@ -41,8 +41,8 @@ public class AuthController {
|
|||||||
*/
|
*/
|
||||||
@PostMapping("/login")
|
@PostMapping("/login")
|
||||||
@ApiOperation("用户登入")
|
@ApiOperation("用户登入")
|
||||||
public void login(@RequestBody LoginDTO loginDTO) {
|
public ResponseEntity<Object> login(@RequestBody LoginDTO loginDTO) {
|
||||||
// authService.login(loginDTO.getUsername(), loginDTO.getPassword());
|
return authService.login(loginDTO);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -12,17 +12,26 @@ import javax.validation.constraints.NotBlank;
|
|||||||
public class LoginDTO {
|
public class LoginDTO {
|
||||||
|
|
||||||
@NotBlank(message = "用户名不能为空")
|
@NotBlank(message = "用户名不能为空")
|
||||||
@ApiModelProperty(value = "用户名", required = true)
|
@ApiModelProperty(value = "用户名")
|
||||||
private String username;
|
private String username;
|
||||||
|
|
||||||
@Email(message = "请输入有效的邮箱")
|
@Email(message = "请输入有效的邮箱")
|
||||||
@ApiModelProperty(value = "邮箱")
|
@ApiModelProperty(value = "邮箱")
|
||||||
private String email;
|
private String email;
|
||||||
|
|
||||||
|
@ApiModelProperty(value = "电话号码")
|
||||||
|
private String phoneNumber;
|
||||||
|
|
||||||
|
@ApiModelProperty(value = "邮箱或者电话号码", required = true)
|
||||||
|
private String emailOrPhoneNumber;
|
||||||
|
|
||||||
@NotBlank(message = "密码不能为空")
|
@NotBlank(message = "密码不能为空")
|
||||||
@ApiModelProperty(value = "密码", required = true)
|
@ApiModelProperty(value = "密码")
|
||||||
private String password;
|
private String password;
|
||||||
|
|
||||||
|
@ApiModelProperty(value = "验证码")
|
||||||
|
private String code;
|
||||||
|
|
||||||
private String token;
|
private String token;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,9 @@ public class RegisterDTO {
|
|||||||
@ApiModelProperty(value = "邮箱", required = true)
|
@ApiModelProperty(value = "邮箱", required = true)
|
||||||
private String email;
|
private String email;
|
||||||
|
|
||||||
|
@ApiModelProperty(value = "电话号码")
|
||||||
|
private String phoneNumber;
|
||||||
|
|
||||||
@NotBlank(message = "密码不能为空")
|
@NotBlank(message = "密码不能为空")
|
||||||
@ApiModelProperty(value = "密码", required = true)
|
@ApiModelProperty(value = "密码", required = true)
|
||||||
private String password;
|
private String password;
|
||||||
|
@ -17,6 +17,8 @@ public class Users implements Serializable {
|
|||||||
private String password;
|
private String password;
|
||||||
/** 邮箱*/
|
/** 邮箱*/
|
||||||
private String email;
|
private String email;
|
||||||
|
/** 手机号*/
|
||||||
|
private String phoneNumber;
|
||||||
/** 创建用户信息时间 */
|
/** 创建用户信息时间 */
|
||||||
private Date createdTime;
|
private Date createdTime;
|
||||||
/** 修改用户信息时间 */
|
/** 修改用户信息时间 */
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.yunan.service;
|
package com.yunan.service;
|
||||||
|
|
||||||
|
|
||||||
|
import com.yunan.dto.LoginDTO;
|
||||||
import com.yunan.dto.RegisterDTO;
|
import com.yunan.dto.RegisterDTO;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
|
|
||||||
@ -11,4 +12,6 @@ import java.io.UnsupportedEncodingException;
|
|||||||
public interface AuthService {
|
public interface AuthService {
|
||||||
|
|
||||||
ResponseEntity<Object> register(RegisterDTO registerDTO) throws UnsupportedEncodingException, AddressException, MessagingException;
|
ResponseEntity<Object> register(RegisterDTO registerDTO) throws UnsupportedEncodingException, AddressException, MessagingException;
|
||||||
|
|
||||||
|
ResponseEntity<Object> login(LoginDTO loginDTO);
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,14 @@
|
|||||||
package com.yunan.service.impl;
|
package com.yunan.service.impl;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
|
import com.yunan.dto.LoginDTO;
|
||||||
import com.yunan.dto.RegisterDTO;
|
import com.yunan.dto.RegisterDTO;
|
||||||
import com.yunan.dto.Users;
|
import com.yunan.dto.Users;
|
||||||
import com.yunan.mapper.AuthMapper;
|
import com.yunan.mapper.AuthMapper;
|
||||||
import com.yunan.response.ErrorResponse;
|
import com.yunan.response.ErrorResponse;
|
||||||
import com.yunan.service.AuthService;
|
import com.yunan.service.AuthService;
|
||||||
import com.yunan.util.RedisUtils;
|
import com.yunan.util.RedisUtils;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
@ -18,6 +20,7 @@ import java.util.Date;
|
|||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
public class AuthServiceImpl implements AuthService {
|
public class AuthServiceImpl implements AuthService {
|
||||||
@Resource
|
@Resource
|
||||||
@ -25,6 +28,19 @@ public class AuthServiceImpl implements AuthService {
|
|||||||
@Autowired
|
@Autowired
|
||||||
RedisUtils redisUtils;
|
RedisUtils redisUtils;
|
||||||
|
|
||||||
|
// 手机号正则表达式
|
||||||
|
private static final String PHONE_REGEX = "^1[3-9]\\d{9}$";
|
||||||
|
// 邮箱正则表达式
|
||||||
|
private static final String EMAIL_REGEX =
|
||||||
|
"^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$";
|
||||||
|
|
||||||
|
public static boolean isValidPhone(String phone) {
|
||||||
|
return phone != null && phone.matches(PHONE_REGEX);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isValidEmail(String input) {
|
||||||
|
return input != null && input.matches(EMAIL_REGEX);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ResponseEntity<Object> register(RegisterDTO registerDTO) {
|
public ResponseEntity<Object> register(RegisterDTO registerDTO) {
|
||||||
@ -52,6 +68,11 @@ public class AuthServiceImpl implements AuthService {
|
|||||||
//状态设置 " 1 " 正常账号
|
//状态设置 " 1 " 正常账号
|
||||||
user.setState(1);
|
user.setState(1);
|
||||||
boolean isSaved = authMapper.insert(user) > 0;
|
boolean isSaved = authMapper.insert(user) > 0;
|
||||||
|
//测试
|
||||||
|
// boolean isSaved = false;
|
||||||
|
// boolean isSaved = true;
|
||||||
|
|
||||||
|
|
||||||
if (isSaved) {
|
if (isSaved) {
|
||||||
return ResponseEntity
|
return ResponseEntity
|
||||||
.status(HttpStatus.OK)
|
.status(HttpStatus.OK)
|
||||||
@ -69,6 +90,67 @@ public class AuthServiceImpl implements AuthService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ResponseEntity<Object> login(LoginDTO loginDTO) {
|
||||||
|
log.info("emai: {}", isValidEmail(loginDTO.getEmailOrPhoneNumber()));
|
||||||
|
log.info("phone: {}", isValidPhone(loginDTO.getEmailOrPhoneNumber()));
|
||||||
|
if(isValidPhone(loginDTO.getEmailOrPhoneNumber())) {
|
||||||
|
loginDTO.setPhoneNumber(loginDTO.getEmailOrPhoneNumber());
|
||||||
|
}else if(isValidEmail(loginDTO.getEmailOrPhoneNumber())) {
|
||||||
|
loginDTO.setEmail(loginDTO.getEmailOrPhoneNumber());
|
||||||
|
}else{
|
||||||
|
ResponseEntity
|
||||||
|
.status(HttpStatus.BAD_REQUEST)
|
||||||
|
.body("账号校验失败");
|
||||||
|
}
|
||||||
|
if(loginDTO.getEmail() != null){
|
||||||
|
LambdaQueryWrapper<Users> queryWrapper = new LambdaQueryWrapper<>();
|
||||||
|
queryWrapper.eq(Users::getEmail, loginDTO.getEmail());
|
||||||
|
Users users = authMapper.selectOne(queryWrapper);
|
||||||
|
if(users == null){
|
||||||
|
return ResponseEntity
|
||||||
|
.status(HttpStatus.BAD_REQUEST)
|
||||||
|
.body(new ErrorResponse("邮箱用户不存在!!!"));
|
||||||
|
}
|
||||||
|
String rightPassword = users.getPassword();
|
||||||
|
if(rightPassword.equals(loginDTO.getPassword())){
|
||||||
|
return ResponseEntity
|
||||||
|
.status(HttpStatus.OK)
|
||||||
|
.body("登入成功!!!");
|
||||||
|
}else {
|
||||||
|
return ResponseEntity
|
||||||
|
.status(HttpStatus.BAD_REQUEST)
|
||||||
|
.body("邮箱或密码错误!!!");
|
||||||
|
}
|
||||||
|
|
||||||
|
}else if(loginDTO.getPhoneNumber() != null){
|
||||||
|
LambdaQueryWrapper<Users> queryWrapper = new LambdaQueryWrapper<>();
|
||||||
|
queryWrapper.eq(Users::getPhoneNumber, loginDTO.getPhoneNumber());
|
||||||
|
Users users = authMapper.selectOne(queryWrapper);
|
||||||
|
if(users == null){
|
||||||
|
return ResponseEntity
|
||||||
|
.status(HttpStatus.BAD_REQUEST)
|
||||||
|
.body(new ErrorResponse("手机号用户不存在!!"));
|
||||||
|
}
|
||||||
|
String rightPassword = users.getPassword();
|
||||||
|
if(rightPassword.equals(loginDTO.getPassword())){
|
||||||
|
return ResponseEntity
|
||||||
|
.status(HttpStatus.OK)
|
||||||
|
.body("登入成功!!!");
|
||||||
|
}else {
|
||||||
|
return ResponseEntity
|
||||||
|
.status(HttpStatus.BAD_REQUEST)
|
||||||
|
.body("手机号或密码错误!!!");
|
||||||
|
}
|
||||||
|
|
||||||
|
}else{
|
||||||
|
return ResponseEntity
|
||||||
|
.status(HttpStatus.BAD_REQUEST)
|
||||||
|
.body(new ErrorResponse("其他错误待管理员处理!!!"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//随机生成用户名算法
|
//随机生成用户名算法
|
||||||
public String createUserName() {
|
public String createUserName() {
|
||||||
// 字符集,包括小写字母和数字
|
// 字符集,包括小写字母和数字
|
||||||
|
Loading…
Reference in New Issue
Block a user