refactor: 重构内部 API 依赖模式(降低耦合,公众号投票结论),在 common 模块新增 api 包,在对应 biz 模块增加实现

This commit is contained in:
2025-07-26 10:24:25 +08:00
parent 3af43ef6c7
commit 7f0059984d
30 changed files with 781 additions and 128 deletions

View File

@@ -0,0 +1,45 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.continew.admin.system.api;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import top.continew.admin.common.api.system.DictApi;
import top.continew.admin.system.service.DictService;
import top.continew.starter.extension.crud.model.resp.LabelValueResp;
import java.util.List;
/**
* 字典业务 API 实现
*
* @author Charles7c
* @since 2025/7/26 10:16
*/
@Service
@RequiredArgsConstructor
public class DictApiImpl implements DictApi {
private final DictService baseService;
@Override
public List<LabelValueResp> listAll() {
List<LabelValueResp> list = baseService.listDict(null, null);
list.addAll(baseService.listEnumDict());
return list;
}
}

View File

@@ -0,0 +1,43 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.continew.admin.system.api;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import top.continew.admin.common.api.system.DictItemApi;
import top.continew.admin.system.service.DictItemService;
import top.continew.starter.extension.crud.model.resp.LabelValueResp;
import java.util.List;
/**
* 字典项业务 API 实现
*
* @author Charles7c
* @since 2025/7/23 20:57
*/
@Service
@RequiredArgsConstructor
public class DictItemApiImpl implements DictItemApi {
private final DictItemService baseService;
@Override
public List<LabelValueResp> listByDictCode(String dictCode) {
return baseService.listByDictCode(dictCode);
}
}

View File

@@ -0,0 +1,49 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.continew.admin.system.api;
import cn.hutool.core.lang.tree.Tree;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import top.continew.admin.common.api.system.MenuApi;
import top.continew.admin.common.enums.DisEnableStatusEnum;
import top.continew.admin.system.model.query.MenuQuery;
import top.continew.admin.system.service.MenuService;
import java.util.List;
/**
* 菜单业务 API 实现
*
* @author Charles7c
* @since 2025/7/26 9:53
*/
@Service
@RequiredArgsConstructor
public class MenuApiImpl implements MenuApi {
private final MenuService baseService;
@Override
public List<Tree<Long>> listTree(List<Long> excludeMenuIds) {
MenuQuery query = new MenuQuery();
query.setStatus(DisEnableStatusEnum.ENABLE);
// 过滤掉租户不能使用的菜单
query.setExcludeMenuIdList(excludeMenuIds);
return baseService.tree(query, null, true);
}
}

View File

@@ -0,0 +1,48 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.continew.admin.system.api;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import top.continew.admin.common.api.system.RoleApi;
import top.continew.admin.system.model.entity.RoleDO;
import top.continew.admin.system.service.RoleService;
import java.util.Optional;
/**
* 角色业务 API 实现
*
* @author Charles7c
* @since 2025/7/26 9:39
*/
@Service
@RequiredArgsConstructor
public class RoleApiImpl implements RoleApi {
private final RoleService baseService;
@Override
public Long getIdByCode(String code) {
return Optional.ofNullable(baseService.getByCode(code)).map(RoleDO::getId).orElse(null);
}
@Override
public void updateUserContext(Long roleId) {
baseService.updateUserContext(roleId);
}
}

View File

