permission.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. console.log(newRoutes, '8589')
  46. // newRoutes.shift();
  47. // 添加一项根目录重定向页面
  48. if (newRoutes[0].path !== '/') {
  49. let path = newRoutes[0].path
  50. if (newRoutes[0].children.length > 0) {
  51. path = `${path}/${newRoutes[0].children[0].path}`
  52. }
  53. if (newRoutes[0].children[0].children.length > 0) {
  54. path = `${path}/${newRoutes[0].children[0].children[0].path}`
  55. }
  56. newRoutes.unshift({
  57. path: '/',
  58. component: Layout,
  59. redirect: path
  60. })
  61. } else {
  62. newRoutes[0].redirect = '/dashboard'
  63. }
  64. console.log(newRoutes)
  65. router.addRoutes(newRoutes) // 2.动态添加路由
  66. global.antRouter = newRoutes // 3.将路由数据传递给全局变量,做侧边栏菜单渲染工作
  67. // const menus = filterAsyncRouter(store.getters.menus) // 1.过滤路由
  68. // console.log(menus);
  69. // router.addRoutes(menus) // 2.动态添加路由
  70. // global.antRouter = menus // 3.将路由数据传递给全局变量,做侧边栏菜单渲染工作
  71. next({
  72. ...to,
  73. replace: true
  74. })
  75. // next()
  76. } catch (error) {
  77. // remove token and go to login page to re-login
  78. await store.dispatch('user/resetToken')
  79. Message.error(error || 'Has Error')
  80. next('/login')
  81. // next(`/login?redirect=${to.path}`);
  82. NProgress.done()
  83. }
  84. }
  85. }
  86. } else {
  87. /* has no token*/
  88. if (whiteList.indexOf(to.path) !== -1) {
  89. // in the free login whitelist, go directly
  90. next()
  91. } else {
  92. // other pages that do not have permission to access are redirected to the login page.
  93. // next(`/login?redirect=${to.path}`);
  94. next('/login')
  95. NProgress.done()
  96. }
  97. }
  98. })
  99. router.afterEach(() => {
  100. // finish progress bar
  101. NProgress.done()
  102. })
  103. // 遍历后台传来的路由字符串,转换为组件对象
  104. // function filterAsyncRouter(asyncRouterMap) {
  105. // const accessedRouters = asyncRouterMap.filter((route) => {
  106. // console.log(route);
  107. // if (route.component) {
  108. // if (route.component === "Layout") {
  109. // route.component = Layout;
  110. // } else {
  111. // route.component = _import(route.component); // 导入组件
  112. // }
  113. // }
  114. // if (route.children && route.children.length) {
  115. // route.children = filterAsyncRouter(route.children);
  116. // }
  117. // return true;
  118. // });
  119. // return accessedRouters;
  120. // }
  121. function buildRootRoute(route) {
  122. const { url, icon, moduleName, moduleId, code, type, fullUrl } = route
  123. var item = {}
  124. item.path = url
  125. item.component = Layout
  126. item.name = fullUrl || code
  127. item.meta = {
  128. title: moduleName,
  129. icon: icon,
  130. moduleId
  131. }
  132. if (code == 'index') {
  133. item.path = '/'
  134. item.alwaysShow = false
  135. const children = route.children
  136. route.children = []
  137. route.children.push({
  138. code: 'dashboard',
  139. moduleName: '首页',
  140. type: 2,
  141. url: '/dashboard',
  142. children: children
  143. })
  144. }
  145. if (code == 'issue') {
  146. const children = route.children
  147. route.children = []
  148. route.children.push({
  149. code: 'issue_index',
  150. moduleName: '文件下发',
  151. type: 2,
  152. url: '/index',
  153. children: children
  154. })
  155. item.alwaysShow = false
  156. }
  157. if (code === 'notice') {
  158. const children = route.children
  159. route.children = []
  160. route.children.push({
  161. code: 'notice_index',
  162. moduleName: '系统通知',
  163. type: 2,
  164. url: '/index',
  165. children: children
  166. })
  167. item.alwaysShow = false
  168. }
  169. item.children = []
  170. if ((route.hasOwnProperty('children') && type === 1) || code == 'issue' || code == 'notice' || code == 'index') {
  171. for (var child of route.children) {
  172. item.children.push(buildRoute(child, fullUrl))
  173. }
  174. }
  175. return item
  176. }
  177. function buildRoute(route, p_url) {
  178. const { url, moduleName, icon, moduleId, code, type, hidden, fullUrl } = route
  179. var item = {}
  180. if (url.substr(0, 1) === '/') {
  181. item.path = url.substr(1)
  182. } else {
  183. item.path = url
  184. }
  185. try {
  186. if (code == 'dashboard') {
  187. // item.component = _import(`${url}/index`)
  188. item.component = resolve => require(['@/views' + `${url}/index`], resolve)
  189. } else {
  190. if (fullUrl) {
  191. item.component = _import(`${fullUrl}`)
  192. } else {
  193. item.component = _import(`${p_url}${url}`)
  194. }
  195. }
  196. } catch (e) {
  197. console.log(e)
  198. }
  199. item.name = fullUrl || code
  200. item.meta = {
  201. title: moduleName,
  202. icon: icon,
  203. moduleId
  204. }
  205. if (route.children && type == 2) {
  206. const roles = []
  207. for (var role of route.children) {
  208. roles.push(role.code)
  209. }
  210. item.meta.roles = roles
  211. }
  212. item.hidden = hidden
  213. item.children = []
  214. // eslint-disable-next-line no-prototype-builtins
  215. if (route.hasOwnProperty('children') && type === 1) {
  216. for (var child of route.children) {
  217. item.children.push(buildThirdRoute(child, `${p_url}${url}`))
  218. }
  219. }
  220. return item
  221. }
  222. function buildThirdRoute(route, p_url) {
  223. const { url, moduleName, icon, moduleId, code, hidden, type, fullUrl } = route
  224. var item = {}
  225. if (url.substr(0, 1) == '/') {
  226. item.path = url.substr(1)
  227. } else {
  228. tem.path = url
  229. }
  230. try {
  231. if (fullUrl) {
  232. item.component = _import(`${fullUrl}`)
  233. } else {
  234. item.component = _import(`${p_url}${url}`)
  235. }
  236. } catch (e) {
  237. console.log(e)
  238. }
  239. item.name = fullUrl || code
  240. item.meta = {
  241. title: moduleName,
  242. icon: icon,
  243. moduleId
  244. }
  245. if (route.children && type == 2) {
  246. const roles = []
  247. for (var role of route.children) {
  248. roles.push(role.code)
  249. }
  250. item.meta.roles = roles
  251. }
  252. item.hidden = hidden
  253. item.children = []
  254. return item
  255. }