permission.js 6.9 KB

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