page.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { CUSTOM_TABBAR_NO_CACHE } from '@/layouts/fg-tabbar/tabbarList'
  2. // uniapp 返回 入参为堆栈
  3. export function goBack(delta = 1) {
  4. console.log(delta)
  5. uni.navigateBack({ delta })
  6. }
  7. // uniapp 跳转页面 可携带参数
  8. // 示例: toPage('/pages/productDetail/productDetail', { id: 123 })
  9. export function toPage(url: string, params?: Record<string, any>, isRedirect = false) {
  10. let targetUrl = url
  11. const tabBarPages = ['/pages/index/index', '/pages/income/income', '/pages/mine/mine']
  12. if (params && Object.keys(params).length > 0) {
  13. const data = JSON.stringify(params)
  14. targetUrl = `${url}?params=${encodeURIComponent(data)}`
  15. }
  16. if (tabBarPages.includes(url)) {
  17. if (CUSTOM_TABBAR_NO_CACHE) {
  18. uni.navigateTo({ url: targetUrl })
  19. }
  20. else {
  21. uni.switchTab({
  22. url: targetUrl,
  23. })
  24. }
  25. }
  26. else {
  27. if (isRedirect) {
  28. uni.redirectTo({
  29. url: targetUrl,
  30. })
  31. }
  32. else {
  33. uni.navigateTo({
  34. url: targetUrl,
  35. })
  36. }
  37. }
  38. }
  39. // page参数解析方法
  40. export function getPageParams(options: any) {
  41. if (options && options.params) {
  42. try {
  43. return JSON.parse(decodeURIComponent(options.params))
  44. }
  45. catch (e) {
  46. console.error('解析页面参数失败:', e)
  47. return {}
  48. }
  49. }
  50. return {}
  51. }