|
@@ -0,0 +1,285 @@
|
|
|
+package com.fdkankan.web.backend;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.fdkankan.common.exception.BaseRuntimeException;
|
|
|
+import com.fdkankan.common.util.*;
|
|
|
+import com.fdkankan.domain.backend.UserRoleEntity;
|
|
|
+import com.fdkankan.service.backend.UserRoleService;
|
|
|
+import com.fdkankan.web.aop.CheckCurrentUser;
|
|
|
+import com.fdkankan.web.aop.WebControllerLog;
|
|
|
+import com.github.pagehelper.PageInfo;
|
|
|
+import com.fdkankan.common.constant.MsgCode;
|
|
|
+import com.fdkankan.domain.dto.request.UserRequest;
|
|
|
+import com.fdkankan.domain.backend.UserEntity;
|
|
|
+import com.fdkankan.domain.dto.response.UserResponse;
|
|
|
+import com.fdkankan.service.backend.UserService;
|
|
|
+import com.github.pagehelper.util.StringUtil;
|
|
|
+import io.swagger.annotations.Api;
|
|
|
+import io.swagger.annotations.ApiImplicitParam;
|
|
|
+import io.swagger.annotations.ApiImplicitParams;
|
|
|
+import io.swagger.annotations.ApiOperation;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by owen on 2020/2/18 0018 12:17
|
|
|
+ */
|
|
|
+@Api(tags = "后台用户管理", description = "后台用户管理")
|
|
|
+@RestController
|
|
|
+@RequestMapping("api/manage/user")
|
|
|
+@Transactional
|
|
|
+public class UserController extends BaseController {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private UserService userService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private UserRoleService userRoleService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RedisTemplate<String, String> redisTemplate;
|
|
|
+
|
|
|
+ @ApiOperation("分页获取用户列表")
|
|
|
+ @PostMapping("list")
|
|
|
+ @ApiImplicitParams({
|
|
|
+ @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "String", required = true),
|
|
|
+ @ApiImplicitParam(name = "pageSize", value = "页数", dataType = "String", required = true)})
|
|
|
+// @WebControllerLog(description = "用户管理---获取用户列表")
|
|
|
+ @CheckCurrentUser()
|
|
|
+ public Result list(@RequestBody UserRequest param){
|
|
|
+ PageInfo<UserResponse> page = new PageInfo<UserResponse>(userService.findAllBySearchKey(param));
|
|
|
+ return Result.success(page);
|
|
|
+ }
|
|
|
+
|
|
|
+ @ApiOperation("新增用户信息")
|
|
|
+ @PostMapping("save")
|
|
|
+ @ApiImplicitParams({
|
|
|
+ @ApiImplicitParam(name = "userName", value = "手机号", dataType = "String", required = true),
|
|
|
+ @ApiImplicitParam(name = "password", value = "密码", dataType = "String", required = true),
|
|
|
+ @ApiImplicitParam(name = "confirmPwd", value = "确认密码", dataType = "String", required = true),
|
|
|
+ @ApiImplicitParam(name = "nickName", value = "姓名", dataType = "String", required = true),
|
|
|
+ @ApiImplicitParam(name = "departmentId", value = "所属架构id", dataType = "String", required = true),
|
|
|
+ @ApiImplicitParam(name = "roleId", value = "角色id", dataType = "String", required = true)})
|
|
|
+ @WebControllerLog(description = "用户管理---新增用户")
|
|
|
+ @CheckCurrentUser()
|
|
|
+ public Result save(@RequestBody UserRequest param, HttpServletRequest req) throws Exception{
|
|
|
+
|
|
|
+ UserEntity userEntity = null;
|
|
|
+ if (param.getRoleId() == null || param.getDepartmentId() == null || StringUtil.isEmpty(param.getPassword()) ||
|
|
|
+ StringUtil.isEmpty(param.getUserName()) || StringUtil.isEmpty(param.getConfirmPwd()) ||
|
|
|
+ StringUtil.isEmpty(param.getNickName())) {
|
|
|
+ throw new BaseRuntimeException(MsgCode.FAILURE_CODE_3001, MsgCode.FAILURE_MSG_3001);
|
|
|
+ }
|
|
|
+ if(!param.getPassword().equals(param.getConfirmPwd())){
|
|
|
+ throw new BaseRuntimeException(MsgCode.FAILURE_CODE_4004, MsgCode.FAILURE_MSG_4004);
|
|
|
+ }
|
|
|
+
|
|
|
+ //对前端传的密码解密
|
|
|
+ if(param.getPassword().length() < 25){
|
|
|
+ throw new BaseRuntimeException(MsgCode.FAILURE_CODE_4001, MsgCode.FAILURE_MSG_4001);
|
|
|
+ }
|
|
|
+ String password = Base64Converter.decode(Base64Converter.subText(param.getPassword()));
|
|
|
+ param.setPassword(password);
|
|
|
+
|
|
|
+ if(!RegexUtils.validatePassword(param.getPassword())){
|
|
|
+ throw new BaseRuntimeException(MsgCode.FAILURE_CODE_4007, MsgCode.FAILURE_MSG_4007);
|
|
|
+ }
|
|
|
+
|
|
|
+ if(!RegexUtils.validateMobilePhone(param.getUserName())){
|
|
|
+ throw new BaseRuntimeException(MsgCode.FAILURE_CODE_4005, MsgCode.FAILURE_MSG_4005);
|
|
|
+ }
|
|
|
+
|
|
|
+ int n = 0;
|
|
|
+ userEntity = userService.findByUserName(param.getUserName());
|
|
|
+ if(userEntity != null){
|
|
|
+ throw new BaseRuntimeException(MsgCode.FAILURE_CODE_4006, MsgCode.FAILURE_MSG_4006);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ userEntity = new UserEntity();
|
|
|
+
|
|
|
+ BeanUtils.copyProperties(param, userEntity);
|
|
|
+ userEntity.setPassword(PasswordUtils.encrypt(param.getUserName(), param.getPassword(), PasswordUtils.getStaticSalt()));
|
|
|
+
|
|
|
+// userEntity.setCreateBy(getTokenUserName());
|
|
|
+
|
|
|
+ n = userService.save(userEntity);
|
|
|
+
|
|
|
+
|
|
|
+ if (n >= 0) {
|
|
|
+ UserRoleEntity userRoleEntity = new UserRoleEntity();
|
|
|
+ userRoleEntity.setUserId(userEntity.getId());
|
|
|
+ userRoleEntity.setRoleId(param.getRoleId());
|
|
|
+ userRoleService.save(userRoleEntity);
|
|
|
+// userService.saveUserRole(userEntity.getId(), param.getRoleId());
|
|
|
+ return Result.success();
|
|
|
+ }
|
|
|
+ return Result.success();
|
|
|
+ }
|
|
|
+
|
|
|
+ @ApiOperation("修改用户信息")
|
|
|
+ @PostMapping("update")
|
|
|
+ @ApiImplicitParams({
|
|
|
+ @ApiImplicitParam(name = "id", value = "用户id", dataType = "String", required = true),
|
|
|
+ @ApiImplicitParam(name = "nickName", value = "姓名", dataType = "String", required = true),
|
|
|
+ @ApiImplicitParam(name = "userName", value = "手机号", dataType = "String", required = true),
|
|
|
+ @ApiImplicitParam(name = "departmentId", value = "所属架构id", dataType = "String", required = true),
|
|
|
+ @ApiImplicitParam(name = "roleId", value = "角色id", dataType = "String", required = true)})
|
|
|
+ @WebControllerLog(description = "用户管理---修改用户")
|
|
|
+ @CheckCurrentUser()
|
|
|
+ public Result update(@RequestBody UserRequest param, HttpServletRequest req) throws Exception{
|
|
|
+
|
|
|
+ UserEntity userEntity = null;
|
|
|
+ if (StringUtil.isEmpty(param.getRoleId()) || StringUtil.isEmpty(param.getDepartmentId()) ||
|
|
|
+ StringUtil.isEmpty(param.getId()) || StringUtil.isEmpty(param.getNickName()) ) {
|
|
|
+ throw new BaseRuntimeException(MsgCode.FAILURE_CODE_3001, MsgCode.FAILURE_MSG_3001);
|
|
|
+ }
|
|
|
+
|
|
|
+ if(StringUtil.isNotEmpty(param.getUserName())){
|
|
|
+ if(!RegexUtils.validateMobilePhone(param.getUserName())){
|
|
|
+ throw new BaseRuntimeException(MsgCode.FAILURE_CODE_4005, MsgCode.FAILURE_MSG_4005);
|
|
|
+ }
|
|
|
+
|
|
|
+ userEntity = userService.findByUserName(param.getUserName());
|
|
|
+ if(userEntity != null && !userEntity.getId().equals(param.getId())){
|
|
|
+ throw new BaseRuntimeException(MsgCode.FAILURE_CODE_4006, MsgCode.FAILURE_MSG_4006);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ int n = 0;
|
|
|
+ userEntity = userService.findById(param.getId());
|
|
|
+ if(userEntity == null){
|
|
|
+ throw new BaseRuntimeException(MsgCode.FAILURE_CODE_4001, MsgCode.FAILURE_MSG_4001);
|
|
|
+ }
|
|
|
+ // 每次修改,删除用户角色表信息,重新添加
|
|
|
+ userService.deleteUserRole(param.getId());
|
|
|
+
|
|
|
+ BeanUtils.copyProperties(param, userEntity);
|
|
|
+ userService.update(userEntity);
|
|
|
+
|
|
|
+
|
|
|
+ if (n >= 0) {
|
|
|
+ UserRoleEntity userRoleEntity = new UserRoleEntity();
|
|
|
+ userRoleEntity.setUserId(userEntity.getId());
|
|
|
+ userRoleEntity.setRoleId(param.getRoleId());
|
|
|
+ userRoleService.save(userRoleEntity);
|
|
|
+// userService.saveUserRole(userEntity.getId(), param.getRoleId());
|
|
|
+ return Result.success();
|
|
|
+ }
|
|
|
+ return Result.success();
|
|
|
+ }
|
|
|
+
|
|
|
+ @ApiOperation("删除用户")
|
|
|
+ @GetMapping("delete/{id}")
|
|
|
+ @ApiImplicitParams({
|
|
|
+ @ApiImplicitParam(name = "id", value = "id", dataType = "String", required = true)})
|
|
|
+ @WebControllerLog(description = "用户管理---删除用户")
|
|
|
+ @CheckCurrentUser()
|
|
|
+ public Result delete(@PathVariable String id){
|
|
|
+
|
|
|
+ userService.deleteById(id);
|
|
|
+// userService.deleteUserRole(id);
|
|
|
+ return Result.success();
|
|
|
+ }
|
|
|
+
|
|
|
+ @ApiOperation("查询用户信息")
|
|
|
+ @GetMapping("detail/{id}")
|
|
|
+ @ApiImplicitParams({
|
|
|
+ @ApiImplicitParam(name = "id", value = "id", dataType = "String", required = true)})
|
|
|
+ public Result detail(@PathVariable String id){
|
|
|
+ String token = request.getHeader("token");
|
|
|
+ if(StringUtil.isEmpty(token)){
|
|
|
+ throw new BaseRuntimeException(MsgCode.FAILURE_CODE_3004, MsgCode.FAILURE_MSG_3004);
|
|
|
+ }
|
|
|
+
|
|
|
+ String jsonStr = redisTemplate.opsForValue().get(token);
|
|
|
+ JSONObject user = null;
|
|
|
+ if (StringUtil.isNotEmpty(jsonStr)) {
|
|
|
+ user = JSONObject.parseObject(jsonStr);
|
|
|
+ }
|
|
|
+
|
|
|
+ if(user == null){
|
|
|
+ throw new BaseRuntimeException(MsgCode.FAILURE_CODE_3004, MsgCode.FAILURE_MSG_3004);
|
|
|
+ }
|
|
|
+
|
|
|
+ UserEntity userEntity = userService.findById(id);
|
|
|
+
|
|
|
+ if(userEntity == null){
|
|
|
+ throw new BaseRuntimeException(MsgCode.FAILURE_CODE_4011, MsgCode.FAILURE_MSG_4011);
|
|
|
+ }
|
|
|
+
|
|
|
+ //判断用户是否有权限查看用户信息
|
|
|
+ if(!user.containsKey("roleKey") || !"admin".equals(user.getString("roleKey"))){
|
|
|
+ if(!id.equals(user.getString("id"))){
|
|
|
+ throw new BaseRuntimeException(MsgCode.FAILURE_CODE_4010, MsgCode.FAILURE_MSG_4010);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ return Result.success(userEntity);
|
|
|
+ }
|
|
|
+
|
|
|
+ @ApiOperation("修改密码")
|
|
|
+ @PostMapping("updatePwd")
|
|
|
+ @ApiImplicitParams({
|
|
|
+ @ApiImplicitParam(name = "password", value = "新密码", dataType = "String", required = true),
|
|
|
+ @ApiImplicitParam(name = "oldPassword", value = "旧密码", dataType = "String", required = true)})
|
|
|
+ @WebControllerLog(description = "用户管理---修改密码")
|
|
|
+ public Result updatePwd(@RequestBody UserRequest param) throws Exception{
|
|
|
+
|
|
|
+ if(StringUtil.isEmpty(getTokenUserName())){
|
|
|
+ throw new BaseRuntimeException(MsgCode.FAILURE_CODE_3004, MsgCode.FAILURE_MSG_3004);
|
|
|
+ }
|
|
|
+ UserEntity userEntity = userService.findByUserName(getTokenUserName());
|
|
|
+
|
|
|
+ //对前端传的密码解密
|
|
|
+ if(param.getPassword().length() < 25){
|
|
|
+ throw new BaseRuntimeException(MsgCode.FAILURE_CODE_4001, MsgCode.FAILURE_MSG_4001);
|
|
|
+ }
|
|
|
+ String password = Base64Converter.decode(Base64Converter.subText(param.getPassword()));
|
|
|
+ param.setPassword(password);
|
|
|
+
|
|
|
+ //对前端传的密码解密
|
|
|
+ if(param.getOldPassword().length() < 25){
|
|
|
+ throw new BaseRuntimeException(MsgCode.FAILURE_CODE_4001, MsgCode.FAILURE_MSG_4001);
|
|
|
+ }
|
|
|
+ String oldPassword = Base64Converter.decode(Base64Converter.subText(param.getOldPassword()));
|
|
|
+ param.setOldPassword(oldPassword);
|
|
|
+
|
|
|
+
|
|
|
+ // 验证原密码
|
|
|
+ String encrypt = PasswordUtils.encrypt(userEntity.getUserName(), param.getOldPassword(), PasswordUtils.getStaticSalt());
|
|
|
+ if (!userEntity.getPassword().equals(encrypt)) {
|
|
|
+ throw new BaseRuntimeException(MsgCode.FAILURE_CODE_4002, MsgCode.FAILURE_MSG_4002);
|
|
|
+ }
|
|
|
+
|
|
|
+ if(!RegexUtils.validatePassword(param.getPassword())){
|
|
|
+ throw new BaseRuntimeException(MsgCode.FAILURE_CODE_4007, MsgCode.FAILURE_MSG_4007);
|
|
|
+ }
|
|
|
+
|
|
|
+ userEntity.setPassword(PasswordUtils.encrypt(userEntity.getUserName(), param.getPassword(), PasswordUtils.getStaticSalt()));
|
|
|
+ userService.update(userEntity);
|
|
|
+ return Result.success();
|
|
|
+ }
|
|
|
+
|
|
|
+ @ApiOperation("重置密码")
|
|
|
+ @GetMapping("resetPass/{id}")
|
|
|
+ @ApiImplicitParams({
|
|
|
+ @ApiImplicitParam(name = "id", value = "id", dataType = "String", required = true)})
|
|
|
+ @WebControllerLog(description = "用户管理---重置密码")
|
|
|
+ @CheckCurrentUser()
|
|
|
+ public Result resetPass(@PathVariable String id){
|
|
|
+
|
|
|
+ UserEntity userEntity = userService.findById(id);
|
|
|
+ userEntity.setPassword(PasswordUtils.encrypt(userEntity.getUserName(), "Fcb20210225", PasswordUtils.getStaticSalt()));
|
|
|
+ userService.update(userEntity);
|
|
|
+ return Result.success();
|
|
|
+ }
|
|
|
+
|
|
|
+}
|