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]
      }
    },
  }
})
相关推荐
2501_9159090620 小时前
iOS 应用反调试技详解术 检测调试器的原理与防护实践
android·ios·小程序·https·uni-app·iphone·webview
00后程序员张1 天前
iOS加固技术路线全面解析:Bitcode模式、源码模式与汇编模式对比及爱加密优势
android·汇编·ios·小程序·uni-app·cocoa·iphone
海纳百川·纳海川1 天前
租房行业数字化:换个思路解决“老问题”
大数据·微信小程序·小程序
上海云盾-小余2 天前
CC 高频请求 + DDoS 流量攻击实战防御:全链路 WAF 流量清洗部署完整流程
网络·爬虫·安全·小程序·ddos
2501_916008892 天前
iOS IPA文件反编译与打包操作方法,拆包分析防护和加固打包
android·macos·ios·小程序·uni-app·cocoa·iphone
2301_810313182 天前
海外教育APP定制发展现状与开发选型核心要点解析
小程序·团队开发·软件需求
2501_915921432 天前
iOS手游安全反外挂解决方案:基于Xcode的加固混淆特色详解
android·安全·ios·小程序·uni-app·iphone·xcode
禾高网络2 天前
回收小程序在环保和商业领域发挥更大的作用
java·大数据·人工智能·小程序
2501_916007472 天前
iOS App分发教程 App Store、TestFlight 与 Ad Hoc 的配置与上传方法
android·ios·小程序·https·uni-app·iphone·webview
游戏开发爱好者83 天前
iOS应用加固方案全解析:源码加固与IPA在线加固对比
android·macos·ios·小程序·uni-app·cocoa·iphone