index.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { useRoute, useRouter, onBeforeRouteLeave } from 'vue-router'
  2. import animateRouter from '@mobile/router/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 getParamString = (name: string) => {
  27. return route.params[name] ?? ''
  28. }
  29. // 获取查询字符串
  30. const getQueryString = (name: string) => {
  31. const qs = route.query[name]
  32. if (qs) {
  33. return qs.toString()
  34. }
  35. return ''
  36. }
  37. const getQueryStringToNumber = (name: string) => {
  38. const reg = /^[0-9]+.?[0-9]*/
  39. const value = getQueryString(name)
  40. return reg.test(value) ? Number(value) : 0
  41. }
  42. // 返回指定页面
  43. // const backTo = <T extends object>(name: string, params?: T) => {
  44. // const { state } = animateRouter
  45. // const total = state.history.length - 1
  46. // const index = state.history.findIndex(((e) => e.name === name))
  47. // if (total > -1) {
  48. // if (index > -1) {
  49. // setGlobalUrlParams(params)
  50. // const delta = total - index
  51. // if (delta > 0) {
  52. // router.go(-delta)
  53. // } else {
  54. // router.replace({ name })
  55. // }
  56. // }
  57. // } else {
  58. // router.replace({ name })
  59. // }
  60. // }
  61. // 返回主页
  62. const backHome = () => {
  63. const state = animateRouter.getState()
  64. const delta = state.historyRoutes.length - 1
  65. const params = { tabIndex: 0 }
  66. if (delta) {
  67. setGlobalUrlParams(params)
  68. router.go(-delta)
  69. } else {
  70. const page = state.historyRoutes[0]
  71. if (page?.name !== 'home-index') {
  72. setGlobalUrlParams(params)
  73. router.replace({ name: 'home-index' })
  74. }
  75. }
  76. }
  77. // 返回上个页面
  78. const routerBack = <T extends object>(params?: T) => {
  79. setGlobalUrlParams(params)
  80. router.back()
  81. }
  82. // 路由跳转
  83. const routerTo = (name: string, replace = false) => {
  84. if (replace) {
  85. router.replace({ name })
  86. } else {
  87. router.push({ name })
  88. }
  89. }
  90. // 路由守卫
  91. const beforeRouteLeave = (callback: () => boolean) => {
  92. onBeforeRouteLeave((to, from, next) => {
  93. if (callback()) {
  94. next()
  95. } else {
  96. next(false)
  97. }
  98. })
  99. }
  100. return {
  101. route,
  102. router,
  103. hasHistory,
  104. setGlobalUrlParams,
  105. getGlobalUrlParams,
  106. getParamString,
  107. getQueryString,
  108. getQueryStringToNumber,
  109. backHome,
  110. routerBack,
  111. routerTo,
  112. beforeRouteLeave,
  113. }
  114. }