permission.js 6.8 KB

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