gaosp
2024-01-05 ea911d3b42cac8948eb6f1285c7ad46706707643
src/permission.js
@@ -1,3 +1,9 @@
/*
 * @Date: 2022-11-09 12:37:25
 * @LastEditors: Sneed
 * @LastEditTime: 2024-01-05 22:50:54
 * @FilePath: /belleson-frontend/Users/mache/Documents/demo/mdc/src/permission.js
 */
import router from './router'
import store from './store'
import NProgress from 'nprogress' // Progress 进度条
@@ -16,49 +22,50 @@
router.beforeEach((to, from, next) => {
  NProgress.start()
  initApplicationCfg().then(res => {
    // console.log(to.path)
    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()
          }
        })
        // store.dispatch('GetInfo').then(res => { // 拉取用户信息
        //   if (to.path === '/') {
        //     next('/home')
        //   }
        //   // 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()
        //   }
        // })
        next()
      } else {
        next()
      }