feat: 新增支持新版租户功能

This commit is contained in:
H0nGzA1
2023-04-29 00:55:23 +08:00
parent 19e18a7b6f
commit 7f7d88fe5b
3 changed files with 76 additions and 65 deletions

View File

@@ -1,61 +1,67 @@
import { pluginsAll } from '/@/views/plugins/index';
/** /**
* @description 校验是否为租户模式。租户模式把域名替换成 域名 加端口 * @description 校验是否为租户模式。租户模式把域名替换成 域名 加端口
*/ */
export const getBaseURL = function () { export const getBaseURL = function () {
var baseURL = import.meta.env.VITE_API_URL as any var baseURL = import.meta.env.VITE_API_URL as any;
var param = baseURL.split('/')[3] || '' var param = baseURL.split('/')[3] || '';
if (window.pluginsAll && window.pluginsAll.indexOf('dvadmin-tenants-web') !== -1 && (!param || baseURL.startsWith('/'))) { // @ts-ignore
if (pluginsAll && pluginsAll.indexOf('dvadmin3-tenants-web') !== -1 && (!param || baseURL.startsWith('/'))) {
// 1.把127.0.0.1 替换成和前端一样域名 // 1.把127.0.0.1 替换成和前端一样域名
// 2.把 ip 地址替换成和前端一样域名 // 2.把 ip 地址替换成和前端一样域名
// 3.把 /api 或其他类似的替换成和前端一样域名 // 3.把 /api 或其他类似的替换成和前端一样域名
// document.domain // document.domain
var host = baseURL.split('/')[2]
var host = baseURL.split('/')[2];
if (host) { if (host) {
var prot = baseURL.split(':')[2] || 80 var port = baseURL.split(':')[2] || 80;
if (prot === 80 || prot === 443) { if (port === 80 || port === 443) {
host = document.domain host = document.domain;
} else { } else {
host = document.domain + ':' + prot host = document.domain + ':' + port;
} }
baseURL = baseURL.split('/')[0] + '//' + baseURL.split('/')[1] + host + '/' + param baseURL = baseURL.split('/')[0] + '//' + baseURL.split('/')[1] + host + '/' + param;
} else { } else {
baseURL = location.protocol + '//' + location.hostname + (location.port ? ':' : '') + location.port + baseURL baseURL = location.protocol + '//' + location.hostname + (location.port ? ':' : '') + location.port + baseURL;
} }
} }
if (!baseURL.endsWith('/')) { if (!baseURL.endsWith('/')) {
baseURL += '/' baseURL += '/';
}
return baseURL
} }
return baseURL;
};
export const getWsBaseURL = function () { export const getWsBaseURL = function () {
let baseURL = import.meta.env.VITE_API_URL as any let baseURL = import.meta.env.VITE_API_URL as any;
let param = baseURL.split('/')[3] || '' let param = baseURL.split('/')[3] || '';
if (window.pluginsAll && window.pluginsAll.indexOf('dvadmin-tenants-web') !== -1 && (!param || baseURL.startsWith('/'))) { // @ts-ignore
if (pluginsAll && pluginsAll.indexOf('dvadmin3-tenants-web') !== -1 && (!param || baseURL.startsWith('/'))) {
// 1.把127.0.0.1 替换成和前端一样域名 // 1.把127.0.0.1 替换成和前端一样域名
// 2.把 ip 地址替换成和前端一样域名 // 2.把 ip 地址替换成和前端一样域名
// 3.把 /api 或其他类似的替换成和前端一样域名 // 3.把 /api 或其他类似的替换成和前端一样域名
// document.domain // document.domain
var host = baseURL.split('/')[2] var host = baseURL.split('/')[2];
if (host) { if (host) {
var prot = baseURL.split(':')[2] || 80 var port = baseURL.split(':')[2] || 80;
if (prot === 80 || prot === 443) { if (port === 80 || port === 443) {
host = document.domain host = document.domain;
} else { } else {
host = document.domain + ':' + prot host = document.domain + ':' + port;
} }
baseURL = baseURL.split('/')[0] + '//' + baseURL.split('/')[1] + host + '/' + param baseURL = baseURL.split('/')[0] + '//' + baseURL.split('/')[1] + host + '/' + param;
} else { } else {
baseURL = location.protocol + '//' + location.hostname + (location.port ? ':' : '') + location.port + baseURL baseURL = location.protocol + '//' + location.hostname + (location.port ? ':' : '') + location.port + baseURL;
} }
} else if (param !== '' || baseURL.startsWith('/')) { } else if (param !== '' || baseURL.startsWith('/')) {
baseURL = (location.protocol === 'https:' ? 'wss://' : 'ws://') + location.hostname + (location.port ? ':' : '') + location.port + baseURL baseURL = (location.protocol === 'https:' ? 'wss://' : 'ws://') + location.hostname + (location.port ? ':' : '') + location.port + baseURL;
} }
if (!baseURL.endsWith('/')) { if (!baseURL.endsWith('/')) {
baseURL += '/' baseURL += '/';
} }
if (baseURL.startsWith('http')) { // https 也默认会被替换成 wss if (baseURL.startsWith('http')) {
baseURL = baseURL.replace('http', 'ws') // https 也默认会被替换成 wss
} baseURL = baseURL.replace('http', 'ws');
return baseURL
} }
return baseURL;
};