@@ -0,0 +1,64 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.continew.admin.system.api;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import top.continew.admin.common.api.system.RoleMenuApi;
import top.continew.admin.system.model.entity.RoleMenuDO;
import top.continew.admin.system.service.RoleMenuService;
import top.continew.starter.core.util.CollUtils;
import java.util.List;
import java.util.Set;
/**
* 角色和菜单关联业务 API 实现
*
* @author Charles7c
* @since 2025/7/26 9:39
*/
@Service
@RequiredArgsConstructor
public class RoleMenuApiImpl implements RoleMenuApi {
private final RoleMenuService baseService;
@Override
public Set<Long> listRoleIdByNotInMenuIds(List<Long> menuIds) {
List<RoleMenuDO> roleMenuList = baseService.lambdaQuery()
.select(RoleMenuDO::getRoleId)
.notIn(RoleMenuDO::getMenuId, menuIds)
.list();
return CollUtils.mapToSet(roleMenuList, RoleMenuDO::getRoleId);
}
@Override
public List<Long> listMenuIdByRoleIds(List<Long> roleIds) {
return baseService.listMenuIdByRoleIds(roleIds);
}
@Override
public void deleteByNotInMenuIds(List<Long> menuIds) {
baseService.lambdaUpdate().notIn(RoleMenuDO::getMenuId, menuIds).remove();
}
@Override
public boolean add(List<Long> menuIds, Long roleId) {
return baseService.add(menuIds, roleId);
}
}

View File

