index.js 4.25 KB
// packageMine/pages/order/index/index.js
import {
  ORDER_STATUS,
  OrderStatusTypes,
  OrderStatusEnum,
} from "~/constants/mine.js";
import { calcOrderProp } from "~/packageMine/pages/order/utils";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    pageNum: 1,
    tab: Object.freeze(
      Object.values(ORDER_STATUS).filter(
        (item) =>
          item.type === OrderStatusTypes.QUERY_TAB ||
          item.type === OrderStatusTypes.MY_ORDER
      )
    ),
    list: [],
    listFlag: true,
    hasUserInfo: true,
    inputDebounceTimer: null,
    curTab: { index: 0, requestType: "0", requestMethod: "getOrderList" },
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    // 清理防抖定时器,避免内存泄漏
    if (this.data.inputDebounceTimer) {
      clearTimeout(this.data.inputDebounceTimer);
    }
  },

  navbarReady(e) {
    wx.p.wxApi.getViewInfo(".tab", (res) => {
      this.setData({
        padding_bottom: e.detail.height,
        margin_top: e.detail.height + res.height,
      });
    });
  },
  back() {
    let { length } = getCurrentPages();
    if (length == 1) wx.p.router.switchTab("/pages/tab/mine/index");
    else wx.p.router.back();
  },
  changeTab(e) {
    const tab = e.detail.item;
    this.setIndexAndRequest(tab);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.p.router.getRouterParams(options).then((option) => {
      let tab = option.tab;
      if (!tab || !tab.index) {
        tab = ORDER_STATUS[OrderStatusEnum.ALL];
      }
      this.setIndexAndRequest(tab);
    });
  },
  setIndexAndRequest(tab) {
    this.setData(
      {
        curTab: tab,
      },
      () => this.clearAndGetList()
    );
  },
  onShow() {
    if (this.refresh) this.clearAndGetList();
  },
  onHide() {
    this.refresh = true;
  },
  async clearData(cb) {
    const res = !wx.p.isLogin;
    if (res) return;
    this.setData(
      {
        listFlag: false,
        list: [],
        nothing: false,
        pageNum: 1,
      },
      cb
    );
  },

  clearAndGetList() {
    this.clearData(() => this.getOrderList());
  },
  search() {
    this.clearAndGetList();
  },
  clear() {
    this.setData(
      {
        keyword: "",
      },
      () => this.clearAndGetList()
    );
  },

  input(e) {
    if (this.data.inputDebounceTimer) {
      clearTimeout(this.data.inputDebounceTimer);
    }
    const timer = setTimeout(() => {
      this.setData(
        {
          keyword: e.detail.value,
        },
        () => this.clearAndGetList()
      );
    }, 500);
    this.setData({
      inputDebounceTimer: timer,
    });
  },

  blur() {
    let { keyword } = this.data;
    this.setData({
      keyword,
    });
  },
  getOrderList() {
    wx.p.wxApi.showLoading("加载中...");
    let {
      curTab: { requestType, requestMethod },
      pageNum,
      keyword,
    } = this.data;
    const userInfo = wx.p.storage.get("userInfo") || {};
    let { merchantId, channelSource, phone } = userInfo;
    //如果 merchantId, channelSource, phone  这三个参数有一个未获取到,给sentry上报异常,并且带上具体的值
    if (!merchantId || !channelSource || !phone) {
      // 设置自定义上下文信息
      wx.p.sentry.reportParamError(
        "订单列表异常:merchantId, channelSource, phone 参数异常",
        userInfo
      );
    }
    let obj = {
      requestType,
      merchantId,
      phone,
      pageSize: 99999,
      currentPage: pageNum,
      scoreSource: channelSource,
    };
    if (keyword) {
      keyword = keyword.replace(/\ /g, "");
      if (keyword.length) obj.goodsInfo = keyword;
    }
    let list = [];
    wx.p.api.mineApi[requestMethod](obj)
      .then((data) => {
        data?.list?.forEach((e) => {
          const { ___qty, ___price, ___totalPrice } = calcOrderProp(e);
          list.push({
            ...e,
            ___price,
            ___totalPrice,
            ___qty,
          });
        });
        this.setData({
          list,
          listFlag: true,
        });
      }).catch((err) => {
        wx.p.sentry.reportError(`订单列表异常:${err.message}`, {...obj,err});
      })
      .finally(() => {
        wx.hideLoading();
      });
  },
  onPullDownRefresh: function () {
    this.clearAndGetList();
  },
});