taro+pinia+小程序存储配置持久化

主要通过taro的getStorageSync,setStorageSync实现配置持久化

js 复制代码
// https://pinia.esm.dev/introduction.html
import { defineStore } from 'pinia';
import { CreditCardDateUtils } from '@/untils/compute';
import { getStorageSync, setStorageSync } from "@tarojs/taro";


interface dataType {
  id: string,
  name: string,
  statementDay: number,//出账日
  dueDay: number,//还款日
  dueDate?: string,//还款日
  freeDay?: number,//免息天数
  currentFreeDay: number,//剩余免息天数
  isCurrentCycle: boolean,//是否本周期
}

export const useCardStore = defineStore('card', {
  state: () => {
    return {
      _cardData: <dataType[]>[],
    };
  },
  getters: {
    getCardInfo: state => {
      return (id: string) => state._cardData.find((item) => (item.id === id));
    },
    getCardDataList: state => {
      return () => state._cardData;
    },
    getIndexCardDataList: state => {
      const dataList = state._cardData.map((item) => {
        const creditCardUtils = new CreditCardDateUtils(Number(item.statementDay), Number(item.dueDay));
        item.freeDay = creditCardUtils.calculateMonthlyGracePeriod();
        item.currentFreeDay = creditCardUtils.calculateGracePeriodDays();
        const { dueDate, isCurrentCycle } = creditCardUtils.getCreditCardDueDate();
        item.dueDate = dueDate.toLocaleDateString();
        item.isCurrentCycle = isCurrentCycle;
        return item;
      }).sort((a, b) => {
        // 1. 当前周期的卡片优先
        if (a.isCurrentCycle && !b.isCurrentCycle) return -1;
        if (!a.isCurrentCycle && b.isCurrentCycle) return 1;

        // 2. 同一周期内按剩余免息天数降序排列
        return b.currentFreeDay - a.currentFreeDay;
      });
      console.log(dataList, 'dataList')
      return () => dataList;
    }
  },
  actions: {
    delCardData(id: string) {
      this._cardData = this._cardData.filter((item) => item.id !== id);
      console.log(id);
      console.log(this._cardData);
    },
    setCardData(cardData: dataType) {
      const index = this._cardData.findIndex((item: dataType) => item.id == cardData.id);
      console.log(this._cardData, 'this._cardData');
      console.log(cardData, 'cardData');
      console.log(index, 'index');
      if (index <= -1) {
        this._cardData.push(cardData);
      } else {
        this._cardData[index] = cardData;
      }
    },
  },
  // persist: true,
  // 配置持久化
  persist: {
    // 调整为兼容多端的API
    storage: {
      setItem(key, value) {
        setStorageSync(key, value) // [!code warning]
      },
      getItem(key) {
        return getStorageSync(key) // [!code warning]
      }
    },
  }
})
相关推荐
paopaokaka_luck5 小时前
基于SpringBoot+Uniapp的非遗文化宣传小程序(AI问答、协同过滤算法、Echarts图形化分析)
java·vue.js·spring boot·后端·学习·小程序·uni-app
说私域5 小时前
开源AI智能客服、AI智能名片与S2B2C商城小程序在客户复购与转介绍中的协同效应研究
人工智能·小程序·开源
weixin_lynhgworld5 小时前
短剧小程序系统开发:重塑影视内容传播格局
小程序
紫眸少年丶9 小时前
uni-app开发小程序,根据图片提取主题色值
前端·小程序·uni-app
waillyer1 天前
taro跳转路由取值
前端·javascript·taro
超级土豆粉1 天前
Taro 路由相关 API 详解与实战
前端·javascript·react.js·ecmascript·taro
JIngJaneIL2 天前
跑腿小程序|基于微信小程序的跑腿平台小程序设计与实现(源码+数据库+文档)
java·数据库·微信小程序·小程序·论文·毕设·跑腿小程序
源码宝2 天前
掌上医院微信小程序平台如何对接医保在线支付?
微信小程序·小程序·医保支付
惊鸿2872 天前
Taro3+小程序分包配置指南
前端·taro