permission.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 (route.code == "bigViews") {
  61. router.addRoutes([{
  62. path: '/bigViews',
  63. code: "bigViews",
  64. name: "bigViews",
  65. component: _import(`${route.fullUrl}/index`),
  66. hidden: true
  67. }])
  68. } else if (moduleObj[route.code] !== false) {
  69. lay.children.push(...buildRoute(route))
  70. }
  71. }
  72. lay.redirect = getc(lay).path
  73. router.addRoutes([lay])
  74. global.antRouter = []
  75. for (var route of store.getters.menus) {
  76. if (route.code == "bigViews") {
  77. global.antRouter.push({
  78. path: '/bigViews',
  79. code: "bigViews",
  80. name: "bigViews",
  81. component: _import(`${route.fullUrl}/index`),
  82. hidden: true
  83. })
  84. } else if (moduleObj[route.code] !== false) {
  85. global.antRouter.push(...buildRoute(route, '', false))
  86. }
  87. }
  88. next({
  89. ...to,
  90. replace: true
  91. })
  92. } catch (error) {
  93. await store.dispatch('user/resetToken')
  94. Message.error(error || 'Has Error')
  95. next('/login')
  96. NProgress.done()
  97. }
  98. }
  99. }
  100. } else {
  101. if (whiteList.indexOf(to.path) !== -1) {
  102. next()
  103. } else {
  104. next('/login')
  105. NProgress.done()
  106. }
  107. }
  108. })
  109. router.afterEach(() => {
  110. NProgress.done()
  111. })
  112. function buildRoute(route, parentUrl = '', bool = true) {
  113. const { url, moduleName, icon, moduleId, code, type, fullUrl, status, isCache } = route
  114. var item = {}
  115. var itemparent = null
  116. item.path = ~[3, 4].indexOf(type) ? fullUrl : parentUrl + url
  117. item.name = ~[3].indexOf(type) ? fullUrl : code
  118. item.type = type
  119. item.status = status
  120. item.meta = {
  121. url,
  122. title: moduleName,
  123. icon: icon,
  124. moduleId,
  125. status: status,
  126. isCache: isCache
  127. }
  128. if (route.children && route.children.length) {
  129. if (type == 2) {
  130. var childrenPage = (route.children || []).filter(item => item.type == 2)
  131. if (childrenPage.length) {
  132. itemparent = {
  133. ...item,
  134. status: false,
  135. meta: {
  136. url: `/${code}_children`,
  137. isCache: 0,
  138. status: false,
  139. roles: [],
  140. roleItems: []
  141. },
  142. component: RouterView,
  143. children: []
  144. }
  145. childrenPage.map(child => {
  146. itemparent.children.push(...buildRoute(child, `${parentUrl}/${code}_children`, bool))
  147. })
  148. }
  149. try {
  150. item.component = _import(`${fullUrl}/index`)
  151. item.component.name = item.name
  152. } catch (e) {
  153. console.log(e)
  154. }
  155. const roles = []
  156. const roleItems = []
  157. for (var role of route.children.filter(item => item.type == 3)) {
  158. roleItems.push({
  159. code: role.code,
  160. moduleName: role.moduleName
  161. })
  162. roles.push(role.code)
  163. }
  164. item.meta.roles = roles
  165. item.meta.roleItems = roleItems
  166. if (!pages.find(ite => ite.path === item.path)) {
  167. item.path = `${item.path}${bool ? '/:pageName?/:pageType?/:pageCode?/:pagePam?' : ''}`
  168. pages.push(item)
  169. }
  170. } else if (type == 1) {
  171. item.component = RouterView
  172. item.children = []
  173. route.children.filter(itema => itema.type !== 3).map(child => {
  174. item.children.push(...buildRoute(child, item.path, bool))
  175. })
  176. }
  177. } else {
  178. if (type == 4) {
  179. item.component = IframeView
  180. if (!pages.find(ite => ite.path === item.path)) {
  181. pages.push(item)
  182. }
  183. } else if (type == 2) {
  184. try {
  185. item.component = _import(`${fullUrl}/index`)
  186. item.component.name = item.name
  187. if (!pages.find(ite => ite.path === item.path)) {
  188. item.path = `${item.path}${bool ? '/:pageName?/:pageType?/:pageCode?/:pagePam?' : ''}`
  189. pages.push(item)
  190. }
  191. } catch (e) {
  192. console.log(e)
  193. }
  194. }
  195. }
  196. return itemparent ? [item, itemparent] : [item]
  197. }