用户编辑、用户添加、查询用户信息接口
This commit is contained in:
parent
c33a03e29d
commit
58343a8aed
@ -1,7 +1,9 @@
|
||||
package com.jsl.oa.controllers;
|
||||
|
||||
import com.jsl.oa.model.voData.UserAddVo;
|
||||
import com.jsl.oa.model.voData.UserAllCurrentVO;
|
||||
import com.jsl.oa.model.voData.UserEditProfileVO;
|
||||
import com.jsl.oa.model.voData.UserEditVo;
|
||||
import com.jsl.oa.services.UserService;
|
||||
import com.jsl.oa.utils.BaseResponse;
|
||||
import com.jsl.oa.utils.ErrorCode;
|
||||
@ -145,4 +147,35 @@ public class UserController {
|
||||
return userService.userCurrentAll(request, userAllCurrentVO);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @Description: TODO 管理员添加用户
|
||||
* @Date: 2024/1/18
|
||||
* @Param userEditProfileVO:
|
||||
* @Param bindingResult:
|
||||
**/
|
||||
@PostMapping("/user/add")
|
||||
public BaseResponse userAdd(@RequestBody @Validated UserAddVo userAddVo, BindingResult bindingResult, HttpServletRequest request){
|
||||
// 判断是否有参数错误
|
||||
if (bindingResult.hasErrors()) {
|
||||
return ResultUtil.error(ErrorCode.REQUEST_BODY_ERROR, Processing.getValidatedErrorList(bindingResult));
|
||||
}
|
||||
return userService.userAdd(userAddVo,request);
|
||||
}
|
||||
|
||||
|
||||
@PutMapping("/user/edit")
|
||||
public BaseResponse userEdit(@RequestBody @Validated UserEditVo userEditVo, BindingResult bindingResult, HttpServletRequest request){
|
||||
// 判断是否有参数错误
|
||||
if (bindingResult.hasErrors()) {
|
||||
return ResultUtil.error(ErrorCode.REQUEST_BODY_ERROR, Processing.getValidatedErrorList(bindingResult));
|
||||
}
|
||||
return userService.userEdit(userEditVo,request);
|
||||
}
|
||||
|
||||
|
||||
@GetMapping("/user/profile/get")
|
||||
public BaseResponse userProflieGet(@RequestParam Long id){
|
||||
return userService.userProflieGet(id);
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package com.jsl.oa.dao;
|
||||
|
||||
import com.jsl.oa.mapper.RoleMapper;
|
||||
import com.jsl.oa.mapper.UserMapper;
|
||||
import com.jsl.oa.model.doData.RoleDO;
|
||||
import com.jsl.oa.model.doData.RoleUserDO;
|
||||
import com.jsl.oa.model.doData.UserCurrentDO;
|
||||
import com.jsl.oa.model.doData.UserDO;
|
||||
@ -154,4 +155,80 @@ public class UserDAO {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return
|
||||
* @Description: TODO 用户添加
|
||||
* @Date: 2024/1/16
|
||||
* @Param userDO: user 数据库表实体类
|
||||
*/
|
||||
public boolean userAdd(UserDO userDO){
|
||||
return userMapper.insertUser(userDO);
|
||||
}
|
||||
|
||||
public void userEdit(UserDO userDO){ userMapper.updateUser(userDO); }
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @Description: TODO 根据username检测用户是否重复
|
||||
* @Date: 2024/1/16
|
||||
* @Param username: 用户名
|
||||
**/
|
||||
public Boolean isRepeatUser(String username){
|
||||
if(userMapper.getUserInfoByUsername(username)==null){
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @Description: TODO 检测用户工号是否重复
|
||||
* @Date: 2024/1/18
|
||||
* @Param userNum:
|
||||
**/
|
||||
public Boolean isRepeatUserNum(String userNum){
|
||||
if(userMapper.getUserByUserNum(userNum) != null){
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Description: TODO 根据用户id获取用户数据
|
||||
* @Date: 2024/1/17
|
||||
* @Param userId:
|
||||
**/
|
||||
public UserDO getUserById(Long userId){
|
||||
return userMapper.getUserById(userId);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @Description: TODO 根据用户id查询对应用户权限
|
||||
* @Date: 2024/1/18
|
||||
* @Param uid:用户id
|
||||
**/
|
||||
public RoleUserDO getRoleFromUser(Long uid){
|
||||
return userMapper.getRoleIdByUserId(uid);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @Description: TODO 检验用户权限是否为管理员
|
||||
* @Date: 2024/1/18
|
||||
* @Param null:用户id
|
||||
**/
|
||||
public Boolean isManagerByRoleId(Long roleId){
|
||||
RoleDO role = userMapper.getRoleById(roleId);
|
||||
if(role == null){
|
||||
return false;
|
||||
}
|
||||
if(role.getRoleName().equals("管理员")){
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
package com.jsl.oa.mapper;
|
||||
|
||||
import com.jsl.oa.model.doData.RoleDO;
|
||||
import com.jsl.oa.model.doData.RoleUserDO;
|
||||
import com.jsl.oa.model.doData.UserCurrentDO;
|
||||
import com.jsl.oa.model.doData.UserDO;
|
||||
import com.jsl.oa.model.voData.UserAllCurrentVO;
|
||||
@ -69,4 +71,21 @@ public interface UserMapper {
|
||||
|
||||
@Select("SELECT * FROM organize_oa.oa_user WHERE phone = #{phone}")
|
||||
UserCurrentDO getUserCurrentByPhone(String phone);
|
||||
|
||||
|
||||
@Select("SELECT * FROM organize_oa.oa_role_user WHERE uid = #{userId}")
|
||||
RoleUserDO getRoleIdByUserId(Long userId);
|
||||
|
||||
@Select("SELECT * FROM organize_oa.oa_role WHERE id = #{roleId}")
|
||||
RoleDO getRoleById(Long roleId);
|
||||
|
||||
|
||||
|
||||
@Update("UPDATE organize_oa.oa_user " +
|
||||
"SET address = #{address}, phone = #{phone}, email = #{email}, age = #{age}, " +
|
||||
"signature = #{signature}, sex = #{sex}, avatar = #{avatar}, nickname = #{nickname}, " +
|
||||
"description = #{description} " +
|
||||
"WHERE id = #{id}")
|
||||
void updateUser(UserDO userDO);
|
||||
|
||||
}
|
||||
|
43
src/main/java/com/jsl/oa/model/voData/UserAddVo.java
Normal file
43
src/main/java/com/jsl/oa/model/voData/UserAddVo.java
Normal file
@ -0,0 +1,43 @@
|
||||
package com.jsl.oa.model.voData;
|
||||
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
|
||||
import javax.validation.constraints.*;
|
||||
|
||||
|
||||
@Getter
|
||||
public class UserAddVo {
|
||||
|
||||
@NotBlank(message = "用户名不能为空")
|
||||
@Pattern(regexp = "^[0-9A-Za-z_]{3,40}$", message = "用户名只能为字母、数字或下划线")
|
||||
private String username;
|
||||
|
||||
@NotBlank(message = "密码不能为空")
|
||||
private String password;
|
||||
|
||||
@NotBlank(message = "家乡不能为空")
|
||||
private String address;
|
||||
|
||||
@NotBlank(message = "电话不能为空")
|
||||
@Pattern(regexp = "^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}$", message = "电话格式错误")
|
||||
private String phone;
|
||||
|
||||
@NotBlank(message = "邮箱不能为空")
|
||||
@Pattern(regexp = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$", message = "邮箱格式错误")
|
||||
private String email;
|
||||
|
||||
@Min(value = 0, message = "保密:0,男:1,女:2")
|
||||
@Max(value = 2, message = "保密:0,男:1,女:2")
|
||||
@NotNull(message = "性别不能为空")
|
||||
private Short sex;
|
||||
|
||||
@NotNull(message = "年龄不能为空")
|
||||
private Short age;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
34
src/main/java/com/jsl/oa/model/voData/UserEditVo.java
Normal file
34
src/main/java/com/jsl/oa/model/voData/UserEditVo.java
Normal file
@ -0,0 +1,34 @@
|
||||
package com.jsl.oa.model.voData;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.*;
|
||||
|
||||
|
||||
@Data
|
||||
public class UserEditVo {
|
||||
|
||||
@NotNull
|
||||
private Long id;
|
||||
|
||||
private String address;
|
||||
|
||||
@Pattern(regexp = "^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}$", message = "电话格式错误")
|
||||
private String phone;
|
||||
|
||||
@Pattern(regexp = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$", message = "邮箱格式错误")
|
||||
private String email;
|
||||
|
||||
@Min(value = 0, message = "保密:0,男:1,女:2")
|
||||
@Max(value = 2, message = "保密:0,男:1,女:2")
|
||||
private Short sex;
|
||||
|
||||
private Short age;
|
||||
private String signature;
|
||||
private String avatar;
|
||||
private String nickname;
|
||||
private String description;
|
||||
|
||||
}
|
||||
|
||||
|
25
src/main/java/com/jsl/oa/model/voData/UserProfile.java
Normal file
25
src/main/java/com/jsl/oa/model/voData/UserProfile.java
Normal file
@ -0,0 +1,25 @@
|
||||
package com.jsl.oa.model.voData;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import lombok.Data;
|
||||
import lombok.Value;
|
||||
import org.springframework.boot.context.properties.bind.DefaultValue;
|
||||
|
||||
|
||||
@Data
|
||||
public class UserProfile {
|
||||
|
||||
private String username;
|
||||
private String address;
|
||||
private String phone;
|
||||
private String email;
|
||||
private Short age;
|
||||
private String signature;
|
||||
private String avatar;
|
||||
private String nickname;
|
||||
private String sex;
|
||||
private String description;
|
||||
|
||||
}
|
||||
|
||||
|
@ -1,8 +1,10 @@
|
||||
package com.jsl.oa.services;
|
||||
|
||||
import com.jsl.oa.model.doData.UserDO;
|
||||
import com.jsl.oa.model.voData.UserAddVo;
|
||||
import com.jsl.oa.model.voData.UserAllCurrentVO;
|
||||
import com.jsl.oa.model.voData.UserEditProfileVO;
|
||||
import com.jsl.oa.model.voData.UserEditVo;
|
||||
import com.jsl.oa.utils.BaseResponse;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
@ -80,4 +82,14 @@ public interface UserService {
|
||||
* @return {@link BaseResponse}
|
||||
*/
|
||||
BaseResponse userCurrent(HttpServletRequest request, String id, String username, String email, String phone);
|
||||
|
||||
|
||||
BaseResponse userAdd(UserAddVo userAddVo, HttpServletRequest request);
|
||||
|
||||
BaseResponse userEdit(UserEditVo userEditVo, HttpServletRequest request);
|
||||
|
||||
|
||||
BaseResponse userProflieGet(Long id);
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,16 +1,15 @@
|
||||
package com.jsl.oa.services.impl;
|
||||
|
||||
import com.jsl.oa.dao.UserDAO;
|
||||
import com.jsl.oa.exception.BusinessException;
|
||||
import com.jsl.oa.mapper.RoleMapper;
|
||||
import com.jsl.oa.model.doData.RoleDO;
|
||||
import com.jsl.oa.model.doData.RoleUserDO;
|
||||
import com.jsl.oa.model.doData.UserCurrentDO;
|
||||
import com.jsl.oa.model.doData.UserDO;
|
||||
import com.jsl.oa.model.voData.UserAllCurrentVO;
|
||||
import com.jsl.oa.model.voData.UserEditProfileVO;
|
||||
import com.jsl.oa.model.voData.*;
|
||||
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 com.jsl.oa.utils.*;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.mindrot.jbcrypt.BCrypt;
|
||||
@ -126,4 +125,115 @@ public class UserServiceImpl implements UserService {
|
||||
return ResultUtil.error(ErrorCode.USER_NOT_EXIST);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public BaseResponse userAdd(UserAddVo userAddVo, HttpServletRequest request) {
|
||||
|
||||
//检测用户是否为管理员
|
||||
BaseResponse checkManagerResult = isManager(request);
|
||||
if(checkManagerResult.getCode() != 200){
|
||||
return checkManagerResult;
|
||||
}
|
||||
|
||||
//如果用户不重复,添加用户
|
||||
if(!userDAO.isRepeatUser(userAddVo.getUsername())){
|
||||
// 生成工号
|
||||
String userNum;
|
||||
do {
|
||||
userNum = Processing.createJobNumber((short) 2);
|
||||
} while (userDAO.isRepeatUserNum(userNum));
|
||||
|
||||
// 数据上传
|
||||
UserDO userDO = new UserDO();
|
||||
userDO.setJobId(userNum)
|
||||
.setUsername(userAddVo.getUsername())
|
||||
.setPassword(BCrypt.hashpw(userAddVo.getPassword(), BCrypt.gensalt()))
|
||||
.setAddress(userAddVo.getAddress())
|
||||
.setPhone(userAddVo.getPhone())
|
||||
.setEmail(userAddVo.getEmail())
|
||||
.setAge(userAddVo.getAge())
|
||||
.setSex(userAddVo.getSex())
|
||||
.setAccountNoLocked(false);
|
||||
// 插入数据
|
||||
if (userDAO.userAdd(userDO)) {
|
||||
userDO.setPassword(null);
|
||||
return ResultUtil.success("添加用户成功", userDO);
|
||||
} else {
|
||||
throw new BusinessException(ErrorCode.DATABASE_INSERT_ERROR);
|
||||
}
|
||||
}else return ResultUtil.error(ErrorCode.USER_EXIST);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public BaseResponse userEdit(UserEditVo userEditVo, HttpServletRequest request) {
|
||||
//检测用户是否为管理员
|
||||
BaseResponse checkManagerResult = isManager(request);
|
||||
if(checkManagerResult.getCode() != 200){
|
||||
return checkManagerResult;
|
||||
}
|
||||
//根据id获取用户信息
|
||||
UserDO userDO = userDAO.getUserById(userEditVo.getId());
|
||||
if(userDO == null){
|
||||
return ResultUtil.error(ErrorCode.USER_NOT_EXIST);
|
||||
}
|
||||
|
||||
//修改非空属性
|
||||
try {
|
||||
Processing.copyProperties(userEditVo,userDO);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
//向数据库中修改属性
|
||||
userDAO.userEdit(userDO);
|
||||
|
||||
return ResultUtil.success("用户信息修改成功");
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseResponse userProflieGet(Long id) {
|
||||
|
||||
UserDO userDO = userDAO.getUserById(id);
|
||||
if(userDO == null){
|
||||
return ResultUtil.error(ErrorCode.USER_NOT_EXIST);
|
||||
}
|
||||
UserProfile userProfile = new UserProfile();
|
||||
try {
|
||||
Processing.copyProperties(userDO,userProfile);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
userProfile.setSex(Processing.getSex(userDO.getSex()));
|
||||
return ResultUtil.success(userProfile);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @Description: TODO 判断用户是否为管理员
|
||||
* @Date: 2024/1/18
|
||||
* @Param request: 请求头
|
||||
**/
|
||||
public BaseResponse isManager(HttpServletRequest request){
|
||||
//获取token
|
||||
String originalAuthorization = request.getHeader("Authorization");
|
||||
String token = originalAuthorization.replace("Bearer ", "");
|
||||
//获取操作用户的权限
|
||||
RoleUserDO roleUserDO = userDAO.getRoleFromUser(JwtUtil.getUserId(token));
|
||||
//用户权限不为空
|
||||
if(roleUserDO == null){
|
||||
return ResultUtil.error(ErrorCode.USER_ROLE_NOT_EXIST);
|
||||
}
|
||||
//用户权限应为管理员
|
||||
if(!userDAO.isManagerByRoleId(roleUserDO.getRid())){
|
||||
return ResultUtil.error(ErrorCode.USER_ROLE_NOT_MANAGER);
|
||||
}
|
||||
return ResultUtil.success();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -10,6 +10,8 @@ public enum ErrorCode {
|
||||
USER_EXIST("UserExist", 40013, "用户名已存在"),
|
||||
TIMESTAMP_ERROR("TimestampError", 40014, "时间戳错误"),
|
||||
USER_NOT_EXIST("UserNotExist", 40015, "用户不存在"),
|
||||
USER_ROLE_NOT_EXIST("UserRoleNotExist", 40016, "用户权限不存在"),
|
||||
USER_ROLE_NOT_MANAGER("UserRoleNotExist", 40017, "用户权限非管理员权限"),
|
||||
UNAUTHORIZED("Unauthorized", 40100, "未授权"),
|
||||
TOKEN_EXPIRED("TokenExpired", 40101, "Token已过期"),
|
||||
VERIFICATION_INVALID("VerificationInvalid", 40102, "验证码无效"),
|
||||
|
@ -9,6 +9,7 @@ import org.springframework.validation.BindingResult;
|
||||
import org.springframework.validation.ObjectError;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Random;
|
||||
|
||||
@ -173,4 +174,70 @@ public class Processing {
|
||||
String charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||
return charset.charAt(index);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @Description: TODO VO类与实体类属性赋值
|
||||
* @Date: 2024/1/18
|
||||
* @Param source:
|
||||
* @Param dest:
|
||||
**/
|
||||
public static <T, S> T copyProperties(S source, T target) throws Exception {
|
||||
Class<?> sourceClass = source.getClass();
|
||||
Class<?> targetClass = target.getClass();
|
||||
|
||||
Field[] sourceFields = sourceClass.getDeclaredFields();
|
||||
for (Field sourceField : sourceFields) {
|
||||
String fieldName = sourceField.getName();
|
||||
Field targetField = null;
|
||||
try {
|
||||
targetField = targetClass.getDeclaredField(fieldName);
|
||||
} catch (NoSuchFieldException e) {
|
||||
// 目标对象不存在该属性,忽略
|
||||
continue;
|
||||
}
|
||||
|
||||
sourceField.setAccessible(true);
|
||||
targetField.setAccessible(true);
|
||||
|
||||
Object value = sourceField.get(source);
|
||||
|
||||
if(value == null){
|
||||
continue;
|
||||
}
|
||||
|
||||
//如果获取的值不为数字且等于“”,则跳过
|
||||
if ( !(value instanceof Number) && value.equals("")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!sourceField.getType().equals(targetField.getType())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
targetField.set(target, value);
|
||||
}
|
||||
|
||||
return target;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Description: TODO 将性别转为字符形式
|
||||
* @Date: 2024/1/18
|
||||
|
||||
**/
|
||||
public static String getSex(short sex){
|
||||
if(sex == 0){
|
||||
return "保密";
|
||||
}
|
||||
if(sex == 1){
|
||||
return "男";
|
||||
}
|
||||
if(sex == 2){
|
||||
return "女";
|
||||
}
|
||||
return " ";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,8 +1,11 @@
|
||||
server:
|
||||
port: 8155
|
||||
max-http-header-size: 10240
|
||||
spring:
|
||||
datasource:
|
||||
url: jdbc:mysql://localhost:3306
|
||||
username: organize_oa
|
||||
password: 123456
|
||||
url: jdbc:mysql://localhost:3306?organize_oa
|
||||
username: root
|
||||
password: Zrx@20041009
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
redis:
|
||||
database: 0
|
||||
@ -10,6 +13,10 @@ spring:
|
||||
port: 6379
|
||||
profiles:
|
||||
active: dev
|
||||
mail:
|
||||
host: 171.38.91.172
|
||||
username: zrx
|
||||
password: Zrx@20041009
|
||||
mybatis:
|
||||
configuration:
|
||||
map-underscore-to-camel-case: true
|
||||
|
Loading…
x
Reference in New Issue
Block a user