feat:【system 系统管理】租户支持匹配多域名、微信小程序 appid 等
parent
eca7bb81d3
commit
7b76e969af
|
@ -0,0 +1,17 @@
|
||||||
|
import request from '@/sheep/request';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过网站域名获取租户信息
|
||||||
|
* @param {string} website - 网站域名
|
||||||
|
* @returns {Promise<Object>} 租户信息
|
||||||
|
*/
|
||||||
|
export function getTenantByWebsite(website) {
|
||||||
|
return request({
|
||||||
|
url: '/system/tenant/get-by-website',
|
||||||
|
method: 'GET',
|
||||||
|
params: { website },
|
||||||
|
custom: {
|
||||||
|
isToken: false, // 避免登录情况下,跨租户访问被拦截
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
|
@ -26,7 +26,8 @@ const options = {
|
||||||
loadingMsg: '加载中',
|
loadingMsg: '加载中',
|
||||||
// 需要授权才能请求 默认放开
|
// 需要授权才能请求 默认放开
|
||||||
auth: false,
|
auth: false,
|
||||||
// ...
|
// 是否传递 token
|
||||||
|
isToken: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Loading全局实例
|
// Loading全局实例
|
||||||
|
@ -90,14 +91,14 @@ http.interceptors.request.use(
|
||||||
}
|
}
|
||||||
|
|
||||||
// 增加 token 令牌、terminal 终端、tenant 租户的请求头
|
// 增加 token 令牌、terminal 终端、tenant 租户的请求头
|
||||||
const token = getAccessToken();
|
const token = config.custom.isToken ? getAccessToken() : undefined;
|
||||||
if (token) {
|
if (token) {
|
||||||
config.header['Authorization'] = token;
|
config.header['Authorization'] = token;
|
||||||
}
|
}
|
||||||
config.header['terminal'] = getTerminal();
|
config.header['terminal'] = getTerminal();
|
||||||
|
|
||||||
config.header['Accept'] = '*/*';
|
config.header['Accept'] = '*/*';
|
||||||
config.header['tenant-id'] = tenantId;
|
config.header['tenant-id'] = getTenantId();
|
||||||
return config;
|
return config;
|
||||||
},
|
},
|
||||||
(error) => {
|
(error) => {
|
||||||
|
@ -297,6 +298,11 @@ export const getRefreshToken = () => {
|
||||||
return uni.getStorageSync('refresh-token');
|
return uni.getStorageSync('refresh-token');
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/** 获得租户编号 */
|
||||||
|
export const getTenantId = () => {
|
||||||
|
return uni.getStorageSync('tenant-id') || tenantId;
|
||||||
|
};
|
||||||
|
|
||||||
const request = (config) => {
|
const request = (config) => {
|
||||||
return http.middleware(config);
|
return http.middleware(config);
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
import DiyApi from '@/sheep/api/promotion/diy';
|
import DiyApi from '@/sheep/api/promotion/diy';
|
||||||
|
import { getTenantByWebsite } from '@/sheep/api/infra/tenant';
|
||||||
|
import { getTenantId } from '@/sheep/request';
|
||||||
import { defineStore } from 'pinia';
|
import { defineStore } from 'pinia';
|
||||||
import $platform from '@/sheep/platform';
|
import $platform from '@/sheep/platform';
|
||||||
import $router from '@/sheep/router';
|
import $router from '@/sheep/router';
|
||||||
|
@ -60,6 +62,9 @@ const app = defineStore({
|
||||||
$router.error('EnvError');
|
$router.error('EnvError');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 加载租户
|
||||||
|
await adaptTenant();
|
||||||
|
|
||||||
// 加载装修配置
|
// 加载装修配置
|
||||||
await adaptTemplate(this.template, templateId);
|
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 adaptTemplate = async (appTemplate, templateId) => {
|
||||||
const { data: diyTemplate } = templateId
|
const { data: diyTemplate } = templateId
|
||||||
? // 查询指定模板,一般是预览时使用
|
? // 查询指定模板,一般是预览时使用
|
||||||
|
|
Loading…
Reference in New Issue