tj
2025-06-05 bba272999cc546f65781bf3d20245a3f819af67f
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<template>
  <div v-if="histories.length > 1" ref="resueTab" class="reuse-tab">
    <swiper
      class="reuse-tab-wrap"
      slides-per-view="auto"
      :space-between="1"
      :initial-slide="0"
      effect="slide"
      :prevent-clicks="false"
      :free-mode="true"
      :mousewheel="true"
      direction="horizontal"
    >
      <swiper-slide v-for="(item, index) in histories" :key="item.path">
        <router-link
          class="reuse-tab-item"
          :class="item.path === $route.path ? 'active' : ''"
          :to="item.path"
          @contextmenu.prevent="onTags(index, $event)"
        >
          <i v-if="!filterIcon(stageList[item.stageId].icon)" :class="stageList[item.stageId].icon"></i>
          <img v-else :src="stageList[item.stageId].icon" style="width: 16px" />
          <span style="padding: 0 5px">{{ stageList[item.stageId].title }}</span>
          <span class="el-icon-close" @click.prevent.stop="close(index)" />
        </router-link>
      </swiper-slide>
    </swiper>
 
    <ul v-show="visible" :style="{ left: left + 'px', top: top + 'px' }" class="contextmenu">
      <li @click="closeAll">关闭所有</li>
      <li @click="closeOthers">关闭其他</li>
      <li @click="closeLeft" v-if="hasLeft">关闭左侧</li>
      <li @click="closeRight" v-if="hasRight">关闭右侧</li>
    </ul>
  </div>
</template>
 
<script>
import { mapGetters } from 'vuex'
import emitter from 'lin/util/emitter'
import { Swiper, SwiperSlide } from 'swiper/vue'
import SwiperCore, { Mousewheel } from 'swiper'
 
import 'swiper/swiper.scss'
 
SwiperCore.use([Mousewheel])
 
export default {
  components: { Swiper, SwiperSlide },
  data() {
    return {
      histories: [],
      visible: false,
      hasLeft: true,
      hasRight: true,
      top: 0,
      left: 0,
      index: 0,
    }
  },
  watch: {
    $route(to) {
      // 对路由变化作出响应...
      const { histories } = this
      const flag = histories.find(item => item.path === to.path)
      if (flag) {
        return
      }
 
      const ele = {}
      ele.stageId = to.name
      ele.path = to.path
      ele.routePath = to.matched[to.matched.length - 1].path
      this.histories = [ele, ...histories]
    },
    loggedIn(val) {
      if (val) {
        return
      }
      this.closeAll()
    },
    visible(value) {
      if (value) {
        document.body.addEventListener('click', this.closeMenu)
      } else {
        document.body.removeEventListener('click', this.closeMenu)
      }
    },
    // 舞台改变时触发
    stageList() {
      this.init()
    },
    histories(arr) {
      if (arr.length < 2) {
        emitter.emit('noReuse')
      } else {
        emitter.emit('hasReuse')
      }
    },
  },
  created() {
    // 关闭窗口时执行
    window.onbeforeunload = () => {
      // 缓存历史记录
      window.localStorage.setItem('history', JSON.stringify(this.histories))
    }
  },
  computed: {
    loggedIn() {
      return this.$store.state.loggedIn
    },
    defaultRoute() {
      return this.$store.state.defaultRoute
    },
    ...mapGetters(['getStageByRoute', 'getStageByName', 'stageList']),
  },
  mounted() {
    this.init()
    emitter.on('clearTap', () => {
      this.histories = []
    })
  },
  methods: {
    init() {
      const histories = []
 
      // 获取当前的历史记录, 可能从本地存储, 可能直接获取当前的
      let localHistory
      if (this.histories.length > 0) {
        localHistory = [...this.histories]
      } else {
        localHistory = window.localStorage.getItem('history') || '[]'
        localHistory = JSON.parse(localHistory)
      }
 
      localHistory.forEach(item => {
        let findResult
        if (item.name) {
          findResult = this.getStageByName(item.name)
        } else {
          findResult = this.getStageByRoute(item.routePath)
        }
        if (!findResult) {
          return
        }
 
        histories.push({ ...item, stageId: findResult.name })
        this.histories = histories
      })
    },
    filterIcon(icon) {
      if (!icon) {
        return false
      }
      return icon.indexOf('/') !== -1
    },
    closeAll() {
      this.histories = []
      this.$router.push(this.defaultRoute)
    },
    closeOthers() {
      this.$router.push(this.histories[this.index].path)
      this.histories = []
    },
    closeLeft() {
      this.histories.splice(0, this.index)
    },
    closeRight() {
      this.histories.splice(this.index + 1, this.histories.length - this.index - 1)
    },
    onTags(index, event) {
      this.closeMenu()
      const menuMinWidth = 126
      const offsetLeft = this.$el?.getBoundingClientRect()?.left
      const { offsetWidth } = this.$el
      const maxLeft = offsetWidth - menuMinWidth
      const left = event.clientX - offsetLeft + 15
 
      if (left > maxLeft) {
        this.left = maxLeft
      } else {
        this.left = left
      }
 
      if (index === 0) {
        this.hasLeft = false
      }
 
      if (index + 1 === this.histories.length) {
        this.hasRight = false
      }
 
      this.top = 18
      this.index = index
      this.visible = true
    },
    closeMenu() {
      this.visible = false
      this.hasLeft = true
      this.hasRight = true
    },
    close(index) {
      // 检测是否是当前页, 如果是当前页则自动切换路由
      if (this.$route.path === this.histories[index].path) {
        if (index > 0) {
          this.$router.push(this.histories[index - 1].path)
        } else if (this.histories.length > 1) {
          this.$router.push(this.histories[1].path)
        } else {
          this.$router.push(this.defaultRoute)
        }
      }
      // 删除该历史记录
      this.histories.splice(index, 1)
      this.histories = [...this.histories]
    },
  },
}
</script>
 
