| | |
| | | <script setup> |
| | | import { onLaunch, onShow, onHide } from '@dcloudio/uni-app' |
| | | import { useI18n } from 'vue-i18n' |
| | | import { onLaunch } from '@dcloudio/uni-app'; |
| | | import { getToken } from '@/utils/auth'; |
| | | import { useUserStore } from '@/stores/user'; |
| | | |
| | | const { t } = useI18n() |
| | | // 定义页面白名单 |
| | | const pageWhiteList = [ |
| | | 'pages/login/Login', |
| | | 'pages/login/DingTalkLogin' |
| | | ]; |
| | | |
| | | // 白名单检查 |
| | | const isPageInWhiteList = (currentPage) => { |
| | | return pageWhiteList.some(path => currentPage.includes(path)); |
| | | } |
| | | |
| | | // 全局标记 |
| | | let isProcessingSSO = false; |
| | | |
| | | onLaunch(() => { |
| | | console.log('App Launch') |
| | | try { |
| | | const currentLang = uni.getStorageSync('language') |
| | | console.log('当前语言:', currentLang) |
| | | } catch (error) { |
| | | console.error('语言配置错误:', error) |
| | | } |
| | | }) |
| | | console.log('App Launch'); |
| | | |
| | | 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(); |
| | | }); |
| | | |
| | | onShow(() => { |
| | | console.log('App Show') |
| | | }) |
| | | |
| | | onHide(() => { |
| | | console.log('App Hide') |
| | | }) |
| | | // 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('无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])}`); |
| | | } |
| | | } |
| | | |
| | | if (queryParams.length > 0) { |
| | | loginUrl += `?${queryParams.join('&')}`; |
| | | } |
| | | } |
| | | |
| | | setTimeout(() => { |
| | | uni.redirectTo({ url: loginUrl }); |
| | | }, 100); |
| | | } |
| | | return; |
| | | } |
| | | |
| | | try { |
| | | const current = await uni.$uapi.get("/getInfo"); |
| | | |
| | | if (current && current.user) { |
| | | userStore.setUserInfo(current.user); |
| | | if (current.roles) { |
| | | userStore.setroleKey(current.roles); |
| | | } |
| | | |
| | | if (isPageInWhiteList(currentPage)) { |
| | | uni.switchTab({ url: '/pages/index/index' }); |
| | | } |
| | | } else { |
| | | console.error('token无效'); |
| | | userStore.clearUser(); |
| | | if (!isPageInWhiteList(currentPage)) { |
| | | uni.redirectTo({ url: '/pages/login/Login' }); |
| | | } |
| | | } |
| | | } catch (error) { |
| | | console.error('初始化失败:', error); |
| | | userStore.clearUser(); |
| | | if (!isPageInWhiteList(currentPage)) { |
| | | uni.redirectTo({ url: '/pages/login/Login' }); |
| | | } |
| | | } |
| | | }; |
| | | </script> |
| | | |
| | | <style lang="scss"> |
| | | @import "@/uni_modules/uview-plus/index.scss"; |
| | | |
| | | // 主题颜色 |
| | | $primary-color: #0f95b0; |
| | | $primary-color: #67AFAB; |
| | | $primary-light: rgba($primary-color, 0.1); |
| | | $primary-gradient: linear-gradient(135deg, #0f95b0, #89C4C1); |
| | | $primary-gradient: linear-gradient(135deg, #67AFAB, #89C4C1); |
| | | |
| | | // 文字颜色 |
| | | $text-primary: #333333; |