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