p-honggang.li
2 天以前 349091f54dab47b15ca81613f02765490a2ce7d0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
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 = []
    },
  },
})