View File

@@ -1,7 +1,7 @@
import axios from 'axios'; import axios from 'axios';
import { get } from 'lodash-es'; import { get } from 'lodash-es';
import { ElMessage, ElMessageBox } from 'element-plus' import { ElMessage, ElMessageBox } from 'element-plus';
import type { Action } from 'element-plus' import type { Action } from 'element-plus';
// @ts-ignore // @ts-ignore
import { errorLog, errorCreate } from './tools.ts'; import { errorLog, errorCreate } from './tools.ts';
@@ -9,6 +9,7 @@ import { errorLog, errorCreate } from './tools.ts';
// import { useUserStore } from "../store/modules/user"; // import { useUserStore } from "../store/modules/user";
import { Local, Session } from '/@/utils/storage'; import { Local, Session } from '/@/utils/storage';
import qs from 'qs'; import qs from 'qs';
import { getBaseURL } from './baseUrl';
/** /**
* @description 创建请求实例 * @description 创建请求实例
*/ */
@@ -17,16 +18,19 @@ function createService() {
const service = axios.create({ const service = axios.create({
timeout: 20000, timeout: 20000,
headers: { headers: {
'Content-Type': 'application/json;charset=utf-8' 'Content-Type': 'application/json;charset=utf-8',
}, },
paramsSerializer: { paramsSerializer: {
serialize(params) { serialize(params) {
return qs.stringify(params, { indices: false,encoder: (val:string) => { return qs.stringify(params, {
indices: false,
encoder: (val: string) => {
if (typeof val === 'boolean') { if (typeof val === 'boolean') {
return val ? 1 : 0; return val ? 1 : 0;
} }
return val; return val;
} }); },
});
}, },
}, },
}); });
@@ -76,7 +80,7 @@ function createService() {
callback: (action: Action) => { callback: (action: Action) => {
window.location.reload(); window.location.reload();
}, },
}) });
errorCreate(`${dataAxios.msg}: ${response.config.url}`); errorCreate(`${dataAxios.msg}: ${response.config.url}`);
break; break;
case 2000: case 2000:
@@ -108,7 +112,7 @@ function createService() {
callback: (action: Action) => { callback: (action: Action) => {
window.location.reload(); window.location.reload();
}, },
}) });
break; break;
case 403: case 403:
error.message = '拒绝访问'; error.message = '拒绝访问';
@@ -162,7 +166,7 @@ function createRequestFunction(service: any) {
'Content-Type': get(config, 'headers.Content-Type', 'application/json'), 'Content-Type': get(config, 'headers.Content-Type', 'application/json'),
}, },
timeout: 5000, timeout: 5000,
baseURL: import.meta.env.VITE_API_URL as any, baseURL: getBaseURL(),
data: {}, data: {},
}; };

View File

@@ -1,5 +1,5 @@
import { defineAsyncComponent, AsyncComponentLoader } from 'vue'; import { defineAsyncComponent, AsyncComponentLoader } from 'vue';
export let pluginsAll: any = [];
// 扫描插件目录并注册插件 // 扫描插件目录并注册插件
export const scanAndInstallPlugins = (app: any) => { export const scanAndInstallPlugins = (app: any) => {
const components = import.meta.glob('./**/*.vue'); const components = import.meta.glob('./**/*.vue');
@@ -11,5 +11,6 @@ export const scanAndInstallPlugins = (app: any) => {
const pluginsName = key.match(/\/([^\/]*)\//)?.[1]; const pluginsName = key.match(/\/([^\/]*)\//)?.[1];
pluginNames.add(pluginsName); pluginNames.add(pluginsName);
} }
console.log('已发现插件:', Array.from(pluginNames)); pluginsAll = Array.from(pluginNames);
console.log('已发现插件:', pluginsAll);
}; };