| | |
| | | <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() |
| | | // 定义页面白名单 - 这些页面不需要token校验 |
| | | const pageWhiteList = [ |
| | | 'pages/login/Login', |
| | | 'pages/login/DingTalkLogin' |
| | | ] |
| | | |
| | | onLaunch(() => { |
| | | console.log('App Launch') |
| | | try { |
| | | const currentLang = uni.getStorageSync('language') |
| | | console.log('当前语言:', currentLang) |
| | | } catch (error) { |
| | | console.error('语言配置错误:', error) |
| | | // 改进的白名单检查方法 |
| | | const isPageInWhiteList = (currentPage) => { |
| | | return pageWhiteList.some(path => currentPage.includes(path)) |
| | | } |
| | | }) |
| | | |
| | | onShow(() => { |
| | | console.log('App Show') |
| | | }) |
| | | onLaunch(async () => { |
| | | console.log('App Launch') |
| | | |
| | | onHide(() => { |
| | | console.log('App Hide') |
| | | const userStore = useUserStore() |
| | | |
| | | try { |
| | | const token = getToken() |
| | | const launchOptions = uni.getLaunchOptionsSync() |
| | | const currentPage = launchOptions.path || '' |
| | | console.log(launchOptions); |
| | | console.log(launchOptions.path); |
| | | |
| | | if (!token) { |
| | | if (!isPageInWhiteList(currentPage)) { |
| | | console.log('未通过白名单跳转登录页') |
| | | return uni.redirectTo({ url: '/pages/login/Login' }) |
| | | } |
| | | return |
| | | } |
| | | |
| | | // 校验token有效性:通过调用/current/user/current_roles接口 |
| | | const current = await uni.$uapi.get("/system/user/profile"); |
| | | |
| | | // 如果接口返回成功,说明token有效,继续获取用户信息 |
| | | if (current ) { |
| | | // const resuser = await uni.$uapi.get("/system/user/profile"); |
| | | userStore.setUserInfo(current); |
| | | // userStore.setroleKey(current[0].roleKey); |
| | | |
| | | // 如果当前是登录页,跳转首页 |
| | | if (isPageInWhiteList(currentPage)) { |
| | | uni.switchTab({ url: '/pages/index/index' }) |
| | | } |
| | | } else { |
| | | // 接口返回但角色信息为空,视为token无效 |
| | | console.error('角色信息获取失败,token可能无效') |
| | | userStore.clearUser() // 清除本地用户信息 |
| | | uni.redirectTo({ url: '/pages/login/Login' }) |
| | | } |
| | | |
| | | } catch (error) { |
| | | console.error('初始化失败:', error) |
| | | // token无效或其他错误,清除本地用户信息并跳转登录页 |
| | | userStore.clearUser() |
| | | 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; |