import router from './router' import store from './store' import { ElMessage } from 'element-plus' import { getConfigJson, getPermission } from '@/utils/api' import { loadGeoData } from '@/utils/tools' import axios from 'axios' import { storageKey } from '@/utils/constants' import { loadI18n } from '@/i18n' const loginWhiteList = ['/login', '/'] // 免登陆白名单 const permissionWhiteList = [...loginWhiteList, '/entityDetail'] // 权限白名单 router.beforeEach(async (to, from, next) => { // 加载iso-3166-2资源 loadGeoData() // 加载baseUrl if (!axios.defaults.baseURL) { const config = await getConfigJson() axios.defaults.baseURL = config.baseUrl } if (localStorage.getItem(storageKey.token)) { // 加载i18n if (!localStorage.getItem(storageKey.i18n)) { await loadI18n() } // 加载权限 if (permissionWhiteList.indexOf(to.path) !== -1) { next() } else { if (store.getters.menuList.length === 0) { const { menuList, buttonList, roleList } = await getPermission() store.commit('setMenuList', menuList) store.commit('setButtonList', buttonList) store.commit('setRoleList', roleList) } if (to.path) { if (hasMenu(store.getters.menuList, to.path)) { next() } else { ElMessage.error('No access') // TODO 国际化 } } } } else { if (loginWhiteList.indexOf(to.path) !== -1) { next() } else { next({ path: '/login' }) } } }) // menuList中是否包含route权限 export function hasMenu (menuList, route) { return menuList.some(menu => { if (menu.route === route) { return true } else { if (menu.children) { if (hasMenu(menu.children, route)) { return true } } } return false }) } export function hasButton (buttonList, code) { return buttonList.some(button => button === code) } // 用法 v-has="code" | v-has="[code...]" 任意匹配一个 | v-has:all="[code...]" 全匹配 export const hasPermission = { beforeMount (el, binding) { // 节点权限处理 const buttonCode = binding.value const arg = binding.arg if (buttonCode) { if (buttonCode instanceof Array) { let has = true if (arg && arg === 'all') { // 全匹配 buttonCode.forEach(button => { if (has) { has = hasButton(store.getters.buttonList, button) } }) } else { // 任意匹配 has = buttonCode.some(button => { return hasButton(store.getters.buttonList, button) }) } if (!has) { el.parentNode.removeChild(el) } } else { // 单个匹配 if (!hasButton(store.getters.buttonList, buttonCode)) { el.parentNode && el.parentNode.removeChild(el) } } } } } // 根据orderNum排序 export function sortByOrderNum (menuList) { const r = menuList.sort((a, b) => { return a.orderNum - b.orderNum }) r.forEach(menu => { if (menu.children && getChildMenu(menu).length > 0) { menu.children = menu.children.sort((a, b) => { return a.orderNum - b.orderNum }) } }) return r } function getChildMenu (menu) { return menu.children.filter(m => m.type === 1) } /* 获取第一个菜单 */ export function getWelcomeMenu (menu) { for (let i = 0; i < menu.length; i++) { const m = menu[i] if (m.type === 1) { if (m.children && getChildMenu(m).length > 0) { return getWelcomeMenu(m.children) } else { return m } } } }