permission.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. }
  131. if (code == 'index') {
  132. item.path = '/'
  133. item.alwaysShow = false
  134. const children = route.children
  135. route.children = []
  136. route.children.push({
  137. code: 'dashboard',
  138. moduleName: '首页',
  139. type: 2,
  140. url: '/dashboard',
  141. children: children
  142. })
  143. }
  144. if (code == 'issue') {
  145. const children = route.children
  146. route.children = []
  147. route.children.push({
  148. code: 'issue_index',
  149. moduleName: '文件下发',
  150. type: 2,
  151. url: '/index',
  152. children: children
  153. })
  154. item.alwaysShow = false
  155. }
  156. if (code === 'notice') {
  157. const children = route.children
  158. route.children = []
  159. route.children.push({
  160. code: 'notice_index',
  161. moduleName: '系统通知',
  162. type: 2,
  163. url: '/index',
  164. children: children
  165. })
  166. item.alwaysShow = false
  167. }
  168. item.children = []
  169. if ((route.hasOwnProperty('children') && type === 1) || code == 'issue' || code == 'notice' || code == 'index') {
  170. for (var child of route.children) {
  171. item.children.push(buildRoute(child, fullUrl))
  172. }
  173. }
  174. return item
  175. }
  176. function buildRoute(route, p_url) {
  177. const { url, moduleName, icon, moduleId, code, type, hidden, fullUrl } = route
  178. var item = {}
  179. if (url.substr(0, 1) === '/') {
  180. item.path = url.substr(1)
  181. } else {
  182. item.path = url
  183. }
  184. try {
  185. if (code == 'dashboard') {
  186. // item.component = _import(`${url}/index`)
  187. item.component = resolve => require(['@/views' + `${url}/index`], resolve)
  188. } else {
  189. if (fullUrl) {
  190. item.component = _import(`${fullUrl}`)
  191. } else {
  192. item.component = _import(`${p_url}${url}`)
  193. }
  194. }
  195. } catch (e) {
  196. console.log(e)
  197. }
  198. item.name = fullUrl || code
  199. item.meta = {
  200. title: moduleName,
  201. icon: icon
  202. }
  203. if (route.children && type == 2) {
  204. const 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. // eslint-disable-next-line no-prototype-builtins
  213. if (route.hasOwnProperty('children') && type === 1) {
  214. for (var child of route.children) {
  215. item.children.push(buildThirdRoute(child, `${p_url}${url}`))
  216. }
  217. }
  218. return item
  219. }
  220. function buildThirdRoute(route, p_url) {
  221. const { url, moduleName, icon, moduleId, code, hidden, type, fullUrl } = route
  222. var item = {}
  223. if (url.substr(0, 1) == '/') {
  224. item.path = url.substr(1)
  225. } else {
  226. tem.path = url
  227. }
  228. try {
  229. if (fullUrl) {
  230. item.component = _import(`${fullUrl}`)
  231. } else {
  232. item.component = _import(`${p_url}${url}`)
  233. }
  234. } catch (e) {
  235. console.log(e)
  236. }
  237. item.name = fullUrl || code
  238. item.meta = {
  239. title: moduleName,
  240. icon: icon
  241. }
  242. if (route.children && type == 2) {
  243. const roles = []
  244. for (var role of route.children) {
  245. roles.push(role.code)
  246. }
  247. item.meta.roles = roles
  248. }
  249. item.hidden = hidden
  250. item.children = []
  251. return item
  252. }