feat:【system 系统管理】租户支持匹配多域名、微信小程序 appid 等
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
import DiyApi from '@/sheep/api/promotion/diy';
|
||||
import { getTenantByWebsite } from '@/sheep/api/infra/tenant';
|
||||
import { getTenantId } from '@/sheep/request';
|
||||
import { defineStore } from 'pinia';
|
||||
import $platform from '@/sheep/platform';
|
||||
import $router from '@/sheep/router';
|
||||
@@ -60,6 +62,9 @@ const app = defineStore({
|
||||
$router.error('EnvError');
|
||||
}
|
||||
|
||||
// 加载租户
|
||||
await adaptTenant();
|
||||
|
||||
// 加载装修配置
|
||||
await adaptTemplate(this.template, templateId);
|
||||
|
||||
@@ -119,6 +124,55 @@ const app = defineStore({
|
||||
},
|
||||
});
|
||||
|
||||
/** 初始化租户编号 */
|
||||
const adaptTenant = async () => {
|
||||
// 1. 获取当前租户 ID
|
||||
const oldTenantId = getTenantId();
|
||||
let newTenantId = null;
|
||||
|
||||
try {
|
||||
// 2.1 情况一:H5:根据 url 参数、域名来获取新的租户ID
|
||||
// #ifdef H5
|
||||
// H5 环境下的处理逻辑
|
||||
if (window?.location) {
|
||||
// 优先从 URL 查询参数获取 tenantId
|
||||
const urlParams = new URLSearchParams(window.location.search);
|
||||
newTenantId = urlParams.get('tenantId');
|
||||
|
||||
// 如果 URL 参数中没有,则通过 host 获取
|
||||
if (!newTenantId && window.location.host) {
|
||||
const { data } = await getTenantByWebsite(window.location.host);
|
||||
newTenantId = data?.id;
|
||||
}
|
||||
}
|
||||
// #endif
|
||||
|
||||
// 2.2 情况二:微信小程序:小程序环境下的处理逻辑 - 根据 appId 获取租户
|
||||
// #ifdef MP
|
||||
const appId = uni.getAccountInfoSync()?.miniProgram?.appId;
|
||||
if (appId) {
|
||||
const { data } = await getTenantByWebsite(appId);
|
||||
newTenantId = data?.id;
|
||||
}
|
||||
// #endif
|
||||
|
||||
// 3. 如果是新租户(不相等),则进行切换
|
||||
// noinspection EqualityComparisonWithCoercionJS
|
||||
if (newTenantId && newTenantId != oldTenantId) {
|
||||
// 清理掉登录用户的 token
|
||||
const userStore = user();
|
||||
userStore.setToken();
|
||||
|
||||
// 设置新的 tenantId 到本地存储
|
||||
uni.setStorageSync('tenant-id', newTenantId);
|
||||
console.log('租户 ID 已更新:', `${oldTenantId} -> ${newTenantId}`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('adaptTenant 执行失败:', error);
|
||||
}
|
||||
};
|
||||
|
||||
/** 初始化装修模版 */
|
||||
const adaptTemplate = async (appTemplate, templateId) => {
|
||||
const { data: diyTemplate } = templateId
|
||||
? // 查询指定模板,一般是预览时使用
|
||||
|
||||
Reference in New Issue
Block a user