switch.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import { defineStore } from 'pinia';
  2. import { ref } from 'vue';
  3. import type { StorageLike } from 'pinia-plugin-persistedstate';
  4. // 为 UniApp 创建适配的 storage(与 store/index.ts 保持一致)
  5. const uniStorage: StorageLike = {
  6. getItem: (key: string): string | null => {
  7. try {
  8. return uni.getStorageSync(key);
  9. } catch (error) {
  10. console.error('读取存储失败:', error);
  11. return null;
  12. }
  13. },
  14. setItem: (key: string, value: string): void => {
  15. try {
  16. uni.setStorageSync(key, value);
  17. } catch (error) {
  18. console.error('写入存储失败:', error);
  19. }
  20. },
  21. };
  22. // 判断当前是测试版还是正式版
  23. const accountInfo = uni.getAccountInfoSync();
  24. const prefixMap = {
  25. release: 'prod_', // production的缩写
  26. trial: 'beta_', // beta测试版
  27. develop: 'dev_', // development的缩写
  28. };
  29. const SWITCH_STORE_KEY = `${prefixMap[accountInfo?.miniProgram?.envVersion] || 'dev_'}switch_store`;
  30. /**
  31. * Switch页面跳转参数存储接口
  32. */
  33. export interface SwitchPageParams {
  34. [key: string]: {
  35. data: any; // 存储的参数数据
  36. timestamp: number; // 存储时间戳
  37. expire?: number; // 过期时间(毫秒),可选
  38. };
  39. }
  40. /**
  41. * Switch页面跳转参数存储Store
  42. * 用于在小程序switch页面跳转时传递参数
  43. * 简化版:只保留基于页面路径的方法,获取数据后自动清除
  44. */
  45. export const useSwitchStore = defineStore(
  46. 'switch',
  47. () => {
  48. // 存储所有switch页面跳转参数
  49. const switchParams = ref<SwitchPageParams>({});
  50. /**
  51. * 为指定页面路径设置参数
  52. * @param pagePath 目标页面路径(如:'/pages/switch/index1')
  53. * @param data 要存储的参数数据
  54. * @param expire 过期时间(毫秒),默认1分钟
  55. */
  56. const setParamsForPage = (pagePath: string, data: any, expire: number = 1 * 60 * 1000): void => {
  57. const params = {
  58. data,
  59. timestamp: Date.now(),
  60. expire,
  61. };
  62. // 使用页面路径作为键存储
  63. switchParams.value[pagePath] = params;
  64. };
  65. /**
  66. * 获取指定页面路径的参数
  67. * @param pagePath 目标页面路径(如:'/pages/switch/index1')
  68. * @returns 存储的参数数据,如果不存在或已过期则返回null
  69. */
  70. const getAndClearParamsForPage = (pagePath: string): any => {
  71. // 从内存中获取
  72. const params = switchParams.value[pagePath];
  73. // 检查参数是否存在
  74. if (!params) {
  75. return null;
  76. }
  77. // 检查是否过期
  78. const { data, timestamp, expire } = params;
  79. // if (expire && Date.now() - timestamp > expire) {
  80. // // 清理过期数据
  81. // delete switchParams.value[pagePath];
  82. // return null;
  83. // }
  84. // // 获取数据后清理
  85. // delete switchParams.value[pagePath];
  86. return data;
  87. };
  88. /**
  89. * 清除指定页面路径的参数
  90. * @param pagePath 目标页面路径(如:'/pages/switch/index1')
  91. */
  92. const clearParamsForPage = (pagePath: string): void => {
  93. // 从内存中删除
  94. delete switchParams.value[pagePath];
  95. };
  96. /**
  97. * 清理所有过期的switch页面跳转参数
  98. */
  99. const cleanupExpiredParams = (): void => {
  100. const now = Date.now();
  101. // 清理内存中的过期参数
  102. Object.keys(switchParams.value).forEach((key) => {
  103. const params = switchParams.value[key];
  104. if (params.expire && now - params.timestamp > params.expire) {
  105. delete switchParams.value[key];
  106. }
  107. });
  108. };
  109. return {
  110. // 状态
  111. switchParams,
  112. // 方法
  113. setParamsForPage,
  114. getAndClearParamsForPage,
  115. clearParamsForPage,
  116. cleanupExpiredParams,
  117. };
  118. },
  119. {
  120. // 启用持久化
  121. persist: {
  122. key: SWITCH_STORE_KEY,
  123. storage: uniStorage,
  124. },
  125. }
  126. );