index.js 6.37 KB
// cookbook/index.js
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
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getCookList();
    },

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

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
      this.setData({
        totalWidth:getApp().system.windowWidth,
        singleWidth:parseInt((getApp().system.windowWidth*0.92-16)/2),
        totalHeight:getApp().system.windowHeight,
        kindHeight:getApp().system.windowHeight-68
      })
    },

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

    },

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

    },

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

    },

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

    },

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

    },
  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: 80,
      //status:this.data.fromSoucre!=''?1:'',
      identity:1
    };
    if(orderid){
      param.orderid=orderid;
    }
    App.Cookbook.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,
        })
      },1000)

    }).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,
    });

  },
  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){
          book.num=opt.num;
        }
      })
    })
    this.setData({
      countTotal:this.data.countTotal+opt.type,
      cookbooklist:books
    });
    this.getOrderInfo();
  },
  //订单详情
  getOrderInfo:function(){
    App.Orders.GetOrderById({orderid:this.data.orderid}).then(json=>{
      let order=json.data[0].order[0];
      if(order.status<2){
        this.setData({aiOrderStatus:json.data[0].order[0].status});
      }else{
        var title=''
        if(json.data[0].order[0].status==2 || json.data[0].order[0].status==3){
          if(this.data.fromSoucre || util.cache('identity')==2){
            return;
          }
          title='手慢了,小伙伴已下单'
        }
        if(json.data[0].order[0].status==5){
          title='订单已结账'
        }
        if(json.data[0].order[0].status==7){
          title='订单已清台'
        }
        wx.showToast({
          icon:'none',
          title:title,
          duration:1500,
        });
        let that=this;
        setTimeout(function(){
          if(json.data[0].order[0].status>5){
            wx.reLaunch({
              url:'/pages/menu/s_nothing?sellerid='+that.data.sellerid+'&orderid='+that.data.orderid,
            })
          }else{
            that.goToOrder(that.data.orderid)
          }
        },1500)
      }

    })
  },
})