integral-detail.js 5.57 KB
import {
  getBindtapData,
  getObjByListKeyValue
} from '../../utils/util';
let Date = require('../../utils/date.js');

let app = getApp();
Page({
  data: {
    authorizeVisible: false,
    userInfo: {},
    dateStart: "",
    dateEnd: "",
    dateStr: "",
    total: 0,
    page: 1,
    size: 10,
    isShowAll: false,
    status: "",
    dataList: [],
    navIndex: 0,
    navList: [{
        t1: "全部",
        t2: "",
        index: "0",
        pointsType: "",
        type: "",
      },
      {
        t1: "",
        t2: "购买积分",
        index: "1",
        pointsType: "buy",
        type: "buyNum",
      },
      {
        t1: "",
        t2: "推广积分",
        index: "2",
        pointsType: "promotion",
        type: "promotionNum",
      },
      {
        t1: "",
        t2: "签到积分",
        index: "3",
        pointsType: "sign",
        type: "signNum",
      },
      {
        t1: "",
        t2: "奖励积分",
        index: "4",
        pointsType: "award",
        type: "awardNum",
      },
      {
        t1: "",
        t2: "兑换积分",
        index: "5",
        pointsType: "exchange",
        type: "exchangeNum",
      },
    ],
  },
  onShareAppMessage() {},
  showAuth() {
    this.setData({
      authorizeVisible: true
    })
  },
  onLoad(options) {
    let dateStart = new Date().last().week().toString("yyyy-MM-dd");
    let dateEnd = Date.today().toString("yyyy-MM-dd");
    let dateStr = Date.today().toString("yyyy-MM");
    this.setData({
      dateStart,
      dateEnd,
      dateStr
    })
    this.queryPoints().then((result) => {
      this.refreshStatus();
    });

    this.initData();
  },

  initData() {
    this.queryMember().then((result) => {});
  },

  // 到达底部
  onReachBottom() {
    if (this.data.dataList.length < this.data.total) {
      this.setData({
        page: this.data.page + 1
      });
      this.queryPointsOverview();
    }
  },

  // 重置页面列表 点击搜索条件时需要
  resetPage() {
    this.setData({
      page: 1,
      dataList: []
    })
  },

  /**
   * 获取会员信息
   */
  queryMember() {
    return new Promise((resolve, reject) => {
      app.post({
        url: app.api.member,
        data: {}
      }).then((result) => {
        this.setData({
          userInfo: result
        })
        resolve();
      })
    });
  },

  /**
   * 积分值类型
   */
  queryPoints() {
    return new Promise((resolve, reject) => {
      app.post({
        url: app.api.points,
        data: {}
      }).then((result) => {
        console.log("queryPoints result:", result);

        let navList = this.data.navList;
        navList.forEach((element, index) => {
          if (index == 0) {
            element.t1 = "全部"
          } else {
            element.t1 = result[element.type] + ""
          }
        });
        this.setData({
          navList: navList
        })

        resolve();
      })
    });
  },

  /**
   * 积分概览
   */
  queryPointsOverview(isAll = false) {
    this.setData({
      isShowAll: isAll
    })
    return new Promise((resolve, reject) => {
      let year = isAll ? "" : this.data.dateStr.slice(0, 4);
      let month = isAll ? "" : this.data.dateStr.slice(5, 7);
      app.post({
        url: app.api.pointsOverview,
        data: {
          page: this.data.page,
          size: this.data.size,
          pointsType: this.data.status,
          year: year,
          month: month,
        }
      }).then((result) => {
        let navList = this.data.navList;
        let dataList = result.list;
        dataList = this.data.dataList.concat(dataList);
        dataList.forEach(element => {
          element.time = new Date(element.createTime).toString("yyyy.MM.dd");
          element.words = getObjByListKeyValue(element.pointsType, "pointsType", navList).t2;
        });
        this.setData({
          dataList: dataList,
          total: result.total
        })
        resolve();
      })
    });
  },

  /**
   * 请求所有数据
   */
  onShowAllHandler() {
    this.resetPage();
    this.queryPointsOverview(true);
  },

  bindDateChange(e) {
    console.log("bindDateChange:", e)
    this.setData({
      dateStr: e.detail.value
    })
    this.resetPage();
    this.refreshStatus();
  },

  /**
   * 选择导航
   * @param {*} evt
   */
  onNavSelectHandler(evt) {
    let navIndex = this.data.navIndex;
    let curIndex = getBindtapData(evt, "index");
    if (navIndex != curIndex) {
      this.setData({
        navIndex: curIndex
      })
      this.refreshStatus();
    }
  },

  /**
   * 刷新状态重新请求
   */
  refreshStatus() {
    let status = "";
    let navIndex = this.data.navIndex + "";
    switch (navIndex) {
      // 全部
      case "0":
        status = "";
        break;

        // 购买积分
      case "1":
        status = "buy";
        break;

        // 推广积分
      case "2":
        status = "promotion";
        break;

        // 签到积分
      case "3":
        status = "sign";
        break;

        // 奖励积分
      case "4":
        status = "award";
        break;

        // 兑换积分
      case "5":
        status = "exchange";
        break;

      default:
        break;
    }
    this.setData({
      status: status
    })
    this.resetPage();
    this.queryPointsOverview();
  },

  // 隐藏蒙层
  hideMask() {
    this.setData({
      authorizeVisible: false,
    })
  },
  // 子组件事件
  evtcomp(evt) {
    let {
      name,
      data
    } = evt.detail;
    switch (name) {

      // 隐藏弹窗
      case "_evt_hide_mask":
        this.hideMask();
        break;

      default:
        break;
    }
  },
})