/*
 * @Descripttion: HJ
 * @Company: 思宏科技
 * @version: v1.0
 * @Author: HJ
 * @Date: 2020-09-17 13:58:49
 * @LastEditors: WorkerChen z874512298@163.com
 * @LastEditTime: 2022-05-13 14:27:01
 */
export const loadData = {
  loadCount: 0,
  pageLoad: true,
};
export const loadEnd = (self) => {
  let { loadCount } = self.data;
  if (loadCount === 0) wx.showLoading({ title: "加载中..." });
  const { loadEndCount, pageLoad } = self.data;
  ++loadCount;
  if (pageLoad && loadCount >= loadEndCount) {
    self.setData(
      {
        pageLoad: false,
        loadCount: 0,
      },
      () => {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        const { loadEnd } = self;
        loadEnd && loadEnd();
      }
    );
  } else {
    self.setData({
      loadCount,
    });
  }
};

// 购物车本地存储的key
export const _GLOBAL_CART_KEY = "carts";
// 本地存储的购物车上限
const _LIMIT = 10;
// uidata kye
export const _GLOBAL_UI_DATA_KEY = "uiDataList";

// 设置购物车
// TODO  优化
export const setShoppingCart = (key, data, globalKey = _GLOBAL_CART_KEY) => {
  let carts = wx.getStorageSync(globalKey) || [];
  if (!carts || !Array.isArray(carts)) carts = new Map();
  else carts = new Map(carts);
  carts.set(key, data);
  console.log(carts,'设置购物车')
  carts = Array.from(carts);
  if (carts.length > _LIMIT) {
    carts.shift();
  }
  wx.setStorageSync(globalKey, carts);
};

// 获取购物车
export const getShoppingCart = (key, globalKey = _GLOBAL_CART_KEY) => {
  let cart = wx.getStorageSync(globalKey);
  cart = new Map(cart);
  return cart.get(key) || "";
};
