permission.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. Message.error('没有分配任何页面')
  47. await new Promise(r => {
  48. setTimeout(() => {
  49. r()
  50. }, 1500)
  51. })
  52. await store.dispatch('user/logout')
  53. next({ path: '/login' })
  54. }
  55. var userInfo = JSON.parse(localStorage.getItem('greemall_user'))
  56. var moduleObj = {
  57. // 辅材配件
  58. auxiliaryFittings: userInfo.moduleMaterialPart,
  59. // 维保
  60. engineeringMaintenance: userInfo.moduleWb,
  61. // 延保
  62. valueAddedService: userInfo.moduleYb
  63. }
  64. // 设置路由
  65. lay.children = []
  66. for (var route of store.getters.menus) {
  67. if (route.code == 'bigViews') {
  68. store.commit('SET_SHOW_BIG_DATS', true)
  69. router.addRoutes([
  70. {
  71. path: '/bigViews',
  72. code: 'bigViews',
  73. name: 'bigViews',
  74. component: _import(`${route.fullUrl}/index`),
  75. hidden: true
  76. }
  77. ])
  78. } else if (route.code == 'networkSigning') {
  79. router.addRoutes([
  80. {
  81. path: '/networkSigning',
  82. code: 'networkSigning',
  83. name: 'networkSigning',
  84. component: _import(`${route.fullUrl}/index`),
  85. hidden: true
  86. }
  87. ])
  88. } else if (moduleObj[route.code] !== false && route.status) {
  89. lay.children.push(...buildRoute(route))
  90. }
  91. }
  92. lay.redirect = getc(lay).path
  93. router.addRoutes([lay])
  94. global.antRouter = []
  95. for (var route of store.getters.menus) {
  96. if (route.code == 'bigViews') {
  97. global.antRouter.push({
  98. path: '/bigViews',
  99. code: 'bigViews',
  100. name: 'bigViews',
  101. component: _import(`${route.fullUrl}/index`),
  102. hidden: true
  103. })
  104. } else if (route.code == 'networkSigning') {
  105. global.antRouter.push([
  106. {
  107. path: '/networkSigning',
  108. code: 'networkSigning',
  109. name: 'networkSigning',
  110. component: _import(`${route.fullUrl}/index`),
  111. hidden: true
  112. }
  113. ])
  114. } else if (moduleObj[route.code] !== false && route.status) {
  115. global.antRouter.push(...buildRoute(route, '', false))
  116. }
  117. }
  118. next({
  119. ...to,
  120. replace: true
  121. })
  122. } catch (error) {
  123. await store.dispatch('user/resetToken')
  124. Message.error(error || 'Has Error')
  125. next('/login')
  126. NProgress.done()
  127. }
  128. }
  129. }
  130. } else {
  131. if (whiteList.indexOf(to.path) !== -1) {
  132. next()
  133. } else {
  134. next('/login')
  135. NProgress.done()
  136. }
  137. }
  138. })
  139. router.afterEach(() => {
  140. NProgress.done()
  141. })
  142. function buildRoute(route, parentUrl = '', bool = true) {
  143. const { url, moduleName, icon, moduleId, code, type, fullUrl, status, isCache } = route
  144. var item = {}
  145. var itemparent = null
  146. item.path = ~[3, 4].indexOf(type) ? fullUrl : parentUrl + url
  147. item.name = ~[3].indexOf(type) ? fullUrl : code
  148. item.type = type
  149. item.status = status
  150. item.meta = {
  151. url,
  152. title: moduleName,
  153. icon: icon,
  154. moduleId,
  155. status: status,
  156. isCache: isCache
  157. }
  158. if (route.children && route.children.length) {
  159. if (type == 2) {
  160. var childrenPage = (route.children || []).filter(item => item.type == 2)
  161. if (childrenPage.length) {
  162. itemparent = {
  163. ...item,
  164. status: false,
  165. meta: {
  166. url: `/${code}_children`,
  167. isCache: 0,
  168. status: false,
  169. roles: [],
  170. roleItems: []
  171. },
  172. component: RouterView,
  173. children: []
  174. }
  175. childrenPage.map(child => {
  176. if (child.status) {
  177. itemparent.children.push(...buildRoute(child, `${parentUrl}/${code}_children`, bool))
  178. }
  179. })
  180. }
  181. try {
  182. item.component = _import(`${fullUrl}/index`)
  183. item.component.name = item.name
  184. } catch (e) {
  185. console.log(e)
  186. }
  187. const roles = []
  188. const roleItems = []
  189. for (var role of route.children.filter(item => item.type == 3)) {
  190. roleItems.push({
  191. code: role.code,
  192. moduleName: role.moduleName
  193. })
  194. roles.push(role.code)
  195. }
  196. item.meta.roles = roles
  197. item.meta.roleItems = roleItems
  198. if (!pages.find(ite => ite.path === item.path)) {
  199. item.path = `${item.path}${bool ? '/:pageName?/:pageType?/:pageCode?/:pagePam?' : ''}`
  200. pages.push(item)
  201. }
  202. } else if (type == 1) {
  203. item.component = RouterView
  204. item.children = []
  205. route.children
  206. .filter(itema => itema.type !== 3)
  207. .map(child => {
  208. if (child.status) {
  209. item.children.push(...buildRoute(child, item.path, bool))
  210. }
  211. })
  212. }
  213. } else {
  214. if (type == 4) {
  215. item.component = IframeView
  216. if (!pages.find(ite => ite.path === item.path)) {
  217. pages.push(item)
  218. }
  219. } else if (type == 2) {
  220. try {
  221. item.component = _import(`${fullUrl}/index`)
  222. item.component.name = item.name
  223. if (!pages.find(ite => ite.path === item.path)) {
  224. item.path = `${item.path}${bool ? '/:pageName?/:pageType?/:pageCode?/:pagePam?' : ''}`
  225. pages.push(item)
  226. }
  227. } catch (e) {
  228. console.log(e)
  229. }
  230. }
  231. }
  232. return itemparent ? [item, itemparent] : [item]
  233. }