gaoshp
2024-01-05 b5db49ce1e4c678ce4a7120928811a9621128b8e
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
import router from './router'
import store from './store'
import NProgress from 'nprogress' // Progress 进度条
import 'nprogress/nprogress.css'// Progress 进度条样式
import { getToken } from '@/utils/auth' // 验权
import { Message } from 'element-ui'
import { initApplicationCfg } from './api/MdcApi'
if (process.env.NODE_ENV !== 'development') {
  if (window.console) {
    window.console.log = function() {}
  } else if (console) {
    console.log = function() {}
  }
}
const whiteList = ['/'] // 不重定向白名单
router.beforeEach((to, from, next) => {
  NProgress.start()
  initApplicationCfg().then(res => {
    // console.log(to.path)
    if (getToken().value) {
      if (store.getters.workshopList.length === 0) {
        store.dispatch('GetInfo').then(res => { // 拉取用户信息
          if (to.path === '/') {
            next('/dashboard/index')
          }
          // if (to.path !== '/' && res.code === 0) {
          if (res.code === 0) {
            Message({
              message: res.msg,
              type: 'error',
              duration: 3 * 1000
            })
          }
          // if (to.path === '/' && store.getters.workshopList.length > 0) {
          //   next({ path: `/workshop/${store.getters.workshopList[0].workshopId}` })
          // } else
          if (to.path.indexOf('workshop') > -1) {
            // 车间
            const workshopIds = store.getters.workshopList.map((item) => item.status.id)
            if (to.params.pid === 'undefined') {
              next('/')
            } else if (workshopIds.indexOf(to.params.pid) > -1) {
              next({ path: `/workshop/${to.params.pid}` })
            } else {
              next({ path: `/workshop/${workshopIds[0]}` })
            }
          } else if (to.path.indexOf('device') > -1) {
            // 设备
            const cateIds = store.getters.deviceCategories.map((item) => item.type)
            // console.log(to.params.did)
            if (to.params.did === 'undefined') {
              next('/')
            } else if (cateIds.indexOf(to.params.did) > -1) {
              next({ path: `/device/${to.params.did}` })
            } else {
              next({ path: `/device/${cateIds[0]}` })
            }
          } else {
            next()
          }
        })
      } else {
        next()
      }
    } else {
      if (whiteList.indexOf(to.path) !== -1) {
        next()
      } else {
        next('/')
        NProgress.done()
      }
    }
  })
})
router.afterEach((to) => {
  if (to.path.indexOf('workshop') > -1) {
    // 车间
    // const [{ roomName = '' }] = store.getters.workshopList.filter((item) => item.workshopId === to.params.pid)
    // console.log(roomName)
    // to.matched[to.matched.length - 1].meta.title = roomName
  } else if (to.path.indexOf('device') > -1) {
    // 设备
    // const [{ remark = '' }] = store.getters.deviceCategories.filter((item) => item.type === to.params.did)
    // to.matched[to.matched.length - 1].meta.title = remark
  }
  NProgress.done() // 结束Progress
})