permission.js 7.9 KB

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