diff --git a/management-web-app/src/main/java/cn/iocoder/mall/managementweb/controller/admin/AdminController.java b/management-web-app/src/main/java/cn/iocoder/mall/managementweb/controller/admin/AdminController.java new file mode 100644 index 000000000..721027661 --- /dev/null +++ b/management-web-app/src/main/java/cn/iocoder/mall/managementweb/controller/admin/AdminController.java @@ -0,0 +1,74 @@ +package cn.iocoder.mall.managementweb.controller.admin; + +import cn.iocoder.common.framework.util.HttpUtil; +import cn.iocoder.common.framework.vo.CommonResult; +import cn.iocoder.mall.managementweb.controller.admin.dto.AdminCreateDTO; +import cn.iocoder.mall.managementweb.controller.admin.dto.AdminUpdateInfoDTO; +import cn.iocoder.mall.managementweb.controller.admin.dto.AdminUpdateStatusDTO; +import cn.iocoder.mall.managementweb.manager.admin.AdminManager; +import cn.iocoder.mall.security.admin.core.context.AdminSecurityContextHolder; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.servlet.http.HttpServletRequest; + +import static cn.iocoder.common.framework.vo.CommonResult.success; + +@Api("管理员 API") +@RestController +@RequestMapping("/admin") +public class AdminController { + + @Autowired + private AdminManager adminManager; + + // =========== 管理员管理 API =========== +// @GetMapping("/page") +// @RequiresPermissions("system.admin.page") +// @ApiOperation(value = "管理员分页") +// public CommonResult> page(AdminPageDTO adminPageDTO) { +// PageResult page = adminService.getAdminPage(adminPageDTO); +// PageResult resultPage = AdminConvert.INSTANCE.convertAdminVOPage(page); +// // 拼接结果 +// if (!resultPage.getList().isEmpty()) { +// // 查询角色数组 +// Map> roleMap = adminService.getAdminRolesMap(CollectionUtil.convertList(resultPage.getList(), AdminBO::getId)); +// resultPage.getList().forEach(admin -> admin.setRoles(AdminConvert.INSTANCE.convertAdminVORoleList(roleMap.get(admin.getId())))); +// +// // 查询对应部门 +// List deptmentBOS = deptmentService.getAllDeptments(); +// Map deptNameMap = deptmentBOS.stream().collect(Collectors.toMap(d->d.getId(), d->d.getName())); +// //管理员所在部门被删后,变成未分配状态 +// deptNameMap.put(0, "未分配"); +// resultPage.getList().forEach(admin->{ +// admin.setDeptment(new AdminVO.Deptment(admin.getDeptmentId(), deptNameMap.get(admin.getDeptmentId()))); +// }); +// } +// return success(resultPage); +// } + + @ApiOperation(value = "创建管理员") + @PostMapping("/create") + public CommonResult createAdmin(AdminCreateDTO createDTO, HttpServletRequest request) { + return success(adminManager.createAdmin(createDTO, AdminSecurityContextHolder.getAdminId(), HttpUtil.getIp(request))); + } + + @PostMapping("/update") + @ApiOperation(value = "更新管理员") + public CommonResult updateAdmin(AdminUpdateInfoDTO updateInfoDTO) { + adminManager.updateAdmin(updateInfoDTO); + return success(true); + } + + @PostMapping("/update_status") + @ApiOperation(value = "更新管理员状态") + public CommonResult updateUserStatus(AdminUpdateStatusDTO updateStatusDTO) { + adminManager.updateAdminStatus(updateStatusDTO); + return success(true); + } + +} diff --git a/system/system-service-api/src/main/java/cn/iocoder/mall/system/api/dto/admin/AdminAddDTO.java b/management-web-app/src/main/java/cn/iocoder/mall/managementweb/controller/admin/dto/AdminCreateDTO.java similarity index 81% rename from system/system-service-api/src/main/java/cn/iocoder/mall/system/api/dto/admin/AdminAddDTO.java rename to management-web-app/src/main/java/cn/iocoder/mall/managementweb/controller/admin/dto/AdminCreateDTO.java index 1288e16af..69aeb5be2 100644 --- a/system/system-service-api/src/main/java/cn/iocoder/mall/system/api/dto/admin/AdminAddDTO.java +++ b/management-web-app/src/main/java/cn/iocoder/mall/managementweb/controller/admin/dto/AdminCreateDTO.java @@ -1,4 +1,4 @@ -package cn.iocoder.mall.system.api.dto.admin; +package cn.iocoder.mall.managementweb.controller.admin.dto; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; @@ -9,12 +9,20 @@ import org.hibernate.validator.constraints.Length; import javax.validation.constraints.NotEmpty; import javax.validation.constraints.NotNull; import javax.validation.constraints.Pattern; -import java.io.Serializable; -@ApiModel("管理员添加 DTO") +@ApiModel("管理员创建 DTO") @Data @Accessors(chain = true) -public class AdminAddDTO implements Serializable { +public class AdminCreateDTO { + + @ApiModelProperty(value = "昵称", required = true, example = "小王") + @NotEmpty(message = "昵称不能为空") + @Length(max = 10, message = "昵称长度最大为 10 位") + private String nickname; + + @ApiModelProperty(value = "部门编号", required = true, example = "1") + @NotNull(message = "部门不能为空") + private Integer departmentId; @ApiModelProperty(value = "登陆账号", required = true, example = "15601691300") @NotEmpty(message = "登陆账号不能为空") @@ -22,18 +30,9 @@ public class AdminAddDTO implements Serializable { @Pattern(regexp = "^[A-Za-z0-9]+$", message = "账号格式为数字以及字母") private String username; - @ApiModelProperty(value = "昵称", required = true, example = "小王") - @NotEmpty(message = "昵称不能为空") - @Length(max = 10, message = "昵称长度最大为 10 位") - private String nickname; - @ApiModelProperty(value = "密码", required = true, example = "buzhidao") @NotEmpty(message = "密码不能为空") @Length(min = 4, max = 16, message = "密码长度为 4-16 位") private String password; - @ApiModelProperty(value = "部门ID", required = true, example = "1") - @NotNull(message = "部门不能为空") - private Integer deptmentId; - } diff --git a/system/system-rest/src/main/java/cn/iocoder/mall/system/rest/request/admin/AdminsAdminPageRequest.java b/management-web-app/src/main/java/cn/iocoder/mall/managementweb/controller/admin/dto/AdminPageDTO.java similarity index 73% rename from system/system-rest/src/main/java/cn/iocoder/mall/system/rest/request/admin/AdminsAdminPageRequest.java rename to management-web-app/src/main/java/cn/iocoder/mall/managementweb/controller/admin/dto/AdminPageDTO.java index d89773056..b921afbd0 100644 --- a/system/system-rest/src/main/java/cn/iocoder/mall/system/rest/request/admin/AdminsAdminPageRequest.java +++ b/management-web-app/src/main/java/cn/iocoder/mall/managementweb/controller/admin/dto/AdminPageDTO.java @@ -1,4 +1,4 @@ -package cn.iocoder.mall.system.rest.request.admin; +package cn.iocoder.mall.managementweb.controller.admin.dto; import cn.iocoder.common.framework.vo.PageParam; import io.swagger.annotations.ApiModel; @@ -7,11 +7,11 @@ import lombok.Data; import lombok.EqualsAndHashCode; import lombok.experimental.Accessors; -@ApiModel("管理员 - 管理员模块 - 管理员分页信息 Request") +@ApiModel("管理员分页查询 DTO") @Data @EqualsAndHashCode(callSuper = true) @Accessors(chain = true) -public class AdminsAdminPageRequest extends PageParam { +public class AdminPageDTO extends PageParam { @ApiModelProperty(value = "真实名字,模糊匹配", example = "小王") private String name; diff --git a/system/system-service-api/src/main/java/cn/iocoder/mall/system/api/dto/admin/AdminUpdateDTO.java b/management-web-app/src/main/java/cn/iocoder/mall/managementweb/controller/admin/dto/AdminUpdateInfoDTO.java similarity index 76% rename from system/system-service-api/src/main/java/cn/iocoder/mall/system/api/dto/admin/AdminUpdateDTO.java rename to management-web-app/src/main/java/cn/iocoder/mall/managementweb/controller/admin/dto/AdminUpdateInfoDTO.java index b55dde4f8..287083838 100644 --- a/system/system-service-api/src/main/java/cn/iocoder/mall/system/api/dto/admin/AdminUpdateDTO.java +++ b/management-web-app/src/main/java/cn/iocoder/mall/managementweb/controller/admin/dto/AdminUpdateInfoDTO.java @@ -1,4 +1,4 @@ -package cn.iocoder.mall.system.api.dto.admin; +package cn.iocoder.mall.managementweb.controller.admin.dto; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; @@ -9,12 +9,11 @@ import org.hibernate.validator.constraints.Length; import javax.validation.constraints.NotEmpty; import javax.validation.constraints.NotNull; import javax.validation.constraints.Pattern; -import java.io.Serializable; -@ApiModel("管理员更新 DTO") +@ApiModel("管理员更新信息 DTO") @Data @Accessors(chain = true) -public class AdminUpdateDTO implements Serializable { +public class AdminUpdateInfoDTO { @ApiModelProperty(value = "管理员编号", required = true, example = "1") @NotNull(message = "管理员编号不能为空") @@ -26,17 +25,18 @@ public class AdminUpdateDTO implements Serializable { @Pattern(regexp = "^[A-Za-z0-9]+$", message = "账号格式为数字以及字母") private String username; + @ApiModelProperty(value = "密码", required = true, example = "buzhidao") + @NotEmpty(message = "密码不能为空") + @Length(min = 4, max = 16, message = "密码长度为 4-16 位") + private String password; + @ApiModelProperty(value = "昵称", required = true, example = "小王") @NotEmpty(message = "昵称不能为空") @Length(max = 10, message = "昵称长度最大为 10 位") private String nickname; - @ApiModelProperty(value = "密码", example = "buzhidao") - @Length(min = 4, max = 16, message = "密码长度为 4-16 位") - private String password; - - @ApiModelProperty(value = "部门ID", required = true, example = "1") + @ApiModelProperty(value = "部门编号", required = true, example = "1") @NotNull(message = "部门不能为空") - private Integer deptmentId; + private Integer departmentId; } diff --git a/system/system-service-api/src/main/java/cn/iocoder/mall/system/api/dto/admin/AdminUpdateStatusDTO.java b/management-web-app/src/main/java/cn/iocoder/mall/managementweb/controller/admin/dto/AdminUpdateStatusDTO.java similarity index 93% rename from system/system-service-api/src/main/java/cn/iocoder/mall/system/api/dto/admin/AdminUpdateStatusDTO.java rename to management-web-app/src/main/java/cn/iocoder/mall/managementweb/controller/admin/dto/AdminUpdateStatusDTO.java index 0643e9d0a..a447d0492 100644 --- a/system/system-service-api/src/main/java/cn/iocoder/mall/system/api/dto/admin/AdminUpdateStatusDTO.java +++ b/management-web-app/src/main/java/cn/iocoder/mall/managementweb/controller/admin/dto/AdminUpdateStatusDTO.java @@ -1,4 +1,4 @@ -package cn.iocoder.mall.system.api.dto.admin; +package cn.iocoder.mall.managementweb.controller.admin.dto; import cn.iocoder.common.framework.enums.CommonStatusEnum; import cn.iocoder.common.framework.validator.InEnum; diff --git a/system/system-rest/src/main/java/cn/iocoder/mall/system/rest/response/admin/AdminsAdminPageResponse.java b/management-web-app/src/main/java/cn/iocoder/mall/managementweb/controller/admin/vo/AdminsAdminPageResponse.java similarity index 97% rename from system/system-rest/src/main/java/cn/iocoder/mall/system/rest/response/admin/AdminsAdminPageResponse.java rename to management-web-app/src/main/java/cn/iocoder/mall/managementweb/controller/admin/vo/AdminsAdminPageResponse.java index 978287942..e5fbf5bca 100644 --- a/system/system-rest/src/main/java/cn/iocoder/mall/system/rest/response/admin/AdminsAdminPageResponse.java +++ b/management-web-app/src/main/java/cn/iocoder/mall/managementweb/controller/admin/vo/AdminsAdminPageResponse.java @@ -1,4 +1,4 @@ -package cn.iocoder.mall.system.rest.response.admin; +package cn.iocoder.mall.managementweb.controller.admin.vo; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; diff --git a/system/system-rest/src/main/java/cn/iocoder/mall/system/rest/response/admin/AdminsUserPageResponse.java b/management-web-app/src/main/java/cn/iocoder/mall/managementweb/controller/admin/vo/AdminsUserPageResponse.java similarity index 94% rename from system/system-rest/src/main/java/cn/iocoder/mall/system/rest/response/admin/AdminsUserPageResponse.java rename to management-web-app/src/main/java/cn/iocoder/mall/managementweb/controller/admin/vo/AdminsUserPageResponse.java index 2e16e19ac..9aba8643b 100644 --- a/system/system-rest/src/main/java/cn/iocoder/mall/system/rest/response/admin/AdminsUserPageResponse.java +++ b/management-web-app/src/main/java/cn/iocoder/mall/managementweb/controller/admin/vo/AdminsUserPageResponse.java @@ -1,4 +1,4 @@ -package cn.iocoder.mall.system.rest.response.admin; +package cn.iocoder.mall.managementweb.controller.admin.vo; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; diff --git a/management-web-app/src/main/java/cn/iocoder/mall/managementweb/controller/passport/AdminPassportController.java b/management-web-app/src/main/java/cn/iocoder/mall/managementweb/controller/passport/AdminPassportController.java index 4abce09bc..5332a23fd 100644 --- a/management-web-app/src/main/java/cn/iocoder/mall/managementweb/controller/passport/AdminPassportController.java +++ b/management-web-app/src/main/java/cn/iocoder/mall/managementweb/controller/passport/AdminPassportController.java @@ -4,7 +4,7 @@ import cn.iocoder.common.framework.util.HttpUtil; import cn.iocoder.common.framework.vo.CommonResult; import cn.iocoder.mall.managementweb.controller.passport.dto.AdminPassportLoginDTO; import cn.iocoder.mall.managementweb.controller.passport.vo.AdminPassportVO; -import cn.iocoder.mall.managementweb.manager.admin.AdminPassportManager; +import cn.iocoder.mall.managementweb.manager.passport.AdminPassportManager; import cn.iocoder.security.annotations.RequiresNone; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; diff --git a/system/system-rest/src/main/java/cn/iocoder/mall/system/rest/request/admin/AdminsUserPageRequest.java b/management-web-app/src/main/java/cn/iocoder/mall/managementweb/controller/user/dto/AdminsUserPageRequest.java similarity index 93% rename from system/system-rest/src/main/java/cn/iocoder/mall/system/rest/request/admin/AdminsUserPageRequest.java rename to management-web-app/src/main/java/cn/iocoder/mall/managementweb/controller/user/dto/AdminsUserPageRequest.java index 7f99f4bf9..1cf5388ae 100644 --- a/system/system-rest/src/main/java/cn/iocoder/mall/system/rest/request/admin/AdminsUserPageRequest.java +++ b/management-web-app/src/main/java/cn/iocoder/mall/managementweb/controller/user/dto/AdminsUserPageRequest.java @@ -1,4 +1,4 @@ -package cn.iocoder.mall.system.rest.request.admin; +package cn.iocoder.mall.managementweb.controller.user.dto; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; diff --git a/system/system-rest/src/main/java/cn/iocoder/mall/system/rest/request/admin/AdminsUserUpdateStatusRequest.java b/management-web-app/src/main/java/cn/iocoder/mall/managementweb/controller/user/dto/AdminsUserUpdateStatusRequest.java similarity index 92% rename from system/system-rest/src/main/java/cn/iocoder/mall/system/rest/request/admin/AdminsUserUpdateStatusRequest.java rename to management-web-app/src/main/java/cn/iocoder/mall/managementweb/controller/user/dto/AdminsUserUpdateStatusRequest.java index e6b31a495..72250de83 100644 --- a/system/system-rest/src/main/java/cn/iocoder/mall/system/rest/request/admin/AdminsUserUpdateStatusRequest.java +++ b/management-web-app/src/main/java/cn/iocoder/mall/managementweb/controller/user/dto/AdminsUserUpdateStatusRequest.java @@ -1,4 +1,4 @@ -package cn.iocoder.mall.system.rest.request.admin; +package cn.iocoder.mall.managementweb.controller.user.dto; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; @@ -24,4 +24,5 @@ public class AdminsUserUpdateStatusRequest { @ApiModelProperty(name = "status", value = "用户状态。1 - 开启;2 - 禁用", required = true, example = "1") @NotNull(message = "用户状态不能为空") private Integer status; + } diff --git a/management-web-app/src/main/java/cn/iocoder/mall/managementweb/manager/admin/AdminManager.java b/management-web-app/src/main/java/cn/iocoder/mall/managementweb/manager/admin/AdminManager.java new file mode 100644 index 000000000..29e11baa7 --- /dev/null +++ b/management-web-app/src/main/java/cn/iocoder/mall/managementweb/manager/admin/AdminManager.java @@ -0,0 +1,25 @@ +package cn.iocoder.mall.managementweb.manager.admin; + +import cn.iocoder.mall.managementweb.controller.admin.dto.AdminCreateDTO; +import cn.iocoder.mall.managementweb.controller.admin.dto.AdminUpdateInfoDTO; +import cn.iocoder.mall.managementweb.controller.admin.dto.AdminUpdateStatusDTO; +import org.springframework.stereotype.Service; + +@Service +public class AdminManager { + + //TODO 目前需要增加搜索所有子部门的用户 + + + public Integer createAdmin(AdminCreateDTO createDTO, Integer createAdminId, String createIp) { + return null; + } + + public void updateAdmin(AdminUpdateInfoDTO updateInfoDTO) { + } + + public void updateAdminStatus(AdminUpdateStatusDTO updateStatusDTO) { + + } + +} diff --git a/management-web-app/src/main/java/cn/iocoder/mall/managementweb/manager/admin/AdminPassportManager.java b/management-web-app/src/main/java/cn/iocoder/mall/managementweb/manager/passport/AdminPassportManager.java similarity index 97% rename from management-web-app/src/main/java/cn/iocoder/mall/managementweb/manager/admin/AdminPassportManager.java rename to management-web-app/src/main/java/cn/iocoder/mall/managementweb/manager/passport/AdminPassportManager.java index 35df88de6..12df450c9 100644 --- a/management-web-app/src/main/java/cn/iocoder/mall/managementweb/manager/admin/AdminPassportManager.java +++ b/management-web-app/src/main/java/cn/iocoder/mall/managementweb/manager/passport/AdminPassportManager.java @@ -1,4 +1,4 @@ -package cn.iocoder.mall.managementweb.manager.admin; +package cn.iocoder.mall.managementweb.manager.passport; import cn.iocoder.common.framework.enums.UserTypeEnum; import cn.iocoder.common.framework.vo.CommonResult; diff --git a/system-service-project/system-service-api/src/main/java/cn/iocoder/mall/systemservice/enums/SystemErrorCodeEnum.java b/system-service-project/system-service-api/src/main/java/cn/iocoder/mall/systemservice/enums/SystemErrorCodeEnum.java index 898bdbaf9..63e1d1cb2 100644 --- a/system-service-project/system-service-api/src/main/java/cn/iocoder/mall/systemservice/enums/SystemErrorCodeEnum.java +++ b/system-service-project/system-service-api/src/main/java/cn/iocoder/mall/systemservice/enums/SystemErrorCodeEnum.java @@ -26,12 +26,12 @@ public enum SystemErrorCodeEnum implements ServiceExceptionUtil.Enumerable verifyPassword(AdminVerifyPasswordDTO verifyPasswordDTO); + CommonResult createAdmin(AdminCreateDTO createDTO); + + CommonResult updateAdmin(AdminUpdateDTO updateDTO); + + CommonResult> pageAdmin(AdminPageDTO pageDTO); + } diff --git a/system-service-project/system-service-api/src/main/java/cn/iocoder/mall/systemservice/rpc/admin/dto/AdminCreateDTO.java b/system-service-project/system-service-api/src/main/java/cn/iocoder/mall/systemservice/rpc/admin/dto/AdminCreateDTO.java new file mode 100644 index 000000000..d934a47e1 --- /dev/null +++ b/system-service-project/system-service-api/src/main/java/cn/iocoder/mall/systemservice/rpc/admin/dto/AdminCreateDTO.java @@ -0,0 +1,57 @@ +package cn.iocoder.mall.systemservice.rpc.admin.dto; + +import lombok.Data; +import lombok.experimental.Accessors; +import org.hibernate.validator.constraints.Length; + +import javax.validation.constraints.NotEmpty; +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Pattern; +import java.io.Serializable; +import java.util.Date; + +/** + * 管理员添加 BO + */ +@Data +@Accessors(chain = true) +public class AdminCreateDTO implements Serializable { + + /** + * 昵称 + */ + @NotEmpty(message = "昵称不能为空") + @Length(max = 10, message = "昵称长度最大为 10 位") + private String nickname; + /** + * 部门编号 + */ + @NotNull(message = "部门不能为空") + private Integer departmentId; + + /** + * 登陆账号 + */ + @NotEmpty(message = "登陆账号不能为空") + @Length(min = 5, max = 16, message = "账号长度为 5-16 位") + @Pattern(regexp = "^[A-Za-z0-9]+$", message = "账号格式为数字以及字母") + private String username; + /** + * 密码 + */ + @NotEmpty(message = "密码不能为空") + @Length(min = 4, max = 16, message = "密码长度为 4-16 位") + private String password; + + /** + * 创建管理员编号 + */ + @NotNull(message = "创建管理员编号不能为空") + private String createAdminId; + /** + * 创建 IP + */ + @NotNull(message = "创建 IP 不能为空") + private Date createIp; + +} diff --git a/system-service-project/system-service-api/src/main/java/cn/iocoder/mall/systemservice/rpc/admin/dto/AdminPageDTO.java b/system-service-project/system-service-api/src/main/java/cn/iocoder/mall/systemservice/rpc/admin/dto/AdminPageDTO.java new file mode 100644 index 000000000..9b636b203 --- /dev/null +++ b/system-service-project/system-service-api/src/main/java/cn/iocoder/mall/systemservice/rpc/admin/dto/AdminPageDTO.java @@ -0,0 +1,22 @@ +package cn.iocoder.mall.systemservice.rpc.admin.dto; + +import cn.iocoder.common.framework.vo.PageParam; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.experimental.Accessors; + +@ApiModel("管理员分页查询 DTO") +@Data +@EqualsAndHashCode(callSuper = true) +@Accessors(chain = true) +public class AdminPageDTO extends PageParam { + + @ApiModelProperty(value = "真实名字,模糊匹配", example = "小王") + private String name; + + @ApiModelProperty(value = "部门编号") + private Integer departmentId; + +} diff --git a/system-service-project/system-service-api/src/main/java/cn/iocoder/mall/systemservice/rpc/admin/dto/AdminUpdateDTO.java b/system-service-project/system-service-api/src/main/java/cn/iocoder/mall/systemservice/rpc/admin/dto/AdminUpdateDTO.java new file mode 100644 index 000000000..3c7384627 --- /dev/null +++ b/system-service-project/system-service-api/src/main/java/cn/iocoder/mall/systemservice/rpc/admin/dto/AdminUpdateDTO.java @@ -0,0 +1,55 @@ +package cn.iocoder.mall.systemservice.rpc.admin.dto; + +import cn.iocoder.common.framework.enums.CommonStatusEnum; +import cn.iocoder.common.framework.validator.InEnum; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import lombok.experimental.Accessors; +import org.hibernate.validator.constraints.Length; + +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Pattern; +import java.io.Serializable; + +/** + * 管理员修改 DTO + */ +@Data +@Accessors(chain = true) +public class AdminUpdateDTO implements Serializable { + + /** + * 管理员编号 + */ + @NotNull(message = "管理员编号不能为空") + private Integer id; + /** + * 昵称 + */ + @ApiModelProperty(value = "昵称", required = true, example = "小王") + @Length(max = 10, message = "昵称长度最大为 10 位") + private String nickname; + /** + * 部门编号 + */ + @ApiModelProperty(value = "部门编号", required = true, example = "1") + private Integer departmentId; + /** + * 状态 + */ + @InEnum(value = CommonStatusEnum.class, message = "修改状态必须是 {value}") + private Integer status; + + /** + * 登录账号 + */ + @Pattern(regexp = "^[A-Za-z0-9]+$", message = "账号格式为数字以及字母") + private String username; + /** + * 密码 + */ + @ApiModelProperty(value = "密码", required = true, example = "buzhidao") + @Length(min = 4, max = 16, message = "密码长度为 4-16 位") + private String password; + +} diff --git a/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/convert/admin/AdminConvert.java b/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/convert/admin/AdminConvert.java index db880169f..981946950 100644 --- a/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/convert/admin/AdminConvert.java +++ b/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/convert/admin/AdminConvert.java @@ -2,8 +2,14 @@ package cn.iocoder.mall.systemservice.convert.admin; import cn.iocoder.common.framework.vo.PageResult; import cn.iocoder.mall.systemservice.dal.mysql.dataobject.admin.AdminDO; +import cn.iocoder.mall.systemservice.rpc.admin.dto.AdminCreateDTO; +import cn.iocoder.mall.systemservice.rpc.admin.dto.AdminPageDTO; +import cn.iocoder.mall.systemservice.rpc.admin.dto.AdminUpdateDTO; import cn.iocoder.mall.systemservice.rpc.admin.vo.AdminVO; import cn.iocoder.mall.systemservice.service.admin.bo.AdminBO; +import cn.iocoder.mall.systemservice.service.admin.bo.AdminCreateBO; +import cn.iocoder.mall.systemservice.service.admin.bo.AdminPageBO; +import cn.iocoder.mall.systemservice.service.admin.bo.AdminUpdateBO; import com.baomidou.mybatisplus.core.metadata.IPage; import org.mapstruct.Mapper; import org.mapstruct.Mapping; @@ -16,9 +22,21 @@ public interface AdminConvert { AdminBO convert(AdminDO bean); - @Mapping(source = "records", target = "list") - PageResult convertPage(IPage bean); + AdminVO convert(AdminBO bean); - AdminVO convert(AdminBO adminBO); + AdminDO convert(AdminCreateBO bean); + + AdminDO convert(AdminUpdateBO bean); + + AdminCreateBO convert(AdminCreateDTO bean); + + AdminUpdateBO convert(AdminUpdateDTO bean); + + @Mapping(source = "records", target = "list") + PageResult convertPage(IPage page); + + AdminPageBO convert(AdminPageDTO page); + + PageResult convert(PageResult adminPage); } diff --git a/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/dal/mysql/dataobject/admin/AdminDO.java b/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/dal/mysql/dataobject/admin/AdminDO.java index 5c4d232f0..b56f31a9c 100644 --- a/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/dal/mysql/dataobject/admin/AdminDO.java +++ b/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/dal/mysql/dataobject/admin/AdminDO.java @@ -7,8 +7,12 @@ import lombok.Data; import lombok.EqualsAndHashCode; import lombok.experimental.Accessors; +import java.util.Date; + /** * 管理员实体 + * + * uk_username 索引:基于 {@link #username} 字段 */ @TableName(value = "admin") @Data @@ -50,4 +54,13 @@ public class AdminDO extends BaseDO { */ private String passwordSalt; + /** + * 创建管理员编号 + */ + private String createAdminId; + /** + * 创建 IP + */ + private Date createIp; + } diff --git a/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/dal/mysql/mapper/admin/AdminMapper.java b/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/dal/mysql/mapper/admin/AdminMapper.java index 6cc5588a1..a7337e955 100644 --- a/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/dal/mysql/mapper/admin/AdminMapper.java +++ b/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/dal/mysql/mapper/admin/AdminMapper.java @@ -1,8 +1,12 @@ package cn.iocoder.mall.systemservice.dal.mysql.mapper.admin; +import cn.iocoder.mall.mybatis.query.QueryWrapperX; import cn.iocoder.mall.systemservice.dal.mysql.dataobject.admin.AdminDO; +import cn.iocoder.mall.systemservice.service.admin.bo.AdminPageBO; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import org.springframework.stereotype.Repository; @Repository @@ -14,10 +18,10 @@ public interface AdminMapper extends BaseMapper { ); } -// default IPage selectPage(AdminPageDTO adminPageDTO) { -// return selectPage(new Page<>(adminPageDTO.getPageNo(), adminPageDTO.getPageSize()), -// new QueryWrapperX().likeIfPresent("name", adminPageDTO.getName()) -// .eqIfPresent("department_id", adminPageDTO.getDepartmentId())); -// } + default IPage selectPage(AdminPageBO adminPageBO) { + return selectPage(new Page<>(adminPageBO.getPageNo(), adminPageBO.getPageSize()), + new QueryWrapperX().likeIfPresent("name", adminPageBO.getName()) + .eqIfPresent("department_id", adminPageBO.getDepartmentId())); + } } diff --git a/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/manager/admin/AdminManager.java b/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/manager/admin/AdminManager.java index 025b5c495..484c20290 100644 --- a/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/manager/admin/AdminManager.java +++ b/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/manager/admin/AdminManager.java @@ -1,18 +1,29 @@ package cn.iocoder.mall.systemservice.manager.admin; +import cn.iocoder.common.framework.enums.UserTypeEnum; +import cn.iocoder.common.framework.util.StringUtils; +import cn.iocoder.common.framework.vo.PageResult; import cn.iocoder.mall.systemservice.convert.admin.AdminConvert; +import cn.iocoder.mall.systemservice.enums.admin.AdminStatusEnum; +import cn.iocoder.mall.systemservice.rpc.admin.dto.AdminCreateDTO; +import cn.iocoder.mall.systemservice.rpc.admin.dto.AdminPageDTO; +import cn.iocoder.mall.systemservice.rpc.admin.dto.AdminUpdateDTO; import cn.iocoder.mall.systemservice.rpc.admin.dto.AdminVerifyPasswordDTO; import cn.iocoder.mall.systemservice.rpc.admin.vo.AdminVO; import cn.iocoder.mall.systemservice.service.admin.AdminService; import cn.iocoder.mall.systemservice.service.admin.bo.AdminBO; +import cn.iocoder.mall.systemservice.service.oauth.OAuth2Service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; @Service public class AdminManager { @Autowired private AdminService adminService; + @Autowired + private OAuth2Service oauth2Service; public AdminVO verifyPassword(AdminVerifyPasswordDTO verifyPasswordDTO) { AdminBO adminBO = adminService.verifyPassword(verifyPasswordDTO.getUsername(), @@ -20,4 +31,25 @@ public class AdminManager { return AdminConvert.INSTANCE.convert(adminBO); } + public AdminVO createAdmin(AdminCreateDTO createDTO) { + AdminBO adminBO = adminService.createAdmin(AdminConvert.INSTANCE.convert(createDTO)); + return AdminConvert.INSTANCE.convert(adminBO); + } + + @Transactional + public void updateAdmin(AdminUpdateDTO updateDTO) { + // 更新管理员信息 + adminService.updateAdmin(AdminConvert.INSTANCE.convert(updateDTO)); + // 如果修改密码,或者禁用管理员 + if (StringUtils.hasText(updateDTO.getPassword()) + || AdminStatusEnum.INACTIVE.getStatus().equals(updateDTO.getStatus())) { + oauth2Service.removeToken(updateDTO.getId(), UserTypeEnum.ADMIN.getValue()); + } + } + + public PageResult pageAdmin(AdminPageDTO pageDTO) { + PageResult adminPage = adminService.pageAdmin(AdminConvert.INSTANCE.convert(pageDTO)); + return AdminConvert.INSTANCE.convert(adminPage); + } + } diff --git a/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/rpc/admin/AdminRpcImpl.java b/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/rpc/admin/AdminRpcImpl.java index 3360d8417..24f7be311 100644 --- a/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/rpc/admin/AdminRpcImpl.java +++ b/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/rpc/admin/AdminRpcImpl.java @@ -1,7 +1,11 @@ package cn.iocoder.mall.systemservice.rpc.admin; import cn.iocoder.common.framework.vo.CommonResult; +import cn.iocoder.common.framework.vo.PageResult; import cn.iocoder.mall.systemservice.manager.admin.AdminManager; +import cn.iocoder.mall.systemservice.rpc.admin.dto.AdminCreateDTO; +import cn.iocoder.mall.systemservice.rpc.admin.dto.AdminPageDTO; +import cn.iocoder.mall.systemservice.rpc.admin.dto.AdminUpdateDTO; import cn.iocoder.mall.systemservice.rpc.admin.dto.AdminVerifyPasswordDTO; import cn.iocoder.mall.systemservice.rpc.admin.vo.AdminVO; import org.apache.dubbo.config.annotation.Service; @@ -20,4 +24,21 @@ public class AdminRpcImpl implements AdminRpc { return success(adminManager.verifyPassword(verifyPasswordDTO)); } + @Override + public CommonResult createAdmin(AdminCreateDTO createDTO) { + AdminVO adminVO = adminManager.createAdmin(createDTO); + return success(adminVO.getId()); + } + + @Override + public CommonResult updateAdmin(AdminUpdateDTO updateDTO) { + adminManager.updateAdmin(updateDTO); + return success(true); + } + + @Override + public CommonResult> pageAdmin(AdminPageDTO pageDTO) { + return success(adminManager.pageAdmin(pageDTO)); + } + } diff --git a/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/service/admin/AdminService.java b/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/service/admin/AdminService.java index a7325c94a..151e09e2d 100644 --- a/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/service/admin/AdminService.java +++ b/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/service/admin/AdminService.java @@ -1,16 +1,24 @@ package cn.iocoder.mall.systemservice.service.admin; +import cn.iocoder.common.framework.enums.CommonStatusEnum; import cn.iocoder.common.framework.util.DigestUtils; import cn.iocoder.common.framework.util.ServiceExceptionUtil; +import cn.iocoder.common.framework.util.StringUtils; +import cn.iocoder.common.framework.vo.PageResult; import cn.iocoder.mall.systemservice.convert.admin.AdminConvert; import cn.iocoder.mall.systemservice.dal.mysql.dataobject.admin.AdminDO; import cn.iocoder.mall.systemservice.dal.mysql.mapper.admin.AdminMapper; -import cn.iocoder.mall.systemservice.enums.SystemErrorCodeEnum; import cn.iocoder.mall.systemservice.enums.admin.AdminStatusEnum; +import cn.iocoder.mall.systemservice.enums.admin.AdminUsernameEnum; import cn.iocoder.mall.systemservice.service.admin.bo.AdminBO; +import cn.iocoder.mall.systemservice.service.admin.bo.AdminCreateBO; +import cn.iocoder.mall.systemservice.service.admin.bo.AdminPageBO; +import cn.iocoder.mall.systemservice.service.admin.bo.AdminUpdateBO; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import static cn.iocoder.mall.systemservice.enums.SystemErrorCodeEnum.*; + @Service public class AdminService { @@ -20,24 +28,143 @@ public class AdminService { public AdminBO verifyPassword(String username, String password, String ip) { AdminDO adminDO = adminMapper.selectByUsername(username); if (adminDO == null) { - throw ServiceExceptionUtil.exception(SystemErrorCodeEnum.ADMIN_NOT_FOUND); + throw ServiceExceptionUtil.exception(ADMIN_NOT_FOUND); } // 校验密码是否正确 String encodedPassword = DigestUtils.bcrypt(password, adminDO.getPasswordSalt()); if (!encodedPassword.equals(adminDO.getPassword())) { // TODO 需要补充密码错误上限 - throw ServiceExceptionUtil.exception(SystemErrorCodeEnum.ADMIN_PASSWORD_ERROR); + throw ServiceExceptionUtil.exception(ADMIN_PASSWORD_ERROR); } // 账号被禁用 if (!AdminStatusEnum.ACTIVE.getStatus().equals(adminDO.getStatus())) { - throw ServiceExceptionUtil.exception(SystemErrorCodeEnum.ADMIN_IS_DISABLE); + throw ServiceExceptionUtil.exception(ADMIN_IS_DISABLE); } // 返回 return AdminConvert.INSTANCE.convert(adminDO); } -// public PageResult getAdminPage(AdminPageDTO pageDTO) { -// return AdminConvert.INSTANCE.convertPage(adminMapper.selectPage(pageDTO)); + public PageResult pageAdmin(AdminPageBO adminPageBO) { + return AdminConvert.INSTANCE.convertPage(adminMapper.selectPage(adminPageBO)); + } + + public AdminBO createAdmin(AdminCreateBO createBO) { + // 校验账号唯一 + if (adminMapper.selectByUsername(createBO.getUsername()) != null) { + throw ServiceExceptionUtil.exception(ADMIN_USERNAME_EXISTS); + } + // 加密密码 + String passwordSalt = genPasswordSalt(); + String password = encodePassword(createBO.getPassword(), passwordSalt); + // 保存到数据库 + AdminDO admin = AdminConvert.INSTANCE.convert(createBO) + .setPassword(password).setPasswordSalt(passwordSalt) + .setStatus(CommonStatusEnum.ENABLE.getValue()); + adminMapper.insert(admin); + // 返回成功 + return AdminConvert.INSTANCE.convert(admin); + } + + private String genPasswordSalt() { + return DigestUtils.genBcryptSalt(); + } + + private String encodePassword(String password, String salt) { + return DigestUtils.bcrypt(password, salt); + } + + public void updateAdmin(AdminUpdateBO updateDTO) { + // 校验账号存在 + AdminDO admin = adminMapper.selectById(updateDTO.getId()); + if (admin == null) { + throw ServiceExceptionUtil.exception(ADMIN_NOT_FOUND); + } + // 校验是否为特殊账号,不允许编辑 + if (AdminUsernameEnum.ADMIN.getUsername().equals(admin.getUsername()) + || AdminUsernameEnum.DEMO.getUsername().equals(admin.getUsername())) { + throw ServiceExceptionUtil.exception(ADMIN_ADMIN_CAN_NOT_UPDATE); + } + // 校验账号唯一 + if (StringUtils.hasText(updateDTO.getUsername())) { + AdminDO usernameAdmin = adminMapper.selectByUsername(updateDTO.getUsername()); + if (usernameAdmin != null && !usernameAdmin.getId().equals(updateDTO.getId())) { + throw ServiceExceptionUtil.exception(ADMIN_USERNAME_EXISTS); + } + } + // 如果有更新状态,则校验是否已经是该状态 + if (updateDTO.getStatus() != null && updateDTO.getStatus().equals(admin.getStatus())) { + throw ServiceExceptionUtil.exception(ADMIN_STATUS_EQUALS); + } + // 更新到数据库 + AdminDO updateAdmin = AdminConvert.INSTANCE.convert(updateDTO); + // 如果更新密码,需要特殊加密 + if (StringUtils.hasText(updateDTO.getPassword())) { + String passwordSalt = genPasswordSalt(); + String password = encodePassword(updateDTO.getPassword(), passwordSalt); + updateAdmin.setPassword(password).setPasswordSalt(passwordSalt); + } + adminMapper.updateById(updateAdmin); + } + +// +// @Override +// public Map> getAdminRolesMap(Collection adminIds) { +// // 查询管理员拥有的角色关联数据 +// List adminRoleList = adminRoleMapper.selectListByAdminIds(adminIds); +// if (adminRoleList.isEmpty()) { +// return Collections.emptyMap(); +// } +// // 查询角色数据 +// List roleList = roleService.getRoleList(CollectionUtil.convertSet(adminRoleList, AdminRoleDO::getRoleId)); +// Map roleMap = CollectionUtil.convertMap(roleList, RoleBO::getId); +// // 拼接数据 +// Multimap result = ArrayListMultimap.create(); +// adminRoleList.forEach(adminRole -> result.put(adminRole.getAdminId(), roleMap.get(adminRole.getRoleId()))); +// return result.asMap(); +// } +// +// @Override +// public List getRoleList(Integer adminId) { +// // 查询管理员拥有的角色关联数据 +// List adminRoleList = adminRoleMapper.selectByAdminId(adminId); +// if (adminRoleList.isEmpty()) { +// return Collections.emptyList(); +// } +// // 查询角色数据 +// return roleService.getRoleList(CollectionUtil.convertSet(adminRoleList, AdminRoleDO::getRoleId)); +// } +// +// @Override +// @Transactional +// public Boolean assignAdminRole(Integer adminId, AdminAssignRoleDTO adminAssignRoleDTO) { +// // 校验账号存在 +// AdminDO admin = adminMapper.selectById(adminAssignRoleDTO.getId()); +// if (admin == null) { +// throw ServiceExceptionUtil.exception(AdminErrorCodeEnum.ADMIN_USERNAME_NOT_REGISTERED.getCode()); +// } +// // 校验是否有不存在的角色 +// if (!CollectionUtil.isEmpty(adminAssignRoleDTO.getRoleIds())) { +// List roles = roleService.getRoles(adminAssignRoleDTO.getRoleIds()); +// if (roles.size() != adminAssignRoleDTO.getRoleIds().size()) { +// throw ServiceExceptionUtil.exception(AdminErrorCodeEnum.ADMIN_ASSIGN_ROLE_NOT_EXISTS.getCode()); +// } +// } +// // TODO 芋艿,这里先简单实现。即方式是,删除老的分配的角色关系,然后添加新的分配的角色关系 +// // 标记管理员角色源关系都为删除 +// adminRoleMapper.deleteByAdminId(adminAssignRoleDTO.getId()); +// // 创建 RoleResourceDO 数组,并插入到数据库 +// if (!CollectionUtil.isEmpty(adminAssignRoleDTO.getRoleIds())) { +// List adminRoleDOs = adminAssignRoleDTO.getRoleIds().stream().map(roleId -> { +// AdminRoleDO roleResource = new AdminRoleDO().setAdminId(adminAssignRoleDTO.getId()).setRoleId(roleId); +// roleResource.setCreateTime(new Date()); +// roleResource.setDeleted(DeletedStatusEnum.DELETED_NO.getValue()); +// return roleResource; +// }).collect(Collectors.toList()); +// adminRoleMapper.insertList(adminRoleDOs); +// } +// // TODO 插入操作日志 +// // 返回成功 +// return true; // } } diff --git a/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/service/admin/bo/AdminCreateBO.java b/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/service/admin/bo/AdminCreateBO.java new file mode 100644 index 000000000..6bd7ffbb3 --- /dev/null +++ b/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/service/admin/bo/AdminCreateBO.java @@ -0,0 +1,56 @@ +package cn.iocoder.mall.systemservice.service.admin.bo; + +import lombok.Data; +import lombok.experimental.Accessors; +import org.hibernate.validator.constraints.Length; + +import javax.validation.constraints.NotEmpty; +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Pattern; +import java.util.Date; + +/** + * 管理员添加 BO + */ +@Data +@Accessors(chain = true) +public class AdminCreateBO { + + /** + * 昵称 + */ + @NotEmpty(message = "昵称不能为空") + @Length(max = 10, message = "昵称长度最大为 10 位") + private String nickname; + /** + * 部门编号 + */ + @NotNull(message = "部门不能为空") + private Integer departmentId; + + /** + * 登录账号 + */ + @NotEmpty(message = "登陆账号不能为空") + @Length(min = 5, max = 16, message = "账号长度为 5-16 位") + @Pattern(regexp = "^[A-Za-z0-9]+$", message = "账号格式为数字以及字母") + private String username; + /** + * 密码 + */ + @NotEmpty(message = "密码不能为空") + @Length(min = 4, max = 16, message = "密码长度为 4-16 位") + private String password; + + /** + * 创建管理员编号 + */ + @NotNull(message = "创建管理员编号不能为空") + private String createAdminId; + /** + * 创建 IP + */ + @NotNull(message = "创建 IP 不能为空") + private Date createIp; + +} diff --git a/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/service/admin/bo/AdminPageBO.java b/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/service/admin/bo/AdminPageBO.java new file mode 100644 index 000000000..f3face9cb --- /dev/null +++ b/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/service/admin/bo/AdminPageBO.java @@ -0,0 +1,24 @@ +package cn.iocoder.mall.systemservice.service.admin.bo; + +import cn.iocoder.common.framework.vo.PageParam; +import io.swagger.annotations.ApiModel; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.experimental.Accessors; + +@ApiModel("管理员分页查询 BO") +@Data +@EqualsAndHashCode(callSuper = true) +@Accessors(chain = true) +public class AdminPageBO extends PageParam { + + /** + * 真实名字,模糊匹配 + */ + private String name; + /** + * 部门编号 + */ + private Integer departmentId; + +} diff --git a/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/service/admin/bo/AdminUpdateBO.java b/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/service/admin/bo/AdminUpdateBO.java new file mode 100644 index 000000000..73358a867 --- /dev/null +++ b/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/service/admin/bo/AdminUpdateBO.java @@ -0,0 +1,52 @@ +package cn.iocoder.mall.systemservice.service.admin.bo; + +import cn.iocoder.common.framework.enums.CommonStatusEnum; +import cn.iocoder.common.framework.validator.InEnum; +import lombok.Data; +import lombok.experimental.Accessors; +import org.hibernate.validator.constraints.Length; + +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Pattern; + +/** + * 管理员修改 BO + */ +@Data +@Accessors(chain = true) +public class AdminUpdateBO { + + /** + * 管理员编号 + */ + @NotNull(message = "管理员编号不能为空") + private Integer id; + /** + * 昵称 + */ + @Length(max = 10, message = "昵称长度最大为 10 位") + private String nickname; + /** + * 部门编号 + */ + @NotNull(message = "部门不能为空") + private Integer departmentId; + /** + * 状态 + */ + @InEnum(value = CommonStatusEnum.class, message = "修改状态必须是 {value}") + private Integer status; + + /** + * 登录账号 + */ + @Length(min = 5, max = 16, message = "账号长度为 5-16 位") + @Pattern(regexp = "^[A-Za-z0-9]+$", message = "账号格式为数字以及字母") + private String username; + /** + * 密码 + */ + @Length(min = 4, max = 16, message = "密码长度为 4-16 位") + private String password; + +} diff --git a/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/service/oauth/OAuth2Service.java b/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/service/oauth/OAuth2Service.java index c60bf0bab..ed466ce09 100644 --- a/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/service/oauth/OAuth2Service.java +++ b/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/service/oauth/OAuth2Service.java @@ -80,6 +80,12 @@ public class OAuth2Service { return OAuth2Convert.INSTANCE.convert(oauth2AccessTokenDO); } + @Transactional + public void removeToken(Integer userId, Integer userType) { + oauth2AccessTokenMapper.deleteByUserIdAndUserType(userId, userType); + oauth2RefreshTokenMapper.deleteByUserIdAndUserType(userId, userType); + } + private OAuth2AccessTokenDO createOAuth2AccessToken(OAuth2RefreshTokenDO refreshTokenDO, String createIp) { OAuth2AccessTokenDO accessToken = new OAuth2AccessTokenDO() .setId(generateAccessToken()) diff --git a/system/system-biz-api/pom.xml b/system/system-biz-api/pom.xml deleted file mode 100644 index adb7183c8..000000000 --- a/system/system-biz-api/pom.xml +++ /dev/null @@ -1,23 +0,0 @@ - - - - system - cn.iocoder.mall - 1.0-SNAPSHOT - - 4.0.0 - - system-biz-api - - - - - cn.iocoder.mall - common-framework - 1.0-SNAPSHOT - - - - diff --git a/system/system-biz-api/src/main/java/cn/iocoder/mall/system/biz/package-info.java b/system/system-biz-api/src/main/java/cn/iocoder/mall/system/biz/package-info.java deleted file mode 100644 index 877d51d39..000000000 --- a/system/system-biz-api/src/main/java/cn/iocoder/mall/system/biz/package-info.java +++ /dev/null @@ -1,6 +0,0 @@ -/** - * 该项目,主要用于暴露一些共享的枚举类等。 - * - * 例如说,RPC 接口提供错误码给调用方 - */ -package cn.iocoder.mall.system.biz; diff --git a/system/system-service-api/src/main/java/cn/iocoder/mall/system/api/constant/AdminConstants.java b/system/system-service-api/src/main/java/cn/iocoder/mall/system/api/constant/AdminConstants.java deleted file mode 100644 index 3188b4df4..000000000 --- a/system/system-service-api/src/main/java/cn/iocoder/mall/system/api/constant/AdminConstants.java +++ /dev/null @@ -1,15 +0,0 @@ -package cn.iocoder.mall.system.api.constant; - -public class AdminConstants { - - /** - * 账号 - 管理员 - */ - public static final String USERNAME_ADMIN = "admin"; - - /** - * 账号 - 演示账号 - */ - public static final String USERNAME_DEMO = "yudaoyuanma"; - -} diff --git a/system/system-service-api/src/main/java/cn/iocoder/mall/system/api/dto/admin/AdminPageDTO.java b/system/system-service-api/src/main/java/cn/iocoder/mall/system/api/dto/admin/AdminPageDTO.java deleted file mode 100644 index 16fd757eb..000000000 --- a/system/system-service-api/src/main/java/cn/iocoder/mall/system/api/dto/admin/AdminPageDTO.java +++ /dev/null @@ -1,21 +0,0 @@ -package cn.iocoder.mall.system.api.dto.admin; - -import cn.iocoder.common.framework.vo.PageParam; -import io.swagger.annotations.ApiModel; -import io.swagger.annotations.ApiModelProperty; -import lombok.Data; -import lombok.experimental.Accessors; - -@ApiModel(value = "管理员分页 DTO") -@Data -@Accessors(chain = true) -public class AdminPageDTO extends PageParam { - - @ApiModelProperty(value = "昵称,模糊匹配", example = "小王") - private String nickname; - - - @ApiModelProperty(value = "所在部门ID") - private Integer deptmentId; - -} diff --git a/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/service/AdminServiceImpl.java b/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/service/AdminServiceImpl.java index b8e377c00..52658dc3b 100644 --- a/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/service/AdminServiceImpl.java +++ b/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/service/AdminServiceImpl.java @@ -43,161 +43,6 @@ public class AdminServiceImpl implements AdminService { @Autowired private RoleServiceImpl roleService; - @Override - public PageResult getAdminPage(AdminPageDTO adminPageDTO) { - IPage page = adminMapper.selectPage(adminPageDTO); - return AdminConvert.INSTANCE.convert(page); - } - @Override - public AdminBO addAdmin(Integer adminId, AdminAddDTO adminAddDTO) { - // 校验账号唯一 - if (adminMapper.selectByUsername(adminAddDTO.getUsername()) != null) { - throw ServiceExceptionUtil.exception(AdminErrorCodeEnum.ADMIN_USERNAME_EXISTS.getCode()); - } - // 保存到数据库 - AdminDO admin = AdminConvert.INSTANCE.convert(adminAddDTO) - .setPassword(encodePassword(adminAddDTO.getPassword())) // 加密密码 - .setStatus(CommonStatusEnum.ENABLE.getValue()); - admin.setCreateTime(new Date()); - admin.setDeleted(DeletedStatusEnum.DELETED_NO.getValue()); - adminMapper.insert(admin); - // TODO 插入操作日志 - // 返回成功 - return AdminConvert.INSTANCE.convert(admin); - } - - @Override - public Boolean updateAdmin(Integer adminId, AdminUpdateDTO adminUpdateDTO) { - // 校验账号存在 - AdminDO admin = adminMapper.selectById(adminUpdateDTO.getId()); - if (admin == null) { - throw ServiceExceptionUtil.exception(AdminErrorCodeEnum.ADMIN_USERNAME_NOT_REGISTERED.getCode()); - } - if (AdminConstants.USERNAME_ADMIN.equals(admin.getUsername()) - || AdminConstants.USERNAME_DEMO.equals(admin.getUsername())) { // 特殊账号,不允许编辑 - throw ServiceExceptionUtil.exception(AdminErrorCodeEnum.ADMIN_ADMIN_CAN_NOT_UPDATE.getCode()); - } - // 校验账号唯一 - AdminDO usernameAdmin = adminMapper.selectByUsername(adminUpdateDTO.getUsername()); - if (usernameAdmin != null && !usernameAdmin.getId().equals(adminUpdateDTO.getId())) { - throw ServiceExceptionUtil.exception(AdminErrorCodeEnum.ADMIN_USERNAME_EXISTS.getCode()); - } - // 更新到数据库 - AdminDO updateAdmin = AdminConvert.INSTANCE.convert(adminUpdateDTO); - adminMapper.updateById(updateAdmin); - // TODO 插入操作日志 - // 返回成功 - return true; - } - - @Override - @Transactional - public Boolean updateAdminStatus(Integer adminId, AdminUpdateStatusDTO adminUpdateStatusDTO) { - // 校验账号存在 - AdminDO admin = adminMapper.selectById(adminUpdateStatusDTO.getId()); - if (admin == null) { - throw ServiceExceptionUtil.exception(AdminErrorCodeEnum.ADMIN_USERNAME_NOT_REGISTERED.getCode()); - } - if (AdminConstants.USERNAME_ADMIN.equals(admin.getUsername()) - || AdminConstants.USERNAME_DEMO.equals(admin.getUsername())) { // 特殊账号,不允许编辑 - throw ServiceExceptionUtil.exception(AdminErrorCodeEnum.ADMIN_ADMIN_STATUS_CAN_NOT_UPDATE.getCode()); - } - // 如果状态相同,则返回错误 - if (adminUpdateStatusDTO.getStatus().equals(admin.getStatus())) { - throw ServiceExceptionUtil.exception(AdminErrorCodeEnum.ADMIN_STATUS_EQUALS.getCode()); - } - // 更新管理员状态 - AdminDO updateAdmin = new AdminDO().setId(adminUpdateStatusDTO.getId()).setStatus(adminUpdateStatusDTO.getStatus()); - adminMapper.updateById(updateAdmin); - // 如果是关闭管理员,则标记 token 失效。否则,管理员还可以继续蹦跶 - if (CommonStatusEnum.DISABLE.getValue().equals(adminUpdateStatusDTO.getStatus())) { - oauth2Service.removeToken(new OAuth2RemoveTokenByUserDTO().setUserId(adminId).setUserType(UserTypeEnum.ADMIN.getValue())); - } - // TODO 插入操作日志 - // 返回成功 - return true; - } - - @Override - @Transactional - public Boolean deleteAdmin(Integer adminId, Integer updateAdminId) { - // 校验账号存在 - AdminDO admin = adminMapper.selectById(updateAdminId); - if (admin == null) { - throw ServiceExceptionUtil.exception(AdminErrorCodeEnum.ADMIN_USERNAME_NOT_REGISTERED.getCode()); - } - // 只有禁用的账号才可以删除 - if (CommonStatusEnum.ENABLE.getValue().equals(admin.getStatus())) { - throw ServiceExceptionUtil.exception(AdminErrorCodeEnum.ADMIN_DELETE_ONLY_DISABLE.getCode()); - } - // 标记删除 AdminDO - adminMapper.deleteById(updateAdminId); // 标记删除 - // 标记删除 AdminRole - adminRoleMapper.deleteByAdminId(updateAdminId); - // TODO 插入操作日志 - // 返回成功 - return true; - } - - @Override - public Map> getAdminRolesMap(Collection adminIds) { - // 查询管理员拥有的角色关联数据 - List adminRoleList = adminRoleMapper.selectListByAdminIds(adminIds); - if (adminRoleList.isEmpty()) { - return Collections.emptyMap(); - } - // 查询角色数据 - List roleList = roleService.getRoleList(CollectionUtil.convertSet(adminRoleList, AdminRoleDO::getRoleId)); - Map roleMap = CollectionUtil.convertMap(roleList, RoleBO::getId); - // 拼接数据 - Multimap result = ArrayListMultimap.create(); - adminRoleList.forEach(adminRole -> result.put(adminRole.getAdminId(), roleMap.get(adminRole.getRoleId()))); - return result.asMap(); - } - - @Override - public List getRoleList(Integer adminId) { - // 查询管理员拥有的角色关联数据 - List adminRoleList = adminRoleMapper.selectByAdminId(adminId); - if (adminRoleList.isEmpty()) { - return Collections.emptyList(); - } - // 查询角色数据 - return roleService.getRoleList(CollectionUtil.convertSet(adminRoleList, AdminRoleDO::getRoleId)); - } - - @Override - @Transactional - public Boolean assignAdminRole(Integer adminId, AdminAssignRoleDTO adminAssignRoleDTO) { - // 校验账号存在 - AdminDO admin = adminMapper.selectById(adminAssignRoleDTO.getId()); - if (admin == null) { - throw ServiceExceptionUtil.exception(AdminErrorCodeEnum.ADMIN_USERNAME_NOT_REGISTERED.getCode()); - } - // 校验是否有不存在的角色 - if (!CollectionUtil.isEmpty(adminAssignRoleDTO.getRoleIds())) { - List roles = roleService.getRoles(adminAssignRoleDTO.getRoleIds()); - if (roles.size() != adminAssignRoleDTO.getRoleIds().size()) { - throw ServiceExceptionUtil.exception(AdminErrorCodeEnum.ADMIN_ASSIGN_ROLE_NOT_EXISTS.getCode()); - } - } - // TODO 芋艿,这里先简单实现。即方式是,删除老的分配的角色关系,然后添加新的分配的角色关系 - // 标记管理员角色源关系都为删除 - adminRoleMapper.deleteByAdminId(adminAssignRoleDTO.getId()); - // 创建 RoleResourceDO 数组,并插入到数据库 - if (!CollectionUtil.isEmpty(adminAssignRoleDTO.getRoleIds())) { - List adminRoleDOs = adminAssignRoleDTO.getRoleIds().stream().map(roleId -> { - AdminRoleDO roleResource = new AdminRoleDO().setAdminId(adminAssignRoleDTO.getId()).setRoleId(roleId); - roleResource.setCreateTime(new Date()); - roleResource.setDeleted(DeletedStatusEnum.DELETED_NO.getValue()); - return roleResource; - }).collect(Collectors.toList()); - adminRoleMapper.insertList(adminRoleDOs); - } - // TODO 插入操作日志 - // 返回成功 - return true; - } } diff --git a/system/system-start/src/main/java/cn/iocoder/mall/system/application/controller/admins/AdminController.java b/system/system-start/src/main/java/cn/iocoder/mall/system/application/controller/admins/AdminController.java index 3031cd9d9..7e28c3fa1 100644 --- a/system/system-start/src/main/java/cn/iocoder/mall/system/application/controller/admins/AdminController.java +++ b/system/system-start/src/main/java/cn/iocoder/mall/system/application/controller/admins/AdminController.java @@ -50,56 +50,6 @@ public class AdminController { @Autowired private DeptmentService deptmentService; - // =========== 管理员管理 API =========== - //TODO 目前需要增加搜索所有子部门的用户 - @GetMapping("/page") - @RequiresPermissions("system.admin.page") - @ApiOperation(value = "管理员分页") - public CommonResult> page(AdminPageDTO adminPageDTO) { - PageResult page = adminService.getAdminPage(adminPageDTO); - PageResult resultPage = AdminConvert.INSTANCE.convertAdminVOPage(page); - // 拼接结果 - if (!resultPage.getList().isEmpty()) { - // 查询角色数组 - Map> roleMap = adminService.getAdminRolesMap(CollectionUtil.convertList(resultPage.getList(), AdminBO::getId)); - resultPage.getList().forEach(admin -> admin.setRoles(AdminConvert.INSTANCE.convertAdminVORoleList(roleMap.get(admin.getId())))); - // 查询对应部门 - List deptmentBOS = deptmentService.getAllDeptments(); - Map deptNameMap = deptmentBOS.stream().collect(Collectors.toMap(d->d.getId(), d->d.getName())); - //管理员所在部门被删后,变成未分配状态 - deptNameMap.put(0, "未分配"); - resultPage.getList().forEach(admin->{ - admin.setDeptment(new AdminVO.Deptment(admin.getDeptmentId(), deptNameMap.get(admin.getDeptmentId()))); - }); - } - - return success(resultPage); - } - - @PostMapping("/add") - @ApiOperation(value = "创建管理员") - public CommonResult add(AdminAddDTO adminAddDTO) { - return success(adminService.addAdmin(AdminSecurityContextHolder.getContext().getAdminId(), adminAddDTO)); - } - - @PostMapping("/update") - @ApiOperation(value = "更新管理员") - public CommonResult update(AdminUpdateDTO adminUpdateDTO) { - return success(adminService.updateAdmin(AdminSecurityContextHolder.getContext().getAdminId(), adminUpdateDTO)); - } - - @PostMapping("/update_status") - @ApiOperation(value = "更新管理员状态") - public CommonResult updateStatus(AdminUpdateStatusDTO adminUpdateStatusDTO) { - return success(adminService.updateAdminStatus(AdminSecurityContextHolder.getContext().getAdminId(), adminUpdateStatusDTO)); - } - - @PostMapping("/delete") - @ApiOperation(value = "删除管理员") - @ApiImplicitParam(name = "id", value = "管理员编号", required = true, example = "1") - public CommonResult delete(@RequestParam("id") Integer id) { - return success(adminService.deleteAdmin(AdminSecurityContextHolder.getContext().getAdminId(), id)); - } }