| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- import { defineStore } from 'pinia';
- import { ref } from 'vue';
- import type { StorageLike } from 'pinia-plugin-persistedstate';
- // 为 UniApp 创建适配的 storage(与 store/index.ts 保持一致)
- const uniStorage: StorageLike = {
- getItem: (key: string): string | null => {
- try {
- return uni.getStorageSync(key);
- } catch (error) {
- console.error('读取存储失败:', error);
- return null;
- }
- },
- setItem: (key: string, value: string): void => {
- try {
- uni.setStorageSync(key, value);
- } catch (error) {
- console.error('写入存储失败:', error);
- }
- },
- };
- // 判断当前是测试版还是正式版
- const accountInfo = uni.getAccountInfoSync();
- const prefixMap = {
- release: 'prod_', // production的缩写
- trial: 'beta_', // beta测试版
- develop: 'dev_', // development的缩写
- };
- const SWITCH_STORE_KEY = `${prefixMap[accountInfo?.miniProgram?.envVersion] || 'dev_'}switch_store`;
- /**
- * Switch页面跳转参数存储接口
- */
- export interface SwitchPageParams {
- [key: string]: {
- data: any; // 存储的参数数据
- timestamp: number; // 存储时间戳
- expire?: number; // 过期时间(毫秒),可选
- };
- }
- /**
- * Switch页面跳转参数存储Store
- * 用于在小程序switch页面跳转时传递参数
- * 简化版:只保留基于页面路径的方法,获取数据后自动清除
- */
- export const useSwitchStore = defineStore(
- 'switch',
- () => {
- // 存储所有switch页面跳转参数
- const switchParams = ref<SwitchPageParams>({});
- /**
- * 为指定页面路径设置参数
- * @param pagePath 目标页面路径(如:'/pages/switch/index1')
- * @param data 要存储的参数数据
- * @param expire 过期时间(毫秒),默认1分钟
- */
- const setParamsForPage = (pagePath: string, data: any, expire: number = 1 * 60 * 1000): void => {
- const params = {
- data,
- timestamp: Date.now(),
- expire,
- };
- // 使用页面路径作为键存储
- switchParams.value[pagePath] = params;
- };
- /**
- * 获取指定页面路径的参数
- * @param pagePath 目标页面路径(如:'/pages/switch/index1')
- * @returns 存储的参数数据,如果不存在或已过期则返回null
- */
- const getAndClearParamsForPage = (pagePath: string): any => {
- // 从内存中获取
- const params = switchParams.value[pagePath];
- // 检查参数是否存在
- if (!params) {
- return null;
- }
- // 检查是否过期
- const { data, timestamp, expire } = params;
- // if (expire && Date.now() - timestamp > expire) {
- // // 清理过期数据
- // delete switchParams.value[pagePath];
- // return null;
- // }
- // // 获取数据后清理
- // delete switchParams.value[pagePath];
- return data;
- };
- /**
- * 清除指定页面路径的参数
- * @param pagePath 目标页面路径(如:'/pages/switch/index1')
- */
- const clearParamsForPage = (pagePath: string): void => {
- // 从内存中删除
- delete switchParams.value[pagePath];
- };
- /**
- * 清理所有过期的switch页面跳转参数
- */
- const cleanupExpiredParams = (): void => {
- const now = Date.now();
- // 清理内存中的过期参数
- Object.keys(switchParams.value).forEach((key) => {
- const params = switchParams.value[key];
- if (params.expire && now - params.timestamp > params.expire) {
- delete switchParams.value[key];
- }
- });
- };
- return {
- // 状态
- switchParams,
- // 方法
- setParamsForPage,
- getAndClearParamsForPage,
- clearParamsForPage,
- cleanupExpiredParams,
- };
- },
- {
- // 启用持久化
- persist: {
- key: SWITCH_STORE_KEY,
- storage: uniStorage,
- },
- }
- );
|