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]
      }
    },
  }
})
相关推荐
程序员陆通12 小时前
零基础AI编程开发微信小程序赚流量主广告实战
微信小程序·小程序·ai编程
paopaokaka_luck12 小时前
校园快递小程序(腾讯地图API、二维码识别、Echarts图形化分析)
vue.js·spring boot·后端·小程序·uni-app
头发还在的女程序员16 小时前
ThinkPHP+Mysql 灵活用工小程序-技术深度解析与实践指南
数据库·mysql·小程序
编程猪猪侠20 小时前
解决uni-app微信小程序编译报错:unexpected character `1`
微信小程序·小程序·uni-app
2501_9159090621 小时前
iOS 签名证书全生命周期实战,从开发到上架的多阶段应用
android·ios·小程序·https·uni-app·iphone·webview
suncentwl1 天前
论答题pk小程序软件版权的
小程序·答题小程序·答题pk·知识竞赛·答题pk软件
weixin_hcs22011 天前
小程序打通美团核销:解锁到店综合业态私域密码,赋能6000+门店破局增长
小程序·美团团购·美团新平台·java接入美团团购·快速接入美团团购
weixin_177297220691 天前
盲盒抽谷机小程序系统开发:解锁盲盒新玩法,开启潮玩社交新时代
小程序
2501_915918412 天前
iOS 文件管理全流程实战,从开发调试到数据迁移
android·ios·小程序·https·uni-app·iphone·webview
说私域3 天前
基于开源AI智能名片链动2+1模式S2B2C商城小程序的运营策略创新研究
人工智能·小程序·开源