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,37 @@
/*
* 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.common.api.system;
import top.continew.starter.extension.crud.model.resp.LabelValueResp;
import java.util.List;
/**
* 字典业务 API
*
* @author Charles7c
* @since 2025/7/26 10:16
*/
public interface DictApi {
/**
* 查询字典列表
*
* @return 字典列表(包含枚举字典列表)
*/
List<LabelValueResp> listAll();
}

View File

@@ -14,19 +14,19 @@
* limitations under the License.
*/
package top.continew.admin.common.service;
package top.continew.admin.common.api.system;
import top.continew.starter.extension.crud.model.resp.LabelValueResp;
import java.util.List;
/**
* 公共字典项业务接口
* 字典项业务 API
*
* @author Charles7c
* @since 2025/4/9 20:17
*/
public interface CommonDictItemService {
public interface DictItemApi {
/**
* 根据字典编码查询

View File

@@ -0,0 +1,38 @@
/*
* 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.common.api.system;
import cn.hutool.core.lang.tree.Tree;
import java.util.List;
/**
* 菜单业务 API
*
* @author Charles7c
* @since 2025/7/26 9:53
*/
public interface MenuApi {
/**
* 查询树结构列表
*
* @param excludeMenuIds 排除的菜单 ID 列表
* @return 树结构列表
*/
List<Tree<Long>> listTree(List<Long> excludeMenuIds);
}

View File

@@ -0,0 +1,41 @@
/*
* 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.common.api.system;
/**
* 角色业务 API
*
* @author Charles7c
* @since 2025/7/26 9:39
*/
public interface RoleApi {
/**
* 根据编码查询 ID
*
* @param code 编码
* @return 角色 ID
*/
Long getIdByCode(String code);
/**
* 更新用户上下文
*
* @param roleId 角色 ID
*/
void updateUserContext(Long roleId);
}

View File

@@ -0,0 +1,61 @@
/*
* 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.common.api.system;
import java.util.List;
import java.util.Set;
/**
* 角色和菜单关联业务 API
*
* @author Charles7c
* @since 2025/7/26 9:39
*/
public interface RoleMenuApi {
/**
* 根据菜单 ID 列表查询角色 ID 列表
*
* @param menuIds 菜单 ID 列表Not In
* @return 角色 ID 列表
*/
Set<Long> listRoleIdByNotInMenuIds(List<Long> menuIds);
/**
* 根据角色 ID 列表查询菜单 ID 列表
*
* @param roleIds 角色 ID 列表
* @return 菜单 ID 列表
*/
List<Long> listMenuIdByRoleIds(List<Long> roleIds);
/**
* 根据菜单 ID 列表删除
*
* @param menuIds 菜单 ID 列表Not In
*/
void deleteByNotInMenuIds(List<Long> menuIds);
/**
* 新增
*
* @param menuIds 菜单 ID 列表
* @param roleId 角色 ID
* @return 是否新增成功true成功false无变更/失败)
*/
boolean add(List<Long> menuIds, Long roleId);
}

View File

@@ -14,19 +14,19 @@
* limitations under the License.
*/
package top.continew.admin.common.service;
package top.continew.admin.common.api.system;
import cn.crane4j.annotation.ContainerMethod;
import cn.crane4j.annotation.MappingType;
import top.continew.admin.common.constant.ContainerConstants;
/**
* 公共用户业务接口
* 用户业务 API
*
* @author Charles7c
* @since 2025/1/9 20:17
*/
public interface CommonUserService {
public interface UserApi {
/**
* 根据 ID 查询昵称
@@ -40,4 +40,12 @@ public interface CommonUserService {
*/
@ContainerMethod(namespace = ContainerConstants.USER_NICKNAME, type = MappingType.ORDER_OF_KEYS)
String getNicknameById(Long id);
/**
* 重置密码
*
* @param newPassword 新密码
* @param id ID
*/
void resetPassword(String newPassword, Long id);
}

View File

@@ -0,0 +1,36 @@
/*
* 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.common.api.tenant;
import java.util.List;
/**
* 套餐和菜单关联业务 API
*
* @author Charles7c
* @since 2025/7/23 21:13
*/
public interface PackageMenuApi {
/**
* 根据套餐 ID 查询
*
* @param packageId 套餐 ID
* @return 菜单 ID 列表
*/
List<Long> listMenuIdsByPackageId(Long packageId);
}

View File

@@ -0,0 +1,34 @@
/*
* 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.common.api.tenant;
/**
* 租户业务 API
*
* @author Charles7c
* @since 2025/7/23 21:13
*/
public interface TenantApi {
/**
* 绑定租户管理员用户
*
* @param tenantId 租户 ID
* @param userId 用户 ID
*/
void bindAdminUser(Long tenantId, Long userId);
}

View File

@@ -14,25 +14,25 @@
* limitations under the License.
*/
package top.continew.admin.tenant.handler;
package top.continew.admin.common.api.tenant;
import top.continew.admin.tenant.model.req.TenantReq;
import top.continew.admin.common.model.dto.TenantDTO;
/**
* 租户数据处理器
* 租户数据 API
*
* @author 小熊
* @author Charles7c
* @since 2024/12/2 20:08
*/
public interface TenantDataHandler {
public interface TenantDataApi {
/**
* 初始化数据
*
* @param tenant 租户信息
*/
void init(TenantReq tenant);
void init(TenantDTO tenant);
/**
* 清除数据

View File

@@ -24,7 +24,7 @@ import cn.idev.excel.metadata.GlobalConfiguration;
import cn.idev.excel.metadata.data.ReadCellData;
import cn.idev.excel.metadata.data.WriteCellData;
import cn.idev.excel.metadata.property.ExcelContentProperty;
import top.continew.admin.common.service.CommonDictItemService;
import top.continew.admin.common.api.system.DictItemApi;
import top.continew.starter.core.constant.StringConstants;
import top.continew.starter.extension.crud.model.resp.LabelValueResp;
@@ -86,7 +86,7 @@ public class ExcelDictConverter implements Converter<Object> {
if (dictExcelProperty == null) {
throw new IllegalArgumentException("Excel 字典转换器异常:请为字段添加 @DictExcelProperty 注解");
}
CommonDictItemService dictItemService = SpringUtil.getBean(CommonDictItemService.class);
return dictItemService.listByDictCode(dictExcelProperty.value());
DictItemApi dictItemApi = SpringUtil.getBean(DictItemApi.class);
return dictItemApi.listByDictCode(dictExcelProperty.value());
}
}

View File

@@ -20,7 +20,7 @@ import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.extra.spring.SpringUtil;
import top.continew.admin.common.service.CommonUserService;
import top.continew.admin.common.api.system.UserApi;
import top.continew.starter.core.util.ExceptionUtils;
/**
@@ -177,7 +177,7 @@ public class UserContextHolder {
* @return 用户昵称
*/
public static String getNickname(Long userId) {
return ExceptionUtils.exToNull(() -> SpringUtil.getBean(CommonUserService.class).getNicknameById(userId));
return ExceptionUtils.exToNull(() -> SpringUtil.getBean(UserApi.class).getNicknameById(userId));
}
/**

View File

@@ -0,0 +1,60 @@
/*
* 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.common.model.dto;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
/**
* 租户信息
*
* @author Charles7c
* @since 2025/7/23 21:05
*/
@Data
public class TenantDTO implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
/**
* ID
*/
private Long id;
/**
* 名称
*/
private String name;
/**
* 管理员用户名
*/
private String username;
/**
* 管理员密码
*/
private String password;
/**
* 套餐 ID
*/
private Long packageId;
}

View File

@@ -14,12 +14,4 @@
<name>${project.artifactId}</name>
<description>代码生成器插件</description>
<dependencies>
<!-- 系统管理模块 -->
<dependency>
<groupId>top.continew.admin</groupId>
<artifactId>continew-system</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -25,13 +25,13 @@ import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import top.continew.admin.common.api.system.DictApi;
import top.continew.admin.generator.model.entity.FieldConfigDO;
import top.continew.admin.generator.model.entity.GenConfigDO;
import top.continew.admin.generator.model.query.GenConfigQuery;
import top.continew.admin.generator.model.req.GenConfigReq;
import top.continew.admin.generator.model.resp.GeneratePreviewResp;
import top.continew.admin.generator.service.GeneratorService;
import top.continew.admin.system.service.DictService;
import top.continew.starter.extension.crud.model.query.PageQuery;
import top.continew.starter.extension.crud.model.resp.LabelValueResp;
import top.continew.starter.extension.crud.model.resp.PageResp;
@@ -52,7 +52,7 @@ import java.util.List;
public class GeneratorController {
private final GeneratorService baseService;
private final DictService dictService;
private final DictApi dictApi;
@Operation(summary = "分页查询生成配置", description = "分页查询生成配置列表")
@SaCheckPermission("code:generator:list")
@@ -111,12 +111,10 @@ public class GeneratorController {
baseService.generateCode(tableNames);
}
@Operation(summary = "查询字典", description = "查询字典列表")
@Operation(summary = "查询字典", description = "查询字典列表(包含枚举字典)")
@SaCheckPermission("code:generator:config")
@GetMapping("/dict")
public List<LabelValueResp> listDict() {
List<LabelValueResp> dictList = dictService.listDict(null, null);
dictList.addAll(dictService.listEnumDict());
return dictList;
return dictApi.listAll();
}
}

View File

@@ -14,12 +14,4 @@
<name>${project.artifactId}</name>
<description>租户插件</description>
<dependencies>
<!-- 系统管理模块 -->
<dependency>
<groupId>top.continew.admin</groupId>
<artifactId>continew-system</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,42 @@
/*
* 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.tenant.api;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import top.continew.admin.common.api.tenant.PackageMenuApi;
import top.continew.admin.tenant.service.PackageMenuService;
import java.util.List;
/**
* 套餐和菜单关联业务 API 实现
*
* @author Charles7c
* @since 2025/7/23 21:13
*/
@Service
@RequiredArgsConstructor
public class PackageMenuApiImpl implements PackageMenuApi {
private final PackageMenuService baseService;
@Override
public List<Long> listMenuIdsByPackageId(Long packageId) {
return baseService.listMenuIdsByPackageId(packageId);
}
}

View File

@@ -0,0 +1,47 @@
/*
* 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.tenant.api;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import top.continew.admin.common.api.tenant.TenantApi;
import top.continew.admin.tenant.constant.TenantCacheConstants;
import top.continew.admin.tenant.mapper.TenantMapper;
import top.continew.admin.tenant.model.entity.TenantDO;
import top.continew.starter.cache.redisson.util.RedisUtils;
import top.continew.starter.extension.crud.model.entity.BaseIdDO;
/**
* 租户业务 API 实现
*
* @author Charles7c
* @since 2025/7/23 21:13
*/
@Service
@RequiredArgsConstructor
public class TenantApiImpl implements TenantApi {
private final TenantMapper baseMapper;
@Override
public void bindAdminUser(Long tenantId, Long userId) {
baseMapper.lambdaUpdate().set(TenantDO::getAdminUser, userId).eq(BaseIdDO::getId, tenantId).update();
// 更新租户缓存
TenantDO entity = baseMapper.selectById(tenantId);
RedisUtils.set(TenantCacheConstants.TENANT_KEY_PREFIX + tenantId, entity);
}
}

View File

@@ -23,11 +23,9 @@ import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import top.continew.admin.common.api.system.MenuApi;
import top.continew.admin.common.base.controller.BaseController;
import top.continew.admin.common.config.TenantExtensionProperties;
import top.continew.admin.common.enums.DisEnableStatusEnum;
import top.continew.admin.system.model.query.MenuQuery;
import top.continew.admin.system.service.MenuService;
import top.continew.admin.tenant.model.query.PackageQuery;
import top.continew.admin.tenant.model.req.PackageReq;
import top.continew.admin.tenant.model.resp.PackageDetailResp;
@@ -52,16 +50,12 @@ import java.util.List;
public class PackageController extends BaseController<PackageService, PackageResp, PackageDetailResp, PackageQuery, PackageReq> {
private final TenantExtensionProperties tenantExtensionProperties;
private final MenuService menuService;
private final MenuApi menuApi;
@Operation(summary = "查询租户套餐菜单", description = "查询租户套餐菜单树列表")
@SaCheckPermission("tenant:package:list")
@GetMapping("/menu/tree")
public List<Tree<Long>> listMenuTree() {
MenuQuery query = new MenuQuery();
query.setStatus(DisEnableStatusEnum.ENABLE);
// 过滤掉租户不能使用的菜单
query.setExcludeMenuIdList(tenantExtensionProperties.getIgnoreMenus());
return menuService.tree(query, null, true);
return menuApi.listTree(tenantExtensionProperties.getIgnoreMenus());
}
}

View File

@@ -25,11 +25,9 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import top.continew.admin.common.api.system.UserApi;
import top.continew.admin.common.base.controller.BaseController;
import top.continew.admin.common.util.SecureUtils;
import top.continew.admin.system.model.entity.user.UserDO;
import top.continew.admin.system.model.req.user.UserPasswordResetReq;
import top.continew.admin.system.service.UserService;
import top.continew.admin.tenant.model.entity.TenantDO;
import top.continew.admin.tenant.model.query.TenantQuery;
import top.continew.admin.tenant.model.req.TenantAdminUserPwdUpdateReq;
@@ -56,7 +54,7 @@ import top.continew.starter.extension.tenant.util.TenantUtils;
@CrudRequestMapping(value = "/tenant/management", api = {Api.PAGE, Api.GET, Api.CREATE, Api.UPDATE, Api.DELETE})
public class TenantController extends BaseController<TenantService, TenantResp, TenantDetailResp, TenantQuery, TenantReq> {
private final UserService userService;
private final UserApi userApi;
@Operation(summary = "修改租户管理员密码", description = "修改租户管理员密码")
@SaCheckPermission("tenant:management:updateAdminUserPwd")
@@ -65,12 +63,9 @@ public class TenantController extends BaseController<TenantService, TenantResp,
TenantDO tenant = baseService.getById(id);
String encryptPassword = req.getPassword();
TenantUtils.execute(id, () -> {
UserDO user = userService.getById(tenant.getAdminUser());
String password = ExceptionUtils.exToNull(() -> SecureUtils.decryptByRsaPrivateKey(encryptPassword));
ValidationUtils.throwIfNull(password, "新密码解密失败");
UserPasswordResetReq passwordResetReq = new UserPasswordResetReq();
passwordResetReq.setNewPassword(password);
userService.resetPassword(passwordResetReq, user.getId());
userApi.resetPassword(password, tenant.getAdminUser());
});
}
}

View File

@@ -16,24 +16,25 @@
package top.continew.admin.tenant.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.alicp.jetcache.anno.Cached;
import lombok.RequiredArgsConstructor;
import me.ahoo.cosid.provider.IdGeneratorProvider;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.continew.admin.common.api.system.RoleApi;
import top.continew.admin.common.api.system.RoleMenuApi;
import top.continew.admin.common.api.tenant.TenantDataApi;
import top.continew.admin.common.base.service.BaseServiceImpl;
import top.continew.admin.common.config.TenantExtensionProperties;
import top.continew.admin.common.constant.CacheConstants;
import top.continew.admin.common.constant.SysConstants;
import top.continew.admin.common.enums.DisEnableStatusEnum;
import top.continew.admin.system.model.entity.RoleDO;
import top.continew.admin.system.model.entity.RoleMenuDO;
import top.continew.admin.system.service.RoleMenuService;
import top.continew.admin.system.service.RoleService;
import top.continew.admin.common.model.dto.TenantDTO;
import top.continew.admin.tenant.constant.TenantCacheConstants;
import top.continew.admin.tenant.constant.TenantConstants;
import top.continew.admin.tenant.handler.TenantDataHandler;
import top.continew.admin.tenant.mapper.TenantMapper;
import top.continew.admin.tenant.model.entity.TenantDO;
import top.continew.admin.tenant.model.query.TenantQuery;
@@ -44,13 +45,13 @@ import top.continew.admin.tenant.service.PackageService;
import top.continew.admin.tenant.service.TenantService;
import top.continew.starter.cache.redisson.util.RedisUtils;
import top.continew.starter.core.constant.StringConstants;
import top.continew.starter.core.util.CollUtils;
import top.continew.starter.core.util.validation.CheckUtils;
import top.continew.starter.extension.tenant.util.TenantUtils;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
@@ -64,12 +65,12 @@ import java.util.Set;
@RequiredArgsConstructor
public class TenantServiceImpl extends BaseServiceImpl<TenantMapper, TenantDO, TenantResp, TenantDetailResp, TenantQuery, TenantReq> implements TenantService {
private final Map<String, TenantDataApi> tenantDataApiMap = SpringUtil.getBeansOfType(TenantDataApi.class);
private final TenantExtensionProperties tenantExtensionProperties;
private final PackageService packageService;
private final IdGeneratorProvider idGeneratorProvider;
private final TenantDataHandler tenantDataHandler;
private final RoleMenuService roleMenuService;
private final RoleService roleService;
private final RoleMenuApi roleMenuApi;
private final RoleApi roleApi;
@Override
public Long create(TenantReq req) {
@@ -83,7 +84,7 @@ public class TenantServiceImpl extends BaseServiceImpl<TenantMapper, TenantDO, T
Long id = super.create(req);
// 初始化租户数据
req.setId(id);
tenantDataHandler.init(req);
tenantDataApiMap.forEach((key, value) -> value.init(BeanUtil.copyProperties(req, TenantDTO.class)));
return id;
}
@@ -107,7 +108,7 @@ public class TenantServiceImpl extends BaseServiceImpl<TenantMapper, TenantDO, T
public void beforeDelete(List<Long> ids) {
// 在租户中执行数据清除
for (Long id : ids) {
TenantUtils.execute(id, tenantDataHandler::clear);
TenantUtils.execute(id, () -> tenantDataApiMap.forEach((key, value) -> value.clear()));
}
}
@@ -162,27 +163,20 @@ public class TenantServiceImpl extends BaseServiceImpl<TenantMapper, TenantDO, T
// 删除旧菜单
tenantIdList.forEach(tenantId -> TenantUtils.execute(tenantId, () -> {
// 更新在线用户上下文
List<RoleMenuDO> roleMenuList = roleMenuService.lambdaQuery()
.select(RoleMenuDO::getRoleId)
.notIn(RoleMenuDO::getMenuId, newMenuIds)
.list();
Set<Long> roleIdSet = CollUtils.mapToSet(roleMenuList, RoleMenuDO::getRoleId);
roleIdSet.forEach(roleService::updateUserContext);
Set<Long> roleIdSet = roleMenuApi.listRoleIdByNotInMenuIds(newMenuIds);
roleIdSet.forEach(roleApi::updateUserContext);
// 删除旧菜单
roleMenuService.lambdaUpdate().notIn(RoleMenuDO::getMenuId, newMenuIds).remove();
roleMenuApi.deleteByNotInMenuIds(newMenuIds);
}));
// 租户管理员:新增菜单
tenantIdList.forEach(tenantId -> TenantUtils.execute(tenantId, () -> {
RoleDO role = roleService.getByCode(SysConstants.TENANT_ADMIN_ROLE_CODE);
List<Long> oldMenuIdList = roleMenuService.listMenuIdByRoleIds(List.of(role.getId()));
Long roleId = roleApi.getIdByCode(SysConstants.TENANT_ADMIN_ROLE_CODE);
List<Long> oldMenuIdList = roleMenuApi.listMenuIdByRoleIds(List.of(roleId));
Collection<Long> addMenuIdList = CollUtil.disjunction(newMenuIds, oldMenuIdList);
if (CollUtil.isNotEmpty(addMenuIdList)) {
List<RoleMenuDO> roleMenuList = addMenuIdList.stream()
.map(menuId -> new RoleMenuDO(role.getId(), menuId))
.toList();
roleMenuService.saveBatch(roleMenuList, roleMenuList.size());
roleMenuApi.add(addMenuIdList.stream().toList(), roleId);
// 更新在线用户上下文
roleService.updateUserContext(role.getId());
roleApi.updateUserContext(roleId);
}
}));
// 删除缓存

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

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package top.continew.admin.tenant.handler;
package top.continew.admin.system.api;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.ListUtil;
@@ -24,11 +24,15 @@ 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;
@@ -41,23 +45,16 @@ 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.admin.tenant.constant.TenantCacheConstants;
import top.continew.admin.tenant.mapper.TenantMapper;
import top.continew.admin.tenant.model.entity.TenantDO;
import top.continew.admin.tenant.model.req.TenantReq;
import top.continew.admin.tenant.service.PackageMenuService;
import top.continew.starter.cache.redisson.util.RedisUtils;
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.crud.model.entity.BaseIdDO;
import top.continew.starter.extension.tenant.util.TenantUtils;
import java.time.LocalDateTime;
import java.util.List;
/**
* 租户数据处理器
* 租户数据 API 实现
*
* @author 小熊
* @author Charles7c
@@ -65,16 +62,16 @@ import java.util.List;
*/
@Service
@RequiredArgsConstructor
public class TenantDataHandlerForSystem implements TenantDataHandler {
public class TenantDataApiForSystemImpl implements TenantDataApi {
private final PackageMenuService packageMenuService;
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 RoleMenuService roleMenuService;
private final RoleMenuMapper roleMenuMapper;
private final RoleService roleService;
private final TenantMapper tenantMapper;
private final FileService fileService;
private final LogMapper logMapper;
private final MessageMapper messageMapper;
private final MessageMapper messageUserMapper;
@@ -87,7 +84,7 @@ public class TenantDataHandlerForSystem implements TenantDataHandler {
@Override
@Transactional(rollbackFor = Exception.class)
public void init(TenantReq tenant) {
public void init(TenantDTO tenant) {
Long tenantId = tenant.getId();
TenantUtils.execute(tenantId, () -> {
// 初始化部门
@@ -95,14 +92,14 @@ public class TenantDataHandlerForSystem implements TenantDataHandler {
// 初始化角色
Long roleId = this.initRoleData(tenant);
// 角色绑定菜单
List<Long> menuIds = packageMenuService.listMenuIdsByPackageId(tenant.getPackageId());
List<Long> menuIds = packageMenuApi.listMenuIdsByPackageId(tenant.getPackageId());
roleMenuService.add(menuIds, roleId);
// 初始化管理用户
Long userId = this.initUserData(tenant, deptId);
// 用户绑定角色
roleService.assignToUsers(roleId, ListUtil.of(userId));
// 租户绑定用户
this.bindTenantUser(tenantId, userId);
tenantApi.bindAdminUser(tenantId, userId);
});
}
@@ -146,7 +143,7 @@ public class TenantDataHandlerForSystem implements TenantDataHandler {
* @param tenant 租户信息
* @return 部门 ID
*/
private Long initDeptData(TenantReq tenant) {
private Long initDeptData(TenantDTO tenant) {
DeptDO dept = new DeptDO();
dept.setName(tenant.getName());
dept.setParentId(SysConstants.SUPER_PARENT_ID);
@@ -164,7 +161,7 @@ public class TenantDataHandlerForSystem implements TenantDataHandler {
* @param tenant 租户信息
* @return 角色 ID
*/
private Long initRoleData(TenantReq tenant) {
private Long initRoleData(TenantDTO tenant) {
RoleDO role = new RoleDO();
role.setName("系统管理员");
role.setCode(SysConstants.TENANT_ADMIN_ROLE_CODE);
@@ -185,7 +182,7 @@ public class TenantDataHandlerForSystem implements TenantDataHandler {
* @param deptId 部门 ID
* @return 用户 ID
*/
private Long initUserData(TenantReq tenant, Long deptId) {
private Long initUserData(TenantDTO tenant, Long deptId) {
// 解密密码
String rawPassword = ExceptionUtils.exToNull(() -> SecureUtils.decryptByRsaPrivateKey(tenant.getPassword()));
ValidationUtils.throwIfNull(rawPassword, "密码解密失败");
@@ -205,17 +202,4 @@ public class TenantDataHandlerForSystem implements TenantDataHandler {
userMapper.insert(user);
return user.getId();
}
/**
* 绑定租户管理员用户
*
* @param tenantId 租户 ID
* @param userId 用户 ID
*/
public void bindTenantUser(Long tenantId, Long userId) {
tenantMapper.lambdaUpdate().set(TenantDO::getAdminUser, userId).eq(BaseIdDO::getId, tenantId).update();
// 更新租户缓存
TenantDO entity = tenantMapper.selectById(tenantId);
RedisUtils.set(TenantCacheConstants.TENANT_KEY_PREFIX + tenantId, entity);
}
}

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 {