indexList.js 8.28 KB
// pages/reserve/indexList.js
import util from '../../utils/util';
import App from '../../utils/api';
import DA from './data';
const {regeneratorRuntime} = global
import PubSub from '../../utils/PubSub'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    multiArray: ['全部菜系'],
    multiIndex: [0],
    tags:[
      {id:1,name:'环境好'},
      {id:2,name:'菜品有特色'},
      {id:3,name:'超大包间'},
    ],
    markers:[],
    hasMore:true,
    hasDisabled:true,
    hasSuggestion:false,
    placeholder:'您好,几位,什么时候',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  eventChannel:null,
  onLoad: async function (options) {
    //lat=34.21719&lng=108.89155&map={"errMsg":"getMapRegion:ok","southwest":{"longitude":108.87545674591064,"latitude":34.204520114013846},"northeast":{"longitude":108.90764325408935,"latitude":34.229857981005225},"scale":15}&multiIndex=1&tags=1,2
    this.options=options;
    if(options.hasSuggestion){
      this.setData({
        hasSuggestion:true
      })
    }
    let tags=[
      {id:1,name:'环境好'},
      {id:2,name:'菜品有特色'},
      {id:3,name:'超大包间'},
    ];
    if(options.tags){
      let ts=options.tags.split(',');
      tags.forEach(t=>{
        if(ts._indexOf(t.id)>-1){
          t.active=true;
        }
      })
    }
    this.setData({
      multiArray:[['全部菜系'].concat(await DA.getCates())],
      multiIndex:[options.multiIndex],
      tags:tags,
    })
    try{
      this.eventChannel = this.getOpenerEventChannel()
      // 监听acceptDataFromOpenerPage事件,获取上一页面通过eventChannel传送到当前页面的数据
      this.eventChannel.on('acceptDataFromMapPage', findSellerData=> {
        if(findSellerData){
          this.findSellerData=findSellerData
          console.log('findSellerData',findSellerData)
          this.setData({
            findSellerData:findSellerData,
          })
        }
      })
    }catch(e){}
    setTimeout(()=>{this.getList()},300)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.selectComponent('#getpos').getPos();
  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getListMore()
  },
  hasGetSellerEnd:false,
  async getList(){
    let mapParam=this.options
    this.offset=1 //page
    this.size=20 //page size
    if(mapParam.keyword){
      this.param={
        lat_extend:mapParam.lat,
        lng_extend:mapParam.lng,
        keyword:mapParam.keyword.trim(),
        offset:this.offset,
        size:this.size,
        distance:500000,
      };
      if(mapParam.lat){
        this.param.centre_pos=`${mapParam.lat},${mapParam.lng}`
      }
      if(mapParam.isopen_md){
        this.param.isopen_md=1
      }
    }else{
      //计算视窗中心
      let map=JSON.parse(mapParam.map);
      let lat=map.northeast.latitude-(map.northeast.latitude-map.southwest.latitude)/2;
      let lng=map.northeast.longitude-(map.northeast.longitude-map.southwest.longitude)/2;
      this.param={
        lat:lat,
        lng:lng,
        lat_extend:mapParam.lat,
        lng_extend:mapParam.lng,
        offset:this.offset,
        size:this.size,
        distance:50,
      };
      if(mapParam.centre_pos){
        this.param.centre_pos=mapParam.centre_pos
      }
    }

    if(this.data.multiIndex[0]>0){
      this.param.cuisine=await DA.getCatesID(this.data.multiIndex[0]-1);
    }
    let tags=[];
    this.data.tags.forEach(t=>{
      if(t.active){
        tags.push(t.id)
      }
    })
    if(tags.length>0){
      this.param.tags=tags;
    }
    let placeholder='您好,几位,什么时候';
    if(this.findSellerData){
      console.log(this.findSellerData)
      this.param=Object.assign(this.param,this.findSellerData.paramObj)
      //4位  今天  午餐
      let sp=this.findSellerData.params
      if(sp.pnum.val>30){
        placeholder = '30 位以上 '
      }else{
        placeholder = sp.pnum.val+' 位 '
      }
      placeholder += util.unixtoTime(sp.date.val,' `DAY/MM-DD` ')
      if(sp.dur){
        placeholder+=' '+['','早餐','午餐','晚餐','夜宵'][sp.dur.val]
      }
    }
    this.setData({
      placeholder:placeholder,
    })
    App.Customer.SellerLists(this.param).then(res=>{
      this.setData({
        rsl:[],
        hasMore:res.data.length==this.size,
        markers:this.sellerFormat(res.data),
        hasDisabled:this.getHasDisabled(res.data.length>0,this.data.multiIndex[0]>0 || tags.length>0 || this.findSellerData)
      })
      this.triggerEvent('event',{type:'getListEnd',total:res.data.length})
      this.hasGetSellerEnd=true
      if(res.data.length==0 && this.offset==1 && this.location){
        this.onpos({detail:this.location})
      }
    })
  },
  /**
   * @param hasLimit 是否有搜索条件
   * @returns {boolean}
   */
  getHasDisabled(hasData,hasLimit){
    if(this.offset==1 && !hasLimit && !hasData) {
      return true
    }
    return false
  },
  getListMore(){
    this.param.offset+=1;
    let markers=this.data.markers;
    App.Customer.SellerLists(this.param).then(res=>{
      this.setData({
        hasMore:res.data.length==this.size,
        markers:markers.concat(this.sellerFormat(res.data)),
      })
    })
  },
  caixiShow(){
    if(this.data.hasDisabled) return
    this.selectComponent('#caixi').show();
  },
  caixiActive(e){
    this.bindMultiPickerChange({
      detail:{
        value:[e.detail]
      }
    })
    PubSub.emit(
      'searchParam',
      {
        fun:'caixiActive',
        e:e,
      }
    )
  },
  bindMultiPickerChange(e){
    console.log(e);
    let val=util.val(e);
    this.setData({
      multiIndex:val
    })
    setTimeout(()=>{this.getList();},0)
  },
  tagsChange(e){
    if(this.data.hasDisabled) return
    let id=util.attr(e,'id');
    let tags=this.data.tags;
    let activeNum=0;
    tags.forEach(t=>{
      if(t.id==id){
        t.active=!t.active
      }
      if(t.active){
        activeNum++
      }
    })
    this.setData({
      tags:tags,
    })
    PubSub.emit(
      'searchParam',
      {
        fun:'tagsChange',
        e:e,
      }
    )
    setTimeout(()=>{this.getList();},0)
  },
  gotoDetail(e){
    wx.navigateTo({
      url:'/pages/reserve/detail?id='+util.attr(e,'sid')
    })
  },
  sellerFormat(sellers){
    // sellers.forEach((seller) => {
    //   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';
    //     }
    //   }
    // })
    sellers.forEach(seller=>{
      if(this.findSellerData){
        seller.findSellerParamObj=this.findSellerData.paramObj
      }
    })
    return sellers;
  },
  dispalySearchHow(){
    this.selectComponent('#search-how').show();
  },
  howEvent(e){
    this.eventChannel.emit('findSellerData',e.detail)
    this.findSellerData=e.detail
    this.setData({
      findSellerData:e.detail
    })
    setTimeout(()=>{this.getList();},0)
    this.selectComponent('#search-how').hide();
  },
  location:null,
  onpos(e){
    console.log('onpos',this.hasGetSellerEnd,e);
    this.location=e.detail
    if(!this.hasGetSellerEnd){
      return setTimeout(()=>{
        this.onpos(e)
      },500)
    }
    if(this.data.markers.length<1){
      this.GetRecommendSellerList()
    }
  },
  GetRecommendSellerList(){
    let param={sid:'sid'}
    if(this.location){
      param.lat=this.location.latitude
      param.lng=this.location.longitude
    }
    App.Customer.GetRecommendSellerList(param).then(res=>{
      this.setData({
        rsl:res.data
      })
    })
  },
  hasGoto:false,
  bindtransition(e){
    if('change'==e.type){
      this.hasGoto=false
    }
    if('animationfinish'==e.type){
      if(this.hasGoto){
        wx.switchTab({
          url: '/pages/reserve/index'
        })
        return;
      }else{
        this.hasGoto=true
      }
    }
  },
})