feat: token

This commit is contained in:
ygx
2025-12-27 12:57:38 +08:00
parent d760e8f6cc
commit 7a5fa1f4ea
4 changed files with 95 additions and 10 deletions

View File

@@ -40,7 +40,12 @@ axiosInstance.interceptors.request.use(
const userInfo = info[SystemStoreEnum.USER_INFO]
if (userInfo && userInfo[SystemStoreUserInfoEnum.USER_TOKEN]) {
config.headers['authorization'] = userInfo[SystemStoreUserInfoEnum.USER_TOKEN]
console.log('[GoView] 请求拦截器:添加 authorization 头:', userInfo[SystemStoreUserInfoEnum.USER_TOKEN])
} else {
console.warn('[GoView] 请求拦截器:未找到 token')
}
} else {
console.warn('[GoView] 请求拦截器:未找到 GO_SYSTEM_STORE')
}
return config

View File

@@ -45,8 +45,10 @@ async function appInit() {
// iframe 嵌入模式:从 URL 参数中提取 token 并自动登录
const urlParams = getUrlQueryParams()
const systemStore = useSystemStore()
if (urlParams.token) {
const systemStore = useSystemStore()
// URL 中有 token保存到本地
systemStore.setItem(SystemStoreEnum.USER_INFO, {
[SystemStoreUserInfoEnum.USER_TOKEN]: urlParams.token,
[SystemStoreUserInfoEnum.TOKEN_NAME]: 'token',
@@ -54,22 +56,32 @@ async function appInit() {
[SystemStoreUserInfoEnum.USER_NAME]: 'iframe-user',
[SystemStoreUserInfoEnum.NICK_NAME]: 'iframe-user',
})
console.log('[GoView] iframe 模式:已从 URL 参数中获取 token 并初始化')
console.log('[GoView] iframe 模式:已从 URL 参数中获取 token 并保存到本地')
} else {
console.warn('[GoView] iframe 模式:未在 URL 中检测到 token 参数')
// 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) {
const designStore = useDesignStore()
// URL 中有 mode设置主题并保存到本地
const isDark = urlParams.mode === 'dark'
const themeName = isDark ? ThemeEnum.DARK : ThemeEnum.LIGHT
// 如果主题不同则设置并持久化到localStorage
if (designStore.themeName !== themeName) {
designStore.setTheme(isDark)
console.log(`[GoView] iframe 模式:已设置主题为 ${urlParams.mode} 并存储到本地`)
}
} else {
// URL 中没有 mode使用本地已保存的主题
console.log(`[GoView] iframe 模式:使用本地主题 ${designStore.themeName}`)
}
// 解决路由守卫Axios中可使用DialogMessage 等全局组件

View File

@@ -8,12 +8,14 @@ import { setHtmlTheme } from '@/utils'
/**
* 从 URL 参数中提取 token 并自动登录
* 如果 URL 中没有 token则使用本地存储的 token
*/
function autoLoginFromUrlToken(query: any) {
const token = query.token
const systemStore = useSystemStore()
if (token) {
const systemStore = useSystemStore()
// 存储 token 到 store
// URL 中有 token保存到 store
systemStore.setItem(SystemStoreEnum.USER_INFO, {
[SystemStoreUserInfoEnum.USER_TOKEN]: token,
[SystemStoreUserInfoEnum.TOKEN_NAME]: 'token',
@@ -21,28 +23,40 @@ function autoLoginFromUrlToken(query: any) {
[SystemStoreUserInfoEnum.USER_NAME]: 'iframe-user',
[SystemStoreUserInfoEnum.NICK_NAME]: 'iframe-user',
})
console.log('[GoView] 已从 URL 参数中获取 token 并自动登录')
console.log('[GoView] 已从 URL 参数中获取 token 并保存到本地')
return true
} else {
// URL 中没有 token检查本地是否有 token
const localToken = systemStore.getUserInfo?.[SystemStoreUserInfoEnum.USER_TOKEN]
if (localToken) {
console.log('[GoView] 使用本地存储的 token')
return true
}
}
return false
}
/**
* 从 URL 参数中提取 mode 并切换主题
* 如果 URL 中没有 mode则使用本地存储的主题
*/
function autoSwitchThemeFromUrlMode(query: any) {
const mode = query.mode
const designStore = useDesignStore()
if (mode) {
const designStore = useDesignStore()
// URL 中有 mode设置主题并保存到本地
const isDark = mode === 'dark'
const themeName = isDark ? ThemeEnum.DARK : ThemeEnum.LIGHT
// 如果主题不同则设置并持久化到localStorage
if (designStore.themeName !== themeName) {
designStore.setTheme(isDark)
setHtmlTheme(themeName)
console.log(`[GoView] 已切换主题为 ${mode} 并存储到本地`)
}
} else {
// URL 中没有 mode使用本地已保存的主题
console.log(`[GoView] 使用本地主题 ${designStore.themeName}`)
}
}

View File

@@ -8,8 +8,62 @@
</template>
<script setup lang="ts">
import { onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { ProjectItemsList } from './components/ProjectItemsList'
import { ProjectLayoutCreate } from '../layout/components/ProjectLayoutCreate'
import { useSystemStore } from '@/store/modules/systemStore/systemStore'
import { useDesignStore } from '@/store/modules/designStore/designStore'
import { SystemStoreEnum, SystemStoreUserInfoEnum } from '@/store/modules/systemStore/systemStore.d'
import { ThemeEnum } from '@/enums/styleEnum'
import { setHtmlTheme } from '@/utils'
const route = useRoute()
const systemStore = useSystemStore()
const designStore = useDesignStore()
// 组件挂载时处理 URL 参数中的 token 和 mode
onMounted(() => {
const { token, mode } = route.query
// 处理 token 参数
if (token && typeof token === 'string') {
console.log('[GoView] 准备更新 tokenURL 中的 token:', token)
console.log('[GoView] 更新前的 userInfo:', JSON.stringify(systemStore.getUserInfo))
systemStore.setItem(SystemStoreEnum.USER_INFO, {
[SystemStoreUserInfoEnum.USER_TOKEN]: token,
[SystemStoreUserInfoEnum.TOKEN_NAME]: 'token',
[SystemStoreUserInfoEnum.USER_ID]: 'iframe-user',
[SystemStoreUserInfoEnum.USER_NAME]: 'iframe-user',
[SystemStoreUserInfoEnum.NICK_NAME]: 'iframe-user',
})
console.log('[GoView] 更新后的 userInfo:', JSON.stringify(systemStore.getUserInfo))
console.log('[GoView] /project/items 路由:已从 URL 参数中获取 token 并保存到本地')
} else {
const localToken = systemStore.getUserInfo?.[SystemStoreUserInfoEnum.USER_TOKEN]
if (localToken) {
console.log('[GoView] /project/items 路由:使用本地存储的 token:', localToken)
} else {
console.warn('[GoView] /project/items 路由URL 和本地都没有 token')
}
}
// 处理 mode 参数
if (mode && typeof mode === 'string') {
const isDark = mode === 'dark'
const themeName = isDark ? ThemeEnum.DARK : ThemeEnum.LIGHT
if (designStore.themeName !== themeName) {
designStore.setTheme(isDark)
setHtmlTheme(themeName)
console.log(`[GoView] /project/items 路由:已设置主题为 ${mode} 并存储到本地`)
}
} else {
console.log(`[GoView] /project/items 路由:使用本地主题 ${designStore.themeName}`)
}
})
</script>
<style lang="scss" scoped>