schangxiang@126.com
2025-05-20 b5904672914f1088841208b4084fc7619576fa2e
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
import {
  createRouter,
  createWebHashHistory,
  RouteRecordRaw,
  createWebHistory,
} from 'vue-router'
import pkg from '../package.json'
import sdk from 'sdk'
import { menu, menuMap } from '@/config/menu'
const { request } = sdk.utils
const routes: Array<{
  name: string
  path: string
  component: any
  icon: any
  meta: {
    widgetName: string
  }
}> = []
 
const widgetsMap: any = import.meta.glob('./widgets/*/*.vue')
const widgetsTsxMap: any = import.meta.glob('./widgets/*/Views/*.tsx')
const Map = Object.assign(widgetsMap, widgetsTsxMap)
for (const filePath in Map) {
  if (!filePath.match(/.*\.settings\.vue/)) {
    const component = widgetsMap[filePath]
    const patchName = filePath.split('/')[2]
    routes.push({
      path: `/${pkg.name}/` + patchName,
      name: menuMap?.[patchName]?.name,
      icon: menuMap?.[patchName]?.icon,
      meta: {
        widgetName: patchName,
      },
      component,
    })
  }
}
const allRoutes = routes[0]
  ? [
      {
        ...routes[0],
        path: '/',
        name: '默认页',
      },
      ...routes,
    ]
  : routes
 
export const routeInfo = {
  routes: menu,
}
export const router = createRouter({
  history: createWebHashHistory(),
  routes: allRoutes,
})
 
let lastProjectId = sessionStorage.getItem('X-Project')
 
const initCMSToken = async () => {
  const XProject: any = sessionStorage.getItem('X-Project')
  const token = sessionStorage.getItem('Token')
  let type = ''
  if (!token) {
    type = 'Tourist'
  }
  if (!token || (XProject && lastProjectId !== XProject)) {
    lastProjectId = XProject
    sessionStorage.setItem('X-Project', XProject)
 
    // token
    const rs = await request({
      url: `/api/v1/auth/requesttoken`,
      method: 'post',
      data: type,
      // data: 'string',
    })
    sessionStorage.setItem('Token', rs)
  }
}
 
router.beforeEach(() => {
  initCMSToken()
})