Newer
Older
smartwell_front_dz / src / permission.js
wangxitong on 8 Jul 2024 5 KB 达州变更
import router from './router'
import store from './store'
import NProgress from 'nprogress' // progress bar 进度条
import 'nprogress/nprogress.css' // progress bar style 进度条样式
// import { Message } from 'element-ui'
import { getToken, getUser, setUser } from '@/utils/auth' // getToken from cookie
import { loginByToken, isSubSystem, sysCode, loginByUsername } from './settings'
import Vue from 'vue'
import { setToken, removeToken } from '@/utils/auth'
import { RSAencrypt } from '@/utils/security'
import { uerLogin } from '@/api/overview/wellOverview'

NProgress.configure({ showSpinner: false })// NProgress configuration

const whiteList = ['/login', 'config/baseconfig', '/overview3d'] // 不重定向白名单
// 全局钩子
router.beforeEach((to, from, next) => {
  NProgress.start() // 加载进度条
  // 如果有token
  const oldToken = getToken() // 获取原token
  const newToken = to.query.token
  console.log('form other system token:' + to.query.token)
  // 允许使用token登录并且新旧token不一致,使用新token重新登录
  if (loginByToken && newToken && oldToken !== newToken) {
    // 调用自动登录接口,否则跳转到登录页
    store.dispatch('TokenLogin', newToken).then(() => {
      console.log('自动登录成功')
      console.log(to.path)
      next({ ...to, replace: true }) // 登录成功继续
    }).catch(() => {
      next('/401') // 否则全部重定向到401没有权限页面
    })
  } else {
    // 如果cookie中已有token,直接进行下一步
    if (oldToken) {
      // 当进入非登陆页时
      if (store.getters.roleList && store.getters.roleList.length === 0) { // 判断当前用户是否已拉取完user_info信息
        try {
          store.dispatch('GetInfo').then(() => {
            // 如果是子系统,参数为setting文件中的sysCode属性
            if (isSubSystem) {
              store.dispatch('GetMenus', { code: sysCode }).then((accessRoute) => {
                router.addRoutes(accessRoute)
                next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
              })
            } else { // 否则直接获取全部菜单权限
              store.dispatch('GetMenus').then((accessRoute) => {
                router.addRoutes(accessRoute)
                next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
              })
            }
          })
        } catch (err) {
          store.dispatch('FedLogOut').then(() => {
            // Message.error(err || '权限验证失败,请重新登录系统')
            next({ path: '/' })
          })
        }
      } else { // 角色不为空
        next()
      }
    }
    else {
      // has no token
      // 免登录白名单,直接进入
      if(to.path === '/overview3d') {
        // console.log(to.query)
        const userCode = to.query.userCode?to.query.userCode:to.query.usercode
        uerLogin(userCode).then(response => {
          if (response.code === 200) {
            setToken(response.data.token)
            store.dispatch('TokenLogin', response.data.token).then(() => {
              try {
                store.dispatch('GetInfo').then(() => {
                  // 如果是子系统,参数为setting文件中的sysCode属性
                  if (isSubSystem) {
                    store.dispatch('GetMenus', { code: sysCode }).then((accessRoute) => {
                      router.addRoutes(accessRoute)
                      next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
                    })
                  } else { // 否则直接获取全部菜单权限
                    store.dispatch('GetMenus').then((accessRoute) => {
                      router.addRoutes(accessRoute)
                      next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
                    })
                  }
                })
              } catch (err) {}
            })
          }
        })
      } else if (whiteList.indexOf(to.path) !== -1) {
        next()
        // next({ ...to, replace: true }) // 登录成功继续
      } else {
        // 如果允许通过链接上的token登录
        // 获取链接里的token
        const token = to.query.token
        console.log('form other system token:' + to.query.token)
        if (loginByToken && token) {
          // 调用自动登录接口,否则跳转到登录页
          store.dispatch('TokenLogin', token).then(() => {
            console.log('自动登录成功')
            console.log(to.path)
            next({ ...to, replace: true }) // 登录成功继续
          }).catch(() => {
            next('/401') // 否则全部重定向到401没有权限页面
          })
        } else {
          // 判断是独立运行的子系统还是和其他基础子系统一起的
          const config = Vue.prototype.baseConfig
          if (config.singleSys) {
            next(`/login`) // 重定向到本系统登录页
          } else { // 跳转到配置的主页
            window.location.href = config.mainPage
          }
        }
        NProgress.done()
      }
    }
  }
})

router.afterEach(() => {
  NProgress.done() // 结束Progress
})