history.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <template>
  2. <div class="router-history" v-if="showLayout=== true" >
  3. <el-tabs
  4. v-model="activeValue"
  5. :closable="!(historys.length === 1 && $route.name === defaultRouter)"
  6. type="card"
  7. @contextmenu.prevent="openContextMenu($event)"
  8. @tab-click="changeTab"
  9. @tab-remove="removeTab"
  10. >
  11. <el-tab-pane
  12. v-for="item in historys"
  13. :key="name(item)"
  14. :label="item.meta.title"
  15. :name="name(item)"
  16. :tab="item"
  17. class="gva-tab"
  18. >
  19. <template #label>
  20. <span
  21. :tab="item"
  22. :style="{
  23. color: activeValue === name(item) ? userStore.activeColor : '#333',
  24. }"
  25. ><i
  26. class="dot"
  27. :style="{
  28. backgroundColor:
  29. activeValue === name(item) ? userStore.activeColor : '#ddd',
  30. }"
  31. />
  32. {{ fmtTitle(item.meta.title,item) }}</span>
  33. </template>
  34. </el-tab-pane>
  35. </el-tabs>
  36. <!--自定义右键菜单html代码-->
  37. <ul
  38. v-show="contextMenuVisible"
  39. :style="{ left: left + 'px', top: top + 'px' }"
  40. class="contextmenu"
  41. >
  42. <li @click="closeAll">关闭所有</li>
  43. <li @click="closeLeft">关闭左侧</li>
  44. <li @click="closeRight">关闭右侧</li>
  45. <li @click="closeOther">关闭其他</li>
  46. </ul>
  47. </div>
  48. </template>
  49. <script setup>
  50. import { emitter } from '@/utils/bus.js'
  51. import { computed, onUnmounted, ref, watch } from 'vue'
  52. import { useRoute, useRouter } from 'vue-router'
  53. import { useUserStore } from '@/pinia/modules/user'
  54. import { fmtTitle } from '@/utils/fmtRouterTitle'
  55. defineOptions({
  56. name: 'HistoryComponent',
  57. })
  58. const route = useRoute()
  59. const router = useRouter()
  60. const showLayout = ref(true)
  61. const getFmtString = (item) => {
  62. return item.name + JSON.stringify(item.query) + JSON.stringify(item.params)
  63. }
  64. const historys = ref([])
  65. const activeValue = ref('')
  66. const contextMenuVisible = ref(false)
  67. const userStore = useUserStore()
  68. // if(userStore.userInfo.authorityId==1){
  69. // showLayout.value=false
  70. // }
  71. const name = (item) => {
  72. return (
  73. item.name + JSON.stringify(item.query) + JSON.stringify(item.params)
  74. )
  75. }
  76. const left = ref(0)
  77. const top = ref(0)
  78. const isCollapse = ref(false)
  79. const isMobile = ref(false)
  80. const rightActive = ref('')
  81. const defaultRouter = computed(() => userStore.userInfo.authority.defaultRouter)
  82. const openContextMenu = (e) => {
  83. if (
  84. historys.value.length === 1 &&
  85. route.name === defaultRouter.value
  86. ) {
  87. return false
  88. }
  89. let id = ''
  90. if (e.srcElement.nodeName === 'SPAN') {
  91. id = e.srcElement.offsetParent.id
  92. } else {
  93. id = e.srcElement.id
  94. }
  95. if (id) {
  96. contextMenuVisible.value = true
  97. let width
  98. if (isCollapse.value) {
  99. width = 54
  100. } else {
  101. width = 220
  102. }
  103. if (isMobile.value) {
  104. width = 0
  105. }
  106. left.value = e.clientX - width
  107. top.value = e.clientY + 10
  108. rightActive.value = id.substring(4)
  109. }
  110. }
  111. const closeAll = () => {
  112. historys.value = [
  113. {
  114. name: defaultRouter.value,
  115. meta: {
  116. title: '首页',
  117. },
  118. query: {},
  119. params: {},
  120. },
  121. ]
  122. router.push({ name: defaultRouter.value })
  123. contextMenuVisible.value = false
  124. sessionStorage.setItem('historys', JSON.stringify(historys.value))
  125. }
  126. const closeLeft = () => {
  127. let right
  128. const rightIndex = historys.value.findIndex((item) => {
  129. if (getFmtString(item) === rightActive.value) {
  130. right = item
  131. }
  132. return getFmtString(item) === rightActive.value
  133. })
  134. const activeIndex = historys.value.findIndex(
  135. (item) => getFmtString(item) === activeValue.value
  136. )
  137. historys.value.splice(0, rightIndex)
  138. if (rightIndex > activeIndex) {
  139. router.push(right)
  140. }
  141. sessionStorage.setItem('historys', JSON.stringify(historys.value))
  142. }
  143. const closeRight = () => {
  144. let right
  145. const leftIndex = historys.value.findIndex((item) => {
  146. if (getFmtString(item) === rightActive.value) {
  147. right = item
  148. }
  149. return getFmtString(item) === rightActive.value
  150. })
  151. const activeIndex = historys.value.findIndex(
  152. (item) => getFmtString(item) === activeValue.value
  153. )
  154. historys.value.splice(leftIndex + 1, historys.value.length)
  155. if (leftIndex < activeIndex) {
  156. router.push(right)
  157. }
  158. sessionStorage.setItem('historys', JSON.stringify(historys.value))
  159. }
  160. const closeOther = () => {
  161. let right
  162. historys.value = historys.value.filter((item) => {
  163. if (getFmtString(item) === rightActive.value) {
  164. right = item
  165. }
  166. return getFmtString(item) === rightActive.value
  167. })
  168. router.push(right)
  169. sessionStorage.setItem('historys', JSON.stringify(historys.value))
  170. }
  171. const isSame = (route1, route2) => {
  172. if (route1.name !== route2.name) {
  173. return false
  174. }
  175. if (Object.keys(route1.query).length !== Object.keys(route2.query).length || Object.keys(route1.params).length !== Object.keys(route2.params).length) {
  176. return false
  177. }
  178. for (const key in route1.query) {
  179. if (route1.query[key] !== route2.query[key]) {
  180. return false
  181. }
  182. }
  183. for (const key in route1.params) {
  184. if (route1.params[key] !== route2.params[key]) {
  185. return false
  186. }
  187. }
  188. return true
  189. }
  190. const setTab = (route) => {
  191. if (!historys.value.some((item) => isSame(item, route))) {
  192. const obj = {}
  193. obj.name = route.name
  194. obj.meta = { ...route.meta }
  195. delete obj.meta.matched
  196. obj.query = route.query
  197. obj.params = route.params
  198. historys.value.push(obj)
  199. }
  200. window.sessionStorage.setItem('activeValue', getFmtString(route))
  201. }
  202. const historyMap = ref({})
  203. const changeTab = (TabsPaneContext) => {
  204. const name = TabsPaneContext?.props?.name
  205. if (!name) return
  206. const tab = historyMap.value[name]
  207. router.push({
  208. name: tab.name,
  209. query: tab.query,
  210. params: tab.params,
  211. })
  212. }
  213. const removeTab = (tab) => {
  214. const index = historys.value.findIndex(
  215. (item) => getFmtString(item) === tab
  216. )
  217. if (getFmtString(route) === tab) {
  218. if (historys.value.length === 1) {
  219. router.push({ name: defaultRouter.value })
  220. } else {
  221. if (index < historys.value.length - 1) {
  222. router.push({
  223. name: historys.value[index + 1].name,
  224. query: historys.value[index + 1].query,
  225. params: historys.value[index + 1].params,
  226. })
  227. } else {
  228. router.push({
  229. name: historys.value[index - 1].name,
  230. query: historys.value[index - 1].query,
  231. params: historys.value[index - 1].params,
  232. })
  233. }
  234. }
  235. }
  236. historys.value.splice(index, 1)
  237. }
  238. watch(() => contextMenuVisible.value, () => {
  239. if (contextMenuVisible.value) {
  240. document.body.addEventListener('click', () => {
  241. contextMenuVisible.value = false
  242. })
  243. } else {
  244. document.body.removeEventListener('click', () => {
  245. contextMenuVisible.value = false
  246. })
  247. }
  248. })
  249. watch(() => route, (to, now) => {
  250. if (to.name === 'Login' || to.name === 'Reload') {
  251. return
  252. }
  253. historys.value = historys.value.filter((item) => !item.meta.closeTab)
  254. setTab(to)
  255. sessionStorage.setItem('historys', JSON.stringify(historys.value))
  256. activeValue.value = window.sessionStorage.getItem('activeValue')
  257. }, { deep: true })
  258. watch(() => historys.value, () => {
  259. sessionStorage.setItem('historys', JSON.stringify(historys.value))
  260. historyMap.value = {}
  261. historys.value.forEach((item) => {
  262. historyMap.value[getFmtString(item)] = item
  263. })
  264. emitter.emit('setKeepAlive', historys.value)
  265. }, {
  266. deep: true
  267. })
  268. const initPage = () => {
  269. // 全局监听 关闭当前页面函数
  270. emitter.on('closeThisPage', () => {
  271. removeTab(name(route))
  272. })
  273. // 全局监听 关闭所有页面函数
  274. emitter.on('closeAllPage', () => {
  275. closeAll()
  276. })
  277. emitter.on('mobile', (data) => {
  278. isMobile.value = data
  279. })
  280. emitter.on('collapse', (data) => {
  281. isCollapse.value = data
  282. })
  283. const initHistorys = [
  284. {
  285. name: defaultRouter.value,
  286. meta: {
  287. title: '首页',
  288. },
  289. query: {},
  290. params: {},
  291. },
  292. ]
  293. historys.value =
  294. JSON.parse(sessionStorage.getItem('historys')) || initHistorys
  295. if (!window.sessionStorage.getItem('activeValue')) {
  296. activeValue.value = getFmtString(route)
  297. } else {
  298. activeValue.value = window.sessionStorage.getItem('activeValue')
  299. }
  300. setTab(route)
  301. if (window.sessionStorage.getItem('needCloseAll') === 'true') {
  302. closeAll()
  303. window.sessionStorage.removeItem('needCloseAll')
  304. }
  305. }
  306. initPage()
  307. onUnmounted(() => {
  308. emitter.off('collapse')
  309. emitter.off('mobile')
  310. })
  311. </script>
  312. <style lang="scss">
  313. .contextmenu {
  314. width: 100px;
  315. margin: 0;
  316. border: 1px solid #ccc;
  317. background: #fff;
  318. z-index: 3000;
  319. position: absolute;
  320. list-style-type: none;
  321. padding: 5px 0;
  322. border-radius: 4px;
  323. font-size: 14px;
  324. color: #333;
  325. box-shadow: 2px 2px 3px 0 rgba(0, 0, 0, 0.2);
  326. }
  327. .el-tabs__item .el-icon-close {
  328. color: initial !important;
  329. }
  330. .el-tabs__item .dot {
  331. content: "";
  332. width: 9px;
  333. height: 9px;
  334. margin-right: 8px;
  335. display: inline-block;
  336. border-radius: 50%;
  337. transition: background-color 0.2s;
  338. }
  339. .contextmenu li {
  340. margin: 0;
  341. padding: 7px 16px;
  342. }
  343. .contextmenu li:hover {
  344. background: #f2f2f2;
  345. cursor: pointer;
  346. }
  347. </style>