| | |
| | | package com.ruoyi.system.controller; |
| | | |
| | | import java.util.*; |
| | | import java.util.concurrent.TimeUnit; |
| | | import java.util.stream.Collectors; |
| | | import javax.annotation.Resource; |
| | | |
| | |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.ruoyi.chargingPile.api.feignClient.SiteClient; |
| | | import com.ruoyi.chargingPile.api.model.Site; |
| | | import com.ruoyi.common.core.constant.MsgConstants; |
| | | import com.ruoyi.common.core.utils.HttpUtils; |
| | | import com.ruoyi.common.core.utils.MsgUtil; |
| | | import com.ruoyi.common.core.utils.StringUtils; |
| | | import com.ruoyi.common.core.web.page.PageInfo; |
| | | import com.ruoyi.common.redis.service.RedisService; |
| | | import com.ruoyi.common.security.service.TokenService; |
| | | import com.ruoyi.common.security.annotation.Logical; |
| | | import com.ruoyi.other.api.domain.TUserSite; |
| | |
| | | @RequestMapping("/user") |
| | | @Api(tags = "用户信息") |
| | | public class SysUserController extends BaseController { |
| | | private static final String RESET_PWD_CODE_KEY = "system:user:resetPwd:code:"; |
| | | private static final String RESET_PWD_LIMIT_KEY = "system:user:resetPwd:limit:"; |
| | | |
| | | @Resource |
| | | private ISysUserService userService; |
| | | |
| | |
| | | |
| | | @Resource |
| | | private ISysPermissionService permissionService; |
| | | |
| | | @Resource |
| | | private PartnerTypeRoleService partnerTypeRoleService; |
| | | |
| | | @Resource |
| | | private ISysConfigService configService; |
| | |
| | | |
| | | @Resource |
| | | private SiteClient siteClient; |
| | | |
| | | @Resource |
| | | private RedisService redisService; |
| | | |
| | | |
| | | |
| | |
| | | /** |
| | | * 获取用户选择列表 |
| | | */ |
| | | @RequiresPermissions(value = {"/systemUserManagement", "/accountingStrategy/select", "/accountingStrategy", "/roleManagement/info"}, logical = Logical.OR) |
| | | @PostMapping("/getChangeUserList") |
| | | @ApiOperation(value = "获取用户选择列表", tags = {"管理后台-获取用户选择列表"}) |
| | | public AjaxResult<PageInfo<SysUser>> getChangeUserList(@RequestBody ChangeUserQuery query) { |
| | | return AjaxResult.success(userService.getChangeUserList(query)); |
| | | PageInfo<SysUser> pageInfo = userService.getChangeUserList(query); |
| | | if (pageInfo != null && pageInfo.getRecords() != null) { |
| | | pageInfo.getRecords().forEach(u -> u.setPassword(null)); |
| | | } |
| | | return AjaxResult.success(pageInfo); |
| | | } |
| | | |
| | | /** |
| | |
| | | /** |
| | | * 根据用户编号获取详细信息 |
| | | */ |
| | | @RequiresPermissions(value = {"/systemUserManagement/update"}, logical = Logical.OR) |
| | | @ApiOperation(value = "获取用户详情", tags = {"管理后台-系统用户管理"}) |
| | | @GetMapping("/getInfo/{userId}") |
| | | public AjaxResult getInfo(@PathVariable Long userId) { |
| | |
| | | List<Site> data = siteClient.getSiteByIds(siteIds).getData(); |
| | | List<String> siteNames = data.stream().map(Site::getName).collect(Collectors.toList()); |
| | | sysUser.setSiteNames(siteNames); |
| | | sysUser.setPassword(null); |
| | | ajax.put(AjaxResult.DATA_TAG, sysUser); |
| | | ajax.put("postIds", postService.selectPostListByUserId(userId)); |
| | | ajax.put("roleIds", sysUser.getRoles().stream().map(SysRole::getRoleId).collect(Collectors.toList())); |
| | |
| | | /** |
| | | * 获取当前用户信息 |
| | | */ |
| | | @InnerAuth |
| | | @GetMapping("/info/{username}") |
| | | public R<LoginUser> info(@PathVariable("username") String username) { |
| | | SysUser sysUser = userService.selectUserByUserName(username); |
| | | if (StringUtils.isNull(sysUser)) { |
| | | return R.fail("用户名/密码错误"); |
| | | return R.fail("用户名或密码错误"); |
| | | } |
| | | sysUser = synchronizePartnerTypeRole(sysUser); |
| | | // 角色集合 |
| | | Set<String> roles = permissionService.getRolePermission(sysUser); |
| | | // 权限集合 |
| | |
| | | @GetMapping("/getInfo") |
| | | public AjaxResult getInfo() { |
| | | SysUser user = userService.selectUserById(tokenService.getLoginUser().getUserid()); |
| | | user = synchronizePartnerTypeRole(user); |
| | | // 角色集合 |
| | | Set<String> roles = permissionService.getRolePermission(user); |
| | | // 权限集合 |
| | | Set<String> permissions = permissionService.getMenuPermission(user); |
| | | user.setPassword(null); |
| | | AjaxResult ajax = AjaxResult.success(); |
| | | ajax.put("user", user); |
| | | ajax.put("roles", roles); |
| | |
| | | return ajax; |
| | | } |
| | | |
| | | private SysUser synchronizePartnerTypeRole(SysUser user) { |
| | | partnerTypeRoleService.synchronize(user); |
| | | if (user != null && Integer.valueOf(2).equals(user.getRoleType()) && user.getObjectId() != null) { |
| | | return userService.selectUserById(user.getUserId()); |
| | | } |
| | | return user; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 发送重置密码验证码 |
| | | */ |
| | | @RequiresPermissions(value = {"/systemUserManagement/reset_passwords"}, logical = Logical.OR) |
| | | @PostMapping("/resetPwd/sendCode") |
| | | @ApiOperation(value = "发送重置密码验证码", tags = {"管理后台-系统用户管理"}) |
| | | public AjaxResult sendResetPwdCode(@RequestBody Map<String, Object> params) { |
| | | Long userId = parseLong(params.get("userId")); |
| | | if (userId == null) { |
| | | return AjaxResult.error("用户不存在"); |
| | | } |
| | | SysUser user = userService.selectUserById(userId); |
| | | if (user == null) { |
| | | return AjaxResult.error("用户不存在"); |
| | | } |
| | | userService.checkUserAllowed(user); |
| | | userService.checkUserDataScope(userId); |
| | | if (StringUtils.isEmpty(user.getPhonenumber())) { |
| | | return AjaxResult.error("当前用户未维护手机号"); |
| | | } |
| | | |
| | | String limitKey = RESET_PWD_LIMIT_KEY + userId; |
| | | if (Boolean.TRUE.equals(redisService.hasKey(limitKey))) { |
| | | return AjaxResult.error("验证码发送过于频繁,请30秒后再试"); |
| | | } |
| | | |
| | | String code = MsgUtil.createCode(); |
| | | String reqStr = MsgUtil.codeMsg(user.getPhonenumber(), code); |
| | | HttpUtils.post(MsgConstants.SEND_URL, reqStr); |
| | | |
| | | redisService.setCacheObject(RESET_PWD_CODE_KEY + userId, code, 5L, TimeUnit.MINUTES); |
| | | redisService.setCacheObject(limitKey, "1", 30L, TimeUnit.SECONDS); |
| | | return AjaxResult.success("验证码已发送"); |
| | | } |
| | | |
| | | /** |
| | | * 重置密码 |
| | |
| | | @Log(title = "用户管理", businessType = BusinessType.UPDATE) |
| | | @PutMapping("/resetPwd") |
| | | @ApiOperation(value = "重置密码", tags = {"管理后台-系统用户管理"}) |
| | | public AjaxResult resetPwd(@RequestBody SysUser user) { |
| | | public AjaxResult resetPwd(@RequestBody Map<String, Object> params) { |
| | | Long userId = parseLong(params.get("userId")); |
| | | String password = parseString(params.get("password")); |
| | | String code = parseString(params.get("code")); |
| | | if (userId == null) { |
| | | return AjaxResult.error("用户不存在"); |
| | | } |
| | | if (StringUtils.isEmpty(password)) { |
| | | return AjaxResult.error("请输入新密码"); |
| | | } |
| | | if (StringUtils.isEmpty(code)) { |
| | | return AjaxResult.error("请输入验证码"); |
| | | } |
| | | |
| | | SysUser user = userService.selectUserById(userId); |
| | | if (user == null) { |
| | | return AjaxResult.error("用户不存在"); |
| | | } |
| | | userService.checkUserAllowed(user); |
| | | userService.checkUserDataScope(user.getUserId()); |
| | | user.setPassword(SecurityUtils.encryptPassword(user.getPassword())); |
| | | userService.checkUserDataScope(userId); |
| | | |
| | | String cacheCode = redisService.getCacheObject(RESET_PWD_CODE_KEY + userId); |
| | | if (StringUtils.isEmpty(cacheCode)) { |
| | | return AjaxResult.error("验证码已过期,请重新获取验证码"); |
| | | } |
| | | if (!code.equals(cacheCode)) { |
| | | return AjaxResult.error("验证码错误"); |
| | | } |
| | | |
| | | user.setPassword(SecurityUtils.encryptPassword(password)); |
| | | user.setUpdateBy(SecurityUtils.getUsername()); |
| | | return toAjax(userService.resetPwd(user)); |
| | | int result = userService.resetPwd(user); |
| | | redisService.deleteObject(RESET_PWD_CODE_KEY + userId); |
| | | return toAjax(result); |
| | | } |
| | | |
| | | /** |
| | | * 重置操作密码 |
| | | */ |
| | | @RequiresPermissions(value = {"/systemUserManagement/reset_passwords"}, logical = Logical.OR) |
| | | @Log(title = "用户管理", businessType = BusinessType.UPDATE) |
| | | @PutMapping("/resetOperationPwd") |
| | | @ApiOperation(value = "重置操作密码", tags = {"管理后台-系统用户管理"}) |
| | | public AjaxResult resetOperationPwd(@RequestBody Map<String, Object> params) { |
| | | Long userId = parseLong(params.get("userId")); |
| | | String operationPassword = parseString(params.get("operationPassword")); |
| | | String code = parseString(params.get("code")); |
| | | if (userId == null) { |
| | | return AjaxResult.error("用户不存在"); |
| | | } |
| | | if (StringUtils.isEmpty(operationPassword)) { |
| | | return AjaxResult.error("请输入新操作密码"); |
| | | } |
| | | if (StringUtils.isEmpty(code)) { |
| | | return AjaxResult.error("请输入验证码"); |
| | | } |
| | | |
| | | SysUser user = userService.selectUserById(userId); |
| | | if (user == null) { |
| | | return AjaxResult.error("用户不存在"); |
| | | } |
| | | userService.checkUserAllowed(user); |
| | | userService.checkUserDataScope(userId); |
| | | |
| | | String cacheCode = redisService.getCacheObject(RESET_PWD_CODE_KEY + userId); |
| | | if (StringUtils.isEmpty(cacheCode)) { |
| | | return AjaxResult.error("验证码已过期,请重新获取验证码"); |
| | | } |
| | | if (!code.equals(cacheCode)) { |
| | | return AjaxResult.error("验证码错误"); |
| | | } |
| | | |
| | | user.setOperationPassword(SecurityUtils.encryptPassword(operationPassword)); |
| | | user.setUpdateBy(SecurityUtils.getUsername()); |
| | | int result = userService.updateUser(user); |
| | | redisService.deleteObject(RESET_PWD_CODE_KEY + userId); |
| | | return toAjax(result); |
| | | } |
| | | private Long parseLong(Object value) { |
| | | if (value == null || StringUtils.isEmpty(String.valueOf(value))) { |
| | | return null; |
| | | } |
| | | return Long.valueOf(String.valueOf(value)); |
| | | } |
| | | |
| | | private String parseString(Object value) { |
| | | return value == null ? null : String.valueOf(value); |
| | | } |
| | | |
| | | /** |
| | | * 状态修改 |
| | | */ |
| | | @RequiresPermissions(value = {"/systemUserManagement/forbidden"}, logical = Logical.OR) |
| | | @Log(title = "用户管理", businessType = BusinessType.UPDATE) |
| | | @PutMapping("/changeStatus") |
| | | public AjaxResult changeStatus(@RequestBody SysUser user) { |
| | |
| | | /** |
| | | * 根据用户编号获取授权角色 |
| | | */ |
| | | @RequiresPermissions(value = {"/systemUserManagement", "/roleManagement/info"}, logical = Logical.OR) |
| | | @GetMapping("/authRole/{userId}") |
| | | public AjaxResult authRole(@PathVariable("userId") Long userId) { |
| | | userService.checkUserDataScope(userId); |
| | | AjaxResult ajax = AjaxResult.success(); |
| | | SysUser user = userService.selectUserById(userId); |
| | | List<SysRole> roles = roleService.selectRolesByUserId(userId); |
| | | user.setPassword(null); |
| | | ajax.put("user", user); |
| | | ajax.put("roles", SysUser.isAdmin(userId) ? roles : roles.stream().filter(r -> !r.isAdmin()).collect(Collectors.toList())); |
| | | return ajax; |
| | |
| | | /** |
| | | * 用户授权角色 |
| | | */ |
| | | @RequiresPermissions(value = {"/systemUserManagement/update", "/roleManagement/info"}, logical = Logical.OR) |
| | | @Log(title = "用户管理", businessType = BusinessType.GRANT) |
| | | @PutMapping("/authRole") |
| | | public AjaxResult insertAuthRole(Long userId, Long[] roleIds) { |
| | |
| | | /** |
| | | * 获取部门树列表 |
| | | */ |
| | | @RequiresPermissions(value = {"/systemUserManagement", "/roleManagement/info"}, logical = Logical.OR) |
| | | @GetMapping("/deptTree") |
| | | public AjaxResult deptTree(SysDept dept) { |
| | | return success(deptService.selectDeptTreeList(dept)); |