@@ -0,0 +1,205 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.continew.admin.system.api;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ReUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.continew.admin.common.api.tenant.PackageMenuApi;
import top.continew.admin.common.api.tenant.TenantApi;
import top.continew.admin.common.api.tenant.TenantDataApi;
import top.continew.admin.common.constant.RegexConstants;
import top.continew.admin.common.constant.SysConstants;
import top.continew.admin.common.enums.DataScopeEnum;
import top.continew.admin.common.enums.DisEnableStatusEnum;
import top.continew.admin.common.enums.GenderEnum;
import top.continew.admin.common.model.dto.TenantDTO;
import top.continew.admin.common.util.SecureUtils;
import top.continew.admin.system.mapper.*;
import top.continew.admin.system.mapper.user.UserMapper;
import top.continew.admin.system.mapper.user.UserPasswordHistoryMapper;
import top.continew.admin.system.mapper.user.UserSocialMapper;
import top.continew.admin.system.model.entity.DeptDO;
import top.continew.admin.system.model.entity.FileDO;
import top.continew.admin.system.model.entity.RoleDO;
import top.continew.admin.system.model.entity.user.UserDO;
import top.continew.admin.system.service.FileService;
import top.continew.admin.system.service.RoleMenuService;
import top.continew.admin.system.service.RoleService;
import top.continew.starter.core.util.CollUtils;
import top.continew.starter.core.util.ExceptionUtils;
import top.continew.starter.core.util.validation.ValidationUtils;
import top.continew.starter.extension.tenant.util.TenantUtils;
import java.time.LocalDateTime;
import java.util.List;
/**
* 租户数据 API 实现
*
* @author 小熊
* @author Charles7c
* @since 2024/12/2 20:12
*/
@Service
@RequiredArgsConstructor
public class TenantDataApiForSystemImpl implements TenantDataApi {
private final PackageMenuApi packageMenuApi;
private final TenantApi tenantApi;
private final RoleService roleService;
private final FileService fileService;
private final RoleMenuService roleMenuService;
private final DeptMapper deptMapper;
private final RoleMapper roleMapper;
private final RoleMenuMapper roleMenuMapper;
private final LogMapper logMapper;
private final MessageMapper messageMapper;
private final MessageMapper messageUserMapper;
private final NoticeMapper noticeMapper;
private final RoleDeptMapper roleDeptMapper;
private final UserMapper userMapper;
private final UserPasswordHistoryMapper userPasswordHistoryMapper;
private final UserRoleMapper userRoleMapper;
private final UserSocialMapper userSocialMapper;
@Override
@Transactional(rollbackFor = Exception.class)
public void init(TenantDTO tenant) {
Long tenantId = tenant.getId();
TenantUtils.execute(tenantId, () -> {
// 初始化部门
Long deptId = this.initDeptData(tenant);
// 初始化角色
Long roleId = this.initRoleData(tenant);
// 角色绑定菜单
List<Long> menuIds = packageMenuApi.listMenuIdsByPackageId(tenant.getPackageId());
roleMenuService.add(menuIds, roleId);
// 初始化管理用户
Long userId = this.initUserData(tenant, deptId);
// 用户绑定角色
roleService.assignToUsers(roleId, ListUtil.of(userId));
// 租户绑定用户
tenantApi.bindAdminUser(tenantId, userId);
});
}
@Override
@Transactional(rollbackFor = Exception.class)
public void clear() {
// 退出所有用户
List<UserDO> userList = userMapper.selectList(null);
for (UserDO user : userList) {
StpUtil.logout(user.getId());
}
Wrapper queryWrapper = Wrappers.query().eq("1", 1);
// 部门清除
deptMapper.delete(queryWrapper);
// 文件清除
List<Long> fileIds = CollUtils.mapToList(fileService.list(), FileDO::getId);
if (!fileIds.isEmpty()) {
fileService.delete(fileIds);
}
// 日志清除
logMapper.delete(queryWrapper);
// 消息清除
messageMapper.delete(queryWrapper);
messageUserMapper.delete(queryWrapper);
// 通知清除
noticeMapper.delete(queryWrapper);
// 角色相关数据清除
roleMapper.delete(queryWrapper);
roleDeptMapper.delete(queryWrapper);
roleMenuMapper.delete(queryWrapper);
// 用户数据清除
userMapper.delete(queryWrapper);
userPasswordHistoryMapper.delete(queryWrapper);
userRoleMapper.delete(queryWrapper);
userSocialMapper.delete(queryWrapper);
}
/**
* 初始化部门数据
*
* @param tenant 租户信息
* @return 部门 ID
*/
private Long initDeptData(TenantDTO tenant) {
DeptDO dept = new DeptDO();
dept.setName(tenant.getName());
dept.setParentId(SysConstants.SUPER_PARENT_ID);
dept.setAncestors("0");
dept.setDescription("系统初始部门");
dept.setSort(1);
dept.setStatus(DisEnableStatusEnum.ENABLE);
deptMapper.insert(dept);
return dept.getId();
}
/**
* 初始化角色数据
*
* @param tenant 租户信息
* @return 角色 ID
*/
private Long initRoleData(TenantDTO tenant) {
RoleDO role = new RoleDO();
role.setName("系统管理员");
role.setCode(SysConstants.TENANT_ADMIN_ROLE_CODE);
role.setDataScope(DataScopeEnum.ALL);
role.setDescription("系统初始角色");
role.setSort(1);
role.setIsSystem(true);
role.setMenuCheckStrictly(true);
role.setDeptCheckStrictly(true);
roleMapper.insert(role);
return role.getId();
}
/**
* 初始化用户数据
*
* @param tenant 租户信息
* @param deptId 部门 ID
* @return 用户 ID
*/
private Long initUserData(TenantDTO tenant, Long deptId) {
// 解密密码
String rawPassword = ExceptionUtils.exToNull(() -> SecureUtils.decryptByRsaPrivateKey(tenant.getPassword()));
ValidationUtils.throwIfNull(rawPassword, "密码解密失败");
ValidationUtils.throwIf(!ReUtil
.isMatch(RegexConstants.PASSWORD, rawPassword), "密码长度为 8-32 个字符,支持大小写字母、数字、特殊字符,至少包含字母和数字");
// 初始化用户
UserDO user = new UserDO();
user.setUsername(tenant.getUsername());
user.setNickname("系统管理员");
user.setPassword(rawPassword);
user.setGender(GenderEnum.UNKNOWN);
user.setDescription("系统初始用户");
user.setStatus(DisEnableStatusEnum.ENABLE);
user.setIsSystem(true);
user.setPwdResetTime(LocalDateTime.now());
user.setDeptId(deptId);
userMapper.insert(user);
return user.getId();
}
}

View File

