navigation.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { useRoute, useRouter, onBeforeRouteLeave } from 'vue-router'
  2. import animateRouter from './animateRouter'
  3. export function useNavigation() {
  4. const route = useRoute()
  5. const router = useRouter()
  6. // 缓存全局Url参数
  7. const setGlobalUrlParams = <T extends object>(params?: T) => {
  8. sessionStorage.setItem('globalUrlParams', JSON.stringify(params ?? {}))
  9. }
  10. // 是否有历史页面
  11. const hasHistory = () => {
  12. const state = animateRouter.getState()
  13. return state.historyRoutes.length > 1
  14. }
  15. // 获取全局Url参数(只能获取一次)
  16. const getGlobalUrlParams = () => {
  17. const params = sessionStorage.getItem('globalUrlParams')
  18. if (params) {
  19. sessionStorage.removeItem('globalUrlParams')
  20. return JSON.parse(params)
  21. } else {
  22. return {}
  23. }
  24. }
  25. // 获取查询字符串
  26. const getQueryString = (name: string) => {
  27. const qs = route.query[name]
  28. return qs?.toString()
  29. }
  30. const getQueryStringToNumber = (name: string) => {
  31. const reg = /^[0-9]+.?[0-9]*/
  32. const value = getQueryString(name) ?? ''
  33. return reg.test(value) ? Number(value) : 0
  34. }
  35. // 返回指定页面
  36. // const backTo = <T extends object>(name: string, params?: T) => {
  37. // const { state } = animateRouter
  38. // const total = state.history.length - 1
  39. // const index = state.history.findIndex(((e) => e.name === name))
  40. // if (total > -1) {
  41. // if (index > -1) {
  42. // setGlobalUrlParams(params)
  43. // const delta = total - index
  44. // if (delta > 0) {
  45. // router.go(-delta)
  46. // } else {
  47. // router.replace({ name })
  48. // }
  49. // }
  50. // } else {
  51. // router.replace({ name })
  52. // }
  53. // }
  54. // 返回主页
  55. const backHome = () => {
  56. const state = animateRouter.getState()
  57. const delta = state.historyRoutes.length - 1
  58. if (delta > 0) {
  59. router.go(-delta)
  60. } else {
  61. const page = state.historyRoutes[0]
  62. if (page?.name !== 'home-index') {
  63. router.replace({ name: 'home-index' })
  64. }
  65. }
  66. }
  67. // 返回上个页面
  68. const routerBack = <T extends object>(params?: T) => {
  69. setGlobalUrlParams(params)
  70. if (hasHistory()) {
  71. router.back()
  72. } else {
  73. router.replace({ path: '/' })
  74. }
  75. }
  76. // 路由跳转
  77. const routerTo = (name: string, replace = false) => {
  78. if (replace) {
  79. router.replace({ name })
  80. } else {
  81. router.push({ name })
  82. }
  83. }
  84. // 路由守卫
  85. const beforeRouteLeave = (callback: () => boolean) => {
  86. onBeforeRouteLeave((to, from, next) => {
  87. if (callback()) {
  88. next()
  89. } else {
  90. next(false)
  91. }
  92. })
  93. }
  94. return {
  95. route,
  96. router,
  97. hasHistory,
  98. setGlobalUrlParams,
  99. getGlobalUrlParams,
  100. getQueryString,
  101. getQueryStringToNumber,
  102. backHome,
  103. routerBack,
  104. routerTo,
  105. beforeRouteLeave,
  106. }
  107. }