实现注册模块
从前端接受数据UserRegisterVO,进行数据校验,随后进行数据处理与载入数据库
This commit is contained in:
parent
5206b6ae6c
commit
7711f6c28a
33
src/main/java/com/jsl/oa/common/doData/UserDO.java
Normal file
33
src/main/java/com/jsl/oa/common/doData/UserDO.java
Normal file
|
@ -0,0 +1,33 @@
|
|||
package com.jsl.oa.common.doData;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import java.sql.Date;
|
||||
|
||||
/**
|
||||
* <h1>users 数据表</h1>
|
||||
* <hr/>
|
||||
* 映射 users 数据表内容进入自定义实体类
|
||||
*
|
||||
* @author 筱锋xiao_lfeng
|
||||
* @since v1.0.0
|
||||
* @version v1.0.0
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
@Accessors(chain = true)
|
||||
public class UserDO {
|
||||
private Integer id;
|
||||
private String userNum;
|
||||
private String username;
|
||||
private String password;
|
||||
private String sex;
|
||||
private Date age;
|
||||
private String unit;
|
||||
private String filed;
|
||||
private String hometown;
|
||||
private String kind;
|
||||
private String status;
|
||||
}
|
|
@ -2,8 +2,6 @@ package com.jsl.oa.common.voData;
|
|||
|
||||
import lombok.Getter;
|
||||
|
||||
import javax.validation.constraints.Max;
|
||||
import javax.validation.constraints.Min;
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.Pattern;
|
||||
|
||||
|
@ -12,9 +10,9 @@ import javax.validation.constraints.Pattern;
|
|||
* <hr/>
|
||||
* 用于处理用户注册表单输入的数据
|
||||
*
|
||||
* @since v1.0.0
|
||||
* @version v1.0.0
|
||||
* @author 筱锋xiao_lfeng
|
||||
* @version v1.0.0
|
||||
* @since v1.0.0
|
||||
*/
|
||||
@Getter
|
||||
public class UserRegisterVO {
|
||||
|
@ -29,8 +27,6 @@ public class UserRegisterVO {
|
|||
@NotBlank(message = "性别不能为空")
|
||||
private String sex;
|
||||
|
||||
@Min(value = 0, message = "年龄不能小于0")
|
||||
@Max(value = 150, message = "年龄不能大于150")
|
||||
@NotBlank(message = "年龄不能为空")
|
||||
private String age;
|
||||
|
||||
|
|
|
@ -1,16 +1,39 @@
|
|||
package com.jsl.oa.controllers;
|
||||
|
||||
import com.jsl.oa.common.voData.UserRegisterVO;
|
||||
import com.jsl.oa.services.UserService;
|
||||
import com.jsl.oa.utils.BaseResponse;
|
||||
import com.jsl.oa.utils.ErrorCode;
|
||||
import com.jsl.oa.utils.Processing;
|
||||
import com.jsl.oa.utils.ResultUtil;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class UserController {
|
||||
import java.text.ParseException;
|
||||
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
public class UserController {
|
||||
private final UserService userService;
|
||||
|
||||
/**
|
||||
* <h1>用户注册</h1>
|
||||
* <hr/>
|
||||
* 用户注册接口
|
||||
*
|
||||
* @return {@link BaseResponse}
|
||||
* @author 筱锋xiao_lfeng
|
||||
*/
|
||||
@PostMapping("/user/register")
|
||||
public BaseResponse userRegister() {
|
||||
return ResultUtil.error(ErrorCode.WRONG_PASSWORD);
|
||||
public BaseResponse userRegister(@RequestBody @Validated UserRegisterVO userRegisterVO, BindingResult bindingResult) throws ParseException {
|
||||
// 判断是否有参数错误
|
||||
if (bindingResult.hasErrors()) {
|
||||
return ResultUtil.error(ErrorCode.PARAMETER_ERROR, Processing.getValidatedErrorList(bindingResult));
|
||||
}
|
||||
return userService.userRegister(userRegisterVO);
|
||||
}
|
||||
}
|
||||
|
|
21
src/main/java/com/jsl/oa/mapper/UserMapper.java
Normal file
21
src/main/java/com/jsl/oa/mapper/UserMapper.java
Normal file
|
@ -0,0 +1,21 @@
|
|||
package com.jsl.oa.mapper;
|
||||
|
||||
import com.jsl.oa.common.doData.UserDO;
|
||||
import org.apache.ibatis.annotations.Insert;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
@Mapper
|
||||
public interface UserMapper {
|
||||
|
||||
@Select("SELECT * FROM organize_oa.users WHERE username = #{username}")
|
||||
UserDO getUserByUsername(String username);
|
||||
|
||||
@Select("SELECT * FROM organize_oa.users WHERE user_num = #{userNum}")
|
||||
UserDO getUserByUserNum(String userNum);
|
||||
|
||||
@Insert("INSERT INTO organize_oa.users (user_num, username, password, sex, age, unit, field, hometown, kind, state) " +
|
||||
"VALUES " +
|
||||
"(#{userNum}, #{username}, #{password}, #{sex}, #{age}, #{unit}, #{filed}, #{hometown}, #{kind}, #{status})")
|
||||
Boolean insertUser(UserDO userDO);
|
||||
}
|
|
@ -1,4 +1,10 @@
|
|||
package com.jsl.oa.services;
|
||||
|
||||
import com.jsl.oa.common.voData.UserRegisterVO;
|
||||
import com.jsl.oa.utils.BaseResponse;
|
||||
|
||||
import java.text.ParseException;
|
||||
|
||||
public interface UserService {
|
||||
BaseResponse userRegister(UserRegisterVO userRegisterVO) throws ParseException;
|
||||
}
|
||||
|
|
|
@ -1,7 +1,67 @@
|
|||
package com.jsl.oa.services;
|
||||
|
||||
import com.jsl.oa.common.doData.UserDO;
|
||||
import com.jsl.oa.common.voData.UserRegisterVO;
|
||||
import com.jsl.oa.mapper.UserMapper;
|
||||
import com.jsl.oa.utils.BaseResponse;
|
||||
import com.jsl.oa.utils.ErrorCode;
|
||||
import com.jsl.oa.utils.Processing;
|
||||
import com.jsl.oa.utils.ResultUtil;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.mindrot.jbcrypt.BCrypt;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class UserServiceImpl implements UserService {
|
||||
private final UserMapper userMapper;
|
||||
|
||||
/**
|
||||
* <h1>用户注册</h1>
|
||||
* <hr/>
|
||||
* 用户注册服务类操作
|
||||
*
|
||||
* @param userRegisterVO 用户注册信息
|
||||
* @return {@link BaseResponse}
|
||||
* @throws ParseException 日期转换异常
|
||||
*/
|
||||
@Override
|
||||
public BaseResponse userRegister(UserRegisterVO userRegisterVO) throws ParseException {
|
||||
// 用户检查是否存在
|
||||
UserDO getUserByUsername = userMapper.getUserByUsername(userRegisterVO.getUsername());
|
||||
// 用户名已存在
|
||||
if (getUserByUsername != null) {
|
||||
return ResultUtil.error(ErrorCode.USERNAME_EXIST);
|
||||
}
|
||||
|
||||
// 生成工号
|
||||
String userNum;
|
||||
do {
|
||||
userNum = Processing.createJobNumber((short) 2);
|
||||
} while (userMapper.getUserByUserNum(userNum) != null);
|
||||
|
||||
// 数据上传
|
||||
Date getDate = new Date(new SimpleDateFormat("yyyy-MM-dd").parse(userRegisterVO.getAge()).getTime());
|
||||
UserDO userDO = new UserDO();
|
||||
userDO.setUserNum(userNum)
|
||||
.setUsername(userRegisterVO.getUsername())
|
||||
.setPassword(BCrypt.hashpw(userRegisterVO.getPassword(), BCrypt.gensalt()))
|
||||
.setSex(userRegisterVO.getSex())
|
||||
.setAge(getDate)
|
||||
.setUnit(userRegisterVO.getUnit())
|
||||
.setFiled(userRegisterVO.getFiled())
|
||||
.setHometown(userRegisterVO.getHometown())
|
||||
.setKind("注册用户")
|
||||
.setStatus("注册状态");
|
||||
// 插入数据
|
||||
if (userMapper.insertUser(userDO)) {
|
||||
return ResultUtil.success("注册成功");
|
||||
} else {
|
||||
return ResultUtil.error(ErrorCode.DATABASE_INSERT_ERROR);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,11 +4,17 @@ import lombok.Getter;
|
|||
|
||||
@Getter
|
||||
public enum ErrorCode {
|
||||
WRONG_PASSWORD("WrongPassword", 40010, "密码错误");
|
||||
WRONG_PASSWORD("WrongPassword", 40010, "密码错误"),
|
||||
PARAMETER_ERROR("ParameterError", 40011, "参数错误"),
|
||||
USERNAME_EXIST("UsernameExist", 40012, "用户名已存在"),
|
||||
DATABASE_INSERT_ERROR("DatabaseInsertError", 50010, "数据库插入错误"),
|
||||
DATABASE_UPDATE_ERROR("DatabaseUpdateError", 50011, "数据库更新错误"),
|
||||
DATABASE_DELETE_ERROR("DatabaseDeleteError", 50012, "数据库删除错误");
|
||||
|
||||
private final String output;
|
||||
private final Integer code;
|
||||
private final String message;
|
||||
|
||||
ErrorCode(String output, Integer code, String message) {
|
||||
this.output = output;
|
||||
this.code = code;
|
||||
|
|
|
@ -4,6 +4,7 @@ import org.springframework.validation.BindingResult;
|
|||
import org.springframework.validation.ObjectError;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* <h1>自定义快捷工具类</h1>
|
||||
|
@ -14,6 +15,16 @@ import java.util.ArrayList;
|
|||
* @version v1.0.0
|
||||
*/
|
||||
public class Processing {
|
||||
|
||||
/**
|
||||
* <h1>获取参数校验错误信息</h1>
|
||||
* <hr/>
|
||||
* 用于获取参数校验错误信息
|
||||
*
|
||||
* @since v1.0.0
|
||||
* @param bindingResult 参数校验结果
|
||||
* @return {@link ArrayList<String>}
|
||||
*/
|
||||
public static ArrayList<String> getValidatedErrorList(BindingResult bindingResult) {
|
||||
ArrayList<String> arrayList = new ArrayList<>();
|
||||
for (ObjectError objectError : bindingResult.getAllErrors()) {
|
||||
|
@ -21,4 +32,44 @@ public class Processing {
|
|||
}
|
||||
return arrayList;
|
||||
}
|
||||
|
||||
/**
|
||||
* <h1>生成工号</h1>
|
||||
* <hr/>
|
||||
* 用于生成工号,默认长度为10
|
||||
*
|
||||
* @since v1.0.0
|
||||
* @param type 0:学生 1:教师 2:其他
|
||||
* @return {@link String}
|
||||
*/
|
||||
public static String createJobNumber(Short type) {
|
||||
return createJobNumber(type, (short) 10);
|
||||
}
|
||||
|
||||
/**
|
||||
* <h1>生成工号</h1>
|
||||
* <hr/>
|
||||
* 用于生成工号
|
||||
*
|
||||
* @since v1.0.0
|
||||
* @param type 0:学生 1:教师 2:其他
|
||||
* @param size 工号长度
|
||||
* @return {@link String}
|
||||
*/
|
||||
public static String createJobNumber(Short type, Short size) {
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
if (type == 0) {
|
||||
stringBuilder.append("STU");
|
||||
} else if (type == 1) {
|
||||
stringBuilder.append("TCH");
|
||||
} else {
|
||||
stringBuilder.append("OTH");
|
||||
}
|
||||
// 生成工号
|
||||
Random random = new Random();
|
||||
for (int i = 0; i < size-3; i++) {
|
||||
stringBuilder.append(random.nextInt(10));
|
||||
}
|
||||
return stringBuilder.toString();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,10 +6,18 @@ public class ResultUtil {
|
|||
return new BaseResponse("Success", 200, "操作成功", null);
|
||||
}
|
||||
|
||||
public static BaseResponse success(String message) {
|
||||
return new BaseResponse("Success", 200, message, null);
|
||||
}
|
||||
|
||||
public static BaseResponse success(Object data) {
|
||||
return new BaseResponse("Success", 200, "操作成功", data);
|
||||
}
|
||||
|
||||
public static BaseResponse success(String message, Object data) {
|
||||
return new BaseResponse("Success", 200, message, data);
|
||||
}
|
||||
|
||||
public static BaseResponse error(ErrorCode errorCode) {
|
||||
return new BaseResponse(errorCode.getOutput(), errorCode.getCode(), errorCode.getMessage());
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user