refactor(org): 迁移账户模块至 company/account,重构公司拥有者创建账户逻辑并统一拥有者文案与注释

This commit is contained in:
小蟑螂
2026-05-25 18:13:53 +08:00
parent 2f44eaa9d2
commit d4bfa4d257
21 changed files with 156 additions and 53 deletions
@@ -29,7 +29,7 @@ public @interface RequireRole {
boolean allowSuper() default true; boolean allowSuper() default true;
/** /**
* 是否允许公司管理员访问(默认允许) * 是否允许公司拥有者访问(默认允许)
*/ */
boolean allowCompany() default true; boolean allowCompany() default true;
@@ -56,12 +56,12 @@ public class RoleAspect {
hasPermission = annotation.allowSuper(); hasPermission = annotation.allowSuper();
break; break;
case "user": case "user":
// 普通用户检查公司成员角色,如果是 owner 则视为公司管理员 // 普通用户检查公司成员角色,如果是 owner 则视为公司拥有者
if (annotation.allowCompany()) { if (annotation.allowCompany()) {
String companyRole = currentUserContext.getCurrentCompanyRole(); String companyRole = currentUserContext.getCurrentCompanyRole();
if ("owner".equals(companyRole)) { if ("owner".equals(companyRole)) {
hasPermission = true; hasPermission = true;
log.debug("角色权限验证 - 用户是公司拥有者,视为公司管理员"); log.debug("角色权限验证 - 用户是公司拥有者");
} else { } else {
hasPermission = annotation.allowUser(); hasPermission = annotation.allowUser();
} }
@@ -16,9 +16,11 @@ import top.xyquan.ecomdmbbackend.modules.org.account.model.vo.AccountVO;
import top.xyquan.ecomdmbbackend.modules.org.account.service.AccountService; import top.xyquan.ecomdmbbackend.modules.org.account.service.AccountService;
/** /**
* 账户管理控制器 * 公司账户管理控制器
* <p>
* 供公司拥有者管理本公司成员账户;需 org:company:manage 权限,超管可访问全部接口。
*/ */
@Tag(name = "组织-账户管理", description = "账户管理相关接口") @Tag(name = "组织-账户管理", description = "公司拥有者管理本公司账户")
@RestController @RestController
@RequestMapping("/company/account") @RequestMapping("/company/account")
public class AccountController { public class AccountController {
@@ -29,7 +31,8 @@ public class AccountController {
this.accountService = accountService; this.accountService = accountService;
} }
@Operation(summary = "创建账户(管理员)", description = "公司管理员创建本公司账户") /** 创建账户:手机号已注册则拉入公司,未注册则新建用户(密码必填) */
@Operation(summary = "创建账户(拥有者)", description = "公司拥有者创建本公司账户;手机号已存在时将用户拉入本公司,不存在则新建用户")
@PostMapping("/create") @PostMapping("/create")
@RequirePermission(value = "org:company:manage", description = "创建账户", allowSuper = true) @RequirePermission(value = "org:company:manage", description = "创建账户", allowSuper = true)
public ApiResponse<AccountVO> createAccountForCompany(@Valid @RequestBody CreateAccountDTO createAccountDTO) { public ApiResponse<AccountVO> createAccountForCompany(@Valid @RequestBody CreateAccountDTO createAccountDTO) {
@@ -37,7 +40,8 @@ public class AccountController {
return ApiResponse.success("账户创建成功", accountVO); return ApiResponse.success("账户创建成功", accountVO);
} }
@Operation(summary = "分页查询账户(管理员)", description = "公司管理员分页查询本公司账户") /** 分页查询当前公司成员对应的账户列表 */
@Operation(summary = "分页查询账户(拥有者)", description = "公司拥有者分页查询本公司账户")
@PostMapping("/list") @PostMapping("/list")
@RequirePermission(value = "org:company:manage", description = "查询账户列表", allowSuper = true) @RequirePermission(value = "org:company:manage", description = "查询账户列表", allowSuper = true)
public ApiResponse<PageResultVO<AccountVO>> getAccountPageForCompany(@Valid @RequestBody PageQueryDTO<AccountFilterDTO> pageQuery) { public ApiResponse<PageResultVO<AccountVO>> getAccountPageForCompany(@Valid @RequestBody PageQueryDTO<AccountFilterDTO> pageQuery) {
@@ -45,7 +49,8 @@ public class AccountController {
return ApiResponse.success("获取账户列表成功", pageResult); return ApiResponse.success("获取账户列表成功", pageResult);
} }
@Operation(summary = "更新账户信息", description = "公司管理员更新本公司账户信息") /** 更新账户基本信息及职位、部门关联 */
@Operation(summary = "更新账户信息", description = "公司拥有者更新本公司账户信息")
@PutMapping("/update/{accountId}") @PutMapping("/update/{accountId}")
@RequirePermission(value = "org:company:manage", description = "更新账户", allowSuper = true) @RequirePermission(value = "org:company:manage", description = "更新账户", allowSuper = true)
public ApiResponse<AccountVO> updateAccount( public ApiResponse<AccountVO> updateAccount(
@@ -55,6 +60,7 @@ public class AccountController {
return ApiResponse.success("账户更新成功", accountVO); return ApiResponse.success("账户更新成功", accountVO);
} }
/** 逻辑删除:停用 sys_user 并移除公司/部门成员关系 */
@Operation(summary = "删除账户", description = "逻辑删除账户") @Operation(summary = "删除账户", description = "逻辑删除账户")
@DeleteMapping("/delete/{accountId}") @DeleteMapping("/delete/{accountId}")
@RequirePermission(value = "org:company:manage", description = "删除账户", allowSuper = true) @RequirePermission(value = "org:company:manage", description = "删除账户", allowSuper = true)
@@ -2,21 +2,28 @@ package top.xyquan.ecomdmbbackend.modules.org.account.model.dto;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size; import jakarta.validation.constraints.Size;
import lombok.Data; import lombok.Data;
/** /**
* 创建账户DTO * 创建公司账户请求 DTO
* <p>
* 由公司拥有者调用;手机号决定走「拉入已有用户」还是「新建用户」分支。
*/ */
@Data @Data
@Schema(description = "创建账户请求") @Schema(description = "创建账户请求")
public class CreateAccountDTO { public class CreateAccountDTO {
@NotBlank(message = "密码不能为空") /**
* 登录密码
* 仅手机号未注册时必填;已注册用户加入公司时可留空
*/
@Size(min = 6, max = 20, message = "密码长度为6-20个字符") @Size(min = 6, max = 20, message = "密码长度为6-20个字符")
@Schema(description = "登录密码", example = "123456") @Schema(description = "登录密码(手机号未注册时必填)", example = "123456")
private String password; private String password;
/** 手机号,同时作为新用户的登录账号(login_code) */
@NotBlank(message = "手机号不能为空") @NotBlank(message = "手机号不能为空")
@Schema(description = "手机号码", example = "13800138000") @Schema(description = "手机号码", example = "13800138000")
private String phone; private String phone;
@@ -25,6 +32,14 @@ public class CreateAccountDTO {
@Schema(description = "用户姓名", example = "张三") @Schema(description = "用户姓名", example = "张三")
private String realName; private String realName;
/**
* 加入公司后的成员角色,默认 member
* owner=公司拥有者,member=普通成员
*/
@Schema(description = "公司成员角色: owner=公司拥有者, member=成员", example = "member")
@Pattern(regexp = "^(owner|member)$", message = "公司成员角色必须是 owner 或 member")
private String companyRole = "member";
@Schema(description = "部门ID", example = "1") @Schema(description = "部门ID", example = "1")
private Long departmentId; private Long departmentId;
@@ -87,9 +87,9 @@ public class AccountVO {
@Schema(description = "更新时间", example = "2023-12-01T10:00:00") @Schema(description = "更新时间", example = "2023-12-01T10:00:00")
private LocalDateTime updateTime; private LocalDateTime updateTime;
@Schema(description = "创建者", example = "管理员") @Schema(description = "创建者", example = "拥有者")
private String createByName; private String createByName;
@Schema(description = "更新者", example = "管理员") @Schema(description = "更新者", example = "拥有者")
private String updateByName; private String updateByName;
} }
@@ -15,7 +15,10 @@ import top.xyquan.ecomdmbbackend.modules.org.account.model.vo.AccountVO;
public interface AccountService extends IService<UserPO> { public interface AccountService extends IService<UserPO> {
/** /**
* 创建账户(管理员接口,自动设置公司ID) * 创建公司账户(拥有者接口)
* <p>
* 公司 ID 从当前登录上下文获取;手机号已存在则拉入公司,不存在则新建用户。
* 仅公司拥有者可调用,超管不受限。
* *
* @param createAccountDTO 创建账户信息 * @param createAccountDTO 创建账户信息
* @return 账户信息 * @return 账户信息
@@ -23,7 +26,7 @@ public interface AccountService extends IService<UserPO> {
AccountVO createAccountForCompany(CreateAccountDTO createAccountDTO); AccountVO createAccountForCompany(CreateAccountDTO createAccountDTO);
/** /**
* 更新账户信息(管理员接口) * 更新账户信息(拥有者接口)
* *
* @param updateAccountDTO 更新账户信息 * @param updateAccountDTO 更新账户信息
* @param accountId 账户ID * @param accountId 账户ID
@@ -55,7 +58,7 @@ public interface AccountService extends IService<UserPO> {
PageResultVO<AccountVO> getAccountPage(PageQueryDTO<AccountFilterDTO> pageQuery); PageResultVO<AccountVO> getAccountPage(PageQueryDTO<AccountFilterDTO> pageQuery);
/** /**
* 分页查询账户(管理员接口,只能查询本公司账户) * 分页查询账户(拥有者接口,只能查询本公司账户)
* *
* @param pageQuery 分页查询参数 * @param pageQuery 分页查询参数
* @return 分页结果 * @return 分页结果
@@ -35,7 +35,10 @@ import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
* 账户管理服务实现类 * 公司账户管理服务实现类
* <p>
* 面向公司拥有者(及超管)的本公司账户 CRUD。
* 创建账户时:手机号已注册则拉入当前公司,未注册则新建用户并加入公司。
*/ */
@Service @Service
public class AccountServiceImpl extends ServiceImpl<UserMapper, UserPO> implements AccountService { public class AccountServiceImpl extends ServiceImpl<UserMapper, UserPO> implements AccountService {
@@ -64,49 +67,125 @@ public class AccountServiceImpl extends ServiceImpl<UserMapper, UserPO> implemen
/** /**
* 创建公司账户 * 创建公司账户
* <p>
* 业务规则:
* <ul>
* <li>仅公司拥有者可操作,超管不受此限</li>
* <li>手机号已存在:不新建用户,将其加入当前公司(已是成员则报错)</li>
* <li>手机号不存在:新建 sys_user,密码必填,系统角色固定为 user</li>
* <li>companyRole 决定其在公司成员表中的角色(owner / member)</li>
* </ul>
*
* @param createAccountDTO 创建账户DTO * @param createAccountDTO 创建账户DTO
* @return 账户VO * @return 账户VO
*/ */
@Override @Override
public AccountVO createAccountForCompany(CreateAccountDTO createAccountDTO) { public AccountVO createAccountForCompany(CreateAccountDTO createAccountDTO) {
// 从用户上下文获取公司ID
Long companyId = currentUserContext.getCurrentCompanyId(); Long companyId = currentUserContext.getCurrentCompanyId();
if (companyId == null) { if (companyId == null) {
throw new BusinessException("无法获取公司信息"); throw new BusinessException("无法获取公司信息");
} }
// 验证手机号码是否已存在 CompanyMemberRole memberRole = CompanyMemberRole.fromCode(createAccountDTO.getCompanyRole());
if (StringUtils.hasText(createAccountDTO.getPhone())) { assertOwnerCanCreateAccount(companyId);
// 按手机号分流:已注册走「拉入公司」,未注册走「新建用户」
UserPO existingUser = findUserByPhone(createAccountDTO.getPhone());
if (existingUser != null) {
return addExistingUserToCompany(existingUser, companyId, createAccountDTO, memberRole);
}
return createNewUserAndAddToCompany(companyId, createAccountDTO, memberRole);
}
/**
* 校验当前用户是否有权在本公司创建账户
* 超管直接放行;普通用户须为该公司拥有者(company_member.role = owner)
*/
private void assertOwnerCanCreateAccount(Long companyId) {
if ("super".equals(currentUserContext.getCurrentUserRole())) {
return;
}
if (!companyMemberService.isOwner(currentUserContext.getCurrentUserId(), companyId)) {
throw new BusinessException("仅公司拥有者可以创建账户");
}
}
/** 按手机号查找已注册用户,未传或查无此人时返回 null */
private UserPO findUserByPhone(String phone) {
if (!StringUtils.hasText(phone)) {
return null;
}
LambdaQueryWrapper<UserPO> wrapper = new LambdaQueryWrapper<>(); LambdaQueryWrapper<UserPO> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(UserPO::getPhone, createAccountDTO.getPhone()); wrapper.eq(UserPO::getPhone, phone);
if (this.count(wrapper) > 0) { return this.getOne(wrapper);
throw new BusinessException("手机号码已存在");
}
} }
// 创建账户 /**
UserPO user = new UserPO(); * 将已存在用户加入当前公司
BeanUtils.copyProperties(createAccountDTO, user); * 不修改密码;可选更新姓名、备注;同时建立公司成员及部门关联
user.setPassword(passwordEncoder.encode(createAccountDTO.getPassword())); */
user.setLoginCode(createAccountDTO.getPhone()); // 使用手机号作为登录账号 private AccountVO addExistingUserToCompany(
user.setRole("user"); // 默认角色 UserPO user,
user.setStatus(1); // 正常状态(与 sys_user 表定义一致:1=正常,0=停用) Long companyId,
user.setPwdSecurityLevel(calculatePasswordSecurityLevel(createAccountDTO.getPassword())); CreateAccountDTO createAccountDTO,
user.setPwdUpdateTime(LocalDateTime.now()); CompanyMemberRole memberRole) {
user.setCreateBy(currentUserContext.getCurrentUserId()); if (user.getStatus() == null || user.getStatus() != 1) {
throw new BusinessException("该手机号对应账户状态异常,无法加入公司");
}
// 保存账户 CompanyMemberPO existingMember = companyMemberService.getByUserIdAndCompanyId(user.getId(), companyId);
this.save(user); if (existingMember != null) {
throw new BusinessException("该用户已是公司成员");
}
// 创建公司成员关系 if (StringUtils.hasText(createAccountDTO.getRealName())) {
companyMemberService.create(user.getId(), companyId, CompanyMemberRole.member, createAccountDTO.getPositionId()); user.setRealName(createAccountDTO.getRealName());
}
if (StringUtils.hasText(createAccountDTO.getRemark())) {
user.setRemark(createAccountDTO.getRemark());
}
user.setUpdateBy(currentUserContext.getCurrentUserId());
this.updateById(user);
companyMemberService.create(user.getId(), companyId, memberRole, createAccountDTO.getPositionId());
if (createAccountDTO.getDepartmentId() != null) {
departmentMemberService.addMember(createAccountDTO.getDepartmentId(), user.getId());
}
return convertToVO(user);
}
/**
* 新建用户并加入当前公司
* 登录账号与手机号相同,系统角色固定为 user,须传入密码
*/
private AccountVO createNewUserAndAddToCompany(
Long companyId,
CreateAccountDTO createAccountDTO,
CompanyMemberRole memberRole) {
if (!StringUtils.hasText(createAccountDTO.getPassword())) {
throw new BusinessException("新用户必须设置密码");
}
UserPO user = new UserPO();
BeanUtils.copyProperties(createAccountDTO, user);
user.setPassword(passwordEncoder.encode(createAccountDTO.getPassword()));
user.setLoginCode(createAccountDTO.getPhone());
user.setRole("user");
user.setStatus(1);
user.setPwdSecurityLevel(calculatePasswordSecurityLevel(createAccountDTO.getPassword()));
user.setPwdUpdateTime(LocalDateTime.now());
user.setCreateBy(currentUserContext.getCurrentUserId());
this.save(user);
companyMemberService.create(user.getId(), companyId, memberRole, createAccountDTO.getPositionId());
// 如果有部门ID,创建部门成员关系
if (createAccountDTO.getDepartmentId() != null) { if (createAccountDTO.getDepartmentId() != null) {
departmentMemberService.addMember(createAccountDTO.getDepartmentId(), user.getId()); departmentMemberService.addMember(createAccountDTO.getDepartmentId(), user.getId());
} }
// 转换为VO并返回
return convertToVO(user); return convertToVO(user);
} }
@@ -392,14 +471,14 @@ public class AccountServiceImpl extends ServiceImpl<UserMapper, UserPO> implemen
} }
/** /**
* 获取角色名称 * 获取角色中文展示名
* @param role 角色 * admin 为历史系统角色码,展示为「拥有者」
* @return 角色名称
*/ */
private String getRoleName(String role) { private String getRoleName(String role) {
if (role == null) return "未知"; if (role == null) return "未知";
return switch (role) { return switch (role) {
case "super" -> "超级管理员"; case "super" -> "超级管理员";
case "admin" -> "拥有者";
case "user" -> "普通用户"; case "user" -> "普通用户";
default -> "未知"; default -> "未知";
}; };
@@ -440,6 +440,7 @@ public class SuperAccountServiceImpl extends ServiceImpl<UserMapper, UserPO> imp
if (role == null) return "未知"; if (role == null) return "未知";
return switch (role) { return switch (role) {
case "super" -> "超级管理员"; case "super" -> "超级管理员";
case "admin" -> "拥有者";
case "user" -> "普通用户"; case "user" -> "普通用户";
default -> "未知"; default -> "未知";
}; };
@@ -80,7 +80,7 @@ public class AuthController {
permissions = List.of("*"); permissions = List.of("*");
break; break;
case "company": case "company":
// 公司管理员拥有公司相关权限 // 公司拥有者拥有公司相关权限
permissions = List.of("company:*"); permissions = List.of("company:*");
break; break;
case "user": case "user":
@@ -57,7 +57,7 @@ public class UserPO {
private Long avatarImageId; private Long avatarImageId;
/** /**
* 关联角色(user-普通账户 admin-公司管理员账户 super-超管账户) * 关联角色(user-普通账户 admin-公司拥有者账户 super-超管账户)
*/ */
@TableField("role") @TableField("role")
private String role; private String role;
@@ -67,9 +67,9 @@ public class PermissionUtils implements ApplicationContextAware {
} }
/** /**
* 检查当前用户是否是公司管理员 * 检查当前用户是否是公司拥有者
* *
* @return 是否是公司管理员 * @return 是否是公司拥有者
*/ */
public static boolean isCompany() { public static boolean isCompany() {
return hasRole("company"); return hasRole("company");
@@ -8,14 +8,14 @@ import type { AccountDTO, AccountVO } from './types';
*/ */
/** /**
* 创建账户(管理员) * 创建账户(拥有者)
*/ */
export async function admin_createAccountApi(data: AccountDTO.CreateAccountDTO) { export async function admin_createAccountApi(data: AccountDTO.CreateAccountDTO) {
return requestClient.post<AccountVO.AccountInfoVO>('/company/account/create', data); return requestClient.post<AccountVO.AccountInfoVO>('/company/account/create', data);
} }
/** /**
* 分页查询账户(管理员) * 分页查询账户(拥有者)
*/ */
export async function admin_getAccountPageApi(params: PageQueryDTO<AccountDTO.AccountFilterDTO>) { export async function admin_getAccountPageApi(params: PageQueryDTO<AccountDTO.AccountFilterDTO>) {
return requestClient.post<PageResultVO<AccountVO.AccountInfoVO>>('/company/account/list', params); return requestClient.post<PageResultVO<AccountVO.AccountInfoVO>>('/company/account/list', params);
@@ -262,7 +262,7 @@ defineExpose({
placeholder="请选择角色" placeholder="请选择角色"
:options="[ :options="[
{ label: '超级管理员', value: 'super' }, { label: '超级管理员', value: 'super' },
{ label: '管理员', value: 'admin' }, { label: '拥有者', value: 'admin' },
{ label: '普通用户', value: 'user' }, { label: '普通用户', value: 'user' },
]" ]"
/> />
@@ -320,14 +320,13 @@ defineExpose({
label="密码" label="密码"
name="password" name="password"
:rules="[ :rules="[
{ required: true, message: '请输入密码' },
{ min: 6, max: 20, message: '密码长度为6-20个字符' }, { min: 6, max: 20, message: '密码长度为6-20个字符' },
]" ]"
> >
<Input <Input
v-model:value="formData.password" v-model:value="formData.password"
type="password" type="password"
placeholder="请输入密码" placeholder="新用户必填;手机号已存在可留空"
/> />
</Form.Item> </Form.Item>