index.js 8.78 KB
// cookbook/index.js
var mta=require('mta-wechat-analysis')
import util from '../../utils/util';
import App from '../../utils/api';
const {regeneratorRuntime} = global
Page({

  /**
   * 页面的初始数据
   */
  data: {
    scrollTop: 0,
    isloading: true,
    toView: "",
    hasEnter: false,

    totalWidth: getApp().system.windowWidth,
    singleWidth: parseInt((getApp().system.windowWidth * 0.92 - 16) / 2),
    totalHeight: getApp().system.windowHeight,
    kindHeight: getApp().system.windowHeight - 68,
    hasListModel: true,

    cookbooklist: [{}, {}, {}, {}, {}, {}, {},],
    showSkeleton: true,
    order: {},
    orderItemCount:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      options: options,
    })
    this.getCookList();
    this.initOrder();
    mta.Page.init({
      "appID":"500667280",
      "eventID":"500667287",
      "statPullDownFresh":true,
      "statShareApp":true,
      "statReachBottom":true,
      "lauchOpts":options, //渠道分析
    });
  },
  initOrder(){
    App.Advance.CreateOrder({
      rid: this.data.options.rid,
      identity:this.data.options.identity,
      status:'0,1',
      openid:getApp().openid,
    }).then(res => {
      let orderItemCount=0;
      res.data.cookbooklist.forEach(cb=>{
        if(this.data.options.identity==1 && cb.status>0){
          wx.redirectTo({
            url:util.http_build_query({
              rid:this.data.options.rid,
              oid:res.data.id,
              identity:1
            },'/pack-cookbook/pages/success')
          });
          return false;
        }
        orderItemCount+=parseInt(cb.num)
      })
      this.setData({
        [`options.oid`]:res.data.id,
        order: res.data,
        orderItemCount:orderItemCount,
      })
    })
  },
  dispalyCart(){
    this.selectComponent("#cart").auto();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let systemInfo = wx.getSystemInfoSync();
    let reg = /ios/i;
    let pt = 20;//导航状态栏上内边距
    let h = 44;//导航状态栏高度
    if(reg.test(systemInfo.system)){
      pt = systemInfo.statusBarHeight;
      h = 44;
    }else{
      pt = systemInfo.statusBarHeight;
      h = 48;
    }
    this.setData({
      totalWidth: getApp().system.windowWidth,
      singleWidth: parseInt((getApp().system.windowWidth * 0.92 - 16) / 2),
      totalHeight: getApp().system.windowHeight,
      kindHeight: getApp().system.windowHeight - pt-h
    })
    setTimeout(()=>{
      this.initScrollHeightList();
    },1000)

  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title:'一起来点菜啦!',
      imageUrl:'http://qn.colorcun.com/reserve/images/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20191115114857.jpg',
    }
  },
  formatCookList(list){
    //原栏目下显示
    let hasShowClass = false;

    //菜单中展示沽清的菜品
    let hasClearCookbook = false;

    //十道必选菜
    let has10CaiOpen = false;
    let flist = [];
    if (list.length > 0) {
      has10CaiOpen = list[0].status == 1;
      list.forEach(function (cate, i) {
        if (cate.status == 1 && cate.cookbooklist[0].length > 0) {
          let cooks = [];
          //let cooks_order=[];
          cate.cookbooklist[0].forEach(function (cook, n) {
            if (cook.status == 1) {
              let flag = true;
              if (has10CaiOpen) {
                if (cate.class_type == 2 && !hasShowClass && flist.length > 0 && flist[0].class_type == 1) {
                  for (let fi = 0; fi < flist[0].cookbooklist[0].length; fi++) {
                    if (flist[0].cookbooklist[0][fi].id == cook.id) {
                      flag = false;
                      break;
                    }
                  }
                }
              }
              cook.item_clear_num = cook.clear_num;
              if (!hasClearCookbook) {//不显示卖光了
                let item_clear_num = cook.clear_num;
                if (cook.spec) {
                  let clear_num = null;
                  cook.spec.forEach(spec => {
                    if (spec.clear_num == 0) {
                      clear_num = 0;
                    } else {
                      clear_num = null;
                    }
                  })
                  if (clear_num == 0) {
                    item_clear_num = 0;
                  }
                }
                cook.item_clear_num = item_clear_num;
                if (item_clear_num == 0) {
                  flag = false;
                }
              }

              if (flag) {
                cooks.push(cook);
                //cooks_order.push(cook.order_o);
              }

            }
          })
          if (cooks.length > 0) {
            //cooks 排序...

            cate.cookbooklist[0] = cooks;
            flist.push(cate)
          }
        }
      })
    }
    return flist;
  },
  getCookList: function (orderid) {
    let param = {
      sellerid: this.data.options.sid,
      identity: this.data.options.identity,
      orderid : this.data.options.oid,
      status:'0,1',
    };
    App.Advance.GetCookbookByClass(param).then(json => {
      //菜单分类添加修饰
      let cookbooklist = this.formatCookList(json.data[0].list);
      setTimeout(() => {
        this.setData({
          cookbooklist: cookbooklist,
          countPirce: json.data[0].countPirce ? json.data[0].countPirce : 0,
          countTotal: json.data[0].countTotal ? json.data[0].countTotal : 0,
          isloading: false,
          showSkeleton: false,
        })
      }, 0)

    }).catch(res => {
      console.log('getCookList-catch', res);
      this.selectComponent("#network").show('网络不给力,请检查网络后重试');
    });
  },
  //菜品分类操作
  changeList: function (e) {
    var _id = e.detail.id;
    console.log('changeList', 'inToView' + _id)
    this.setData({
      toView: 'inToView' + _id,
    });
    this.getClassNameFlag=false;
    setTimeout(res=>{
      this.getClassNameFlag=true;
    },800)
  },
  foodCardCallback(e){
    let opt = e.detail;
    console.log('foodCardCallback', opt);
    if (opt.type == 'specWin') {
      if (opt.value == 'show') {
        this.setData({scrollZindex: 9999});
      } else {
        this.setData({scrollZindex: 'auto'});
      }
      return;
    }

    let books = this.data.cookbooklist;
    books.forEach(menu => {
      menu.cookbooklist[0].forEach(book => {
        if (book.id == opt.cookbookid || 'flushOrder'==opt.type) {
          book.num = opt.num;
        }
      })
    })
    if('flushOrder'==opt.type){
      this.setData({
        countTotal: 0,
        cookbooklist: books
      });
    }else{
      this.setData({
        countTotal: this.data.countTotal + opt.type,
        cookbooklist: books
      });
    }

    setTimeout(()=>{
      this.initOrder();
    },100)
  },
  gotoCart(){
    if(this.data.orderItemCount<1){
      return util.toast('请添加菜品','none');
    }
    //购物车
    wx.navigateTo({
      url: util.http_build_query(this.data.options,'./cart')
    })
  },
  getClassNameFlag:true,
  bindscroll(e){
    if(!this.getClassNameFlag) return;
    this.getClassNameFlag=false;
    //console.log('this.scrollHeightList',this.scrollHeightList)
    let maxMX=9999999;
    let activeId='';
    Object.keys(this.scrollHeightList).forEach(tid=>{
      let mx=parseInt(e.detail.scrollTop)-parseInt(this.scrollHeightList[tid].top)
      if(mx>-100){
        if(mx<maxMX) {
          maxMX=mx;
          activeId=tid;
        }
      }
    })
    if(activeId && this.data.activeId!=activeId){
      console.log(activeId, this.scrollHeightList[activeId].dataset.name)
      this.setData({
        activeId:activeId,
      })
      this.selectComponent("#menu").activeMenuID(activeId);
    }
    this.getClassNameFlag=true;
  },
  scrollHeightList:{},
  initScrollHeightList(scroll){
    //if(Object.keys(this.scrollHeightList).length>0) return;
    let that=this;
    return new Promise(function(success,fail){
      var query = wx.createSelectorQuery();
      query.selectAll('.list-model-masonry').boundingClientRect()
      query.exec(function (res) {
        for(let i=0;i<res[0].length;i++){
          let x=res[0][i];
          //x.top=parseInt(scroll.detail.scrollTop)+parseInt(x.top);
          that.scrollHeightList[x.id]=x;
        }
        success()
      })
    })
  },
})