feat:用户注册
This commit is contained in:
@@ -0,0 +1,54 @@
|
|||||||
|
package com.xiang.xservice.auth.api.dto.req;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import javax.validation.constraints.Email;
|
||||||
|
import javax.validation.constraints.NotBlank;
|
||||||
|
import javax.validation.constraints.Size;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class RegisterRequest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户名(昵称)
|
||||||
|
*/
|
||||||
|
@NotBlank(message = "用户名(昵称)不能为空")
|
||||||
|
@Size(min = 0, max = 20, message = "用户名长度不能超过20")
|
||||||
|
private String name;
|
||||||
|
/**
|
||||||
|
* 用户名
|
||||||
|
*/
|
||||||
|
@NotBlank(message = "用户名不能为空")
|
||||||
|
private String username;
|
||||||
|
/**
|
||||||
|
* 密码
|
||||||
|
*/
|
||||||
|
@NotBlank(message = "密码不能为空")
|
||||||
|
@Size(min = 6, max = 18, message = "密码长度需要在6-18位")
|
||||||
|
private String password;
|
||||||
|
/**
|
||||||
|
* 邮箱
|
||||||
|
*/
|
||||||
|
@Email(message = "邮箱验证不能通过")
|
||||||
|
private String email;
|
||||||
|
/**
|
||||||
|
* 手机号
|
||||||
|
*/
|
||||||
|
@NotBlank(message = "手机号码不能为空")
|
||||||
|
@Size(min = 11, max = 11, message = "手机号码长度验证失败")
|
||||||
|
private String phone;
|
||||||
|
/**
|
||||||
|
* 头像
|
||||||
|
*/
|
||||||
|
private String avatar;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 验证码
|
||||||
|
*/
|
||||||
|
@NotBlank(message = "验证码不能为空")
|
||||||
|
private String code;
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package com.xiang.xservice.auth.api.dto.resp;
|
||||||
|
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class RegisterResp {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户名(昵称)
|
||||||
|
*/
|
||||||
|
private String name;
|
||||||
|
/**
|
||||||
|
* 用户名
|
||||||
|
*/
|
||||||
|
private String username;
|
||||||
|
/**
|
||||||
|
* 邮箱
|
||||||
|
*/
|
||||||
|
private String email;
|
||||||
|
/**
|
||||||
|
* 手机号
|
||||||
|
*/
|
||||||
|
private String phone;
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
package com.xiang.xservice.auth.server.controller;
|
||||||
|
|
||||||
|
import com.xiang.xservice.auth.api.dto.req.RegisterRequest;
|
||||||
|
import com.xiang.xservice.auth.api.dto.resp.RegisterResp;
|
||||||
|
import com.xiang.xservice.auth.service.service.XUserService;
|
||||||
|
import com.xiang.xservice.basic.common.resp.Result;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@RestController
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class UserController {
|
||||||
|
|
||||||
|
private final XUserService userService;
|
||||||
|
|
||||||
|
@PostMapping("/public/user/userRegister")
|
||||||
|
public Result<RegisterResp> register(RegisterRequest request) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
RegisterResp registerResp = userService.userRegister(request);
|
||||||
|
if (Objects.nonNull(registerResp)) {
|
||||||
|
return Result.success("操作成功", registerResp);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("【用户注册】用户注册失败:{}", e.getMessage(), e);
|
||||||
|
}
|
||||||
|
return Result.error("操作失败");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package com.xiang.xservice.auth.service.enums;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@AllArgsConstructor
|
||||||
|
public enum UserStatusEnum {
|
||||||
|
|
||||||
|
DISABLED(0, "禁用"),
|
||||||
|
USING(1, "启用"),
|
||||||
|
;
|
||||||
|
|
||||||
|
private final Integer code;
|
||||||
|
private final String msg;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -9,4 +9,11 @@ import org.springframework.stereotype.Repository;
|
|||||||
public interface XUserMapper {
|
public interface XUserMapper {
|
||||||
|
|
||||||
XUser selectByUsername(String username);
|
XUser selectByUsername(String username);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 新增用户
|
||||||
|
* @param user 用户
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
int insert(XUser user);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,16 @@
|
|||||||
package com.xiang.xservice.auth.service.service;
|
package com.xiang.xservice.auth.service.service;
|
||||||
|
|
||||||
import com.xiang.xservice.auth.api.dto.req.LoginRequest;
|
import com.xiang.xservice.auth.api.dto.req.LoginRequest;
|
||||||
|
import com.xiang.xservice.auth.api.dto.req.RegisterRequest;
|
||||||
import com.xiang.xservice.auth.api.dto.resp.LoginResp;
|
import com.xiang.xservice.auth.api.dto.resp.LoginResp;
|
||||||
|
import com.xiang.xservice.auth.api.dto.resp.RegisterResp;
|
||||||
import com.xiang.xservice.auth.api.dto.resp.UserResp;
|
import com.xiang.xservice.auth.api.dto.resp.UserResp;
|
||||||
import com.xiang.xservice.mysql.annotation.DynamicDataSource;
|
|
||||||
|
|
||||||
public interface XUserService {
|
public interface XUserService {
|
||||||
|
|
||||||
@DynamicDataSource("master")
|
|
||||||
LoginResp login(LoginRequest request);
|
LoginResp login(LoginRequest request);
|
||||||
@DynamicDataSource("master")
|
|
||||||
UserResp getUserByUsername(String username);
|
UserResp getUserByUsername(String username);
|
||||||
|
|
||||||
|
RegisterResp userRegister(RegisterRequest request);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,23 +1,29 @@
|
|||||||
package com.xiang.xservice.auth.service.service.impl;
|
package com.xiang.xservice.auth.service.service.impl;
|
||||||
|
|
||||||
import com.xiang.xservice.auth.api.dto.req.LoginRequest;
|
import com.xiang.xservice.auth.api.dto.req.LoginRequest;
|
||||||
|
import com.xiang.xservice.auth.api.dto.req.RegisterRequest;
|
||||||
import com.xiang.xservice.auth.api.dto.resp.LoginResp;
|
import com.xiang.xservice.auth.api.dto.resp.LoginResp;
|
||||||
|
import com.xiang.xservice.auth.api.dto.resp.RegisterResp;
|
||||||
import com.xiang.xservice.auth.api.dto.resp.UserResp;
|
import com.xiang.xservice.auth.api.dto.resp.UserResp;
|
||||||
import com.xiang.xservice.auth.service.entity.XUser;
|
import com.xiang.xservice.auth.service.entity.XUser;
|
||||||
|
import com.xiang.xservice.auth.service.enums.UserStatusEnum;
|
||||||
import com.xiang.xservice.auth.service.repository.mapper.XUserMapper;
|
import com.xiang.xservice.auth.service.repository.mapper.XUserMapper;
|
||||||
import com.xiang.xservice.auth.service.service.XUserService;
|
import com.xiang.xservice.auth.service.service.XUserService;
|
||||||
|
import com.xiang.xservice.basic.enums.DelStatusEnum;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.security.authentication.AuthenticationManager;
|
import org.springframework.security.authentication.AuthenticationManager;
|
||||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||||
import org.springframework.security.core.Authentication;
|
import org.springframework.security.core.Authentication;
|
||||||
import org.springframework.security.core.context.SecurityContextHolder;
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
import org.springframework.security.oauth2.jwt.JwtClaimsSet;
|
import org.springframework.security.oauth2.jwt.JwtClaimsSet;
|
||||||
import org.springframework.security.oauth2.jwt.JwtEncoder;
|
import org.springframework.security.oauth2.jwt.JwtEncoder;
|
||||||
import org.springframework.security.oauth2.jwt.JwtEncoderParameters;
|
import org.springframework.security.oauth2.jwt.JwtEncoderParameters;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
import java.time.temporal.ChronoUnit;
|
import java.time.temporal.ChronoUnit;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
@@ -29,6 +35,7 @@ public class XUserServiceImpl implements XUserService {
|
|||||||
private final JwtEncoder jwtEncoder;
|
private final JwtEncoder jwtEncoder;
|
||||||
private final XUserMapper userMapper;
|
private final XUserMapper userMapper;
|
||||||
private final AuthenticationManager authenticationManager;
|
private final AuthenticationManager authenticationManager;
|
||||||
|
private final PasswordEncoder passwordEncoder;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public LoginResp login(LoginRequest request) {
|
public LoginResp login(LoginRequest request) {
|
||||||
@@ -81,4 +88,36 @@ public class XUserServiceImpl implements XUserService {
|
|||||||
userResp.setUpdateTime(user.getUpdateTime());
|
userResp.setUpdateTime(user.getUpdateTime());
|
||||||
return userResp;
|
return userResp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public RegisterResp userRegister(RegisterRequest request) {
|
||||||
|
// todo 手机号验证码校验
|
||||||
|
XUser user = userMapper.selectByUsername(request.getUsername());
|
||||||
|
if (Objects.nonNull(user)) {
|
||||||
|
throw new RuntimeException("用户名已存在!");
|
||||||
|
}
|
||||||
|
user = new XUser();
|
||||||
|
user.setName(request.getName());
|
||||||
|
user.setUsername(request.getUsername());
|
||||||
|
user.setPassword(passwordEncoder.encode(request.getPassword()));
|
||||||
|
user.setEmail(request.getEmail());
|
||||||
|
user.setPhone(request.getPhone());
|
||||||
|
user.setAvatar(request.getAvatar());
|
||||||
|
user.setStatus(UserStatusEnum.USING.getCode());
|
||||||
|
user.setDelFlag(DelStatusEnum.NOT_DELETED.getCode());
|
||||||
|
user.setCreateBy("admin");
|
||||||
|
user.setCreateTime(LocalDateTime.now());
|
||||||
|
user.setUpdateBy("admin");
|
||||||
|
user.setUpdateTime(LocalDateTime.now());
|
||||||
|
|
||||||
|
if (userMapper.insert(user) > 0) {
|
||||||
|
RegisterResp registerResp = new RegisterResp();
|
||||||
|
registerResp.setName(registerResp.getName());
|
||||||
|
registerResp.setUsername(registerResp.getUsername());
|
||||||
|
registerResp.setEmail(registerResp.getEmail());
|
||||||
|
registerResp.setPhone(registerResp.getPhone());
|
||||||
|
return registerResp;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user