permission.js 7.3 KB

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