import { isEmpty } from 'lodash-es'
|
import { defineStore } from 'pinia'
|
import { RouteLocationNormalized, RouteRecordRaw } from 'vue-router'
|
import _ from 'lodash'
|
|
interface NavState {
|
// 激活tab的index
|
activeIndex: number
|
// 激活的tab
|
activeRoute: any
|
// tab列表
|
tabsView: any[]
|
cachedViews: any[]
|
excludeViews: any
|
}
|
function returnFilterIndex(tabsView: any, arr: string[]) {
|
let nameIndex: number = -1
|
for (let i = 0; i < arr.length; i++) {
|
nameIndex = _.findIndex(tabsView, function (o: any) {
|
return o.name == arr[i]
|
})
|
if (nameIndex != -1) break
|
}
|
return nameIndex
|
}
|
|
export const useNavTabs = defineStore('navTabs', {
|
state: (): NavState => ({
|
// 激活tab的index
|
activeIndex: 0,
|
// 激活的tab
|
activeRoute: null,
|
// tab列表
|
tabsView: [],
|
// 用于页面缓存,name数组 每个缓存页面需单独导出组件名称
|
cachedViews: [],
|
// 用于刷新页面,活跃页组件名称 name
|
excludeViews: '',
|
}),
|
persist: {
|
storage: localStorage,
|
},
|
getters: {
|
getCachedViews: (state) => {
|
return state.cachedViews
|
},
|
},
|
actions: {
|
updateCachedViews(route: RouteLocationNormalized) {
|
if (route.meta.addTab == false) {
|
return
|
}
|
if (!this.cachedViews.includes(route.name)) {
|
this.cachedViews.push(route.name)
|
}
|
},
|
// 不缓存的页面名称 tabs 刷新活跃页用
|
updateExcludeViews(name: any) {
|
this.excludeViews = name
|
},
|
deleteCachedViews(name: string) {
|
this.cachedViews.map((v, k) => {
|
if (v == name) {
|
this.cachedViews.splice(k, 1)
|
return
|
}
|
})
|
},
|
// 添加活跃标签
|
addTab(route: RouteLocationNormalized) {
|
if (route.meta.addTab == false) {
|
return
|
}
|
for (const key in this.tabsView) {
|
if (this.tabsView[key].path === route.path) {
|
this.tabsView[key].params = !isEmpty(route.params)
|
? route.params
|
: this.tabsView[key].params
|
this.tabsView[key].query = !isEmpty(route.query)
|
? route.query
|
: this.tabsView[key].query
|
return
|
}
|
}
|
let name: any = route.name || ''
|
let nameIndex: number = this.tabsView.length
|
let optionPutUrl: string[] = [
|
'optionPut',
|
'optionPutDetail',
|
'optionPutView',
|
'optionPutNewDetail'
|
]
|
if (optionPutUrl.includes(name)) {
|
nameIndex = returnFilterIndex(this.tabsView, optionPutUrl)
|
}
|
let performanceMeritRatingUrl: string[] = [
|
'meritRatingManage',
|
'meritRatingDetail',
|
'meritRatingView',
|
'meritRatingNewDetail'
|
]
|
if (performanceMeritRatingUrl.includes(name)) {
|
nameIndex = returnFilterIndex(this.tabsView, performanceMeritRatingUrl)
|
}
|
let resultApprovalUrl: string[] = [
|
'resultApproval',
|
'resultApprovalDetail',
|
'resultApprovalView',
|
]
|
if (resultApprovalUrl.includes(name)) {
|
nameIndex = returnFilterIndex(this.tabsView, resultApprovalUrl)
|
}
|
let fileBoxUrl: string[] = [
|
'fileBoxManage',
|
'fileBoxDetail',
|
'fileBoxView',
|
'fileBoxNewDetail'
|
]
|
if (fileBoxUrl.includes(name)) {
|
nameIndex = returnFilterIndex(this.tabsView, fileBoxUrl)
|
}
|
let subscriptionUrl: string[] = ['subscriptionApproval','subscriptionApprovalView']
|
if (subscriptionUrl.includes(name)) {
|
nameIndex = returnFilterIndex(this.tabsView, subscriptionUrl)
|
}
|
let newsCenterConfigUrl:string[]=["newsCenterConfiguration",'newsCenterConfigurationDetail']
|
if (newsCenterConfigUrl.includes(name)) {
|
nameIndex = returnFilterIndex(this.tabsView, newsCenterConfigUrl)
|
}
|
let graphConfigUrl:string[]=["graphAuthorizationList",'graphAuthorizationDetail']
|
if (graphConfigUrl.includes(name)) {
|
nameIndex = returnFilterIndex(this.tabsView, graphConfigUrl)
|
}
|
let graphEditConfigUrl:string[]=["graphAuthorizationEdit",'graphAuthorizationView']
|
if (graphEditConfigUrl.includes(name)) {
|
nameIndex = returnFilterIndex(this.tabsView, graphEditConfigUrl)
|
}
|
// 问题工单productionQuestionView
|
let productionQuestionUrl:string[]= ['productionQuestion','productionQuestionDetail']
|
if (productionQuestionUrl.includes(name)) {
|
nameIndex = returnFilterIndex(this.tabsView, productionQuestionUrl)
|
}
|
// 问题工单审批列表及审批详情和查看
|
let workOrdersApproveUrl:string[]= ['workOrdersApproveManage','workOrdersUserApproval',"workOrdersUserApprovalDetail"]
|
if (workOrdersApproveUrl.includes(name)) {
|
nameIndex = returnFilterIndex(this.tabsView, workOrdersApproveUrl)
|
}
|
// 不需要显示菜单的路由
|
const noShowList: string[] = ['submissionTracing', 'approveDetails']
|
if (noShowList.includes(name)) return
|
if (nameIndex == -1) {
|
this.tabsView.push(route)
|
} else {
|
this.tabsView.splice(nameIndex, 1, route)
|
}
|
},
|
// 关闭标签
|
closeTab(route: RouteLocationNormalized) {
|
this.tabsView.map((v, k) => {
|
if (v.path == route.path) {
|
this.tabsView.splice(k, 1)
|
return
|
}
|
})
|
},
|
|
// 设置活跃路由
|
setActiveRoute(route: RouteLocationNormalized) {
|
const currentRouteIndex: number = this.tabsView.findIndex(
|
(item: RouteLocationNormalized) => {
|
return item.path === route.path
|
}
|
)
|
if (currentRouteIndex === -1) return
|
|
this.activeRoute = route
|
this.activeIndex = currentRouteIndex
|
},
|
// 清空tabs
|
removeTabs() {
|
this.tabsView = []
|
this.activeIndex = 0
|
this.activeRoute = null
|
this.cachedViews = []
|
},
|
},
|
})
|