permission.js 3.95 KB
/*
 * 权限模块
 * 1. 获取菜单权限
 * 2. 
 */
import router from './router'
import store from './store'
import { Message } from 'element-ui'
import Cookies from 'js-cookie'
import { getAtoken } from '@/utils/auth'

let menuName = []
function dealMenu(menu, parent) {
  menu.children.forEach((item, index) => {
    let parentTemp = {}
    parentTemp.unid = item.unid
    parentTemp.id = item.perm_unid
    parentTemp.label = item.note
    parentTemp.path = item.name
    parentTemp.parentId = menu.unid
    parentTemp.parentPermId = menu.perm_unid
    parent.children.push(parentTemp)
    menuName.push(item.name)
    if (item.children && item.children.length > 0) {
      dealChildren(item, parent.children, index)
    }
  })
  window.localStorage.setItem('menuName', JSON.stringify(menuName))
  window.localStorage.setItem('menu', JSON.stringify(parent))
  window.sessionStorage.setItem('loginVal', true)
}

function dealChildren(item, parent, index) {
  item.children.forEach((child, childIndex) => {
    let childTemp = {}
    childTemp.unid = child.unid
    childTemp.id = child.perm_unid
    childTemp.label = child.note
    childTemp.path = child.name
    childTemp.parentId = item.unid
    childTemp.parentPermId = item.perm_unid
    if (!parent[index]['children']) {
      parent[index]['children'] = []
      parent[index]['children'].push(childTemp)
    } else {
      parent[index]['children'].push(childTemp)
    }
    menuName.push(child.name)
    if (child.children && child.children.length > 0) {
      dealChildren(child, parent[index]['children'], childIndex)
    }
  })
}

const { platform } = window._vionConfig
const whiteList = ['login']

router.beforeEach(async(to, from, next) => {
  // loading....
  // determine whether the user has logged in
  const hasAtoken = getAtoken()
  
  if (hasAtoken) {
    if (to.path === '/') {
      // if is logged in, redirect to the home page
      next({ path: '/home' })
    } else {
      try {
        // determine whether the user has his/her permission roles through get some info
        if (!store.getters.accountVal) {
          let accountInfo
          const { userType, accountId, loginName } = store.getters.userInfo
          if (userType === 'super' || !accountId) {
            accountInfo = await store.dispatch('app/getAccounts', userType)
          } else if (accountId) {
            accountInfo = await store.dispatch('app/getAccounts', userType, { id: accountId })
          }
          // 2333333
          Cookies.set('username', loginName)
          Cookies.set('accountId', accountInfo.id)
          Cookies.set('accountName', accountInfo.name)
          // if the first route is mall, you can use mall api
          // store.dispatch('app/getMallList', Object.assign({}, accountInfo, { status: 1 }))
        }

        if (!store.getters.appUnid) {
          const {
            data: {
              menu_tree
            }
          } = await store.dispatch('users/getMenu', platform)
          let menuArray = []
          if (menu_tree.length >= 2) {
            menuArray.push(menu_tree[1])
          } else {
            menuArray.push(menu_tree[0])
          }
          if (menuArray.length) {
            menuArray.forEach((menu, index) => {
              let root = {}
              root.unid = menu.unid
              root.id = menu.perm_unid
              root.children = []
              if (platform === 'mall') {
                root.label = '智慧商业客流分析平台'
              } else {
                root.label = '智慧连锁客流分析平台'
              }
              menuName = []
              dealMenu(menu, root)
            })
            next({ path: '/home' })
          }
        }
      } catch (error) {
        store.dispatch('users/logout')
        Message.error(error || 'Error!!')
        next('/')
        console.error(error)
      }
    }
    next()
  } else {
    // hasn't atoken
    if (whiteList.includes(to.name)) {
      next()
    } else {
      next({
        path: '/'
      })
    }
  }
})