permission.js 7.1 KB

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