From 2cfcfe48b7c72d6262a46c39ec10a2898fac2c79 Mon Sep 17 00:00:00 2001 From: huanghao1412 Date: Fri, 1 Mar 2024 16:26:04 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=A4=A7=E5=B1=8F=E9=A2=84=E8=A7=88?= =?UTF-8?q?=E9=A1=B5=E5=A2=9E=E5=8A=A0=E7=BC=93=E5=AD=98=20=E9=A2=84?= =?UTF-8?q?=E5=8A=A0=E8=BD=BD=E6=89=80=E6=9C=89=E7=BB=84=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.vue | 9 ++- src/api/axios.ts | 27 +++++++-- src/api/path/business.api.ts | 19 +++++- src/components/VDialog/index.vue | 22 +++++++ src/layout/components/LayoutMain/index.vue | 5 ++ src/router/index.ts | 3 +- src/store/modules/modalStore/modalStore.ts | 30 ++++++++++ src/store/modules/routerStore/routerStore.ts | 4 ++ src/utils/utils.ts | 1 - src/views/preview/suspenseIndex.vue | 36 ++++++++++- src/views/preview/utils/storage.ts | 63 ++++++++++++++++++++ 11 files changed, 209 insertions(+), 10 deletions(-) create mode 100644 src/components/VDialog/index.vue create mode 100644 src/store/modules/modalStore/modalStore.ts diff --git a/src/App.vue b/src/App.vue index a2037c3e..0a34be1e 100644 --- a/src/App.vue +++ b/src/App.vue @@ -22,6 +22,7 @@ import { getToken } from '@/api/path' import { useRouterStore } from '@/store/modules/routerStore/routerStore' import { useOriginStore } from '@/store/modules/originStore/originStore' import { useGetMessageByParent } from '@/utils/utils' +import router from '@/router' const {getMessageByParent} = useGetMessageByParent() @@ -43,7 +44,13 @@ getMessageByParent('getStore', (e) => { } }) -getToken() +getMessageByParent('loaded', (e) => { + if(e.data.type === 'changeRoute' && e.data.page === 'customLargeScreen') { + routerStore.setToken(e.data.token) + router.push(e.data.path ? e.data.path : '/chart/preview/null') + } +}) +// getToken() // 暗黑主题 const darkTheme = useDarkThemeHook() diff --git a/src/api/axios.ts b/src/api/axios.ts index 3a7a82e6..3b4b13d4 100644 --- a/src/api/axios.ts +++ b/src/api/axios.ts @@ -55,10 +55,29 @@ axiosInstance.interceptors.response.use( const logOutCodeList = ['00004', '000012', '000013'] if(logOutCodeList.some(_ => _ === errcode)) { - window['$message'].error(errmsg) - setTimeout(() => { - postMessageToParent({ - type: 'logOut' + // window['$message'].error(errmsg) + // setTimeout(() => { + // postMessageToParent({ + // type: 'logOut' + // }) + // }) + + // axios加载时 pinia还没加载好 要异步加载拿到pinia + import('@/store/modules/modalStore/modalStore').then(res => { + const modalStore = res.useModalStore() + modalStore.setModalStore({ + showModal: true, + title: '提示', + content: errmsg, + positiveText: '重新登录', + positiveClick: () => { + modalStore.clear() + setTimeout(() => { + postMessageToParent({ + type: 'logOut' + }) + }, 500) + }, }) }) return Promise.resolve(res.data) diff --git a/src/api/path/business.api.ts b/src/api/path/business.api.ts index 3ece6324..5fd4262e 100644 --- a/src/api/path/business.api.ts +++ b/src/api/path/business.api.ts @@ -4,6 +4,7 @@ import { RequestHttpEnum } from "@/enums/httpEnum"; import { httpErrorHandle } from '@/utils' import moment from 'moment' +/* export function getToken() { // 为了开发时只单独展示goview 需要localStorage // const storage_access_token = localStorage.getItem('access_token_obj') @@ -26,10 +27,26 @@ export function getToken() { // if(query.access_token) localStorage.setItem('access_token_obj', JSON.stringify(obj)) return query.access_token } + */ + +// 初次等待久一点 因为dom加载后才能获取父页面传来的token 才能存入store +export async function getToken() { + return new Promise(resolve => { + let timer = setInterval(() => { + import('@/store/modules/routerStore/routerStore').then(res => { + let routerStore = res.useRouterStore() + if(routerStore.token) { + clearInterval(timer) + resolve(routerStore.token) + } + }) + }, 100) + }) +} export const publicInterface = async (paramType:string, interfaceType:string, paramData?:unknown) =>{ try { - const access_token = getToken() + const access_token = await getToken() const res = await http(RequestHttpEnum.POST)(paramType, { access_token, type: interfaceType, diff --git a/src/components/VDialog/index.vue b/src/components/VDialog/index.vue new file mode 100644 index 00000000..d3f9bdc8 --- /dev/null +++ b/src/components/VDialog/index.vue @@ -0,0 +1,22 @@ + + + + + \ No newline at end of file diff --git a/src/layout/components/LayoutMain/index.vue b/src/layout/components/LayoutMain/index.vue index 11cc3bbc..399bbe71 100644 --- a/src/layout/components/LayoutMain/index.vue +++ b/src/layout/components/LayoutMain/index.vue @@ -7,4 +7,9 @@ + + + diff --git a/src/router/index.ts b/src/router/index.ts index 8b633e99..cecd06d7 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -11,7 +11,8 @@ const RootRoute: Array = [ { path: '/', name: 'Root', - redirect: PageEnum.BASE_HOME, + // redirect: PageEnum.BASE_HOME, + redirect: '/chart/preview/null', component: Layout, meta: { title: 'Root', diff --git a/src/store/modules/modalStore/modalStore.ts b/src/store/modules/modalStore/modalStore.ts new file mode 100644 index 00000000..21b9be27 --- /dev/null +++ b/src/store/modules/modalStore/modalStore.ts @@ -0,0 +1,30 @@ +import { defineStore } from 'pinia' + +export const useModalStore = defineStore({ + id: 'useModalStore', + state: () => ({ + showModal: false, + title: '', + content: '', + positiveText: '', + negativeText: '', + positiveClick: () => {}, + negativeClick: () => {}, + }) as Record, + actions: { + setModalStore(obj: Record) { + for(let k in obj) { + this[k] = obj[k] + } + }, + clear() { + this.showModal = false + this.title = '' + this.content = '' + this.positiveText = '' + this.negativeText = '' + this.positiveClick = () => {} + this.negativeClick = () => {} + } + } +}) diff --git a/src/store/modules/routerStore/routerStore.ts b/src/store/modules/routerStore/routerStore.ts index 15186518..5297bfb9 100644 --- a/src/store/modules/routerStore/routerStore.ts +++ b/src/store/modules/routerStore/routerStore.ts @@ -5,6 +5,7 @@ export const useRouterStore = defineStore({ state: () => ({ // 为true是router交给parent调用自身不跳转 为false使用自身路由 callByParent: false, + token: null as null | string, }), getters: { getCallByParent():boolean { @@ -14,6 +15,9 @@ export const useRouterStore = defineStore({ actions: { setCallByParent(callByParent: boolean) { this.callByParent = callByParent + }, + setToken(v: string | null) { + this.token = v } } }) diff --git a/src/utils/utils.ts b/src/utils/utils.ts index e8bf1cd1..ad5443ba 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -294,7 +294,6 @@ export const JSONStringify = (data: T) => { export const evalFn = (fn: string) => { var Fun = Function // 一个变量指向Function,防止前端编译工具报错 - console.log(fn) return new Fun('return ' + fn)() } diff --git a/src/views/preview/suspenseIndex.vue b/src/views/preview/suspenseIndex.vue index bdb1aa32..ae8af3b0 100644 --- a/src/views/preview/suspenseIndex.vue +++ b/src/views/preview/suspenseIndex.vue @@ -27,7 +27,7 @@