index.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /**
  2. * 全站路由配置
  3. *
  4. * 建议:
  5. * 1. 代码中路由统一使用name属性跳转(不使用path属性)
  6. */
  7. import Vue from 'vue'
  8. import Router from 'vue-router'
  9. import http from '@/utils/httpRequest'
  10. import { isURL } from '@/utils/validate'
  11. import { clearLoginInfo } from '@/utils'
  12. Vue.use(Router)
  13. // 开发环境不使用懒加载, 因为懒加载页面太多的话会造成webpack热更新太慢, 所以只有生产环境使用懒加载
  14. const _import = require('./import-' + process.env.NODE_ENV)
  15. // 全局路由(无需嵌套上左右整体布局)
  16. const globalRoutes = [
  17. { path: '/404', component: _import('common/404'), name: '404', meta: { title: '404未找到' } },
  18. { path: '/login', component: _import('common/login'), name: 'login', meta: { title: '登录' } }
  19. ]
  20. // 主入口路由(需嵌套上左右整体布局)
  21. const mainRoutes = {
  22. path: '/',
  23. component: _import('main'),
  24. name: 'main',
  25. redirect: { path: '/visi-dataset', component: _import('modules/visi/dataset'), meta: { title: '首页' } },
  26. meta: { title: '主入口整体布局' },
  27. children: [
  28. // 通过meta对象设置路由展示方式
  29. // 1. isTab: 是否通过tab展示内容, true: 是, false: 否
  30. // 2. iframeUrl: 是否通过iframe嵌套展示内容, '以http[s]://开头': 是, '': 否
  31. // 提示: 如需要通过iframe嵌套展示内容, 但不通过tab打开, 请自行创建组件使用iframe处理!
  32. { path: '/home', component: _import('common/home'), name: 'home', meta: { title: '首页' } },
  33. // { path: '/dataset', component: _import('common/dataset'), name: 'dataset', meta: { title: '数据集' } },
  34. { path: '/theme', component: _import('common/theme'), name: 'theme', meta: { title: '主题' } },
  35. { path: '/demo-echarts', component: _import('demo/echarts'), name: 'demo-echarts', meta: { title: 'demo-echarts', isTab: false } },
  36. { path: '/demo-ueditor', component: _import('demo/ueditor'), name: 'demo-ueditor', meta: { title: 'demo-ueditor', isTab: false } },
  37. // 创建数据集的路由
  38. { path: '/datasetstatus1-create', component: _import('modules/visi/create1'), name: 'create1', meta: { title: '创建静态数据集', isTab: false } },
  39. { path: '/datasetstatus2-create', component: _import('modules/visi/create2'), name: 'create2', meta: { title: '创建动态数据集', isTab: false } },
  40. { path: '/datasetstatus3-create', component: _import('modules/visi/com/single-file-upLoad'), name: 'create3', meta: { title: '创建单文件数据集', isTab: false } },
  41. { path: '/datasetstatus4-create', component: _import('modules/visi/com/dir-upLoad'), name: 'create4', meta: { title: '创建文件夹数据集', isTab: false } },
  42. // 创建算法的路由
  43. { path: '/normalalg-create', component: _import('modules/alg/algcreate1'), name: 'algcreate1', meta: { title: '创建传统算法', isTab: false } },
  44. { path: '/intelalg-create', component: _import('modules/alg/algcreate2'), name: 'algcreate2', meta: { title: '创建智能算法', isTab: false } },
  45. // 创建训练任务的路由
  46. { path: '/mission-create', component: _import('modules/alg/train-create'), name: 'traincreate', meta: { title: '创建训练任务', isTab: false } },
  47. { path: '/mission-create-tradition', component: _import('modules/alg/train-create-tradition'), name: 'traincreatetradition', meta: { title: '创建训练任务', isTab: false } },
  48. // 查看算法版本的路由
  49. { path: '/versions', component: _import('modules/version/version'), name: 'version', meta: { title: '查看算法版本', isTab: false } },
  50. { path: '/versions-edit', component: _import('modules/version/version-edit'), name: 'versionedit', meta: { title: '编辑算法版本', isTab: false } },
  51. { path: '/start-train', component: _import('modules/version/start-train'), name: 'starttrain', meta: { title: '创建训练任务', isTab: false } },
  52. // 查看文件列表的路由
  53. { path: '/versions-filetest', component: _import('modules/version/filetest'), name: 'filetest', meta: { title: '查看文件列表', isTab: false } },
  54. // 查看传统算法文件列表的路由
  55. { path: '/versions-trafilelist', component: _import('modules/version/trafilelist'), name: 'trafilelist', meta: { title: '查看文件列表', isTab: false } },
  56. // 编辑文件的路由
  57. { path: '/versions-editfile', component: _import('modules/version/file-edit'), name: 'fileedit', meta: { title: '编辑文件', isTab: false } },
  58. // 编辑传统算法文件的路由
  59. { path: '/versions-edittrafile', component: _import('modules/version/trafile-edit'), name: 'trafileedit', meta: { title: '编辑文件', isTab: false } },
  60. // 查看模板文件的路由
  61. { path: '/algs-whatchtemfile', component: _import('modules/alg/templs-file-watch'), name: 'whatchtemfile', meta: { title: '查看模板文件', isTab: false } },
  62. // 查看算法库文件的路由
  63. { path: '/algs-whatchmodelfile', component: _import('modules/alg/models-file-watch'), name: 'whatchmodelfile', meta: { title: '查看算法库文件', isTab: false } },
  64. // 查看训练任务的路由
  65. { path: '/train-watch', component: _import('modules/alg/train-watch'), name: 'trainwatch', meta: { title: '编辑文件', isTab: false } },
  66. // 训练任务tensorboard图像对比的路由
  67. { path: '/train-compare', component: _import('modules/alg/train-compare'), name: 'traincompare', meta: { title: '训练任务tensorboard图像对比', isTab: false } },
  68. // 数据探测路由
  69. { path: '/snoop-measure', component: _import('modules/snoop/measure'), name: 'measure', meta: { title: '创建度量标准', isTab: false } },
  70. { path: '/snoop-job', component: _import('modules/snoop/job'), name: 'job', meta: { title: '创建调度任务', isTab: false } },
  71. { path: '/visi-previewcsv', component: _import('modules/visi/previewcsv'), name: 'previewcsv', meta: { title: '预览csv', isTab: false } },
  72. { path: '/design-create', component: _import('modules/visi/design-graph1'), name: '/design-create', meta: { title: '实验设置', isTab: false } }
  73. ],
  74. beforeEnter (to, from, next) {
  75. let token = Vue.cookie.get('Algtoken')
  76. if (!token || !/\S/.test(token)) {
  77. clearLoginInfo()
  78. next({ name: 'login' })
  79. }
  80. next()
  81. }
  82. }
  83. const router = new Router({
  84. mode: 'hash',
  85. scrollBehavior: () => ({ y: 0 }),
  86. isAddDynamicMenuRoutes: false, // 是否已经添加动态(菜单)路由
  87. routes: globalRoutes.concat(mainRoutes)
  88. })
  89. router.beforeEach((to, from, next) => {
  90. // 添加动态(菜单)路由
  91. // 1. 已经添加 or 全局路由, 直接访问
  92. // 2. 获取菜单列表, 添加并保存本地存储
  93. if (router.options.isAddDynamicMenuRoutes || fnCurrentRouteType(to, globalRoutes) === 'global') {
  94. next()
  95. } else {
  96. http({
  97. url: http.adornUrl('/sys/menu/nav'),
  98. method: 'get',
  99. params: http.adornParams()
  100. }).then(({data}) => {
  101. if (data && data.code === 0) {
  102. fnAddDynamicMenuRoutes(data.menuList)
  103. router.options.isAddDynamicMenuRoutes = true
  104. sessionStorage.setItem('menuList', JSON.stringify(data.menuList || '[]'))
  105. sessionStorage.setItem('permissions', JSON.stringify(data.permissions || '[]'))
  106. next({ ...to, replace: true })
  107. } else {
  108. sessionStorage.setItem('menuList', '[]')
  109. sessionStorage.setItem('permissions', '[]')
  110. next()
  111. }
  112. }).catch((e) => {
  113. console.log(`%c${e} 请求菜单列表和权限失败,跳转至登录页!!`, 'color:blue')
  114. router.push({ name: 'login' })
  115. })
  116. }
  117. })
  118. /**
  119. * 判断当前路由类型, global: 全局路由, main: 主入口路由
  120. * @param {*} route 当前路由
  121. */
  122. function fnCurrentRouteType (route, globalRoutes = []) {
  123. var temp = []
  124. for (var i = 0; i < globalRoutes.length; i++) {
  125. if (route.path === globalRoutes[i].path) {
  126. return 'global'
  127. } else if (globalRoutes[i].children && globalRoutes[i].children.length >= 1) {
  128. temp = temp.concat(globalRoutes[i].children)
  129. }
  130. }
  131. return temp.length >= 1 ? fnCurrentRouteType(route, temp) : 'main'
  132. }
  133. /**
  134. * 添加动态(菜单)路由
  135. * @param {*} menuList 菜单列表
  136. * @param {*} routes 递归创建的动态(菜单)路由
  137. */
  138. function fnAddDynamicMenuRoutes (menuList = [], routes = []) {
  139. var temp = []
  140. for (var i = 0; i < menuList.length; i++) {
  141. if (menuList[i].list && menuList[i].list.length >= 1) {
  142. temp = temp.concat(menuList[i].list)
  143. } else if (menuList[i].url && /\S/.test(menuList[i].url)) {
  144. menuList[i].url = menuList[i].url.replace(/^\//, '')
  145. var route = {
  146. path: menuList[i].url.replace('/', '-'),
  147. component: null,
  148. name: menuList[i].url.replace('/', '-'),
  149. meta: {
  150. menuId: menuList[i].menuId,
  151. title: menuList[i].name,
  152. isDynamic: true,
  153. isTab: false,
  154. iframeUrl: ''
  155. }
  156. }
  157. // url以http[s]://开头, 通过iframe展示
  158. if (isURL(menuList[i].url)) {
  159. route['path'] = `i-${menuList[i].menuId}`
  160. route['name'] = `i-${menuList[i].menuId}`
  161. route['meta']['iframeUrl'] = menuList[i].url
  162. } else {
  163. try {
  164. route['component'] = _import(`modules/${menuList[i].url}`) || null
  165. } catch (e) {}
  166. }
  167. routes.push(route)
  168. }
  169. }
  170. if (temp.length >= 1) {
  171. fnAddDynamicMenuRoutes(temp, routes)
  172. } else {
  173. mainRoutes.name = 'main-dynamic'
  174. mainRoutes.children = routes
  175. router.addRoutes([
  176. mainRoutes,
  177. { path: '*', redirect: { name: '404' } }
  178. ])
  179. sessionStorage.setItem('dynamicMenuRoutes', JSON.stringify(mainRoutes.children || '[]'))
  180. console.log('\n')
  181. console.log('%c!<-------------------- 动态(菜单)路由 s -------------------->', 'color:blue')
  182. console.log(mainRoutes.children)
  183. console.log('%c!<-------------------- 动态(菜单)路由 e -------------------->', 'color:blue')
  184. }
  185. }
  186. export default router