follow.js 3.71 KB
// pages/me/follow.js
import util from '../../utils/util';
import App from '../../utils/api';
const {regeneratorRuntime} = global
import PubSub from '../../utils/PubSub'
import reddot from '../../utils/reddot';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sellers:[],
    hasMore:true,
    hasFirstShow:true,
  },
  sellers:[],
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    PubSub.on(
      'follow',
      data=>{
        console.log('follow.msg',data)
        this.formatSellers(data);
      }
    )
    reddot.init()
  },
  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 (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 1
      })
    }
    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 () {
    PubSub.off('follow')
  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function (e) {
    console.log(e)
    if(this.hasMore){
      this.GetCollectionSellerList();
    }
  },
  offset:1,
  size:10,
  hasMore:true,
  GetCollectionSellerList(){
    App.Customer.GetCollectionSellerList({
      openid:getApp().openid,
      lat:this.location.latitude,
      lng:this.location.longitude,
      offset:this.offset,
      size:this.size,
    }).then(res=>{
      let sellers=res.data;
      let sellersLength=sellers.length;
      this.hasMore=sellersLength>=this.size
      if(this.hasMore){
        this.offset+=1
      }
      sellers.forEach((seller,si) => {
        // if(seller.distance || seller.distance===0){
        //   seller.distance = parseInt(seller.distance);
        //   if (seller.distance > 1000) {
        //     seller.distance = (seller.distance / 1000).toFixed(2) + 'km'
        //   } else {
        //     seller.distance += 'm';
        //   }
        // }
        if(seller.seller_tel){
          seller.seller_tel=seller.seller_tel.split(',');
        }else{
          seller.seller_tel=[]
        }
        seller.hasFollow=true
      })
      if(!this.hasOnShow){
        sellers=[...this.data.sellers,...sellers]
      }
      this.hasOnShow=false;
      this.setData({
        sellers:sellers,
        hasMore:this.hasMore,
        hasFirstShow:false,
      })
      this.sellers=sellers
      wx.hideLoading()
    })
  },
  gotoDetail(e){
    wx.navigateTo({
      url:'/pages/reserve/shopHome?id='+util.attr(e,'sid')
    })
  },
  onpos(e){
    console.log(e);
    this.location=e.detail
    this.GetCollectionSellerList();
  },
  sellerCardEvent(e){
    if(e.detail.type=='phone'){
      this.selectComponent('#as').show(e.detail.seller);
    }
  },
})