mirror of
https://gitee.com/dromara/RuoYi-Vue-Plus.git
synced 2026-09-21 02:25:56 +08:00
add 新增 TenantHelper 支持动态切换租户
This commit is contained in:
parent
6938c7ef0f
commit
ba89797bce
@ -2,6 +2,7 @@ package com.ruoyi.common.mybatis.handler;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
|
||||
import com.ruoyi.common.core.utils.StringUtils;
|
||||
import com.ruoyi.common.mybatis.helper.TenantHelper;
|
||||
import com.ruoyi.common.mybatis.properties.TenantProperties;
|
||||
import com.ruoyi.common.satoken.utils.LoginHelper;
|
||||
import lombok.AllArgsConstructor;
|
||||
@ -24,11 +25,16 @@ public class PlusTenantLineHandler implements TenantLineHandler {
|
||||
@Override
|
||||
public Expression getTenantId() {
|
||||
String tenantId = LoginHelper.getTenantId();
|
||||
if (StringUtils.isNotBlank(tenantId)) {
|
||||
//多租户模式,租户id从当前用户获取
|
||||
return new LongValue(tenantId);
|
||||
if (StringUtils.isBlank(tenantId)) {
|
||||
return new NullValue();
|
||||
}
|
||||
return new NullValue();
|
||||
String dynamicTenantId = TenantHelper.getDynamic();
|
||||
if (StringUtils.isBlank(dynamicTenantId)) {
|
||||
// 返回动态租户
|
||||
return new LongValue(dynamicTenantId);
|
||||
}
|
||||
// 返回固定租户
|
||||
return new LongValue(tenantId);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -36,17 +42,14 @@ public class PlusTenantLineHandler implements TenantLineHandler {
|
||||
String tenantId = LoginHelper.getTenantId();
|
||||
// 判断是否有租户
|
||||
if (StringUtils.isNotBlank(tenantId)) {
|
||||
// 判断是否平台超级管理员,如果是平台超级管理员则拥有所有数据权限
|
||||
if (!LoginHelper.isAdmin()) {
|
||||
// 不需要过滤租户的表
|
||||
List<String> excludes = tenantProperties.getExcludes();
|
||||
// 非业务表
|
||||
excludes.addAll(List.of(
|
||||
"gen_table",
|
||||
"gen_table_column"
|
||||
));
|
||||
return excludes.contains(tableName);
|
||||
}
|
||||
// 不需要过滤租户的表
|
||||
List<String> excludes = tenantProperties.getExcludes();
|
||||
// 非业务表
|
||||
excludes.addAll(List.of(
|
||||
"gen_table",
|
||||
"gen_table_column"
|
||||
));
|
||||
return excludes.contains(tableName);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -1,8 +1,11 @@
|
||||
package com.ruoyi.common.mybatis.helper;
|
||||
|
||||
import cn.dev33.satoken.context.SaHolder;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import com.baomidou.mybatisplus.core.plugins.IgnoreStrategy;
|
||||
import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper;
|
||||
import com.ruoyi.common.core.utils.SpringUtils;
|
||||
import com.ruoyi.common.core.utils.StringUtils;
|
||||
import com.ruoyi.common.mybatis.properties.TenantProperties;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
@ -17,6 +20,8 @@ public class TenantHelper {
|
||||
|
||||
private static final TenantProperties PROPERTIES = SpringUtils.getBean(TenantProperties.class);
|
||||
|
||||
private static final String DYNAMIC_TENANT_KEY = "dynamicTenant";
|
||||
|
||||
/**
|
||||
* 租户功能是否启用
|
||||
*/
|
||||
@ -37,4 +42,33 @@ public class TenantHelper {
|
||||
public static void disableIgnore() {
|
||||
InterceptorIgnoreHelper.clearIgnoreStrategy();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置动态租户
|
||||
*/
|
||||
public static void setDynamic(String tenantId) {
|
||||
StpUtil.getTokenSession().set(DYNAMIC_TENANT_KEY, tenantId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取动态租户(多级缓存)
|
||||
*/
|
||||
public static String getDynamic() {
|
||||
String tenantId = (String) SaHolder.getStorage().get(DYNAMIC_TENANT_KEY);
|
||||
if (StringUtils.isNotBlank(tenantId)) {
|
||||
return tenantId;
|
||||
}
|
||||
tenantId = (String) StpUtil.getTokenSession().get(DYNAMIC_TENANT_KEY);
|
||||
SaHolder.getStorage().set(DYNAMIC_TENANT_KEY, tenantId);
|
||||
return tenantId;
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除动态租户
|
||||
*/
|
||||
public static void clearDynamic() {
|
||||
SaHolder.getStorage().delete(DYNAMIC_TENANT_KEY);
|
||||
StpUtil.getTokenSession().delete(DYNAMIC_TENANT_KEY);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -15,6 +15,7 @@ import com.ruoyi.common.log.annotation.Log;
|
||||
import com.ruoyi.common.log.enums.BusinessType;
|
||||
import com.ruoyi.common.mybatis.core.page.PageQuery;
|
||||
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
|
||||
import com.ruoyi.common.mybatis.helper.TenantHelper;
|
||||
import com.ruoyi.common.web.core.BaseController;
|
||||
import com.ruoyi.system.domain.bo.SysTenantBo;
|
||||
import com.ruoyi.system.domain.bo.SysUserBo;
|
||||
@ -22,6 +23,7 @@ import com.ruoyi.system.domain.vo.SysTenantVo;
|
||||
import com.ruoyi.system.service.ISysTenantService;
|
||||
import com.ruoyi.system.service.ISysUserService;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotEmpty;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@ -134,4 +136,27 @@ public class SysTenantController extends BaseController {
|
||||
@PathVariable Long[] ids) {
|
||||
return toAjax(sysTenantService.deleteWithValidByIds(List.of(ids), true));
|
||||
}
|
||||
|
||||
/**
|
||||
* 动态切换租户
|
||||
*
|
||||
* @param tenantId 租户ID
|
||||
*/
|
||||
@SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY)
|
||||
@GetMapping("/dynamic/{tenantId}")
|
||||
public R<Void> dynamicTenant(@NotBlank(message = "租户ID不能为空") @PathVariable String tenantId) {
|
||||
TenantHelper.setDynamic(tenantId);
|
||||
return R.ok();
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除动态租户
|
||||
*/
|
||||
@SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY)
|
||||
@GetMapping("/dynamic/clear")
|
||||
public R<Void> dynamicClear() {
|
||||
TenantHelper.clearDynamic();
|
||||
return R.ok();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user