113 lines
3.5 KiB
TypeScript
113 lines
3.5 KiB
TypeScript
import { createApp } from 'vue'
|
||
import App from './App.vue'
|
||
import router, { setupRouter } from '@/router'
|
||
import i18n from '@/i18n/index'
|
||
import { setupStore } from '@/store'
|
||
import { setupNaive, setupDirectives, setupCustomComponents, initFunction } from '@/plugins'
|
||
import { GoAppProvider } from '@/components/GoAppProvider/index'
|
||
import { setHtmlTheme, getUrlQueryParams } from '@/utils'
|
||
import { useSystemStore } from '@/store/modules/systemStore/systemStore'
|
||
import { SystemStoreUserInfoEnum, SystemStoreEnum } from '@/store/modules/systemStore/systemStore.d'
|
||
import { useDesignStore } from '@/store/modules/designStore/designStore'
|
||
import { ThemeEnum } from '@/enums/styleEnum'
|
||
import { addCollection } from 'iconify-icon'
|
||
import uimIcons from '@iconify/json/json/uim.json'
|
||
import lineMdIcons from '@iconify/json/json/line-md.json'
|
||
import wiIcons from '@iconify/json/json/wi.json'
|
||
|
||
// 引入全局样式
|
||
import '@/styles/pages/index.scss'
|
||
// 引入动画
|
||
import 'animate.css/animate.min.css'
|
||
// 引入标尺
|
||
import 'vue3-sketch-ruler/lib/style.css'
|
||
// 注册图标
|
||
addCollection(uimIcons)
|
||
addCollection(lineMdIcons)
|
||
addCollection(wiIcons)
|
||
|
||
async function appInit() {
|
||
const goAppProvider = createApp(GoAppProvider)
|
||
|
||
const app = createApp(App)
|
||
|
||
// 注册全局常用的 naive-ui 组件
|
||
setupNaive(app)
|
||
|
||
// 注册全局自定义指令
|
||
setupDirectives(app)
|
||
|
||
// 注册全局自定义组件
|
||
setupCustomComponents(app)
|
||
|
||
// 挂载状态管理
|
||
setupStore(app)
|
||
|
||
// iframe 嵌入模式:从 URL 参数中提取 token 并自动登录
|
||
const urlParams = getUrlQueryParams()
|
||
const systemStore = useSystemStore()
|
||
|
||
if (urlParams.token) {
|
||
// URL 中有 token,保存到本地
|
||
systemStore.setItem(SystemStoreEnum.USER_INFO, {
|
||
[SystemStoreUserInfoEnum.USER_TOKEN]: urlParams.token,
|
||
[SystemStoreUserInfoEnum.TOKEN_NAME]: 'token',
|
||
[SystemStoreUserInfoEnum.USER_ID]: 'iframe-user',
|
||
[SystemStoreUserInfoEnum.USER_NAME]: 'iframe-user',
|
||
[SystemStoreUserInfoEnum.NICK_NAME]: 'iframe-user',
|
||
})
|
||
console.log('[GoView] iframe 模式:已从 URL 参数中获取 token 并保存到本地')
|
||
} else {
|
||
// URL 中没有 token,从本地读取
|
||
const localToken = systemStore.getUserInfo?.[SystemStoreUserInfoEnum.USER_TOKEN]
|
||
if (localToken) {
|
||
console.log('[GoView] iframe 模式:已从本地存储读取 token')
|
||
} else {
|
||
console.warn('[GoView] iframe 模式:URL 和本地都没有 token')
|
||
}
|
||
}
|
||
|
||
// iframe 嵌入模式:从 URL 参数中提取 mode 并设置主题
|
||
const designStore = useDesignStore()
|
||
|
||
if (urlParams.mode) {
|
||
// URL 中有 mode,设置主题并保存到本地
|
||
const isDark = urlParams.mode === 'dark'
|
||
const themeName = isDark ? ThemeEnum.DARK : ThemeEnum.LIGHT
|
||
|
||
if (designStore.themeName !== themeName) {
|
||
designStore.setTheme(isDark)
|
||
console.log(`[GoView] iframe 模式:已设置主题为 ${urlParams.mode} 并存储到本地`)
|
||
}
|
||
} else {
|
||
// URL 中没有 mode,使用本地已保存的主题
|
||
console.log(`[GoView] iframe 模式:使用本地主题 ${designStore.themeName}`)
|
||
}
|
||
|
||
// 解决路由守卫,Axios中可使用,Dialog,Message 等全局组件
|
||
goAppProvider.mount('#appProvider', true)
|
||
|
||
// 挂载路由
|
||
setupRouter(app)
|
||
|
||
// 路由准备就绪后挂载APP实例
|
||
await router.isReady()
|
||
|
||
// Store 准备就绪后处理主题色
|
||
setHtmlTheme()
|
||
|
||
// 语言注册
|
||
app.use(i18n)
|
||
|
||
// 挂载到页面
|
||
app.mount('#app', true)
|
||
|
||
// 挂载到 window
|
||
window['$vue'] = app
|
||
}
|
||
|
||
appInit().then(() => {
|
||
initFunction()
|
||
})
|
||
|