index.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import Vue from 'vue'
  2. import Router from 'vue-router'
  3. import Cookies from "js-cookie"
  4. import Login from '@/views/Login'
  5. import NotFound from '@/views/Error/404'
  6. import Home from '@/views/Home'
  7. import Intro from '@/views/Intro/Intro'
  8. import Calculate from '@/views/Task/Calculate'
  9. import Modified from '@/views/Task/Modified'
  10. import Close from '@/views/Task/Close'
  11. import Generator from '@/views/Generator/Generator'
  12. import api from '@/http/api'
  13. import store from '@/store'
  14. import { getIFramePath, getIFrameUrl } from '@/utils/iframe'
  15. import { getToken } from '@/utils/auth'
  16. import axios from 'axios'
  17. import { findNavTree } from '@/api/user'
  18. Vue.use(Router)
  19. const router = new Router({
  20. routes: [
  21. {
  22. path: '/',
  23. name: '首页',
  24. component: Home,
  25. children: [
  26. {
  27. path: '',
  28. name: '系统介绍',
  29. component: Intro,
  30. meta: {
  31. icon: 'fa fa-home fa-lg',
  32. index: 0
  33. }
  34. },
  35. {
  36. path: '/task/calculate',
  37. name: '出单系统',
  38. component: Calculate,
  39. meta: {
  40. icon: 'fa fa-home fa-lg',
  41. index: 1
  42. }
  43. },
  44. {
  45. path: '/task/modified',
  46. name: '出单系统',
  47. component: Modified,
  48. meta: {
  49. icon: 'fa fa-home fa-lg',
  50. index: 2
  51. }
  52. },
  53. {
  54. path: '/task/close',
  55. name: '已关闭',
  56. component: Close,
  57. meta: {
  58. icon: 'fa fa-home fa-lg',
  59. index: 3
  60. }
  61. }
  62. ]
  63. },
  64. {
  65. path: '/login',
  66. name: '登录',
  67. component: Login
  68. },
  69. {
  70. path: '/404',
  71. name: 'notFound',
  72. component: NotFound
  73. },
  74. ]
  75. })
  76. router.beforeEach((to, from, next) => {
  77. // 登录界面登录成功之后,会把用户信息保存在会话
  78. // 存在时间为会话生命周期,页面关闭即失效。
  79. let token =getToken()
  80. //let userName = sessionStorage.getItem('user')
  81. let userName = Cookies.get('user')
  82. if (to.path === '/login') {
  83. // 如果是访问登录界面,如果用户会话信息存在,代表已登录过,跳转到主页
  84. if (token) {
  85. next({ path: '/' })
  86. } else {
  87. next()
  88. }
  89. } else {
  90. if (!token) {
  91. // 如果访问非登录界面,且户会话信息不存在,代表未登录,则跳转到登录界面
  92. next({ path: '/login' })
  93. } else {
  94. // 加载动态菜单和路由
  95. addDynamicMenuAndRoutes(userName, to, from)
  96. next()
  97. }
  98. }
  99. })
  100. /**
  101. * 加载动态菜单和路由
  102. */
  103. function addDynamicMenuAndRoutes(userName, to, from) {
  104. // 处理IFrame嵌套页面
  105. handleIFrameUrl(to.path)
  106. if (store.state.app.menuRouteLoaded) {
  107. return
  108. }
  109. api.menu.findNavTree({ 'userName': userName }).then(res => {
  110. // 添加动态路由
  111. let dynamicRoutes = addDynamicRoutes(res.data)
  112. // 处理静态组件绑定路由
  113. handleStaticComponent(router, dynamicRoutes)
  114. router.addRoutes(router.options.routes)
  115. // 保存加载状态
  116. store.commit('menuRouteLoaded', true)
  117. // 保存菜单树
  118. store.commit('setNavTree', res.data)
  119. }).then(res => {
  120. api.user.findPermissions({ 'name': userName }).then(res => {
  121. // 保存用户权限标识集合
  122. store.commit('setPerms', res.data)
  123. })
  124. })
  125. }
  126. /**
  127. * 处理路由到本地直接指定页面组件的情况
  128. * 比如'代码生成'是要求直接绑定到'Generator'页面组件
  129. */
  130. function handleStaticComponent(router, dynamicRoutes) {
  131. for (let j = 0; j < dynamicRoutes.length; j++) {
  132. if (dynamicRoutes[j].name == '代码生成') {
  133. dynamicRoutes[j].component = Generator
  134. break
  135. }
  136. }
  137. router.options.routes[0].children = router.options.routes[0].children.concat(dynamicRoutes)
  138. }
  139. /**
  140. * 处理IFrame嵌套页面
  141. */
  142. function handleIFrameUrl(path) {
  143. // 嵌套页面,保存iframeUrl到store,供IFrame组件读取展示
  144. let url = path
  145. let length = store.state.iframe.iframeUrls.length
  146. for (let i = 0; i < length; i++) {
  147. let iframe = store.state.iframe.iframeUrls[i]
  148. if (path != null && path.endsWith(iframe.path)) {
  149. url = iframe.url
  150. store.commit('setIFrameUrl', url)
  151. break
  152. }
  153. }
  154. }
  155. /**
  156. * 添加动态(菜单)路由
  157. * @param {*} menuList 菜单列表
  158. * @param {*} routes 递归创建的动态(菜单)路由
  159. */
  160. function addDynamicRoutes(menuList = [], routes = []) {
  161. var temp = []
  162. for (var i = 0; i < menuList.length; i++) {
  163. if (menuList[i].children && menuList[i].children.length >= 1) {
  164. temp = temp.concat(menuList[i].children)
  165. } else if (menuList[i].url && /\S/.test(menuList[i].url)) {
  166. menuList[i].url = menuList[i].url.replace(/^\//, '')
  167. // 创建路由配置
  168. var route = {
  169. path: menuList[i].url,
  170. component: null,
  171. name: menuList[i].name,
  172. meta: {
  173. icon: menuList[i].icon,
  174. index: menuList[i].id
  175. }
  176. }
  177. let path = getIFramePath(menuList[i].url)
  178. if (path) {
  179. // 如果是嵌套页面, 通过iframe展示
  180. route['path'] = path
  181. route['component'] = resolve => require([`@/views/IFrame/IFrame`], resolve)
  182. // 存储嵌套页面路由路径和访问URL
  183. let url = getIFrameUrl(menuList[i].url)
  184. let iFrameUrl = { 'path': path, 'url': url }
  185. store.commit('addIFrameUrl', iFrameUrl)
  186. } else {
  187. try {
  188. // 根据菜单URL动态加载vue组件,这里要求vue组件须按照url路径存储
  189. // 如url="sys/user",则组件路径应是"@/views/sys/user.vue",否则组件加载不到
  190. let array = menuList[i].url.split('/')
  191. let url = ''
  192. for (let i = 0; i < array.length; i++) {
  193. url += array[i].substring(0, 1).toUpperCase() + array[i].substring(1) + '/'
  194. }
  195. url = url.substring(0, url.length - 1)
  196. route['component'] = resolve => require([`@/views/${url}`], resolve)
  197. } catch (e) { }
  198. }
  199. routes.push(route)
  200. }
  201. }
  202. if (temp.length >= 1) {
  203. addDynamicRoutes(temp, routes)
  204. } else {
  205. }
  206. return routes
  207. }
  208. export default router