rank.js 4.18 KB
import {
  getBindtapData
} from '../../utils/util';

let app = getApp();
Page({
  data: {
    authorizeVisible: false,
    navIndex: 0,
    status: "promotion",
    turnsIndex: 0, //轮次 0:本轮  1:历史
    rankInfo: {},

    historyRankInfoList: [],
    historyIndex: 0,
  },
  onShareAppMessage() {},
  showAuth() {
    this.setData({
      authorizeVisible: true
    })
  },
  onLoad(options) {
    this.initData();
  },
  initData() {
    this.refreshStatus();
  },

  /**
   * 推广积分排行
   */
  queryActivity() {
    return new Promise((resolve, reject) => {
      this.setData({
        rankInfo: {}
      })
      let status = this.data.status;
      app.post({
        url: app.api.activity,
        data: {
          key: status
        }
      }).then((result) => {
        let rankInfo = this.formatRankInfo(result, status);
        this.setData({
          rankInfo: rankInfo
        })
        console.log("rankInfo 1:", rankInfo);
        resolve();
      })
    });
  },

  /**
   * 推广积分排行
   */
  queryActivityHistory() {
    return new Promise((resolve, reject) => {
      this.setData({
        rankInfo: {}
      })
      let status = this.data.status;
      app.post({
        url: app.api.activityHistory,
        data: {
          page: 1,
          size: 999999,
          key: this.data.status
        }
      }).then((result) => {
        let historyRankInfoList = result.list;
        this.setData({
          historyRankInfoList: historyRankInfoList
        })
        console.log("rankInfo 2:", historyRankInfoList);
        this.setHistory();
        resolve();
      })
    });
  },

  /**
   * 格式化
   */
  formatRankInfo(val, status) {
    let rankInfo = val;
    if (status == "promotion") {
      rankInfo.myMember.rank = rankInfo.myMember.promotionNo;
      rankInfo.myMember.points = rankInfo.myMember.promotionPoints;
      rankInfo.memberList.forEach(element => {
        element.rank = element.promotionNo;
        element.points = element.promotionPoints;
      });
    } else {
      rankInfo.myMember.rank = rankInfo.myMember.rankNo;
      rankInfo.myMember.points = rankInfo.myMember.rankPoints;
      rankInfo.memberList.forEach(element => {
        element.rank = element.rankNo;
        element.points = element.rankPoints;
      });
    }
    return rankInfo;
  },

  /**
   * 设置
   */
  setHistory() {
    let index = this.data.historyIndex;
    let status = this.data.status;
    let historyRankInfoList = this.data.historyRankInfoList
    let rankInfo = this.formatRankInfo(historyRankInfoList[index], status);
    this.setData({
      rankInfo
    })
    console.log("rankInfo:", rankInfo);
  },

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

  // 选择轮次 历史/本轮
  onChangeTurnsHandler(evt) {
    let turnsIndex = this.data.turnsIndex;
    let curIndex = getBindtapData(evt, "index");
    if (turnsIndex != curIndex) {
      this.setData({
        turnsIndex: curIndex
      });
      console.log("curIndex:", curIndex);
      if (curIndex == 0) {
        this.queryActivity();
      } else {
        this.queryActivityHistory();
      }
    }

  },

  /**
   * 拾取历史活动
   */
  bindHistoryChange(e) {
    let index = e.detail.value;
    this.setData({
      historyIndex: index,
    })
    this.setHistory()
  },

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

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

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


  /**
   * 排行榜奖励
   * @param {*} evt
   */
  onRankGiftHandler(evt) {
    app.router.push({
      path: "rankGift"
    })
  },

  // 滚动到底部 分页加载
  onRankScrolltolower(e) {

  }
})