| | |
| | | import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; |
| | | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; |
| | | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet; |
| | | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.USER_POST_NOT_EMPTY; |
| | | |
| | | @Tag(name = "管理后台 - 用户") |
| | | @RestController |
| | |
| | | @Operation(summary = "新增用户") |
| | | @PreAuthorize("@ss.hasPermission('system:user:create')") |
| | | public CommonResult<Long> createUser(@Valid @RequestBody UserSaveReqVO reqVO) { |
| | | // 校验岗位必填 |
| | | if (CollUtil.isEmpty(reqVO.getPostIds())) { |
| | | throw exception(USER_POST_NOT_EMPTY); |
| | | } |
| | | Long id = userService.createUser(reqVO); |
| | | return success(id); |
| | | } |
| | |
| | | @Operation(summary = "修改用户") |
| | | @PreAuthorize("@ss.hasPermission('system:user:update')") |
| | | public CommonResult<Boolean> updateUser(@Valid @RequestBody UserSaveReqVO reqVO) { |
| | | // 校验岗位必填 |
| | | if (CollUtil.isEmpty(reqVO.getPostIds())) { |
| | | throw exception(USER_POST_NOT_EMPTY); |
| | | } |
| | | userService.updateUser(reqVO); |
| | | return success(true); |
| | | } |
| | |
| | | } |
| | | |
| | | @GetMapping("/list-by-role-code") |
| | | @Operation(summary = "通过角色标识获取用户精简信息列表", description = "只包含拥有该角色的用户,主要用于前端的下拉选项") |
| | | @Parameter(name = "code", description = "角色标识", required = true, example = "admin") |
| | | @Operation(summary = "通过角色标识获取用户精简信息列表", description = "只包含拥有这些角色的用户,主要用于前端的下拉选项。支持逗号分隔的多个角色标识") |
| | | @Parameter(name = "code", description = "角色标识,支持多个,逗号分隔", required = true, example = "admin,user") |
| | | public CommonResult<List<UserSimpleRespVO>> getSimpleUserListByRoleCode( |
| | | @RequestParam("code") String code) { |
| | | RoleDO role = roleService.getRoleByCode(code); |
| | | if (role == null) { |
| | | if (StrUtil.isBlank(code)) { |
| | | return success(Collections.emptyList()); |
| | | } |
| | | Set<Long> userIds = permissionService.getUserRoleIdListByRoleId(Collections.singleton(role.getId())); |
| | | List<String> codes = StrUtil.split(code, ","); |
| | | List<RoleDO> roles = roleService.getRoleListByCodes(codes); |
| | | if (CollUtil.isEmpty(roles)) { |
| | | return success(Collections.emptyList()); |
| | | } |
| | | Set<Long> userIds = permissionService.getUserRoleIdListByRoleId(convertList(roles, RoleDO::getId)); |
| | | if (CollUtil.isEmpty(userIds)) { |
| | | return success(Collections.emptyList()); |
| | | } |
| | |
| | | @GetMapping("/get-import-template") |
| | | @Operation(summary = "获得导入用户模板") |
| | | public void importTemplate(HttpServletResponse response) throws IOException { |
| | | // 手动创建导出 demo |
| | | // 手动创建导出 demo:部门、岗位要填写系统里已经存在的名称;多个岗位用「、」分隔 |
| | | List<UserImportExcelVO> list = Arrays.asList( |
| | | UserImportExcelVO.builder().username("yunai").deptId(1L).email("yunai@iocoder.cn").mobile("15601691300") |
| | | .nickname("超级管理员").status(CommonStatusEnum.ENABLE.getStatus()).sex(SexEnum.MALE.getSex()).build(), |
| | | UserImportExcelVO.builder().username("yuanma").deptId(2L).email("yuanma@iocoder.cn").mobile("15601701300") |
| | | .nickname("源码").status(CommonStatusEnum.DISABLE.getStatus()).sex(SexEnum.FEMALE.getSex()).build() |
| | | UserImportExcelVO.builder().username("zhangsan").nickname("张三").dept("综合管理部").post("普通员工") |
| | | .email("zhangsan@example.com").mobile("15601691300").password("123456") |
| | | .status(CommonStatusEnum.ENABLE.getStatus()).sex(SexEnum.MALE.getSex()).build(), |
| | | UserImportExcelVO.builder().username("lisi").nickname("李四").dept("综合管理部").post("普通员工、班组长") |
| | | .email("lisi@example.com").mobile("15601701300").password("123456") |
| | | .status(CommonStatusEnum.ENABLE.getStatus()).sex(SexEnum.FEMALE.getSex()).build() |
| | | ); |
| | | // 输出 |
| | | ExcelUtils.write(response, "用户导入模板.xls", "用户列表", UserImportExcelVO.class, list); |