| | |
| | | package com.xiaopeng.account.controller; |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import cn.hutool.core.util.DesensitizedUtil; |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.xiaopeng.account.constant.I18nKeyConstant; |
| | |
| | | import com.xiaopeng.account.model.query.SysUserListQuery; |
| | | import com.xiaopeng.account.service.*; |
| | | import com.xiaopeng.api.model.account.*; |
| | | import com.xiaopeng.api.vo.account.LoginUser; |
| | | import com.xiaopeng.common.core.domain.R; |
| | | import com.xiaopeng.common.core.enums.DisabledEnum; |
| | | import com.xiaopeng.common.core.exception.ServiceException; |
| | | import com.xiaopeng.common.core.utils.RSAUtil; |
| | | import com.xiaopeng.common.core.utils.SensitiveDataCryptoUtil; |
| | | import com.xiaopeng.common.core.utils.StringUtils; |
| | | import com.xiaopeng.common.core.web.controller.BaseController; |
| | | import com.xiaopeng.common.core.web.domain.AjaxResult; |
| | | import com.xiaopeng.common.core.web.page.PageInfo; |
| | | import com.xiaopeng.common.log.annotation.AdminOperationLog; |
| | | import com.xiaopeng.common.log.annotation.Log; |
| | | import com.xiaopeng.common.log.enums.AdminBusinessType; |
| | | import com.xiaopeng.common.log.enums.AdminOperationType; |
| | | import com.xiaopeng.common.log.enums.BusinessType; |
| | | import com.xiaopeng.common.redis.service.RedisService; |
| | | import com.xiaopeng.common.security.annotation.Logical; |
| | | import com.xiaopeng.common.security.annotation.RequiresPermissions; |
| | | import com.xiaopeng.common.security.service.TokenService; |
| | | import com.xiaopeng.common.security.utils.SecurityUtils; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import io.swagger.annotations.ApiParam; |
| | | import org.apache.commons.lang3.ArrayUtils; |
| | | import org.apache.shardingsphere.transaction.annotation.ShardingSphereTransactionType; |
| | | import org.apache.shardingsphere.transaction.core.TransactionType; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.validation.annotation.Validated; |
| | |
| | | import java.util.Objects; |
| | | import java.util.concurrent.TimeUnit; |
| | | import java.util.stream.Collectors; |
| | | import com.xiaopeng.common.log.annotation.AdminOperationLog; |
| | | import com.xiaopeng.common.log.enums.AdminBusinessType; |
| | | import com.xiaopeng.common.log.enums.AdminOperationType; |
| | | |
| | | /** |
| | | * 用户信息 |
| | |
| | | private ISysUserRoleService sysUserRoleService; |
| | | @Autowired |
| | | private RedisService redisService; |
| | | @Resource |
| | | private ISysGroupService sysGroupService; |
| | | |
| | | |
| | | /** |
| | | * 获取用户列表 |
| | | */ |
| | | @RequiresPermissions(value = {"sysUser:list"}, logical = Logical.OR) |
| | | @GetMapping("/api/v1/sys-user/pageList") |
| | | @ApiOperation(value = "账号管理分页列表", tags = {"管理后台-账号管理"}) |
| | | public AjaxResult<PageInfo<SysUser>> list(SysUserListQuery sysUserListQuery) { |
| | | PageInfo<SysUser> pageInfo = new PageInfo<>(sysUserListQuery.getPageNum(), sysUserListQuery.getPageSize()); |
| | | public AjaxResult<PageInfo<SysUser>> list(final SysUserListQuery sysUserListQuery) { |
| | | LoginUser loginUser = tokenService.getLoginUser(); |
| | | PageInfo<SysUser> pageInfo = new PageInfo<SysUser>(sysUserListQuery.getPageNum(), sysUserListQuery.getPageSize()); |
| | | PageInfo<SysUser> page = userService.getList(pageInfo, sysUserListQuery); |
| | | page.getRecords().forEach(user -> { |
| | | user.setPhoneNumber(SensitiveDataCryptoUtil.decryptIfPossible(user.getPhoneNumber())); |
| | | if(!loginUser.getPhoneDesensitizationExempt()){ |
| | | user.setPhoneNumber(DesensitizedUtil.mobilePhone(user.getPhoneNumber())); |
| | | // user.setUserAccount(sysGroupService.getPhoneDesensitization(user.getUserAccount())); |
| | | } |
| | | }); |
| | | return AjaxResult.success(page); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 获取用户不分页列表 |
| | | */ |
| | | @GetMapping("/api/v1/sys-user/listAll") |
| | | @ApiOperation(value = "账号管理不分页列表", tags = {"管理后台-账号管理"}) |
| | | public R<List<SysUser>> listAll() { |
| | | LoginUser loginUser = tokenService.getLoginUser(); |
| | | List<SysUser> listAll = userService.list(); |
| | | for (SysUser sysUser : listAll) { |
| | | sysUser.setPhoneNumber(SensitiveDataCryptoUtil.decryptIfPossible(sysUser.getPhoneNumber())); |
| | | if(!loginUser.getPhoneDesensitizationExempt()){ |
| | | sysUser.setPhoneNumber(DesensitizedUtil.mobilePhone(sysUser.getPhoneNumber())); |
| | | } |
| | | } |
| | | return R.ok(listAll); |
| | | } |
| | | |
| | |
| | | /** |
| | | * 新增用户 |
| | | */ |
| | | @Log(title = "帐号管理", businessType = BusinessType.INSERT) |
| | | @RequiresPermissions(value = {"sysUser:add"}, logical = Logical.OR) |
| | | @PostMapping("/api/v1/sys-user/add") |
| | | @ApiOperation(value = "添加帐号", tags = {"管理后台-帐号管理"}) |
| | | @AdminOperationLog( |
| | | businessType = AdminBusinessType.ACCOUNT_MANAGEMENT, |
| | | operationType = AdminOperationType.ADD, |
| | | content = "添加帐号", |
| | | appendRequestData = true |
| | | ) |
| | | public AjaxResult add(@RequestBody SysUser user) { |
| | | if (StringUtils.isNotEmpty(user.getPhoneNumber()) && !userService.checkPhoneUnique(user)) { |
| | | throw new ServiceException("手机号已开通账号", I18nKeyConstant.ACCOUNT_PHONE_DUPLICATE); |
| | |
| | | } |
| | | user.setUniqueNumber(generateUniqueNumber()); |
| | | user.setPassword(SecurityUtils.encryptPassword(user.getPassword())); |
| | | //TODO 手机号脱敏存储 |
| | | user.setPhoneNumber(SensitiveDataCryptoUtil.encrypt(user.getPhoneNumber())); |
| | | userService.insertUser(user); |
| | | SysUserRole sysUserRole = new SysUserRole(); |
| | | sysUserRole.setRoleId(user.getRoleId()); |
| | |
| | | } |
| | | return success(); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 根据用户编号获取详细信息 |
| | | */ |
| | | @ApiOperation(value = "获取用户详情", tags = {"管理后台-系统用户管理"}) |
| | | @GetMapping("/api/v1/getSysUserInfo") |
| | | public AjaxResult getSysUserInfo() { |
| | | LoginUser loginUser = tokenService.getLoginUser(); |
| | | return AjaxResult.success(loginUser); |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | /** |
| | | * 根据用户编号获取详细信息 |
| | | */ |
| | | @ApiOperation(value = "获取用户详情", tags = {"管理后台-系统用户管理"}) |
| | | @GetMapping("/api/v1/getInfo/{userId}") |
| | | public AjaxResult getInfo(@PathVariable Long userId) { |
| | | LoginUser loginUser = tokenService.getLoginUser(); |
| | | userService.checkUserDataScope(userId); |
| | | AjaxResult ajax = AjaxResult.success(); |
| | | List<SysRole> roles = sysRoleService.list(); |
| | |
| | | ajax.put("posts", postService.selectPostAll()); |
| | | if (StringUtils.isNotNull(userId)) { |
| | | SysUser sysUser = userService.selectUserById(userId); |
| | | if(!loginUser.getPhoneDesensitizationExempt()){ |
| | | sysUser.setPhoneNumber(DesensitizedUtil.mobilePhone(sysUser.getPhoneNumber())); |
| | | } |
| | | ajax.put(AjaxResult.DATA_TAG, sysUser); |
| | | ajax.put("postIds", postService.selectPostListByUserId(userId)); |
| | | // ajax.put("roleIds", sysUser.getRoles().stream().map(SysRole::getId).collect(Collectors.toList())); |
| | |
| | | /** |
| | | * 修改用户 |
| | | */ |
| | | @Log(title = "帐号管理", businessType = BusinessType.UPDATE) |
| | | @RequiresPermissions(value = {"sysUser:edit"}, logical = Logical.OR) |
| | | @PutMapping("/api/v1/sys-user/edit") |
| | | @ApiOperation(value = "编辑帐号", tags = {"管理后台-帐号管理"}) |
| | | @AdminOperationLog( |
| | | businessType = AdminBusinessType.ACCOUNT_MANAGEMENT, |
| | | operationType = AdminOperationType.EDIT, |
| | | content = "编辑帐号", |
| | | appendRequestData = true |
| | | ) |
| | | public AjaxResult edit(@Validated @RequestBody SysUser user) { |
| | | userService.checkUserAllowed(user); |
| | | // userService.checkUserDataScope(user.getId()); |
| | | SysUser sysUser = userService.getOne(Wrappers.lambdaQuery(SysUser.class) |
| | | .eq(SysUser::getPhoneNumber, user.getPhoneNumber()) |
| | | .in(SysUser::getPhoneNumber, SensitiveDataCryptoUtil.queryValues(user.getPhoneNumber())) |
| | | .last("LIMIT 1")); |
| | | |
| | | if (StringUtils.isNotEmpty(user.getPhoneNumber()) && (Objects.nonNull(sysUser) && !user.getId().equals(sysUser.getId()) )) { |
| | |
| | | if (user.getPassword() != null && !user.getPassword().isEmpty()) { |
| | | user.setPassword(null); |
| | | } |
| | | |
| | | |
| | | user.setPhoneNumber(SensitiveDataCryptoUtil.encrypt(user.getPhoneNumber())); |
| | | userService.updateById(user); |
| | | |
| | | //删除旧的角色数据,添加新的角色 |
| | |
| | | |
| | | @Resource |
| | | private TokenService tokenService; |
| | | |
| | | |
| | | /** |
| | | * 删除用户 |
| | | */ |
| | | @Log(title = "帐号管理", businessType = BusinessType.DELETE) |
| | | @RequiresPermissions(value = {"sysUser:delete"}, logical = Logical.OR) |
| | | @DeleteMapping("/api/v1/sys-user/{userIds}") |
| | | @ApiOperation(value = "删除账号", tags = {"管理后台-帐号管理"}) |
| | | @AdminOperationLog( |
| | | businessType = AdminBusinessType.ACCOUNT_MANAGEMENT, |
| | | operationType = AdminOperationType.DELETE, |
| | | content = "删除账号", |
| | | appendRequestData = true |
| | | ) |
| | | public AjaxResult remove(@PathVariable Long[] userIds) { |
| | | if (ArrayUtils.contains(userIds, tokenService.getLoginUser())) { |
| | | throw new ServiceException("当前用户不能删除", I18nKeyConstant.ACCOUNT_CURRENT_USER_NOT_DELETE); |
| | |
| | | return toAjax(userService.deleteUserByIds(userIds)); |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | @RequiresPermissions(value = {"sysUser:disable"}, logical = Logical.OR) |
| | | @PostMapping("/api/v1/sys-user/switchStatus/{id}") |
| | | @ApiOperation(value = "账号管理--禁用/启用", tags = {"管理后台-帐号管理"}) |
| | | @AdminOperationLog( |
| | | businessType = AdminBusinessType.ACCOUNT_MANAGEMENT, |
| | | operationType = AdminOperationType.SWITCH_STATUS, |
| | | content = "账号管理--禁用/启用", |
| | | appendRequestData = true |
| | | ) |
| | | public AjaxResult shopUserStart(@ApiParam(name = "id", value = "帐号ID") @PathVariable("id") Long id) { |
| | | SysUser sysUser = userService.getById(id); |
| | | sysUser.setPhoneNumber(SensitiveDataCryptoUtil.decryptIfPossible(sysUser.getPhoneNumber())); |
| | | if (sysUser.getStatus().equals(DisabledEnum.NO.getCode())) { |
| | | sysUser.setStatus(DisabledEnum.YES.getCode()); |
| | | } else { |
| | |
| | | @PostMapping("/getUserList") |
| | | public R<List<SysUser>> getUserList(@RequestBody List<Integer> userIds) { |
| | | List<SysUser> list = userService.list(new LambdaQueryWrapper<SysUser>().in(SysUser::getId, userIds)); |
| | | list.forEach(user -> user.setPhoneNumber(SensitiveDataCryptoUtil.decryptIfPossible(user.getPhoneNumber()))); |
| | | return R.ok(list); |
| | | } |
| | | |
| | |
| | | public R<SysUser> getSysUser(@RequestParam("userId") Long userId) { |
| | | try { |
| | | SysUser sysUser = userService.selectUserById(userId); |
| | | sysUser.setPhoneNumber(SensitiveDataCryptoUtil.decryptIfPossible(sysUser.getPhoneNumber())); |
| | | return R.ok(sysUser); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | |
| | | } |
| | | |
| | | @PostMapping("/updateSysUser") |
| | | @Transactional(rollbackFor = Exception.class) |
| | | @Transactional |
| | | @ShardingSphereTransactionType(TransactionType.BASE) |
| | | public R<Boolean> updateSysUser(@RequestBody SysUser sysUser) { |
| | | try { |
| | | sysUser.setUpdateBy(SecurityUtils.getUsername()); |
| | |
| | | /** |
| | | * 重置密码 |
| | | */ |
| | | @Log(title = "帐号管理", businessType = BusinessType.UPDATE) |
| | | @RequiresPermissions(value = {"sysUser:resetPassword"}, logical = Logical.OR) |
| | | @PutMapping("/api/v1/sys-user/rePassword") |
| | | @ApiOperation(value = "重置密码", tags = {"管理后台-帐号管理"}) |
| | | @AdminOperationLog( |
| | | businessType = AdminBusinessType.ACCOUNT_MANAGEMENT, |
| | | operationType = AdminOperationType.RESET_PASSWORD, |
| | | content = "重置密码", |
| | | appendRequestData = true |
| | | ) |
| | | public AjaxResult resetPwd(@RequestBody UserPwdResetDTO dto) { |
| | | SysUser user = userService.getById(dto.getUserId()); |
| | | if (Objects.isNull(user)) { |
| | |
| | | /** |
| | | * 状态修改 |
| | | */ |
| | | @Log(title = "用户管理", businessType = BusinessType.UPDATE) |
| | | @PutMapping("/changeStatus") |
| | | public AjaxResult changeStatus(@RequestBody SysUser user) { |
| | | userService.checkUserAllowed(user); |
| | |
| | | /** |
| | | * 用户授权角色 |
| | | */ |
| | | @Log(title = "用户管理", businessType = BusinessType.GRANT) |
| | | @PutMapping("/authRole") |
| | | public AjaxResult insertAuthRole(Long userId, Long[] roleIds) { |
| | | userService.checkUserDataScope(userId); |
| | |
| | | */ |
| | | @PostMapping("/queryUserByPhoneList") |
| | | public R<List<SysUser>> queryUserByPhoneList(@RequestBody List<String> phoneList){ |
| | | List<String> phoneQueryValues = buildPhoneQueryValues(phoneList); |
| | | if (phoneQueryValues.isEmpty()) { |
| | | return R.ok(java.util.Collections.emptyList()); |
| | | } |
| | | List<SysUser> list = userService.list(Wrappers.lambdaQuery(SysUser.class) |
| | | .in(SysUser::getPhoneNumber, phoneList)); |
| | | .in(SysUser::getPhoneNumber, buildPhoneQueryValues(phoneList))); |
| | | return R.ok(list); |
| | | } |
| | | |
| | |
| | | */ |
| | | @PostMapping("/queryUserByPhone") |
| | | public R<SysUser> queryUserByPhone(@RequestBody String phone){ |
| | | if (StringUtils.isBlank(phone)) { |
| | | return R.ok(); |
| | | } |
| | | SysUser user = userService.getOne(Wrappers.lambdaQuery(SysUser.class) |
| | | .eq(SysUser::getPhoneNumber, phone) |
| | | .in(SysUser::getPhoneNumber, SensitiveDataCryptoUtil.queryValues(phone)) |
| | | .last("LIMIT 1")); |
| | | if (user != null) { |
| | | user.setPhoneNumber(SensitiveDataCryptoUtil.decryptIfPossible(user.getPhoneNumber())); |
| | | } |
| | | return R.ok(user); |
| | | } |
| | | |
| | | private List<String> buildPhoneQueryValues(List<String> phoneList) { |
| | | if (phoneList == null) { |
| | | return java.util.Collections.emptyList(); |
| | | } |
| | | return phoneList.stream() |
| | | .filter(StringUtils::isNotBlank) |
| | | .flatMap(phone -> SensitiveDataCryptoUtil.queryValues(phone).stream()) |
| | | .distinct() |
| | | .collect(Collectors.toList()); |
| | | } |
| | | |
| | | |
| | |
| | | * @return |
| | | */ |
| | | @ResponseBody |
| | | @PostMapping("/queryUserByUserName") |
| | | @PostMapping("/logan/queryUserByUserName") |
| | | public R<SysUser> queryUserByUserName(@RequestBody String userName){ |
| | | SysUser user = userService.getOne(Wrappers.lambdaQuery(SysUser.class) |
| | | .eq(SysUser::getUserAccount, userName) |
| | |
| | | */ |
| | | @ResponseBody |
| | | @PostMapping("/addSysUser") |
| | | @Transactional(rollbackFor = Exception.class) |
| | | @Transactional |
| | | @ShardingSphereTransactionType(TransactionType.BASE) |
| | | public R<Long> addSysUser(@RequestBody SysUser user) { |
| | | // if (StringUtils.isNotEmpty(user.getPhonenumber()) && !userService.checkPhoneUnique(user)) { |
| | | // throw new RuntimeException("手机号已开通账号"); |
| | |
| | | @ResponseBody |
| | | @PostMapping("/getSysUserById") |
| | | public SysUser getSysUserById(@RequestParam("userId") Long userId){ |
| | | return userService.getById(userId); |
| | | SysUser user = userService.getById(userId); |
| | | if (user != null) { |
| | | user.setPhoneNumber(SensitiveDataCryptoUtil.decryptIfPossible(user.getPhoneNumber())); |
| | | } |
| | | return user; |
| | | } |
| | | |
| | | |
| | |
| | | * @param ids 管理用户ID列表 |
| | | * @return 管理用户列表 |
| | | */ |
| | | @PostMapping("/inner/sys-user/listByIds") |
| | | @PostMapping("/logan/inner/sys-user/listByIds") |
| | | R<List<SysUser>> listByIds(@RequestBody List<Long> ids){ |
| | | List<SysUser> list = userService.list(new LambdaQueryWrapper<SysUser>().in(SysUser::getId, ids)); |
| | | list.forEach(user -> user.setPhoneNumber(SensitiveDataCryptoUtil.decryptIfPossible(user.getPhoneNumber()))); |
| | | return R.ok(list); |
| | | } |
| | | |
| | | |
| | | |
| | | } |