sc
This commit is contained in:
17
src/App.vue
17
src/App.vue
@@ -1,8 +1,10 @@
|
|||||||
<script setup>
|
<script setup>
|
||||||
import config from './config'
|
import config from './config'
|
||||||
|
import constant from '@/utils/constant'
|
||||||
import { getToken } from '@/utils/auth'
|
import { getToken } from '@/utils/auth'
|
||||||
import { useConfigStore } from '@/store'
|
import { useConfigStore } from '@/store'
|
||||||
import { useUserStore } from '@/store'
|
import { useUserStore } from '@/store'
|
||||||
|
import storage from '@/utils/storage'
|
||||||
|
|
||||||
import { getCurrentInstance } from "vue"
|
import { getCurrentInstance } from "vue"
|
||||||
import { useTenantStore } from '@/store'
|
import { useTenantStore } from '@/store'
|
||||||
@@ -16,18 +18,27 @@
|
|||||||
|
|
||||||
// 初始化应用
|
// 初始化应用
|
||||||
function initApp() {
|
function initApp() {
|
||||||
|
// 获取页面加载时传递的参数
|
||||||
|
const query = uni.getLaunchOptionsSync().query || {}
|
||||||
// 初始化应用配置
|
// 初始化应用配置
|
||||||
initConfig()
|
initConfig(query)
|
||||||
// 检查用户登录状态
|
// 检查用户登录状态
|
||||||
//#ifdef H5
|
//#ifdef H5
|
||||||
checkLogin()
|
checkLogin()
|
||||||
//#endif
|
//#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
function initConfig() {
|
function initConfig(query) {
|
||||||
|
// 存储页面传递的参数到storage
|
||||||
|
if (query.tenantId) {
|
||||||
|
storage.set(constant.tenantId, query.tenantId)
|
||||||
|
}
|
||||||
|
if (query.instanceId) {
|
||||||
|
storage.set(constant.instanceId, query.instanceId)
|
||||||
|
}
|
||||||
useConfigStore().setConfig(config)
|
useConfigStore().setConfig(config)
|
||||||
// 初始化租户信息
|
// 初始化租户信息
|
||||||
useTenantStore().getTenant({ id: config.tenantId })
|
useTenantStore().getTenant()
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkLogin() {
|
function checkLogin() {
|
||||||
|
|||||||
@@ -15,8 +15,10 @@ export const useTenantStore = defineStore('tenant', () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const getTenant = () => {
|
const getTenant = () => {
|
||||||
|
// 按照顺序获取tenantId:1. storage 2. config
|
||||||
|
const tenantId = storage.get(constant.tenantId) || config.appInfo.tenantId;
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
getTenantInfo({ id: config.appInfo.tenantId }).then(res => {
|
getTenantInfo({ id: tenantId }).then(res => {
|
||||||
if (!isEmpty(res.data)) {
|
if (!isEmpty(res.data)) {
|
||||||
SET_TENANT_INFO(res.data);
|
SET_TENANT_INFO(res.data);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,9 @@ const constant = {
|
|||||||
name: 'user_name',
|
name: 'user_name',
|
||||||
roles: 'user_roles',
|
roles: 'user_roles',
|
||||||
permissions: 'user_permissions',
|
permissions: 'user_permissions',
|
||||||
tenantInfo: 'tenant_info'
|
tenantInfo: 'tenant_info',
|
||||||
|
instanceId: 'instance_id',
|
||||||
|
tenantId: 'tenant_id'
|
||||||
};
|
};
|
||||||
|
|
||||||
export default constant;
|
export default constant;
|
||||||
|
|||||||
@@ -1,20 +1,25 @@
|
|||||||
import config from '@/config';
|
import config from '@/config';
|
||||||
|
import constant from '@/utils/constant';
|
||||||
import { getToken } from '@/utils/auth';
|
import { getToken } from '@/utils/auth';
|
||||||
import errorCode from '@/utils/errorCode';
|
import errorCode from '@/utils/errorCode';
|
||||||
import { useUserStore } from '@/store/modules/user';
|
import { useUserStore } from '@/store/modules/user';
|
||||||
import { toast, showConfirm, tansParams } from '@/utils/common';
|
import { toast, showConfirm, tansParams } from '@/utils/common';
|
||||||
|
import storage from '@/utils/storage';
|
||||||
|
|
||||||
let timeout = 10000;
|
let timeout = 10000;
|
||||||
const baseUrl = config.baseUrl;
|
const baseUrl = config.baseUrl;
|
||||||
const tenantId = config.appInfo.tenantId;
|
const configAppInfo = config.appInfo;
|
||||||
const instanceId = config.appInfo.instanceId;
|
|
||||||
|
|
||||||
const request = config => {
|
const request = config => {
|
||||||
// 是否需要设置 token
|
// 是否需要设置 token
|
||||||
const isToken = (config.headers || {}).isToken === false;
|
const isToken = (config.headers || {}).isToken === false;
|
||||||
config.header = config.header || {};
|
config.header = config.header || {};
|
||||||
config.header['tenant-id'] = tenantId || 10001;
|
// 按照顺序获取tenantId:1. storage 2. config
|
||||||
config.header['instance-id'] = instanceId || 1038;
|
const tenantId = storage.get(constant.tenantId) || configAppInfo.tenantId || 10001;
|
||||||
|
// 按照顺序获取instanceId:1. storage 2. config
|
||||||
|
const instanceId = storage.get(constant.instanceId) || configAppInfo.instanceId || 1038;
|
||||||
|
config.header['tenant-id'] = tenantId;
|
||||||
|
config.header['instance-id'] = instanceId;
|
||||||
if (getToken() && !isToken) {
|
if (getToken() && !isToken) {
|
||||||
config.header['Authorization'] = 'Bearer ' + getToken();
|
config.header['Authorization'] = 'Bearer ' + getToken();
|
||||||
}
|
}
|
||||||
@@ -59,7 +64,6 @@ const request = config => {
|
|||||||
resolve(res.data);
|
resolve(res.data);
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
debugger;
|
|
||||||
let { message } = error;
|
let { message } = error;
|
||||||
if (message === 'Network Error') {
|
if (message === 'Network Error') {
|
||||||
message = '后端接口连接异常';
|
message = '后端接口连接异常';
|
||||||
|
|||||||
@@ -1,32 +1,32 @@
|
|||||||
import constant from './constant'
|
import constant from './constant';
|
||||||
|
|
||||||
// 存储变量名
|
// 存储变量名
|
||||||
let storageKey = 'storage_data'
|
let storageKey = 'storage_data';
|
||||||
|
|
||||||
// 存储节点变量名
|
// 存储节点变量名
|
||||||
let storageNodeKeys = [constant.avatar, constant.id, constant.name, constant.roles, constant.permissions]
|
let storageNodeKeys = [constant.avatar, constant.id, constant.name, constant.roles, constant.permissions, constant.instanceId, constant.tenantId];
|
||||||
|
|
||||||
const storage = {
|
const storage = {
|
||||||
set: function(key, value) {
|
set: function (key, value) {
|
||||||
if (storageNodeKeys.indexOf(key) != -1) {
|
if (storageNodeKeys.indexOf(key) != -1) {
|
||||||
let tmp = uni.getStorageSync(storageKey)
|
let tmp = uni.getStorageSync(storageKey);
|
||||||
tmp = tmp ? tmp : {}
|
tmp = tmp ? tmp : {};
|
||||||
tmp[key] = value
|
tmp[key] = value;
|
||||||
uni.setStorageSync(storageKey, tmp)
|
uni.setStorageSync(storageKey, tmp);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
get: function(key) {
|
get: function (key) {
|
||||||
let storageData = uni.getStorageSync(storageKey) || {}
|
let storageData = uni.getStorageSync(storageKey) || {};
|
||||||
return storageData[key] || ""
|
return storageData[key] || '';
|
||||||
},
|
},
|
||||||
remove: function(key) {
|
remove: function (key) {
|
||||||
let storageData = uni.getStorageSync(storageKey) || {}
|
let storageData = uni.getStorageSync(storageKey) || {};
|
||||||
delete storageData[key]
|
delete storageData[key];
|
||||||
uni.setStorageSync(storageKey, storageData)
|
uni.setStorageSync(storageKey, storageData);
|
||||||
},
|
},
|
||||||
clean: function() {
|
clean: function () {
|
||||||
uni.removeStorageSync(storageKey)
|
uni.removeStorageSync(storageKey);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
export default storage
|
export default storage;
|
||||||
|
|||||||
Reference in New Issue
Block a user