UserCurrent
This commit is contained in:
parent
955bdc5552
commit
88e3672b48
@ -9,11 +9,14 @@ import com.jsl.oa.utils.Processing;
|
|||||||
import com.jsl.oa.utils.ResultUtil;
|
import com.jsl.oa.utils.ResultUtil;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.springframework.lang.Nullable;
|
||||||
import org.springframework.validation.BindingResult;
|
import org.springframework.validation.BindingResult;
|
||||||
import org.springframework.validation.annotation.Validated;
|
import org.springframework.validation.annotation.Validated;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <h1>用户控制器</h1>
|
* <h1>用户控制器</h1>
|
||||||
@ -75,6 +78,42 @@ public class UserController {
|
|||||||
return userService.userEditProfile(userEditProfileVO);
|
return userService.userEditProfile(userEditProfileVO);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("/user/current")
|
||||||
|
public BaseResponse userCurrent(HttpServletRequest request, @RequestParam @Nullable String id, @RequestParam @Nullable String username, @RequestParam @Nullable String email, @RequestParam @Nullable String phone) {
|
||||||
|
// 判断是否有参数错误
|
||||||
|
if (id == null && username == null && email == null && phone == null) {
|
||||||
|
return ResultUtil.error(ErrorCode.PARAMETER_ERROR);
|
||||||
|
}
|
||||||
|
// 检查数据是否有问题
|
||||||
|
ArrayList<String> arrayForError = new ArrayList<>();
|
||||||
|
if (id != null) {
|
||||||
|
if (!Pattern.matches("^[0-9]+$", id)) {
|
||||||
|
arrayForError.add("id 只能为数字");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (username != null) {
|
||||||
|
if (!Pattern.matches("^[0-9A-Za-z_]+$", username)) {
|
||||||
|
arrayForError.add("username 只允许 0-9、A-Z、a-z、_");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (email != null) {
|
||||||
|
if (!Pattern.matches("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$", email)) {
|
||||||
|
arrayForError.add("email 格式不正确");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (phone != null) {
|
||||||
|
if (!Pattern.matches("^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}$", phone)) {
|
||||||
|
arrayForError.add("手机格式不正确");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 检查是否出现错误
|
||||||
|
if (arrayForError.isEmpty()) {
|
||||||
|
return userService.userCurrent(request, id, username, email, phone);
|
||||||
|
} else {
|
||||||
|
return ResultUtil.error(ErrorCode.PARAMETER_ERROR, arrayForError);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <h2>获取全部的用户信息</h2>
|
* <h2>获取全部的用户信息</h2>
|
||||||
* <hr/>
|
* <hr/>
|
||||||
|
@ -97,4 +97,61 @@ public class UserDAO {
|
|||||||
});
|
});
|
||||||
return userCurrentDO;
|
return userCurrentDO;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前用户信息
|
||||||
|
*
|
||||||
|
* @param uid 用户id
|
||||||
|
* @return {@link UserCurrentDO}
|
||||||
|
*/
|
||||||
|
public UserCurrentDO userCurrentById(Long uid) {
|
||||||
|
UserCurrentDO userCurrentDO = userMapper.getUserCurrentById(uid);
|
||||||
|
return getUserCurrentForRole(userCurrentDO);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前用户信息
|
||||||
|
*
|
||||||
|
* @param username 用户名
|
||||||
|
* @return {@link UserCurrentDO}
|
||||||
|
*/
|
||||||
|
public UserCurrentDO userCurrentByUsername(String username) {
|
||||||
|
UserCurrentDO userCurrentDO = userMapper.getUserCurrentByUsername(username);
|
||||||
|
return getUserCurrentForRole(userCurrentDO);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前用户信息
|
||||||
|
*
|
||||||
|
* @param email 邮箱
|
||||||
|
* @return {@link UserCurrentDO}
|
||||||
|
*/
|
||||||
|
public UserCurrentDO userCurrentByEmail(String email) {
|
||||||
|
UserCurrentDO userCurrentDO = userMapper.getUserCurrentByEmail(email);
|
||||||
|
return getUserCurrentForRole(userCurrentDO);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前用户信息
|
||||||
|
*
|
||||||
|
* @param phone 手机号
|
||||||
|
* @return {@link UserCurrentDO}
|
||||||
|
*/
|
||||||
|
public UserCurrentDO userCurrentByPhone(String phone) {
|
||||||
|
UserCurrentDO userCurrentDO = userMapper.getUserCurrentByPhone(phone);
|
||||||
|
return getUserCurrentForRole(userCurrentDO);
|
||||||
|
}
|
||||||
|
|
||||||
|
private UserCurrentDO getUserCurrentForRole(UserCurrentDO userCurrentDO) {
|
||||||
|
if (userCurrentDO != null) {
|
||||||
|
RoleUserDO newRoleUserDO = new RoleUserDO();
|
||||||
|
newRoleUserDO.setRid(0L)
|
||||||
|
.setUid(userCurrentDO.getId())
|
||||||
|
.setCreatedAt(new Timestamp(System.currentTimeMillis()));
|
||||||
|
userCurrentDO.setRole(newRoleUserDO);
|
||||||
|
return userCurrentDO;
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -57,4 +57,16 @@ public interface UserMapper {
|
|||||||
"OR phone LIKE CONCAT('%',#{search},'%') " +
|
"OR phone LIKE CONCAT('%',#{search},'%') " +
|
||||||
"ORDER BY `id` LIMIT #{page},#{limit}")
|
"ORDER BY `id` LIMIT #{page},#{limit}")
|
||||||
List<UserCurrentDO> getAllUserBySearch(UserAllCurrentVO userAllCurrentVO);
|
List<UserCurrentDO> getAllUserBySearch(UserAllCurrentVO userAllCurrentVO);
|
||||||
|
|
||||||
|
@Select("SELECT * FROM organize_oa.oa_user WHERE id = #{uid}")
|
||||||
|
UserCurrentDO getUserCurrentById(Long uid);
|
||||||
|
|
||||||
|
@Select("SELECT * FROM organize_oa.oa_user WHERE username = #{username}")
|
||||||
|
UserCurrentDO getUserCurrentByUsername(String username);
|
||||||
|
|
||||||
|
@Select("SELECT * FROM organize_oa.oa_user WHERE email = #{email}")
|
||||||
|
UserCurrentDO getUserCurrentByEmail(String email);
|
||||||
|
|
||||||
|
@Select("SELECT * FROM organize_oa.oa_user WHERE phone = #{phone}")
|
||||||
|
UserCurrentDO getUserCurrentByPhone(String phone);
|
||||||
}
|
}
|
||||||
|
@ -18,8 +18,8 @@ import javax.servlet.http.HttpServletRequest;
|
|||||||
public interface UserService {
|
public interface UserService {
|
||||||
/**
|
/**
|
||||||
* <h2>根据用户名获取用户信息</h2>
|
* <h2>根据用户名获取用户信息</h2>
|
||||||
*
|
* <hr/>
|
||||||
* <p>该方法用于根据用户名获取用户信息</p>
|
* 该方法用于根据用户名获取用户信息
|
||||||
*
|
*
|
||||||
* @param username 用户名
|
* @param username 用户名
|
||||||
* @return 用户信息
|
* @return 用户信息
|
||||||
@ -27,21 +27,33 @@ public interface UserService {
|
|||||||
UserDO getUserInfoByUsername(String username);
|
UserDO getUserInfoByUsername(String username);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户账号删除
|
* <h2>用户账号删除</h2>
|
||||||
|
* <hr/>
|
||||||
|
* 该方法用于用户账号删除
|
||||||
*
|
*
|
||||||
* @param id
|
* @param id 用户id
|
||||||
* @return
|
* @return {@link BaseResponse}
|
||||||
*/
|
*/
|
||||||
BaseResponse userDelete(Long id);
|
BaseResponse userDelete(Long id);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户账号锁定
|
* <h2>账号锁定</h2>
|
||||||
|
* <hr/>
|
||||||
|
* 该方法用于用户账号锁定
|
||||||
*
|
*
|
||||||
* @param id
|
* @param id 用户id
|
||||||
* @return
|
* @return {@link BaseResponse}
|
||||||
*/
|
*/
|
||||||
BaseResponse userLock(Long id);
|
BaseResponse userLock(Long id);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <h2>用户编辑自己的信息</h2>
|
||||||
|
* <hr/>
|
||||||
|
* 该方法用于用户编辑自己的信息
|
||||||
|
*
|
||||||
|
* @param userEditProfileVO 用户编辑自己的信息
|
||||||
|
* @return {@link BaseResponse}
|
||||||
|
*/
|
||||||
BaseResponse userEditProfile(UserEditProfileVO userEditProfileVO);
|
BaseResponse userEditProfile(UserEditProfileVO userEditProfileVO);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -54,4 +66,18 @@ public interface UserService {
|
|||||||
* @return {@link BaseResponse}
|
* @return {@link BaseResponse}
|
||||||
*/
|
*/
|
||||||
BaseResponse userCurrentAll(HttpServletRequest request, UserAllCurrentVO userAllCurrentVO);
|
BaseResponse userCurrentAll(HttpServletRequest request, UserAllCurrentVO userAllCurrentVO);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <h2>获取当前用户信息</h2>
|
||||||
|
* <hr/>
|
||||||
|
* 该方法用于获取当前用户信息
|
||||||
|
*
|
||||||
|
* @param request 请求
|
||||||
|
* @param id 用户id
|
||||||
|
* @param username 用户名
|
||||||
|
* @param email 邮箱
|
||||||
|
* @param phone 手机号
|
||||||
|
* @return {@link BaseResponse}
|
||||||
|
*/
|
||||||
|
BaseResponse userCurrent(HttpServletRequest request, String id, String username, String email, String phone);
|
||||||
}
|
}
|
||||||
|
@ -67,13 +67,7 @@ public class UserServiceImpl implements UserService {
|
|||||||
@Override
|
@Override
|
||||||
public BaseResponse userCurrentAll(HttpServletRequest request, @NotNull UserAllCurrentVO userAllCurrentVO) {
|
public BaseResponse userCurrentAll(HttpServletRequest request, @NotNull UserAllCurrentVO userAllCurrentVO) {
|
||||||
// 检查是否是管理员用户
|
// 检查是否是管理员用户
|
||||||
RoleUserDO roleUserDO = roleMapper.getRoleUserByUid(Processing.getAuthHeaderToUserId(request));
|
if (!checkUserIsAdmin(request)) {
|
||||||
if (roleUserDO != null) {
|
|
||||||
RoleDO roleDO = roleMapper.getRoleByRoleName("admin");
|
|
||||||
if (!roleUserDO.getRid().equals(roleDO.getId())) {
|
|
||||||
return ResultUtil.error(ErrorCode.NOT_ADMIN);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return ResultUtil.error(ErrorCode.NOT_ADMIN);
|
return ResultUtil.error(ErrorCode.NOT_ADMIN);
|
||||||
}
|
}
|
||||||
// 检查数据
|
// 检查数据
|
||||||
@ -106,4 +100,47 @@ public class UserServiceImpl implements UserService {
|
|||||||
}
|
}
|
||||||
return ResultUtil.success(userAllCurrentVOList);
|
return ResultUtil.success(userAllCurrentVOList);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BaseResponse userCurrent(HttpServletRequest request, String id, String username, String email, String phone) {
|
||||||
|
// 检查是否是管理员用户
|
||||||
|
if (!checkUserIsAdmin(request)) {
|
||||||
|
return ResultUtil.error(ErrorCode.NOT_ADMIN);
|
||||||
|
}
|
||||||
|
// 根据顺序优先级进行用户信息获取
|
||||||
|
UserCurrentDO userCurrentDO = null;
|
||||||
|
if (id != null && !id.isEmpty()) {
|
||||||
|
userCurrentDO = userDAO.userCurrentById(Long.valueOf(id));
|
||||||
|
} else if (username != null && !username.isEmpty()) {
|
||||||
|
userCurrentDO = userDAO.userCurrentByUsername(username);
|
||||||
|
} else if (email != null && !email.isEmpty()) {
|
||||||
|
userCurrentDO = userDAO.userCurrentByEmail(email);
|
||||||
|
} else if (phone != null && !phone.isEmpty()) {
|
||||||
|
userCurrentDO = userDAO.userCurrentByPhone(phone);
|
||||||
|
}
|
||||||
|
// 返回结果
|
||||||
|
if (userCurrentDO != null) {
|
||||||
|
return ResultUtil.success(userCurrentDO);
|
||||||
|
} else {
|
||||||
|
return ResultUtil.error(ErrorCode.USER_NOT_EXIST);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <h2>检查用户是否是管理员</h2>
|
||||||
|
* <hr/>
|
||||||
|
* 该方法用于检查用户是否是管理员,类型封装后字节返回结果
|
||||||
|
*
|
||||||
|
* @param request 请求
|
||||||
|
* @return 如果为 true 是管理员,false 不是管理员
|
||||||
|
*/
|
||||||
|
private @NotNull Boolean checkUserIsAdmin(HttpServletRequest request) {
|
||||||
|
RoleUserDO roleUserDO = roleMapper.getRoleUserByUid(Processing.getAuthHeaderToUserId(request));
|
||||||
|
if (roleUserDO != null) {
|
||||||
|
RoleDO roleDO = roleMapper.getRoleByRoleName("admin");
|
||||||
|
return roleUserDO.getRid().equals(roleDO.getId());
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user