index.js 3.26 KB
// pack-activity/pages/index.js
import util from '../../utils/util';
import App from '../../utils/api';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    checkout:null,
    hasLoading:true,
  },

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

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

  },

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

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

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

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

  },

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

  },
  GetCheckout(){
    App.Customer.GetOrderInfo({rid:this.options.rid}).then(res=>{
      let checkout=res.data
      if(checkout){
        let remark=[]
        if(res.data.is_wine>0){
          remark.push('有剩余酒水要退')
        }
        if(res.data.is_pack>0){
          remark.push('菜品打包')
        }
        checkout.remark=remark.join('、')
        if(checkout.status>1){
          this.GetOrderInfoStop()
        }else{
          this.GetOrderInfoRun()
        }
      }
      this.setData({
        checkout:checkout,
        hasLoading:false,
      })
    })
  },
  is_wine:0,
  is_pack:0,
  checkoutData(e){
    this.is_wine=0
    this.is_pack=0
    if(e.detail.value.length>0){
      e.detail.value.forEach(k=>{
        this[k]=1
      })
    }
    console.log(e.detail.value,this.is_wine,this.is_pack)
  },
  checkoutInit(){
    let params={
      rid:this.options.rid,
      sid:this.options.sid,
      is_wine:this.is_wine,
      is_pack:this.is_pack
    }
    App.Customer.InitOrder(params).then(res=>{
      this.GetCheckout()
    })
  },
  setInvoice(e){
    let invoice={
      rid:this.options.rid,
    }
    Object.keys(e.detail).forEach(key=>{
      invoice[key.toLowerCase()]=e.detail[key]
    })
    App.Customer.SetInvoice(invoice).then(res=>{
      setTimeout(()=>{
        util.toast('发票已提交','none')
      },300)
    })
  },
  GetBalance(){//获得余额
    App.User.GetBalance().then(res=>{
      this.setData({
        user:res.data
      })
    })
  },
  GetOrderInfoInterval:null,
  GetOrderInfoRun(){
    if(!this.GetOrderInfoInterval){
      this.GetOrderInfoInterval=setInterval(()=>{
        this.GetCheckout()
      },8000)
    }
  },
  GetOrderInfoStop(){
    if(this.GetOrderInfoInterval){
      clearInterval(this.GetOrderInfoInterval)
      this.GetOrderInfoInterval=null
    }
  },
  txSeller(){
    this.selectComponent('#showModal').options({
      header: '温馨提示',
      body: '已为您催促商家,请耐心等待'
    }).show();
    App.PushMsg.SendUrgeMsg({rid:this.data.checkout.rid})
  },
  pay(){
    App.PayOnline.CombineTransactions({
      rid:this.data.checkout.rid,
    }).then(res=>{
      res.data.timeStamp+=""
      wx.requestPayment({
        ...res.data,
        success:res=>{
          this.GetCheckout()
        },
      })
    })
  },
})