spc/src/router/index.js
2022-12-13 16:41:37 +08:00

200 lines
8.9 KiB
JavaScript

import Vue from 'vue'
import Router from 'vue-router'
import http from '@/utils/request'
import { isURL } from '@/utils/validate'
Vue.use(Router)
// 页面路由(独立页面)
export const pageRoutes = [
{
path: '/404',
component: () => import('@/views/pages/404'),
name: '404',
meta: { title: '404未找到' },
beforeEnter (to, from, next) {
// 拦截处理特殊业务场景
// 如果, 重定向路由包含__双下划线, 为临时添加路由
if (/__.*/.test(to.redirectedFrom)) {
return next(to.redirectedFrom.replace(/__.*/, ''))
}
next()
}
},
{ path: '/login', component: () => import('@/views/pages/login'), name: 'login', meta: { title: '登录' } }
]
// 模块路由(基于主入口布局页面)
export const moduleRoutes = {
path: '/',
component: () => import('@/views/main'),
name: 'main',
redirect: { name: 'home' },
meta: { title: '主入口布局' },
children: [
{ path: '/home', component: () => import('@/views/modules/home'), name: 'home', meta: { title: '首页', isTab: true } },
{ path: '/echart-line', component: () => import('@/views/spc-chart/echart-line'), name: 'echart-line', meta: { title: 'spc折线', isTab: true } },
{ path: '/echart-3line', component: () => import('@/views/spc-chart/echart-3line'), name: 'echart-3line', meta: { title: 'spc3折线', isTab: true } },
{ path: '/echart-2line', component: () => import('@/views/spc-chart/echart-2line'), name: 'echart-2line', meta: { title: 'spc2折线', isTab: true } },
// 基础信息
{ path: '/productType', component: () => import('@/views/spc-basic/productType'), name: 'productType', meta: { title: '产品类型', isTab: true } },
{ path: '/productList', component: () => import('@/views/spc-basic/productList'), name: 'productList', meta: { title: '产品列表', isTab: true } },
{ path: '/factoryManage', component: () => import('@/views/spc-basic/factoryManage'), name: 'factoryManage', meta: { title: '工厂管理', isTab: true } },
{ path: '/processType', component: () => import('@/views/spc-basic/processType'), name: 'processType', meta: { title: '工序类型', isTab: true } },
{ path: '/processDefine', component: () => import('@/views/spc-basic/processDefine'), name: 'processDefine', meta: { title: '工序定义', isTab: true } },
{ path: '/processFlow', component: () => import('@/views/spc-basic/processFlow'), name: 'processFlow', meta: { title: '工艺流程', isTab: true } },
{ path: '/siteManage', component: () => import('@/views/spc-basic/siteManage'), name: 'siteManage', meta: { title: '站点管理', isTab: true } },
{ path: '/machineManage', component: () => import('@/views/spc-basic/machineManage'), name: 'machineManage', meta: { title: '机台管理', isTab: true } },
{ path: '/toolsType', component: () => import('@/views/spc-basic/toolsType'), name: 'toolsType', meta: { title: '量具管理', isTab: true } },
{ path: '/unitList', component: () => import('@/views/spc-basic/unitList'), name: 'unitList', meta: { title: '计量单位管理', isTab: true } },
// 质量规划
{ path: '/controlGraph', component: () => import('@/views/quality-planning/controlGraph'), name: 'controlGraph', meta: { title: '控制图形', isTab: true } },
{ path: '/controlRatio', component: () => import('@/views/quality-planning/controlRatio'), name: 'controlRatio', meta: { title: '控制系数', isTab: true } },
{ path: '/interpretationScheme', component: () => import('@/views/quality-planning/interpretationScheme'), name: 'interpretationScheme', meta: { title: '判读方案', isTab: true } },
// 过程检验
{ path: '/generalOperation', component: () => import('@/views/process-inspection/generalOperation'), name: 'generalOperation', meta: { title: '通用作业', isTab: true } },
{ path: '/XbarRGraph', component: () => import('@/views/process-inspection/Metrological/XbarRGraph'), name: 'XbarRGraph', meta: { title: '均值极差控制图', isTab: true } },
{ path: '/XbarSGraph', component: () => import('@/views/process-inspection/Metrological/XbarSGraph'), name: 'XbarSGraph', meta: { title: '均值标准差控制图', isTab: true } },
{ path: '/XMRGraph', component: () => import('@/views/process-inspection/Metrological/XMRGraph'), name: 'XMRGraph', meta: { title: '单值移动极差控制图', isTab: true } },
{ path: '/CGraph', component: () => import('@/views/process-inspection/Counting/CGraph'), name: 'CGraph', meta: { title: 'C图', isTab: true } },
{ path: '/UGraph', component: () => import('@/views/process-inspection/Counting/UGraph'), name: 'UGraph', meta: { title: 'U图', isTab: true } },
{ path: '/NPGraph', component: () => import('@/views/process-inspection/Counting/NPGraph'), name: 'NPGraph', meta: { title: 'NP图', isTab: true } },
{ path: '/PGraph', component: () => import('@/views/process-inspection/Counting/PGraph'), name: 'PGraph', meta: { title: 'P图', isTab: true } },
]
}
export function addDynamicRoute (routeParams, router) {
// 组装路由名称, 并判断是否已添加, 如是: 则直接跳转
var routeName = routeParams.routeName
var dynamicRoute = window.SITE_CONFIG['dynamicRoutes'].filter(item => item.name === routeName)[0]
if (dynamicRoute) {
return router.push({ name: routeName, params: routeParams.params })
}
// 否则: 添加并全局变量保存, 再跳转
dynamicRoute = {
path: routeName,
component: () => import(`@/views/modules/${routeParams.path}`),
name: routeName,
meta: {
...window.SITE_CONFIG['contentTabDefault'],
menuId: routeParams.menuId,
title: `${routeParams.title}`
}
}
router.addRoutes([
{
...moduleRoutes,
name: `main-dynamic__${dynamicRoute.name}`,
children: [dynamicRoute]
}
])
window.SITE_CONFIG['dynamicRoutes'].push(dynamicRoute)
router.push({ name: dynamicRoute.name, params: routeParams.params })
}
const router = new Router({
mode: 'hash',
scrollBehavior: () => ({ y: 0 }),
routes: pageRoutes.concat(moduleRoutes)
})
router.beforeEach((to, from, next) => {
// 添加动态(菜单)路由
// 已添加或者当前路由为页面路由, 可直接访问
if (window.SITE_CONFIG['dynamicMenuRoutesHasAdded'] || fnCurrentRouteIsPageRoute(to, pageRoutes)) {
return next()
}
// 获取字典列表, 添加并全局变量保存
http.get('/sys/dict/type/all').then(({ data: res }) => {
if (res.code !== 0) {
return
}
window.SITE_CONFIG['dictList'] = res.data
}).catch(() => {})
// 获取菜单列表, 添加并全局变量保存
http.get('/sys/menu/nav').then(({ data: res }) => {
if (res.code !== 0) {
Vue.prototype.$message.error(res.msg)
return next({ name: 'login' })
}
window.SITE_CONFIG['menuList'] = res.data
fnAddDynamicMenuRoutes(window.SITE_CONFIG['menuList'])
next({ ...to, replace: true })
}).catch(() => {
next({ name: 'login' })
})
})
/**
* 判断当前路由是否为页面路由
* @param {*} route 当前路由
* @param {*} pageRoutes 页面路由
*/
function fnCurrentRouteIsPageRoute (route, pageRoutes = []) {
var temp = []
for (var i = 0; i < pageRoutes.length; i++) {
if (route.path === pageRoutes[i].path) {
return true
}
if (pageRoutes[i].children && pageRoutes[i].children.length >= 1) {
temp = temp.concat(pageRoutes[i].children)
}
}
return temp.length >= 1 ? fnCurrentRouteIsPageRoute(route, temp) : false
}
/**
* 添加动态(菜单)路由
* @param {*} menuList 菜单列表
* @param {*} routes 递归创建的动态(菜单)路由
*/
function fnAddDynamicMenuRoutes (menuList = [], routes = []) {
var temp = []
for (var i = 0; i < menuList.length; i++) {
if (menuList[i].children && menuList[i].children.length >= 1) {
temp = temp.concat(menuList[i].children)
continue
}
// 组装路由
var route = {
path: '',
component: null,
name: '',
meta: {
...window.SITE_CONFIG['contentTabDefault'],
menuId: menuList[i].id,
title: menuList[i].name
}
}
// eslint-disable-next-line
let URL = (menuList[i].url || '').replace(/{{([^}}]+)?}}/g, (s1, s2) => eval(s2)) // URL支持{{ window.xxx }}占位符变量
if (isURL(URL)) {
route['path'] = route['name'] = `i-${menuList[i].id}`
route['meta']['iframeURL'] = URL
} else {
URL = URL.replace(/^\//, '').replace(/_/g, '-')
route['path'] = route['name'] = URL.replace(/\//g, '-')
route['component'] = () => import(`@/views/modules/${URL}`)
}
routes.push(route)
}
if (temp.length >= 1) {
return fnAddDynamicMenuRoutes(temp, routes)
}
// 添加路由
router.addRoutes([
{
...moduleRoutes,
name: 'main-dynamic-menu',
children: routes
},
{ path: '*', redirect: { name: '404' } }
])
window.SITE_CONFIG['dynamicMenuRoutes'] = routes
window.SITE_CONFIG['dynamicMenuRoutesHasAdded'] = true
}
export default router