permission.js 6.1 KB

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