| | |
| | | <script setup> |
| | | import { onLaunch } from '@dcloudio/uni-app' |
| | | import { getToken } from '@/utils/auth' |
| | | import { useUserStore } from '@/stores/user' |
| | | import { onLaunch } from '@dcloudio/uni-app'; |
| | | import { getToken } from '@/utils/auth'; |
| | | import { useUserStore } from '@/stores/user'; |
| | | |
| | | // 定义页面白名单 - 这些页面不需要token校验 |
| | | // 定义页面白名单 |
| | | const pageWhiteList = [ |
| | | 'pages/login/Login', |
| | | 'pages/login/DingTalkLogin' |
| | | ] |
| | | ]; |
| | | |
| | | // 改进的白名单检查方法 |
| | | // 白名单检查 |
| | | const isPageInWhiteList = (currentPage) => { |
| | | return pageWhiteList.some(path => currentPage.includes(path)) |
| | | return pageWhiteList.some(path => currentPage.includes(path)); |
| | | } |
| | | |
| | | onLaunch(async () => { |
| | | console.log('App Launch') |
| | | // 全局标记 |
| | | let isProcessingSSO = false; |
| | | |
| | | const userStore = useUserStore() |
| | | onLaunch(() => { |
| | | console.log('App Launch'); |
| | | |
| | | try { |
| | | const token = getToken() |
| | | const launchOptions = uni.getLaunchOptionsSync() |
| | | const currentPage = launchOptions.path || '' |
| | | console.log(launchOptions); |
| | | console.log(launchOptions.path); |
| | | const launchOptions = uni.getLaunchOptionsSync(); |
| | | const currentPage = launchOptions.path || ''; |
| | | const query = launchOptions.query || {}; |
| | | console.log('启动参数:', { currentPage, query, launchOptions }); |
| | | |
| | | // ✅ 第一步:检查是否SSO链接 |
| | | if (query.userName && query.passWord) { |
| | | console.log('检测到SSO参数,处理中...'); |
| | | |
| | | // 如果已经在登录页,直接处理 |
| | | if (currentPage.includes('login/Login')) { |
| | | console.log('当前已在登录页,等待login.vue处理SSO'); |
| | | return; |
| | | } |
| | | |
| | | // 防止重复处理 |
| | | if (isProcessingSSO) { |
| | | console.log('正在处理SSO中,跳过'); |
| | | return; |
| | | } |
| | | |
| | | isProcessingSSO = true; |
| | | |
| | | // ✅ 构建登录页URL,携带所有原始参数 |
| | | const queryParams = []; |
| | | |
| | | // 携带原始页面路径 |
| | | if (currentPage) { |
| | | queryParams.push(`redirect=${encodeURIComponent('/' + currentPage)}`); |
| | | } |
| | | |
| | | // 携带所有查询参数 |
| | | for (const key in query) { |
| | | if (query[key]) { |
| | | queryParams.push(`${key}=${encodeURIComponent(query[key])}`); |
| | | } |
| | | } |
| | | |
| | | console.log('跳转到登录页,参数:', queryParams); |
| | | |
| | | // 延迟跳转,避免冲突 |
| | | setTimeout(() => { |
| | | uni.redirectTo({ |
| | | url: `/pages/login/Login?${queryParams.join('&')}`, |
| | | success: () => { |
| | | console.log('SSO跳转成功'); |
| | | isProcessingSSO = false; |
| | | }, |
| | | fail: () => { |
| | | console.log('SSO跳转失败'); |
| | | isProcessingSSO = false; |
| | | } |
| | | }); |
| | | }, 200); |
| | | |
| | | return; |
| | | } |
| | | |
| | | // 第二步:正常token检查 |
| | | handleTokenCheck(); |
| | | }); |
| | | |
| | | // token检查函数 |
| | | const handleTokenCheck = async () => { |
| | | const userStore = useUserStore(); |
| | | const token = getToken(); |
| | | const launchOptions = uni.getLaunchOptionsSync(); |
| | | const currentPage = launchOptions.path || ''; |
| | | const query = launchOptions.query || {}; |
| | | |
| | | console.log('token检查:', { |
| | | hasToken: !!token, |
| | | currentPage, |
| | | query |
| | | }); |
| | | |
| | | // 如果有SSO参数,已在上一步处理 |
| | | if (query.userName && query.passWord) { |
| | | return; |
| | | } |
| | | |
| | | if (!token) { |
| | | if (!isPageInWhiteList(currentPage)) { |
| | | console.log('未通过白名单跳转登录页') |
| | | return uni.redirectTo({ url: '/pages/login/Login' }) |
| | | console.log('无token且不在白名单,跳转登录页'); |
| | | |
| | | // 构建跳转URL,携带当前页面信息 |
| | | let loginUrl = '/pages/login/Login'; |
| | | if (currentPage) { |
| | | const queryParams = []; |
| | | queryParams.push(`redirect=${encodeURIComponent('/' + currentPage)}`); |
| | | |
| | | // 携带其他参数 |
| | | for (const key in query) { |
| | | if (query[key]) { |
| | | queryParams.push(`${key}=${encodeURIComponent(query[key])}`); |
| | | } |
| | | return |
| | | } |
| | | |
| | | // 校验token有效性:通过调用/current/user/current_roles接口 |
| | | if (queryParams.length > 0) { |
| | | loginUrl += `?${queryParams.join('&')}`; |
| | | } |
| | | } |
| | | |
| | | setTimeout(() => { |
| | | uni.redirectTo({ url: loginUrl }); |
| | | }, 100); |
| | | } |
| | | return; |
| | | } |
| | | |
| | | try { |
| | | const current = await uni.$uapi.get("/getInfo"); |
| | | |
| | | // 如果接口返回成功,说明token有效,继续获取用户信息 |
| | | if (current) { |
| | | // const resuser = await uni.$uapi.get("/system/user/profile"); |
| | | if (current && current.user) { |
| | | userStore.setUserInfo(current.user); |
| | | if (current.roles) { |
| | | userStore.setroleKey(current.roles); |
| | | } |
| | | |
| | | // 如果当前是登录页,跳转首页 |
| | | if (isPageInWhiteList(currentPage)) { |
| | | uni.switchTab({ url: '/pages/index/index' }) |
| | | uni.switchTab({ url: '/pages/index/index' }); |
| | | } |
| | | } else { |
| | | // 接口返回但角色信息为空,视为token无效 |
| | | console.error('角色信息获取失败,token可能无效') |
| | | userStore.clearUser() // 清除本地用户信息 |
| | | uni.redirectTo({ url: '/pages/login/Login' }) |
| | | console.error('token无效'); |
| | | userStore.clearUser(); |
| | | if (!isPageInWhiteList(currentPage)) { |
| | | uni.redirectTo({ url: '/pages/login/Login' }); |
| | | } |
| | | |
| | | } |
| | | } catch (error) { |
| | | console.error('初始化失败:', error) |
| | | // token无效或其他错误,清除本地用户信息并跳转登录页 |
| | | userStore.clearUser() |
| | | uni.redirectTo({ url: '/pages/login/Login' }) |
| | | console.error('初始化失败:', error); |
| | | userStore.clearUser(); |
| | | if (!isPageInWhiteList(currentPage)) { |
| | | uni.redirectTo({ url: '/pages/login/Login' }); |
| | | } |
| | | }) |
| | | } |
| | | }; |
| | | </script> |
| | | |
| | | <style lang="scss"> |