From 7b76e969afaccbad63d572a9ac674974695d9160 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Tue, 19 Aug 2025 23:01:57 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9A=E3=80=90system=20=E7=B3=BB?= =?UTF-8?q?=E7=BB=9F=E7=AE=A1=E7=90=86=E3=80=91=E7=A7=9F=E6=88=B7=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=8C=B9=E9=85=8D=E5=A4=9A=E5=9F=9F=E5=90=8D=E3=80=81?= =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E5=B0=8F=E7=A8=8B=E5=BA=8F=20appid=20?= =?UTF-8?q?=E7=AD=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sheep/api/infra/tenant.js | 17 ++++++++++++ sheep/request/index.js | 12 ++++++--- sheep/store/app.js | 54 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 sheep/api/infra/tenant.js diff --git a/sheep/api/infra/tenant.js b/sheep/api/infra/tenant.js new file mode 100644 index 00000000..6f4485ab --- /dev/null +++ b/sheep/api/infra/tenant.js @@ -0,0 +1,17 @@ +import request from '@/sheep/request'; + +/** + * 通过网站域名获取租户信息 + * @param {string} website - 网站域名 + * @returns {Promise} 租户信息 + */ +export function getTenantByWebsite(website) { + return request({ + url: '/system/tenant/get-by-website', + method: 'GET', + params: { website }, + custom: { + isToken: false, // 避免登录情况下,跨租户访问被拦截 + }, + }); +} diff --git a/sheep/request/index.js b/sheep/request/index.js index 03eb5767..f0fa1e19 100644 --- a/sheep/request/index.js +++ b/sheep/request/index.js @@ -26,7 +26,8 @@ const options = { loadingMsg: '加载中', // 需要授权才能请求 默认放开 auth: false, - // ... + // 是否传递 token + isToken: true, }; // Loading全局实例 @@ -90,14 +91,14 @@ http.interceptors.request.use( } // 增加 token 令牌、terminal 终端、tenant 租户的请求头 - const token = getAccessToken(); + const token = config.custom.isToken ? getAccessToken() : undefined; if (token) { config.header['Authorization'] = token; } config.header['terminal'] = getTerminal(); config.header['Accept'] = '*/*'; - config.header['tenant-id'] = tenantId; + config.header['tenant-id'] = getTenantId(); return config; }, (error) => { @@ -297,6 +298,11 @@ export const getRefreshToken = () => { return uni.getStorageSync('refresh-token'); }; +/** 获得租户编号 */ +export const getTenantId = () => { + return uni.getStorageSync('tenant-id') || tenantId; +}; + const request = (config) => { return http.middleware(config); }; diff --git a/sheep/store/app.js b/sheep/store/app.js index 981ae33d..df72c598 100644 --- a/sheep/store/app.js +++ b/sheep/store/app.js @@ -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 ? // 查询指定模板,一般是预览时使用