通过config.json配置appKey
Showing
4 changed files
with
80 additions
and
20 deletions
public/config.json
0 → 100644
| ... | @@ -6,11 +6,12 @@ import App from './App.vue' | ... | @@ -6,11 +6,12 @@ import App from './App.vue' |
| 6 | // import pinia from './store' | 6 | // import pinia from './store' |
| 7 | import router from './router' | 7 | import router from './router' |
| 8 | import useSettingsStore from './store/modules/settings'; | 8 | import useSettingsStore from './store/modules/settings'; |
| 9 | import sysConfigStore from '@/store/modules/sysConfig' | ||
| 9 | import { ElMessage } from "element-plus" | 10 | import { ElMessage } from "element-plus" |
| 10 | import { openMessageBox } from "@/utils/common" | 11 | import { openMessageBox } from "@/utils/common" |
| 11 | 12 | ||
| 12 | // pinia设置 | 13 | // pinia设置 |
| 13 | import {createPinia} from 'pinia' | 14 | import { createPinia } from 'pinia' |
| 14 | import piniaPluginPersistedstate from 'pinia-plugin-persistedstate' | 15 | import piniaPluginPersistedstate from 'pinia-plugin-persistedstate' |
| 15 | 16 | ||
| 16 | // 自定义指令 | 17 | // 自定义指令 |
| ... | @@ -25,18 +26,52 @@ import '@/assets/styles/globals.scss' | ... | @@ -25,18 +26,52 @@ import '@/assets/styles/globals.scss' |
| 25 | // 加载 iconify 图标(element plus) | 26 | // 加载 iconify 图标(element plus) |
| 26 | import { downloadAndInstall } from '@/iconify-ep' | 27 | import { downloadAndInstall } from '@/iconify-ep' |
| 27 | 28 | ||
| 28 | const pinia = createPinia(); | 29 | // 封装请求配置文件的函数 |
| 29 | pinia.use(piniaPluginPersistedstate); | 30 | const loadConfig = async () => { |
| 30 | 31 | try { | |
| 31 | const app = createApp(App) | 32 | const response = await fetch('/config.json'); |
| 32 | app.use(ElementPlus) | 33 | if (!response.ok) { |
| 33 | app.use(pinia) | 34 | throw new Error(`请求配置失败,状态码: ${response.status}`); |
| 34 | app.use(router) | 35 | } |
| 35 | directive(app) | 36 | const config = await response.json(); |
| 36 | if (useSettingsStore().settings.app.iconifyOfflineUse) { | 37 | return config; |
| 37 | downloadAndInstall() | 38 | } catch (error) { |
| 38 | } | 39 | console.error('加载配置时出错:', error); |
| 39 | app.config.globalProperties.$passCode = "00000";// 定义全局变量 | 40 | throw error; |
| 40 | app.config.globalProperties.$ElMessage = ElMessage; // 全局消息提示 | 41 | } |
| 41 | app.config.globalProperties.$openMessageBox = openMessageBox; // 二次确认提示对话框。 | 42 | }; |
| 42 | app.mount('#main-app') | 43 | |
| 44 | // 初始化应用的异步函数 | ||
| 45 | const initApp = async () => { | ||
| 46 | try { | ||
| 47 | // 加载配置文件 | ||
| 48 | const config = await loadConfig(); | ||
| 49 | |||
| 50 | const pinia = createPinia(); | ||
| 51 | pinia.use(piniaPluginPersistedstate); | ||
| 52 | |||
| 53 | // 将配置信息存储到 Pinia 中 | ||
| 54 | const configStore = sysConfigStore(); | ||
| 55 | configStore.setConfig(config); | ||
| 56 | |||
| 57 | const app = createApp(App) | ||
| 58 | app.use(ElementPlus) | ||
| 59 | app.use(pinia) | ||
| 60 | app.use(router) | ||
| 61 | directive(app) | ||
| 62 | if (useSettingsStore().settings.app.iconifyOfflineUse) { | ||
| 63 | downloadAndInstall() | ||
| 64 | } | ||
| 65 | app.config.globalProperties.$passCode = "00000";// 定义全局变量 | ||
| 66 | app.config.globalProperties.$ElMessage = ElMessage; // 全局消息提示 | ||
| 67 | app.config.globalProperties.$openMessageBox = openMessageBox; // 二次确认提示对话框。 | ||
| 68 | app.mount('#main-app') | ||
| 69 | } catch (error) { | ||
| 70 | console.error('应用初始化失败:', error); | ||
| 71 | // 这里可以添加一些错误处理逻辑,比如显示错误提示给用户 | ||
| 72 | } | ||
| 73 | }; | ||
| 74 | |||
| 75 | |||
| 76 | // 启动应用 | ||
| 77 | initApp(); | ... | ... |
src/store/modules/sysConfig.ts
0 → 100644
| 1 | const sysConfigStore = defineStore( | ||
| 2 | // 唯一ID | ||
| 3 | 'config', | ||
| 4 | () => { | ||
| 5 | let configMap: any = {}; | ||
| 6 | const setConfig = (val) => { | ||
| 7 | configMap = val | ||
| 8 | } | ||
| 9 | |||
| 10 | const getConfig = (field) => { | ||
| 11 | return field ? configMap[field]: configMap; | ||
| 12 | } | ||
| 13 | |||
| 14 | return { | ||
| 15 | configMap, | ||
| 16 | setConfig, | ||
| 17 | getConfig | ||
| 18 | } | ||
| 19 | }, | ||
| 20 | ) | ||
| 21 | |||
| 22 | export default sysConfigStore |
| 1 | import useRouteStore from './route' | 1 | import useRouteStore from './route' |
| 2 | import useMenuStore from './menu' | 2 | import useMenuStore from './menu' |
| 3 | import sysConfigStore from './sysConfig' | ||
| 3 | import router from '@/router' | 4 | import router from '@/router' |
| 4 | import { ElMessage } from 'element-plus' | 5 | import { ElMessage } from 'element-plus' |
| 5 | import apiUser from '@/api/modules/user' | 6 | import apiUser from '@/api/modules/user' |
| ... | @@ -30,12 +31,10 @@ const useUserStore = defineStore( | ... | @@ -30,12 +31,10 @@ const useUserStore = defineStore( |
| 30 | const idassLoginUrl = import.meta.env.VITE_IDASS_BASEURL; | 31 | const idassLoginUrl = import.meta.env.VITE_IDASS_BASEURL; |
| 31 | const timer: any = ref(null); | 32 | const timer: any = ref(null); |
| 32 | //获取token. | 33 | //获取token. |
| 33 | async function getToken(data, state) { | 34 | function getToken(data, state) { |
| 34 | const config = system.appConfig; | ||
| 35 | data.platformGuid = "7f16f697aec111ef8656fa163e60becd"; | 35 | data.platformGuid = "7f16f697aec111ef8656fa163e60becd"; |
| 36 | data.userType = 2; | 36 | data.userType = 2; |
| 37 | // data.appKey = '672c2c38e4b0cac8732a6106'; | 37 | data.appKey = sysConfigStore().getConfig('appKey'); |
| 38 | data.appKey = config.appKey; | ||
| 39 | data.validateUri = location.origin == 'http://localhost:9000' ? 'http://localhost:9000/' : location.origin + '/'; | 38 | data.validateUri = location.origin == 'http://localhost:9000' ? 'http://localhost:9000/' : location.origin + '/'; |
| 40 | return getTokenPromise.value = getTokenByCode(data).then((res: any) => { | 39 | return getTokenPromise.value = getTokenByCode(data).then((res: any) => { |
| 41 | getTokenPromise.value = null; | 40 | getTokenPromise.value = null; |
| ... | @@ -304,6 +303,7 @@ const useUserStore = defineStore( | ... | @@ -304,6 +303,7 @@ const useUserStore = defineStore( |
| 304 | } | 303 | } |
| 305 | return m.children.some(c => c.menuType == 'F' && c.menuName == btnCaption); | 304 | return m.children.some(c => c.menuType == 'F' && c.menuName == btnCaption); |
| 306 | } | 305 | } |
| 306 | |||
| 307 | return { | 307 | return { |
| 308 | account, | 308 | account, |
| 309 | userId, | 309 | userId, | ... | ... |
-
Please register or sign in to post a comment