index.js 4.37 KB
import OrderListService from './orderListService.js';
import event from '../../utils/event.js';
import { getImgUrl } from '../../utils/index.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    api:Object,
    tabs: [
      {
        name: "待付款",
        data:{}
      },
      {
        name: "待发货",
        data: {}
      },
      {
        name: "待收货",
        data: {}
      },
      {
        name: "交易成功",
        data: {}
      },
      {
        name: "交易失败",
        data: {}
      }
    ],
    slideOffset: 0,
    activeIndex: 0,
    sliderWidth: 96,
    contentHeight: 0,
    contentWidth:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    let api = new OrderListService();
    
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          sliderWidth: (res.windowWidth - 20) / that.data.tabs.length,
          sliderOffset: (res.windowWidth - 20) / that.data.tabs.length * that.data.activeIndex,
          contentHeight: res.windowHeight - res.windowWidth / 750 * 108,//计算内容区高度,rpx -> px计算
          contentWidth: res.windowWidth
        });
        that.setData({ api: api });
        that.fetchOrderList(2,1);
      }
    });
    event.one('refresh-order', () => {
      // console.log("======refresh-order")
      let currentType = that.data.activeIndex + 2;
      this.fetchOrderList(currentType,1);
      
      
    });
  },
  bindChange: function (e) {
    var current = e.detail.current;
    let currentType = current + 2;
    this.setData({
      activeIndex: current,
      sliderOffset: this.data.sliderWidth * current
    });
    let tab = this.data.tabs[current].data
    if(!tab.data){
      this.fetchOrderList(currentType, 1);
    }
    
    // console.log("bindChange:" + current + "===" + currentType);
  },

  navTabClick: function (e) {
    this.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id
    });
    // console.log("navTabClick:" + e.currentTarget.id);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },

  /**
   * 请求订单列表数据
   * tabType:买家 ("buy") 2: "待付款", 3: "待发货",4: "待收货",5: "交易成功",6: "交易失败"
   */
  fetchOrderList: function(cType, page) {
    let that = this;
    let index = that.data.activeIndex;
    let tab = that.data.tabs[index];
    wx.showLoading({
      title: '加载中...',
    })
    let params = {
      limit: '20',
      page: page,
      tabType: 'buy',
      type: cType,
    }
    this.data.api.getOrderListInfo(params, () => {
      wx.hideLoading();
    }).then(data => {
      let products = data.data;
      for (let index in products) {
        let productInfo = products[index];
        productInfo.goodsInfo.goodImg = getImgUrl(productInfo.goodsInfo.goodImg,270,270);
        products[index] = productInfo;
      }
      if (tab.data.data && page != 1){
        tab.data.data = tab.data.data.concat(products);
        tab.data.page = data.page;
      } else {
        tab.data = data;
      }
      tab.showLoadMoreView = false;
      that.data.tabs[index] = tab;
      that.setData({ tabs: that.data.tabs });
       
    });
  },
  /**
   * 按钮操作
   */
  actionButtonClick: function (event) {
    let action = event.detail; 

  },
  onLoadMore: function () {
    let that = this;
    let currentIndex = that.data.activeIndex;
    let tabData = that.data.tabs[currentIndex].data;
    let currentPage = tabData.page + 1;
    if (currentPage <= tabData.pagetotal){
      // console.log("onLoadMore" + currentPage);
      let currentType = that.data.activeIndex + 2;
      that.fetchOrderList(currentType, currentPage);
    }
  },
  catchTouchMove: function (e) {
    return false;

  }
  
})