<style lang="scss" scoped>
.swiper-slide {
  width: auto !important;
  min-width: 126px;
  display: flex;
  height: $reuse-tab-height;
  flex-direction: column;
  justify-content: center;
  background-color: $reuse-tab-item-background;
  color: $right-side-font-color;
  margin-right: 1px;
}
 
.reuse-tab-wrap {
  bottom: 0;
  left: 0;
  user-select: none;
  height: $reuse-tab-height;
  background: $header-background;
  font-size: 14px;
  color: #8c98ae;
  display: flex;
  align-items: center;
  overflow: hidden;
 
  .reuse-tab-item {
    box-sizing: border-box;
    width: auto;
    height: $reuse-tab-height;
    min-width: 126px;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 0 1em;
    margin-right: 1px;
    position: relative;
    white-space: nowrap;
 
    > i {
      color: $theme;
    }
 
    .el-icon-close {
      opacity: 0;
      position: absolute;
    }
 
    &:hover {
      background: $theme;
      border: none;
      color: #fff;
 
      > i {
        color: #fff;
      }
 
      .el-icon-close {
        position: absolute;
        display: inline-block;
        width: 14px;
        height: 14px;
        top: 0;
        right: 0;
        opacity: 1;
        border-radius: 0 0 0 14px;
        background: rgba(255, 255, 255, 0.3);
 
        &::before {
          font-size: 12px;
          position: absolute;
          right: -1px;
          transform: scale(0.7);
        }
      }
    }
  }
 
  .active {
    box-sizing: border-box;
    height: 40px;
    color: #ffffff;
    background: $theme;
    border: none;
    position: relative;
 
    > i {
      color: #fff;
    }
 
    .el-icon-close {
      position: absolute;
      display: inline-block;
      width: 14px;
      height: 14px;
      top: 0;
      right: 0;
      opacity: 1;
      border-radius: 0 0 0 14px;
      background: rgba(255, 255, 255, 0.3);
 
      &::before {
        font-size: 12px;
        position: absolute;
        right: -1px;
        transform: scale(0.7);
      }
    }
  }
 
  .reuse-tab-wrap {
    height: 100%;
  }
}
.reuse-tab {
  position: relative;
  .contextmenu {
    margin: 0;
    background: #ffffff;
    z-index: 3000;
    position: absolute;
    list-style-type: none;
    padding: 5px 0;
    border-radius: 4px;
    font-size: 14px;
    font-weight: 400;
    color: #596c8e;
    box-shadow: 2px 2px 3px 0 rgba(0, 0, 0, 0.3);
    li {
      margin: 0;
      padding: 10px 20px;
      cursor: pointer;
      &:hover {
        background: #ebeff8;
        color: #6182c9;
      }
    }
  }
}
</style>