useTabbar.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import type { RouteLocationRaw } from 'vue-router'
  2. import useTabbarStore from '@/store/modules/tabbar'
  3. import Message from 'vue-m-message'
  4. export default function useTabbar() {
  5. const route = useRoute()
  6. const router = useRouter()
  7. const tabbarStore = useTabbarStore()
  8. function getId() {
  9. return route.fullPath
  10. }
  11. function open(to: RouteLocationRaw) {
  12. const index = tabbarStore.list.findIndex(item => item.tabId === getId())
  13. tabbarStore.$patch({
  14. leaveIndex: index,
  15. })
  16. router.push(to)
  17. }
  18. function go(delta: number) {
  19. const tabId = getId()
  20. router.go(delta)
  21. tabbarStore.remove(tabId)
  22. }
  23. function close(to: RouteLocationRaw) {
  24. const tabId = getId()
  25. router.push(to).then(() => {
  26. tabbarStore.remove(tabId)
  27. })
  28. }
  29. function closeById(tabId = getId()) {
  30. const activedTabId = getId()
  31. if (tabbarStore.list.some(item => item.tabId === tabId)) {
  32. if (tabbarStore.list.length > 1) {
  33. // 如果关闭的标签正好是当前路由
  34. if (tabId === activedTabId) {
  35. const index = tabbarStore.list.findIndex(item => item.tabId === tabId)
  36. if (index < tabbarStore.list.length - 1) {
  37. close(tabbarStore.list[index + 1].fullPath)
  38. }
  39. else {
  40. close(tabbarStore.list[index - 1].fullPath)
  41. }
  42. }
  43. else {
  44. tabbarStore.remove(tabId)
  45. }
  46. }
  47. else {
  48. Message.error('当前只有一个标签页,已阻止关闭', {
  49. zIndex: 2000,
  50. })
  51. }
  52. }
  53. else {
  54. Message.error('关闭的页面不存在', {
  55. zIndex: 2000,
  56. })
  57. }
  58. }
  59. /**
  60. * 关闭两侧标签页
  61. */
  62. function closeOtherSide(tabId = getId()) {
  63. const activedTabId = getId()
  64. // 如果操作的是非当前路由标签页,则先跳转到指定路由标签页
  65. if (tabId !== activedTabId) {
  66. const index = tabbarStore.list.findIndex(item => item.tabId === tabId)
  67. router.push(tabbarStore.list[index].fullPath)
  68. }
  69. tabbarStore.removeOtherSide(tabId)
  70. }
  71. /**
  72. * 关闭左侧标签页
  73. */
  74. function closeLeftSide(tabId = getId()) {
  75. const activedTabId = getId()
  76. // 如果操作的是非当前路由标签页,需要判断当前标签页是否在指定标签页左侧,如果是则先跳转到指定路由标签页
  77. if (tabId !== activedTabId) {
  78. const index = tabbarStore.list.findIndex(item => item.tabId === tabId)
  79. const activedIndex = tabbarStore.list.findIndex(item => item.tabId === activedTabId)
  80. if (activedIndex < index) {
  81. router.push(tabbarStore.list[index].fullPath)
  82. }
  83. }
  84. tabbarStore.removeLeftSide(tabId)
  85. }
  86. /**
  87. * 关闭右侧标签页
  88. */
  89. function closeRightSide(tabId = getId()) {
  90. const activedTabId = getId()
  91. // 如果操作的是非当前路由标签页,需要判断当前标签页是否在指定标签页右侧,如果是则先跳转到指定路由标签页
  92. if (tabId !== activedTabId) {
  93. const index = tabbarStore.list.findIndex(item => item.tabId === tabId)
  94. const activedIndex = tabbarStore.list.findIndex(item => item.tabId === activedTabId)
  95. if (activedIndex > index) {
  96. router.push(tabbarStore.list[index].fullPath)
  97. }
  98. }
  99. tabbarStore.removeRightSide(tabId)
  100. }
  101. /**
  102. * 校验指定标签两侧是否有可关闭的标签
  103. */
  104. function checkCloseOtherSide(tabId = getId()) {
  105. return tabbarStore.list.some((item) => {
  106. return item.tabId !== tabId
  107. })
  108. }
  109. /**
  110. * 校验指定标签左侧是否有可关闭的标签
  111. */
  112. function checkCloseLeftSide(tabId = getId()) {
  113. let flag = true
  114. if (tabId === tabbarStore.list[0]?.tabId) {
  115. flag = false
  116. }
  117. else {
  118. const index = tabbarStore.list.findIndex(item => item.tabId === tabId)
  119. flag = tabbarStore.list.some((item, i) => {
  120. return i < index && item.tabId !== tabId
  121. })
  122. }
  123. return flag
  124. }
  125. /**
  126. * 校验指定标签右侧是否有可关闭的标签
  127. */
  128. function checkCloseRightSide(tabId = getId()) {
  129. let flag = true
  130. if (tabId === tabbarStore.list.at(-1)?.tabId) {
  131. flag = false
  132. }
  133. else {
  134. const index = tabbarStore.list.findIndex(item => item.tabId === tabId)
  135. flag = tabbarStore.list.some((item, i) => {
  136. return i >= index && item.tabId !== tabId
  137. })
  138. }
  139. return flag
  140. }
  141. return {
  142. getId,
  143. open,
  144. go,
  145. close,
  146. closeById,
  147. closeOtherSide,
  148. closeLeftSide,
  149. closeRightSide,
  150. checkCloseOtherSide,
  151. checkCloseLeftSide,
  152. checkCloseRightSide,
  153. }
  154. }