history.js 4.21 KB
// pages/me/history.js
import util from '../../utils/util';
import App from '../../utils/api';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sellers:{},
    hasMore:false,
    hasFirstShow:true,
    sellersLength:0,
  },
  sellers:[],
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  formatSellers(data){
    let sellers=[];
    let hasChange=false
    this.sellers.forEach(seller=>{
      if(seller.id==data.sid){
        hasChange=true
        seller.hasFollow=data.hasFollow
      }
      if(seller.hasFollow){
        sellers.push(seller)
      }
    })
    if(!hasChange){
      sellers=[]
    }
    this.setData({
      sellers:sellers
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  hasOnShow:true,
  onShow: function () {
    if(this.data.sellers.length==0){
      wx.showLoading({
        title: '加载中',
      })
      this.setData({
        hasFirstShow:true,
      })
    }
    if(this.hasMore || this.data.sellers.length==0){
      this.sellers=[]
      this.offset=1
      // this.setData({
      //   sellers:[]
      // })
      this.hasOnShow=true;
      this.selectComponent('#getpos').getPos();
    }
  },

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function (e) {
    console.log(e)
    if(this.hasMore){
      this.GetCollectionSellerList();
    }
  },
  offset:1,
  size:200,
  hasMore:true,
  GetCollectionSellerList(){
    App.Customer.GetHistoryList().then(res=>{
      let sellers=res.data;
      let sellersLength=sellers.length;
      this.hasMore=sellersLength>=this.size
      if(this.hasMore){
        this.offset+=1
      }
      let group={}
      sellers.forEach((seller,si) => {
        let day=util.unixtoTime(seller.cdate,"`DAY/MM月DD日`")
        if(!group[day]){
          group[day]={
            day:day,
            sellers:[]
          }
        }
        group[day].sellers.push(seller)
      })
      if(!this.hasOnShow){
        sellers=[...this.data.sellers,...sellers]
      }
      this.hasOnShow=false;
      this.setData({
        sellers:group,
        sellersLength:sellersLength,
        hasFirstShow:false,
      })
      this.sellers=group
      wx.hideLoading()
      if(res.data.length==0 && this.offset==1){
        this.GetRecommendSellerLists()
      }
    })
  },
  gotoDetail(e){
    wx.navigateTo({
      url:'/pages/reserve/detail?id='+util.attr(e,'sid')
    })
  },
  onpos(e){
    console.log(e);
    this.location=e.detail
    this.GetCollectionSellerList();
  },

  hasRecommend:true,
  GetRecommendSellerLists(){
    if(!this.hasRecommend) return
    let param={}
    if(this.location){
      param={
        lat:this.location.latitude,
        lng:this.location.longitude
      }
    }
    App.Customer.GetRecommendSellerLists(param).then(res=>{
      this.setData({
        rsl:res.data
      })
    })
  },
  hasGoto:false,
  bindtransition(e){
    console.log(e)
    if('change'==e.type){
      this.hasGoto=false
    }
    if('animationfinish'==e.type){
      if(this.hasGoto && (e.detail.current+1)==this.data.rsl.length){
        wx.switchTab({
          url: '/pages/reserve/index'
        })
        return;
      }else{
        this.hasGoto=true
      }
    }
  },
  goHome(){
    wx.switchTab({
      url: '/pages/reserve/index'
    })
  },
  clean(){
    wx.showModal({
      title: '                  '+'确定清空浏览记录?',
      cancelColor:'#999999',
      confirmColor:'',
      success: (res)=> {
        if (res.confirm) {
          App.Customer.ClearHistory();
          this.setData({
            sellers:[],
            sellersLength:0,
          })
          this.GetRecommendSellerLists();
        }
      }
    })
  },
  sbtrEvent(e){
    console.log(e);
    if(e.detail.act=='oncall'){
      this.selectComponent('#as').show(e.detail.seller);
    }
  },
})