permission.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import router from './router'
  2. import store from './store'
  3. import { Message } from '@zjlib/element-ui2'
  4. import { getToken } from '@/utils/auth' // get token from cookie
  5. import NProgress from 'nprogress' // progress bar
  6. import 'nprogress/nprogress.css' // progress bar style
  7. import getPageTitle from '@/utils/get-page-title'
  8. import Layout from '@/layout'
  9. import RouterView from '@/views/routerView.vue'
  10. import IframeView from '@/views/iframeView.vue'
  11. NProgress.configure({ showSpinner: false }) // NProgress Configuration
  12. const _import = require('./router/_import_' + process.env.NODE_ENV) // 获取组件的方法
  13. const whiteList = ['/login'] // no redirect whitelist
  14. const lay = {
  15. path: '/',
  16. component: Layout,
  17. children: []
  18. }
  19. import { pages } from './settings'
  20. // 递归找最后一级页面
  21. function getc(obj) {
  22. if (!obj.children || !obj.children.length) {
  23. return obj
  24. } else {
  25. return getc(obj.children[0])
  26. }
  27. }
  28. router.beforeEach(async (to, from, next) => {
  29. NProgress.start()
  30. document.title = getPageTitle(to.meta.title)
  31. const hasToken = getToken()
  32. if (hasToken) {
  33. if (to.path === '/login') {
  34. next({ path: '/' })
  35. NProgress.done()
  36. } else {
  37. const hasGetUserInfo = store.getters.name
  38. if (hasGetUserInfo) {
  39. next()
  40. } else {
  41. try {
  42. await store.dispatch('user/getInfo')
  43. await store.dispatch('user/getRouter')
  44. if (store.getters.menus.length < 1) {
  45. global.antRouter = []
  46. next()
  47. }
  48. var userInfo = JSON.parse(localStorage.getItem('greemall_user'))
  49. var moduleObj = {
  50. // 辅材配件
  51. auxiliaryFittings: userInfo.moduleMaterialPart,
  52. // 维保
  53. engineeringMaintenance: userInfo.moduleWb,
  54. // 延保
  55. valueAddedService: userInfo.moduleYb,
  56. }
  57. // 设置路由
  58. lay.children = []
  59. for (var route of store.getters.menus) {
  60. if (moduleObj[route.code] !== false) {
  61. lay.children.push(...buildRoute(route))
  62. }
  63. }
  64. lay.redirect = getc(lay).path
  65. router.addRoutes([lay])
  66. global.antRouter = []
  67. for (var route of store.getters.menus) {
  68. if (moduleObj[route.code] !== false) {
  69. global.antRouter.push(...buildRoute(route, '', false))
  70. }
  71. }
  72. next({
  73. ...to,
  74. replace: true
  75. })
  76. } catch (error) {
  77. await store.dispatch('user/resetToken')
  78. Message.error(error || 'Has Error')
  79. next('/login')
  80. NProgress.done()
  81. }
  82. }
  83. }
  84. } else {
  85. if (whiteList.indexOf(to.path) !== -1) {
  86. next()
  87. } else {
  88. next('/login')
  89. NProgress.done()
  90. }
  91. }
  92. })
  93. router.afterEach(() => {
  94. NProgress.done()
  95. })
  96. function buildRoute(route, parentUrl = '', bool = true) {
  97. const { url, moduleName, icon, moduleId, code, type, fullUrl, status, isCache } = route
  98. var item = {}
  99. var itemparent = null
  100. item.path = ~[3, 4].indexOf(type) ? fullUrl : parentUrl + url
  101. item.name = ~[3].indexOf(type) ? fullUrl : code
  102. item.type = type
  103. item.status = status
  104. item.meta = {
  105. url,
  106. title: moduleName,
  107. icon: icon,
  108. moduleId,
  109. status: status,
  110. isCache: isCache
  111. }
  112. if (route.children && route.children.length) {
  113. if (type == 2) {
  114. var childrenPage = (route.children || []).filter(item => item.type == 2)
  115. if (childrenPage.length) {
  116. itemparent = {
  117. ...item,
  118. status: false,
  119. meta: {
  120. url: `/${code}_children`,
  121. isCache: 0,
  122. status: false,
  123. roles: [],
  124. roleItems: []
  125. },
  126. component: RouterView,
  127. children: []
  128. }
  129. childrenPage.map(child => {
  130. itemparent.children.push(...buildRoute(child, `${parentUrl}/${code}_children`, bool))
  131. })
  132. }
  133. try {
  134. item.component = _import(`${fullUrl}/index`)
  135. item.component.name = item.name
  136. } catch (e) {
  137. console.log(e)
  138. }
  139. const roles = []
  140. const roleItems = []
  141. for (var role of route.children.filter(item => item.type == 3)) {
  142. roleItems.push({
  143. code: role.code,
  144. moduleName: role.moduleName
  145. })
  146. roles.push(role.code)
  147. }
  148. item.meta.roles = roles
  149. item.meta.roleItems = roleItems
  150. if (!pages.find(ite => ite.path === item.path)) {
  151. item.path = `${item.path}${bool?'/:pageName?/:pageType?/:pageCode?/:pagePam?':''}`
  152. pages.push(item)
  153. }
  154. } else if (type == 1) {
  155. item.component = RouterView
  156. item.children = []
  157. route.children.filter(itema => itema.type !== 3).map(child => {
  158. item.children.push(...buildRoute(child, item.path, bool))
  159. })
  160. }
  161. } else {
  162. if (type == 4) {
  163. item.component = IframeView
  164. if (!pages.find(ite => ite.path === item.path)) {
  165. pages.push(item)
  166. }
  167. } else if (type == 2) {
  168. try {
  169. item.component = _import(`${fullUrl}/index`)
  170. item.component.name = item.name
  171. if (!pages.find(ite => ite.path === item.path)) {
  172. item.path = `${item.path}${bool?'/:pageName?/:pageType?/:pageCode?/:pagePam?':''}`
  173. pages.push(item)
  174. }
  175. } catch (e) {
  176. console.log(e)
  177. }
  178. }
  179. }
  180. return itemparent ? [item, itemparent] : [item]
  181. }