@@ -0,0 +1,54 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.continew.admin.system.api;
import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.Cached;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import top.continew.admin.common.api.system.UserApi;
import top.continew.admin.common.constant.CacheConstants;
import top.continew.admin.system.mapper.user.UserMapper;
import top.continew.admin.system.model.req.user.UserPasswordResetReq;
import top.continew.admin.system.service.UserService;
/**
* 用户业务 API 实现
*
* @author Charles7c
* @since 2025/7/23 20:57
*/
@Service
@RequiredArgsConstructor
public class UserApiImpl implements UserApi {
private final UserService baseService;
private final UserMapper baseMapper;
@Override
@Cached(key = "#id", name = CacheConstants.USER_KEY_PREFIX, cacheType = CacheType.BOTH, syncLocal = true)
public String getNicknameById(Long id) {
return baseMapper.selectNicknameById(id);
}
@Override
public void resetPassword(String newPassword, Long id) {
UserPasswordResetReq req = new UserPasswordResetReq();
req.setNewPassword(newPassword);
baseService.resetPassword(req, id);
}
}

View File

@@ -17,12 +17,12 @@
package top.continew.admin.system.service;
import top.continew.admin.common.base.service.BaseService;
import top.continew.admin.common.service.CommonDictItemService;
import top.continew.admin.system.model.entity.DictItemDO;
import top.continew.admin.system.model.query.DictItemQuery;
import top.continew.admin.system.model.req.DictItemReq;
import top.continew.admin.system.model.resp.DictItemResp;
import top.continew.starter.data.service.IService;
import top.continew.starter.extension.crud.model.resp.LabelValueResp;
import java.util.List;
@@ -32,7 +32,15 @@ import java.util.List;
* @author Charles7c
* @since 2023/9/11 21:29
*/
public interface DictItemService extends BaseService<DictItemResp, DictItemResp, DictItemQuery, DictItemReq>, IService<DictItemDO>, CommonDictItemService {
public interface DictItemService extends BaseService<DictItemResp, DictItemResp, DictItemQuery, DictItemReq>, IService<DictItemDO> {
/**
* 根据字典编码查询字典项列表
*
* @param dictCode 字典编码
* @return 字典项列表
*/
List<LabelValueResp> listByDictCode(String dictCode);
/**
* 根据字典 ID 列表删除

View File

@@ -40,8 +40,6 @@ import java.util.stream.Collectors;
@RequiredArgsConstructor
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenuDO> implements RoleMenuService {
private final RoleMenuMapper baseMapper;
@Override
@Transactional(rollbackFor = Exception.class)
public boolean add(List<Long> menuIds, Long roleId) {

View File

@@ -32,9 +32,7 @@ import cn.hutool.http.ContentType;
import cn.hutool.json.JSONUtil;
import cn.idev.excel.EasyExcel;
import com.alicp.jetcache.anno.CacheInvalidate;
import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.CacheUpdate;
import com.alicp.jetcache.anno.Cached;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
@@ -62,7 +60,6 @@ import top.continew.admin.common.context.UserContext;
import top.continew.admin.common.context.UserContextHolder;
import top.continew.admin.common.enums.DisEnableStatusEnum;
import top.continew.admin.common.enums.GenderEnum;
import top.continew.admin.common.service.CommonUserService;
import top.continew.admin.system.enums.OptionCategoryEnum;
import top.continew.admin.system.mapper.user.UserMapper;
import top.continew.admin.system.model.entity.DeptDO;
@@ -106,7 +103,7 @@ import static top.continew.admin.system.enums.PasswordPolicyEnum.*;
@Slf4j
@Service
@RequiredArgsConstructor
public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserResp, UserDetailResp, UserQuery, UserReq> implements UserService, CommonUserService {
public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserResp, UserDetailResp, UserQuery, UserReq> implements UserService {
private final PasswordEncoder passwordEncoder;
private final UserPasswordHistoryService userPasswordHistoryService;
@@ -221,12 +218,6 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserRes
ids.forEach(onlineUserService::kickOut);
}
@Override
@Cached(key = "#id", name = CacheConstants.USER_KEY_PREFIX, cacheType = CacheType.BOTH, syncLocal = true)
public String getNicknameById(Long id) {
return baseMapper.selectNicknameById(id);
}
@Override
public void downloadImportTemplate(HttpServletResponse response) throws IOException {
try {