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]
      }
    },
  }
})
相关推荐
低代码布道师3 小时前
少儿舞蹈小程序(7)打造您的“活”名片:动态展示机构实力
低代码·小程序
程序员陆通7 小时前
用 Cursor AI 快速开发你的第一个编程小程序
人工智能·小程序
鹧鸪云光伏与储能软件开发7 小时前
投资储能项目能赚多少钱?小程序帮你测算
运维·数据库·小程序·光伏·光伏设计软件·光伏设计
微三云-轩8 小时前
小程序:12亿用户的入口,企业数字化的先锋军
大数据·小程序·开源软件
低代码布道师8 小时前
少儿舞蹈小程序(8)校区信息后台搭建
低代码·小程序
2501_916008898 小时前
iOS 抓包工具有哪些?全面盘点主流工具与功能对比分析
android·ios·小程序·https·uni-app·iphone·webview
2501_915921438 小时前
iOS混淆工具实战 视频流媒体类 App 的版权与播放安全保护
android·ios·小程序·https·uni-app·iphone·webview
—Qeyser12 小时前
好看的背景颜色 uniapp+小程序
小程序·uni-app·uniapp·微信小游戏
2501_9160088912 小时前
uni-app iOS 日志与崩溃分析全流程 多工具协作的实战指南
android·ios·小程序·https·uni-app·iphone·webview
Dark_programmer12 小时前
钉钉小程序 - - - - - 小程序内打开OA文档链接
小程序·钉钉