package com.fdage.controller; import com.fdage.aop.WebControllerLog; import com.fdage.constant.ConstantUrl; import com.fdage.enums.ResponEnum; import com.fdage.pojo.TbRole; import com.fdage.pojo.TbUser; import com.fdage.request.RequestUser; import com.fdage.respon.ResponUser; import com.fdage.service.IRoleService; import com.fdage.service.IUserService; import com.fdage.util.AjaxJson; import com.fdage.util.FileUtil; import com.fdage.util.PasswordUtils; import com.github.pagehelper.PageInfo; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import java.util.List; /** * Created by Hb_zzZ on 2019/9/11. */ @Controller @RequestMapping("/zhoushan/user") @Slf4j @Api(tags = "用户管理模块") public class UserController { @Autowired private IUserService userService; @Autowired private IRoleService roleService; @Value("${upload.head}") private String userHead; @PostMapping("addUser") @ResponseBody @WebControllerLog(description = "用户管理-新增用户") @ApiOperation("新增用户") @ApiImplicitParams({ @ApiImplicitParam(name = "userName", value = "用户名", dataType = "String"), @ApiImplicitParam(name = "roleId", value = "角色id", dataType = "String"), @ApiImplicitParam(name = "phone", value = "手机号码", dataType = "String"), @ApiImplicitParam(name = "head", value = "头像连接地址", dataType = "String"), @ApiImplicitParam(name = "state", value = "状态,0:启用,1:禁用", dataType = "String")}) public AjaxJson addUser(@RequestBody RequestUser bo){ if(bo == null || StringUtils.isEmpty(bo.getUserName()) || bo.getRoleId() == null || StringUtils.isEmpty(bo.getPhone()) || bo.getState() == null){ return AjaxJson.failure(ResponEnum.NOT_NULL.getCode(), ResponEnum.NOT_NULL.getMessage()); } TbUser user = userService.findByUserName(bo.getUserName()); if(user != null){ return AjaxJson.failure(ResponEnum.USER_EXIST.getCode(), ResponEnum.USER_EXIST.getMessage()); } user = new TbUser(); BeanUtils.copyProperties(bo, user); user.setPassword(PasswordUtils.encrypt("1234abcd", bo.getUserName(), PasswordUtils.getStaticSalt())); userService.insert(user); roleService.saveRoleUser(bo.getRoleId(), user.getId()); return AjaxJson.success(user); } @PostMapping("updateUser") @ResponseBody @WebControllerLog(description = "用户管理-修改用户") @ApiOperation("修改用户") @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "id", dataType = "String"), @ApiImplicitParam(name = "userName", value = "用户名", dataType = "String"), @ApiImplicitParam(name = "roleId", value = "角色id", dataType = "String"), @ApiImplicitParam(name = "phone", value = "手机号码", dataType = "String"), @ApiImplicitParam(name = "head", value = "头像连接地址", dataType = "String"), @ApiImplicitParam(name = "state", value = "状态,0:启用,1:禁用", dataType = "String")}) public AjaxJson updateUser(@RequestBody RequestUser bo){ if(bo == null || StringUtils.isEmpty(bo.getUserName()) || bo.getRoleId() == null || StringUtils.isEmpty(bo.getPhone()) || bo.getState() == null ){ return AjaxJson.failure(ResponEnum.NOT_NULL.getCode(), ResponEnum.NOT_NULL.getMessage()); } TbUser user = userService.findByUserName(bo.getUserName()); if(user == null){ return AjaxJson.failure(ResponEnum.USER_NOT_EXIST.getCode(), ResponEnum.USER_NOT_EXIST.getMessage()); } bo.setId(user.getId()); BeanUtils.copyProperties(bo, user); user.setPassword(PasswordUtils.encrypt(bo.getPassword(), bo.getUserName(), PasswordUtils.getStaticSalt())); userService.update(user); roleService.deleteRoleUser(user.getId()); roleService.saveRoleUser(bo.getRoleId(), user.getId()); return AjaxJson.success(); } @PostMapping("deleteUser") @ResponseBody @WebControllerLog(description = "用户管理-删除用户") @ApiOperation("删除用户") @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "id", dataType = "String")}) public AjaxJson deleteUser(@RequestBody RequestUser bo){ if(bo == null || bo.getId() == null){ return AjaxJson.failure(ResponEnum.NOT_NULL.getCode(), ResponEnum.NOT_NULL.getMessage()); } TbUser user = userService.findById(bo.getId()); if(user == null){ return AjaxJson.failure(ResponEnum.USER_NOT_EXIST.getCode(), ResponEnum.USER_NOT_EXIST.getMessage()); } userService.delete(user.getId()); roleService.deleteRoleUser(user.getId()); return AjaxJson.success(); } @PostMapping("updateState") @ResponseBody @WebControllerLog(description = "用户管理-用户启用/停用") @ApiOperation("用户启用/停用") @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "id", dataType = "String"), @ApiImplicitParam(name = "state", value = "状态,0:启用,1:禁用", dataType = "String")}) public AjaxJson updateState(@RequestBody RequestUser bo){ if(bo == null || bo.getId() == null || bo.getState() == null){ return AjaxJson.failure(ResponEnum.NOT_NULL.getCode(), ResponEnum.NOT_NULL.getMessage()); } TbUser user = userService.findById(bo.getId()); if(user == null){ return AjaxJson.failure(ResponEnum.USER_NOT_EXIST.getCode(), ResponEnum.USER_NOT_EXIST.getMessage()); } user.setState(bo.getState()); userService.update(user); return AjaxJson.success(); } @PostMapping("findById") @ResponseBody @ApiOperation("通过id查找用户") @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "id", dataType = "String")}) public AjaxJson findById(@RequestBody RequestUser bo){ if(bo == null || bo.getId() == null){ return AjaxJson.failure(ResponEnum.NOT_NULL.getCode(), ResponEnum.NOT_NULL.getMessage()); } TbUser user = userService.findById(bo.getId()); if(user == null){ return AjaxJson.failure(ResponEnum.USER_NOT_EXIST.getCode(), ResponEnum.USER_NOT_EXIST.getMessage()); } user.setPassword(""); return AjaxJson.success(user); } @PostMapping("updatePassword") @ResponseBody @WebControllerLog(description = "用户管理-用户修改密码") @ApiOperation("用户修改密码") @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "id", dataType = "String"), @ApiImplicitParam(name = "repeatPassword", value = "重复新密码", dataType = "String"), @ApiImplicitParam(name = "newPassword", value = "新密码", dataType = "String"), @ApiImplicitParam(name = "password", value = "当前密码", dataType = "String")}) public AjaxJson updatePassword(@RequestBody RequestUser bo){ if(bo == null || bo.getId() == null || StringUtils.isEmpty(bo.getPassword()) || StringUtils.isEmpty(bo.getRepeatPassword()) || StringUtils.isEmpty(bo.getNewPassword())){ return AjaxJson.failure(ResponEnum.NOT_NULL.getCode(), ResponEnum.NOT_NULL.getMessage()); } if(!bo.getRepeatPassword().equals(bo.getNewPassword())){ return AjaxJson.failure(ResponEnum.PASSWORD_INCONSISTENCY.getCode(), ResponEnum.PASSWORD_INCONSISTENCY.getMessage()); } TbUser user = userService.findById(bo.getId()); if(user == null){ return AjaxJson.failure(ResponEnum.USER_NOT_EXIST.getCode(), ResponEnum.USER_NOT_EXIST.getMessage()); } String encryptPwd = PasswordUtils.encrypt(bo.getPassword(), user.getUserName(), PasswordUtils.getStaticSalt()); if(!encryptPwd.equals(user.getPassword())){ return AjaxJson.failure(ResponEnum.PASSWORD_ERROR.getCode(), ResponEnum.PASSWORD_ERROR.getMessage()); } user.setPassword(PasswordUtils.encrypt(bo.getNewPassword(), user.getUserName(), PasswordUtils.getStaticSalt())); userService.update(user); return AjaxJson.success(); } @PostMapping("resetPassword") @ResponseBody @WebControllerLog(description = "用户管理-用户重置密码") @ApiOperation("用户重置密码") @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "id", dataType = "String")}) public AjaxJson resetPassword(@RequestBody RequestUser bo){ if(bo == null || bo.getId() == null){ return AjaxJson.failure(ResponEnum.NOT_NULL.getCode(), ResponEnum.NOT_NULL.getMessage()); } TbUser user = userService.findById(bo.getId()); if(user == null){ return AjaxJson.failure(ResponEnum.USER_NOT_EXIST.getCode(), ResponEnum.USER_NOT_EXIST.getMessage()); } user.setPassword(PasswordUtils.encrypt("1234abcd", user.getUserName(), PasswordUtils.getStaticSalt())); userService.update(user); return AjaxJson.success(); } @PostMapping("list") @ResponseBody @WebControllerLog(description = "用户管理-获取用户列表") @ApiOperation("获取用户列表") @ApiImplicitParams({ @ApiImplicitParam(name = "status", value = "状态,0:启用,1:禁用", dataType = "String"), @ApiImplicitParam(name = "userName", value = "用户名", dataType = "String"), @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "String"), @ApiImplicitParam(name = "pageSize", value = "每页数量", dataType = "String")}) public AjaxJson list(@RequestBody RequestUser bo){ List list = userService.findUserList(bo); PageInfo pageInfo = new PageInfo<>(list); return AjaxJson.success(pageInfo); } @PostMapping("roleList") @ResponseBody @ApiOperation("获取所有角色") public AjaxJson roleList(){ List list = userService.roleList(); return AjaxJson.success(list); } @RequestMapping("/uploadHead") @ResponseBody @WebControllerLog(description = "用户管理-用户上传头像") @ApiOperation("用户上传头像") @ApiImplicitParams({ @ApiImplicitParam(name = "file", value = "文件流", dataType = "String")}) public AjaxJson upload(@RequestParam(value = "filename", defaultValue = "") String name,@RequestParam("file") MultipartFile file){ if(file == null){ return AjaxJson.failure("参数不能为空"); } String fileName = System.currentTimeMillis() + "_"; if(StringUtils.isNotEmpty(name)){ fileName = fileName + name; }else { fileName = fileName + file.getOriginalFilename(); } log.info("图片地址:" + fileName); boolean flag = FileUtil.upload(file, userHead, fileName); if(!flag){ return AjaxJson.failure("上传图片失败"); } // Map map = new HashMap<>(); // map.put(path + "/" + fileName, "company/" + fileName); // uploadToAlibabaService.upload(map); return AjaxJson.success((Object) ("/head/" + fileName)); } }