package com.zt.life.sys.controller; import java.util.Date; import com.zt.core.security.BCryptPasswordEncoder; import com.zt.life.sys.model.SysUserRegister; import com.zt.life.sys.service.SysUserRegisterService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.*; import com.zt.common.annotation.LogOperation; import com.zt.common.servlet.Result; import com.zt.common.utils.UUIDUtil; import com.zt.modules.sys.service.*; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; /** * 用户管理 * * @author hehz * @since 1.0.0 2020-09-01 */ @RestController @RequestMapping("/sys") @Api(tags = "用户管理") public class SysUserRegisterController { @Autowired private SysUserRegisterService sysUserRegisterService; @Autowired private SysDeptService sysDeptService; @Autowired private SysParamsService paramsService; @Autowired SysJobUserService sysJobUserService; @Autowired SysPostUserService sysPostUserService; @Autowired SysMapService sysMapService; @PostMapping("/userRegister") @ApiOperation("新增") @LogOperation("用户管理--->新增") //@RequiresPermissions("sys:user") @Transactional(rollbackFor = Exception.class) public Result insert(@RequestBody SysUserRegister entity) { // 效验数据 //ValidatorUtils.validateEntity(entity, AddGroup.class, DefaultGroup.class); String result = "OK"; Integer hasUser = sysUserRegisterService.existUsername(entity.getUserName()); if (hasUser ==0) { String password = encrypt(entity.getPassword()); entity.setPassword(password); Long id = UUIDUtil.generateId(); entity.setUserId(id); Date now = new Date(); entity.setCreateTime(now); entity.setUpdateTime(now); entity.setCreateBy("10"); entity.setUpdateBy("10"); entity.setDelFlag(0); sysUserRegisterService.insert(entity); // 保存岗位用户关系 // sysPostUserService.saveOrUpdate(entity.getId(), entity.getPostIdList()); // 保存职位用户关系 // sysJobUserService.saveOrUpdate(entity.getId(), entity.getJobIdList()); sysMapService.saveOrUpdate("userTeamggroup",entity.getUserId(),entity.getTeamgroupIds()); sysMapService.saveOrUpdate("userShip",entity.getUserId(),entity.getShipIds()); }else{ result = "该用户名已经被注册,请修改登录用户名!"; } return Result.ok(result); } @PutMapping("/userChangePassword") @ApiOperation("修改密码") @LogOperation("用户管理--->修改密码") @Transactional(rollbackFor = Exception.class) public Result update(@RequestBody SysUserRegister entity) { // 效验数据 //ValidatorUtils.validateEntity(entity, AddGroup.class, DefaultGroup.class); String result = "OK"; SysUserRegister user = sysUserRegisterService.getByUserName(entity.getUserName()); if (null == user) { result = "该用户不存在!"; } else { if (!matchEncrypt(entity.getOldPassword(), user.getPassword())) { result = "旧密码不正确!"; } else { String password = encrypt(entity.getPassword()); sysUserRegisterService.changePassword(entity.getUserName(), password); } } return Result.ok(result); } public String encrypt(String password) { BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(); return encoder.encode(password); } public boolean matchEncrypt(String password, String passwordHash) { BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(); return encoder.matches(password, passwordHash); } }