forked from gxwebsoft/websoft-cms
14 changed files with 531 additions and 96 deletions
After Width: | Height: | Size: 260 B |
@ -0,0 +1,9 @@ |
|||||
|
import { useState } from '#imports'; |
||||
|
import type { Config } from '~/types/config'; |
||||
|
|
||||
|
export const useConfigInfo = () => |
||||
|
useState<Config>('config', () => { |
||||
|
return {}; |
||||
|
}); |
||||
|
|
||||
|
export const useToken = () => useState('token', () => 'token xxx'); |
@ -0,0 +1,31 @@ |
|||||
|
<script setup lang="ts"> |
||||
|
|
||||
|
</script> |
||||
|
|
||||
|
<template> |
||||
|
<div class="p-20 pt-[200px]"> |
||||
|
<el-dropdown> |
||||
|
<span class="el-dropdown-link"> |
||||
|
Dropdown List |
||||
|
<el-icon class="el-icon--right"> |
||||
|
<arrow-down /> |
||||
|
</el-icon> |
||||
|
</span> |
||||
|
<template #dropdown> |
||||
|
<el-dropdown-menu> |
||||
|
<el-dropdown-item>Action 1</el-dropdown-item> |
||||
|
<el-dropdown-item>Action 2</el-dropdown-item> |
||||
|
<el-dropdown-item>Action 3</el-dropdown-item> |
||||
|
<el-dropdown-item disabled>Action 4</el-dropdown-item> |
||||
|
<el-dropdown-item divided>Action 5</el-dropdown-item> |
||||
|
</el-dropdown-menu> |
||||
|
</template> |
||||
|
</el-dropdown> |
||||
|
|
||||
|
<el-tag type="primary">Tag 1</el-tag> |
||||
|
</div> |
||||
|
</template> |
||||
|
|
||||
|
<style scoped lang="scss"> |
||||
|
|
||||
|
</style> |
@ -0,0 +1,12 @@ |
|||||
|
export interface Config { |
||||
|
tel1?: string; |
||||
|
icpNo?: string; |
||||
|
tel2?: string; |
||||
|
copyright?: string; |
||||
|
qrcode?: string; |
||||
|
domain?: string; |
||||
|
siteName?: string; |
||||
|
siteLogo?: string; |
||||
|
subpageBanner?: string; |
||||
|
bottomBg?: string; |
||||
|
} |
@ -0,0 +1,52 @@ |
|||||
|
/**需要进行持久化的数据:把需要持久化的数据放在下面这个对象中,才会持久化,不需要持久化的数据就不用放到这里了。 */ |
||||
|
const enduring: { [key: string]: () => Ref<any> } = { |
||||
|
useToken, useConfigInfo |
||||
|
} |
||||
|
|
||||
|
//下面的俩函数在app.vue的onMounted中统一调用,或者在其它情况挂载后单独调用。
|
||||
|
/**把所有指定数据保存到本地存储 |
||||
|
* @param key 要保存的数据名。不填的话就是保存全部(一般不填,统一在页面关闭时保存。如果是特别重要的数据,就时不时单独保存一下即可。) |
||||
|
*/ |
||||
|
export const setLocal = (key?: string) => { |
||||
|
if (key) { |
||||
|
console.log('只保存', key); |
||||
|
const useKey = 'use' + key.slice(0, 1).toUpperCase() + key.slice(1).toLowerCase(); //首字母大写,其它全部转小写
|
||||
|
const func = enduring[useKey]; |
||||
|
if (!func) { |
||||
|
console.log('没有找到', useKey, '对应的函数'); |
||||
|
return; |
||||
|
} |
||||
|
localStorage.setItem(key, JSON.stringify(func().value)); |
||||
|
} else { |
||||
|
console.log('正在保存全部数据'); |
||||
|
for (const key in enduring) { |
||||
|
if (Object.prototype.hasOwnProperty.call(enduring, key)) { |
||||
|
const element = enduring[key]; |
||||
|
const setKey = key.toLowerCase().substring(3); //去掉前面的use ,其它全部转小写
|
||||
|
try { |
||||
|
localStorage.setItem(setKey, JSON.stringify(element().value)); |
||||
|
} catch (error) { |
||||
|
console.log(`在设置${setKey}的数据时出现错误`, error); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
}; |
||||
|
/**从本地存储获取数据到state中 */ |
||||
|
export const getLoacl = () => { |
||||
|
for (const key in enduring) { |
||||
|
if (Object.prototype.hasOwnProperty.call(enduring, key)) { |
||||
|
const element = enduring[key]; |
||||
|
const setKey = key.toLowerCase().substring(3); //去掉前面的use ,其它全部转小写
|
||||
|
try { |
||||
|
const localData = localStorage.getItem(setKey) || ''; |
||||
|
if (localData) { |
||||
|
element().value = JSON.parse(localData); |
||||
|
console.log(setKey, '的本地存储数据获取成功', element().value); |
||||
|
} |
||||
|
} catch (error) { |
||||
|
console.log(`在获取${setKey}的数据时出现错误`, error); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
}; |
@ -0,0 +1,88 @@ |
|||||
|
/** |
||||
|
* axios 实例 |
||||
|
*/ |
||||
|
import axios from 'axios'; |
||||
|
import type { AxiosResponse } from 'axios'; |
||||
|
import {API_BASE_URL, TOKEN_HEADER_NAME, LAYOUT_PATH, TOKEN_STORE_NAME} from '@/config'; |
||||
|
import type { ApiResult } from '@/api'; |
||||
|
import { getHostname, getTenantId } from '@/utils/domain'; |
||||
|
|
||||
|
const service = axios.create({ |
||||
|
baseURL: API_BASE_URL |
||||
|
}); |
||||
|
|
||||
|
/** |
||||
|
* 添加请求拦截器 |
||||
|
*/ |
||||
|
service.interceptors.request.use( |
||||
|
(config) => { |
||||
|
const TENANT_ID = localStorage.getItem('TenantId') || 5; |
||||
|
const token = localStorage.getItem(TOKEN_STORE_NAME); |
||||
|
// 添加 token 到 header
|
||||
|
if (token && config.headers) { |
||||
|
config.headers.common[TOKEN_HEADER_NAME] = token; |
||||
|
} |
||||
|
// 获取租户ID
|
||||
|
if (config.headers) { |
||||
|
// 附加企业ID
|
||||
|
const companyId = localStorage.getItem('CompanyId'); |
||||
|
if (companyId) { |
||||
|
config.headers.common['CompanyId'] = companyId; |
||||
|
} |
||||
|
// 通过网站域名获取租户ID
|
||||
|
if (getHostname()) { |
||||
|
config.headers.common['Domain'] = getHostname(); |
||||
|
} |
||||
|
// 解析二级域名获取租户ID
|
||||
|
if (getTenantId()) { |
||||
|
config.headers.common['TenantId'] = getTenantId(); |
||||
|
return config; |
||||
|
} |
||||
|
if (TENANT_ID) { |
||||
|
config.headers.common['TenantId'] = TENANT_ID; |
||||
|
return config; |
||||
|
} |
||||
|
} |
||||
|
return config; |
||||
|
}, |
||||
|
(error) => { |
||||
|
return Promise.reject(error); |
||||
|
} |
||||
|
); |
||||
|
|
||||
|
/** |
||||
|
* 添加响应拦截器 |
||||
|
*/ |
||||
|
service.interceptors.response.use( |
||||
|
(res: AxiosResponse<ApiResult<unknown>>) => { |
||||
|
// 登录过期处理
|
||||
|
if (res.data?.code === 401) { |
||||
|
// const currentPath = unref(router.currentRoute).path;
|
||||
|
// if (currentPath == LAYOUT_PATH) {
|
||||
|
// logout(true);
|
||||
|
// } else {
|
||||
|
// Modal.destroyAll();
|
||||
|
// Modal.info({
|
||||
|
// title: '系统提示',
|
||||
|
// content: '登录状态已过期, 请退出重新登录!',
|
||||
|
// okText: '重新登录',
|
||||
|
// onOk: () => {
|
||||
|
// logout(false, currentPath);
|
||||
|
// }
|
||||
|
// });
|
||||
|
// }
|
||||
|
// return Promise.reject(new Error(res.data.message));
|
||||
|
} |
||||
|
// token 自动续期
|
||||
|
const token = res.headers[TOKEN_HEADER_NAME.toLowerCase()]; |
||||
|
if (token) { |
||||
|
localStorage.setItem(TOKEN_STORE_NAME, token); |
||||
|
} |
||||
|
return res; |
||||
|
}, |
||||
|
(error) => { |
||||
|
return Promise.reject(error); |
||||
|
} |
||||
|
); |
||||
|
|
||||
|
export default service; |
Loading…
Reference in new issue