p-honggang.li
2025-08-29 57c6a02c135c1224e9de2766e412eedd4cd18d58
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * @Description:
 * @Version: 2.0
 * @Autor: wuyun
 * @Date: 2024-07-15 14:15:53
 * @LastEditors: wuyun
 * @LastEditTime: 2024-09-10 17:07:33
 */
import { createRouter, createWebHashHistory,createWebHistory } from 'vue-router'
import { staticRoutes } from '@/router/staticRoutes'
import { useTitle } from '@vueuse/core'
import { useUserInfo } from '@/stores/modules/userInfo'
import { useCommonInfo } from '@/stores/modules/common' // 状态管理器
import { useNavTabs } from '@/stores/modules/navTabs' // 标签导航
import { queryUserDetail } from '@/api/userInfo'
 
const router = createRouter({
  history: createWebHistory('/trade/'),
  routes: staticRoutes,
  scrollBehavior(to, from, savedPosition) {
    return { top: 0 }
  },
})
router.beforeEach(async (to, from, next) => {
  console.log("to",to)
  const userStore = useUserInfo()
  // * 在跳转路由之前,清除所有的请求
  if (to.meta.title) {
    const title = useTitle()
    title.value = to.meta.title.toString()
  }
  const commonStore = useCommonInfo()
  commonStore.updateActiveMenu(to.path)
  const navStore = useNavTabs()
  
  // 开发模式下跳过登录验证
  if (import.meta.env.DEV) {
    // 如果访问登录页面,直接跳转到首页
    if (to.path === '/loginT') {
      next({ path: '/home' })
      return
    }
    // 如果没有token,设置一个默认token并继续
    if (!userStore.getAdminToken) {
      const localUserInfo = localStorage.getItem('userInfo')
      if (localUserInfo) {
          const userInfo = JSON.parse(localUserInfo)
          if (userInfo.adminToken && userInfo.adminToken !== '') {
          const obj: any = {
            adminToken: userInfo.adminToken,
            screenToken: '',
          }
          userStore.updateUserInfo(obj)
        }
      }
    }
    next()
    return
  }
  
  // 生产环境保持原有逻辑
  if (to.path == '/login' || to.path == '/loginT') {
    // 如果路径是 /login 则正常执行
    next()
  } else {
    if (to.query.token) {
      const token = to.query.token as string
      localStorage.setItem('lastRecordTime', new Date().getTime().toString())
      const obj: any = {
        adminToken: token,
        screenToken: '',
      }
      userStore.updateUserInfo(obj)
      const localUserInfo = localStorage.getItem('userInfo')
      if (localUserInfo) {
        const userInfo = JSON.parse(localUserInfo)
        userStore.updateUserDetail(userInfo)
      }
      // chrome
      document.body.scrollTop = 0
      // firefox
      document.documentElement.scrollTop = 0
      // safari
      window.pageYOffset = 0
      next()
    } else if (!userStore.getAdminToken) {
      // 从 localStorage 中获取 userInfo 的 adminToken
      const localUserInfo = localStorage.getItem('userInfo')
      if (localUserInfo) {
        try {
          const userInfo = JSON.parse(localUserInfo)
          if (userInfo.adminToken) {
            // 如果获取到 adminToken,则更新 userStore
            userStore.updateUserInfo({
              adminToken: userInfo.adminToken,
              screenToken: userInfo.screenToken || '',
              fullUnitName: userInfo.fullUnitName || '',
              empCode: userInfo.empCode || '',
              menuList: userInfo.menuList || []
            })
            // 同时更新用户详细信息
            userStore.updateUserDetail(userInfo)
            next()
          } else {
            // 如果没有 adminToken,则用户未登录
            next({ path: '/loginT' })
          }
        } catch (error) {
          console.error('解析 localStorage 中的 userInfo 失败:', error)
          // 解析失败,认为用户未登录
          next({ path: '/loginT' })
        }
      } else {
        // 如果没有 userInfo,则用户未登录
        next({ path: '/loginT' })
      }
    } else {
      // 获取用户信息
      const localUserInfo = localStorage.getItem('userInfo')
      if (localUserInfo) {
        const userInfo = JSON.parse(localUserInfo)
        userStore.updateUserDetail(userInfo)
      }
      next()
    }
  }
})
export default router