permission.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. import router from './router'
  2. import {resetRouter} from './router'
  3. import store from './store'
  4. import { Message } from 'element-ui'
  5. import NProgress from 'nprogress' // progress bar
  6. import 'nprogress/nprogress.css' // progress bar style
  7. import { getToken } from '@/utils/auth' // get token from cookie
  8. import getPageTitle from '@/utils/get-page-title'
  9. import Layout from '@/layout'
  10. const _import = require('./router/_import_' + process.env.NODE_ENV) // 获取组件的方法
  11. NProgress.configure({ showSpinner: false }) // NProgress Configuration
  12. const whiteList = ['/login'] // no redirect whitelist
  13. router.beforeEach(async(to, from, next) => {
  14. // start progress bar
  15. NProgress.start()
  16. // set page title
  17. document.title = getPageTitle(to.meta.title)
  18. // determine whether the user has logged in
  19. const hasToken = getToken()
  20. if (hasToken) {
  21. if (to.path === '/login') {
  22. // if is logged in, redirect to the home page
  23. next({ path: '/' })
  24. NProgress.done()
  25. } else {
  26. const hasGetUserInfo = store.getters.name
  27. if (hasGetUserInfo) {
  28. next()
  29. } else {
  30. try {
  31. // get user info
  32. await store.dispatch('user/getInfo')
  33. // 请求获取路由表
  34. await store.dispatch('user/getRouter')
  35. if (store.getters.menus.length < 1) {
  36. global.antRouter = []
  37. next()
  38. }
  39. // 设置路由
  40. var newRoutes = []
  41. for (var route of store.getters.menus) {
  42. const item = buildRootRoute(route)
  43. newRoutes.push(item)
  44. }
  45. // newRoutes.shift();
  46. // 添加一项根目录重定向页面
  47. if(newRoutes[0].path != '/') {
  48. let path = newRoutes[0].path;
  49. if(newRoutes[0].children.length > 0) {
  50. path = `${path}/${newRoutes[0].children[0].path}`
  51. }
  52. if(newRoutes[0].children[0].children.length > 0) {
  53. path = `${path}/${newRoutes[0].children[0].children[0].path}`
  54. }
  55. newRoutes.unshift({
  56. path: '/',
  57. component: Layout,
  58. redirect: path
  59. })
  60. }else {
  61. newRoutes[0].redirect = '/dashboard'
  62. }
  63. console.log(newRoutes);
  64. router.addRoutes(newRoutes) // 2.动态添加路由
  65. global.antRouter = newRoutes // 3.将路由数据传递给全局变量,做侧边栏菜单渲染工作
  66. // const menus = filterAsyncRouter(store.getters.menus) // 1.过滤路由
  67. // console.log(menus);
  68. // router.addRoutes(menus) // 2.动态添加路由
  69. // global.antRouter = menus // 3.将路由数据传递给全局变量,做侧边栏菜单渲染工作
  70. next({
  71. ...to,
  72. replace: true
  73. })
  74. // next()
  75. } catch (error) {
  76. // remove token and go to login page to re-login
  77. await store.dispatch('user/resetToken')
  78. Message.error(error || 'Has Error')
  79. next(`/login?redirect=${to.path}`)
  80. NProgress.done()
  81. }
  82. }
  83. }
  84. } else {
  85. /* has no token*/
  86. if (whiteList.indexOf(to.path) !== -1) {
  87. // in the free login whitelist, go directly
  88. next()
  89. } else {
  90. // other pages that do not have permission to access are redirected to the login page.
  91. next(`/login?redirect=${to.path}`)
  92. NProgress.done()
  93. }
  94. }
  95. })
  96. router.afterEach(() => {
  97. // finish progress bar
  98. NProgress.done()
  99. })
  100. // 遍历后台传来的路由字符串,转换为组件对象
  101. function filterAsyncRouter(asyncRouterMap) {
  102. const accessedRouters = asyncRouterMap.filter(route => {
  103. console.log(route);
  104. if (route.component) {
  105. if (route.component === 'Layout') {
  106. route.component = Layout
  107. } else {
  108. route.component = _import(route.component) // 导入组件
  109. }
  110. }
  111. if (route.children && route.children.length) {
  112. route.children = filterAsyncRouter(route.children)
  113. }
  114. return true
  115. })
  116. return accessedRouters
  117. }
  118. function buildRootRoute(route) {
  119. const { url, icon, moduleName, moduleId, code, type } = route
  120. var item = {}
  121. item.path = url
  122. item.component = Layout
  123. item.name = code
  124. item.meta = {
  125. title: moduleName,
  126. icon: icon
  127. }
  128. if(code == 'index') {
  129. item.path = '/';
  130. item.alwaysShow = false;
  131. let children = route.children;
  132. route.children = [];
  133. route.children.push(
  134. {
  135. 'code': "dashboard",
  136. 'moduleName': "首页",
  137. 'type': 2,
  138. 'url': '/dashboard',
  139. 'children': children
  140. }
  141. )
  142. }
  143. if (code == 'issue') {
  144. let children = route.children;
  145. route.children = [];
  146. route.children.push(
  147. {
  148. 'code': "issue_index",
  149. 'moduleName': "文件下发",
  150. 'type': 2,
  151. 'url': '/index',
  152. 'children': children
  153. }
  154. )
  155. item.alwaysShow = false;
  156. }
  157. if (code == 'notice') {
  158. let children = route.children;
  159. route.children = [];
  160. route.children.push(
  161. {
  162. 'code': "notice_index",
  163. 'moduleName': "系统通知",
  164. 'type': 2,
  165. 'url': '/index',
  166. 'children': children
  167. }
  168. )
  169. item.alwaysShow = false;
  170. }
  171. item.children = []
  172. if ((route.hasOwnProperty('children') && type === 1) || code == 'issue' || code == 'notice' || code == 'index') {
  173. for (var child of route.children) {
  174. item.children.push(buildRoute(child, url))
  175. }
  176. }
  177. return item
  178. }
  179. function buildRoute(route, p_url) {
  180. const { url, moduleName, icon, moduleId, code, type, hidden } = route
  181. var item = {}
  182. if(url.substr(0, 1) == '/') {
  183. item.path = url.substr(1)
  184. }else {
  185. item.path = url
  186. }
  187. try {
  188. if(code == 'dashboard') {
  189. // item.component = _import(`${url}/index`)
  190. item.component = (resolve) => require(["@/views"+`${url}/index`], resolve)
  191. }
  192. else {
  193. item.component = _import(`${p_url}${url}`)
  194. }
  195. } catch (e) {
  196. console.log(e)
  197. }
  198. item.name = code
  199. item.meta = {
  200. title: moduleName,
  201. icon: icon
  202. }
  203. if((route.children && type == 2)) {
  204. let roles = [];
  205. for(var role of route.children) {
  206. roles.push(role.code)
  207. }
  208. item.meta.roles = roles;
  209. }
  210. item.hidden = hidden
  211. item.children = []
  212. if (route.hasOwnProperty('children') && type === 1) {
  213. for (var child of route.children) {
  214. item.children.push(buildThirdRoute(child, `${p_url}${url}`))
  215. }
  216. }
  217. return item
  218. }
  219. function buildThirdRoute(route, p_url) {
  220. const { url, moduleName, icon, moduleId, code, hidden, type } = route
  221. var item = {}
  222. if(url.substr(0, 1) == '/') {
  223. item.path = url.substr(1)
  224. }else {
  225. tem.path = url
  226. }
  227. try {
  228. item.component = _import(`${p_url}${url}`)
  229. } catch (e) {
  230. console.log(e)
  231. }
  232. item.name = code
  233. item.meta = {
  234. title: moduleName,
  235. icon: icon
  236. }
  237. if(route.children && type == 2) {
  238. let roles = [];
  239. for(var role of route.children) {
  240. roles.push(role.code)
  241. }
  242. item.meta.roles = roles;
  243. }
  244. item.hidden = hidden
  245. item.children = []
  246. return item
  247. }