index_new.js 10.5 KB
// pack-cookbook/pages/index_new.js
var mta = require('mta-wechat-analysis')
import util from '../../utils/util';
import App from '../../utils/api';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: 'A', //样式A,B,C
    status: 'inToView01',
    scrollTop: 0,
    options: {},
    check1: '', //做法
    check2: '', //规格
    isloading: true,
    toView: "inToView27",
    countPirce: '',
    countTotal: '',
    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: [],
    checkItem: {},
    showSkeleton: true,
    order: {},
    orderItemCount: 0,
    checkObj: '',
    quantum: '',
    normsList: [{
        text: '做饭',
      },
      {
        text: '规格',
      }
    ],
    assortArr: [],
  },
  checkModal: '',
  classifyModal: '',
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('options', options);
    this.setData({
      options: options,
    })
    this.getCookList();
    this.initOrder();
    mta.Page.init({
      "appID": "500667280",
      "eventID": "500667287",
      "statPullDownFresh": true,
      "statShareApp": true,
      "statReachBottom": true,
      "lauchOpts": options, //渠道分析
    });
    this.checkModal = this.selectComponent("#checkModal");
    this.classifyModal = this.selectComponent("#classifyModal");
  },

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

  },
  // 选择做法
  checkSingle1(e) {
    const check1 = e.currentTarget.dataset['item'];
    this.setData({
      check1
    })
  },
  // 已选菜单页面跳转
  gotoShoppingCart() {
    // let arr = [];
    // this.data.cookbooklist.forEach(item => {
    //   let obj = {
    //     class_name: item.class_name,
    //     id: item.id,
    //     status: item.status,
    //     class_type: item.class_type,
    //     arr: [],
    //   }
    //   item.cookbooklist[0].forEach(i => {
    //     if (i.quantum != '0') {
    //       obj.arr.push(i)
    //     }
    //   })
    //   arr.push(obj)
    // })
    // console.log(arr);
    let cookbooklist = this.data.cookbooklist;
    let obj = {
      sellerName: this.data.options.sellerName,
      sellerid: this.data.options.sid,
    }
    wx.navigateTo({
      url: `/pack-cookbook/pages/cart_new?cookbooklist=${JSON.stringify(cookbooklist)}&obj=${JSON.stringify(obj)}`,
    })
  },
  // 选择规格
  checkSingle2(e) {
    const check2 = e.currentTarget.dataset['item'];
    this.setData({
      check2
    })
  },
  // 展示做法规格弹窗
  showModal() {
    console.log(this.data.checkItem);
    this.checkModal.showModal()
  },
  // 关闭做法规格弹窗
  closeModal() {
    this.checkModal.closeModal()
  },
  // 展示菜品分类弹框
  showClassifyModal() {
    this.classifyModal.showModal()
  },
  // 关闭菜品分类弹框
  closeClassifyModal() {
    this.classifyModal.closeModal()
  },
  // 选择菜品分类
  checkItem(e) {
    const item = e.currentTarget.dataset['item'];
    var _id = item.id;
    this.closeClassifyModal()
    this.setData({
      toView: 'inToView' + _id
    })
    console.log(_id);
  },
  // 选择做法和规格后点击确定
  determineFun() {
    let index = this.data.checkObj.index;
    let indexs = this.data.checkObj.indexs;
    let quantum = this.data.quantum;
    let check1 = this.data.check1;
    let check2 = this.data.check2;
    let checkObj = `cookbooklist[${indexs}].cookbooklist[0][${index}].quantum`;
    let practice = `cookbooklist[${indexs}].cookbooklist[0][${index}].practice`;
    let unit = `cookbooklist[${indexs}].cookbooklist[0][${index}].unit`;
    this.setData({
      [checkObj]: quantum,
      [practice]: check1,
      [unit]: check2,
    })
    console.log('cookbooklist', this.data.cookbooklist);
    this.closeModal()
  },
  // 点击加号
  addition(e) {
    const item = e.currentTarget.dataset['item'];
    const index = e.currentTarget.dataset['key']; //菜下标
    const indexs = e.currentTarget.dataset['indexs']; //分类下标
    let quantum = item.quantum * 1 + 1;
    this.setData({
      checkObj: {
        indexs,
        index,
      },
      quantum,
      checkItem: item
    })
    console.log('index', index);
    console.log('indexs', indexs);
    console.log('checkItem', this.data.checkItem);
    this.showModal()
  },
  // 点击减号
  subtraction(e) {
    const item = e.currentTarget.dataset['item'];
    const index = e.currentTarget.dataset['key']; //菜下标
    const indexs = e.currentTarget.dataset['indexs']; //分类下标
    let quantum = item.quantum * 1 - 1;
    console.log('index', index);
    console.log('indexs', indexs);
    let quantumObj = `cookbooklist[${indexs}].cookbooklist[0][${index}].quantum`;
    let practice = `cookbooklist[${indexs}].cookbooklist[0][${index}].practice`;
    let unit = `cookbooklist[${indexs}].cookbooklist[0][${index}].unit`;
    this.setData({
      checkObj: {
        indexs,
        index
      },
      quantum,
      [quantumObj]: quantum,
      [practice]: this.data.check1,
      [unit]: this.data.check2,
    })
  },
  // 扫桌码点餐
  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,
              sellerid: this.data.options.sid,
            }, '/pack-cookbook/pages/success')
          });
          return false;
        }
        orderItemCount += parseInt(cb.num)
      })
      this.setData({
        [`options.oid`]: res.data.id,
        order: res.data,
        orderItemCount: orderItemCount,
      })
    })
  },
  // 过滤数据
  formatCookList(list) {
    //原栏目下显示
    let hasShowClass = false;
    //菜单中展示沽清的菜品
    let hasClearCookbook = false;
    //十道必选菜
    let has10CaiOpen = false;
    let flist = [];
    let assortArr = [];
    let check1 = '';
    let check2 = '';
    if (list.length > 0) {
      has10CaiOpen = list[0].status == 1;
      list.forEach(function (cate, i) {
        let obj = {
          class_name: cate.class_name,
          class_type: cate.class_type,
          id: cate.id,
          order: cate.order,
          status: cate.status,
        }
        if (cate.status == 1 && cate.cookbooklist[0].length > 0) {
          assortArr.push(obj)
          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;
                cook.quantum = 0; //选择的数量
                cook.index = n; //分类下菜品的下标
                cook.practice = ''; //做法
                cook.unit = ''; //规格
                // let type;//模拟种类  1为一行显示1个  2为一行显示2个
                // if(n=='0' || n=='3'){
                //   type='1'
                // }else{
                //   type='2'
                // }
                cook.type = '1';
                cook.arr1 = ['香辣', '清蒸', '爆炒', '清汤'];
                cook.arr2 = ['小份', '中份', '份', '例', '两只'];
                check1 = cook.arr1[0];
                check2 = cook.arr2[0];
                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)
          }
        }
      })
    }
    setTimeout(() => {
      this.setData({
        check1,
        check2,
        assortArr
      })
    }, 1);
    console.log('flist', flist);
    return flist;
  },
  // 获取所有分类下所有菜品列表
  getCookList(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 => {
      this.selectComponent("#network").show('网络不给力,请检查网络后重试');
    });
  },
  getStatus(e) {
    this.setData({
      status: e.currentTarget.dataset.index
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

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

  },

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

  },

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

  },

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

  }
})