index.js 5.2 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) {
	  console.log(options,'uuuuu');
    this.options=options
    if(options.sid){
      this.setData({
        seller:{
          id:options.sid
        }
      })
    }
    //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(this.data.checkout && this.data.checkout.status>checkout.status) return '状态已变更';
      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()
        }
        if(checkout.discount && checkout.discount.length>0){
          let subsidy_price=0,subsidy_price_pt=0,subsidy_price_sj=0
          checkout.discount.forEach(q=>{
            if(q.checked){
              let price=q.price/1
              subsidy_price+=price
              if(q.owner==1) {//平台红包
                subsidy_price_pt += price
              }else{
                subsidy_price_sj+= price
              }
            }
          })
          checkout.subsidy_price_pt=subsidy_price_pt.toFixed(2)
          checkout.subsidy_price_sj=subsidy_price_sj.toFixed(2)
          checkout.subsidy_price=subsidy_price.toFixed(2)
        }
      }
      let obj={
        checkout:checkout,
        hasLoading:false,
      }
      if(!this.data.seller){
        obj.seller={
          id:checkout.sid
        }
      }
      this.setData(obj)
    })
  },
  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]
    })
    setTimeout(()=>{
      util.toast('发票已提交','none')
      setTimeout(()=>{
        App.Customer.SetInvoice(invoice).then(res=>{
          this.GetCheckout()
        })
      },300)
    },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(){
    //return this.selectComponent('#xfCard').show();
    App.PayOnline.CombineTransactions({
      rid:this.data.checkout.rid,
    }).then(res=>{
      if(res.ret==200){
        res.data.timeStamp+=""
        wx.requestPayment({
          ...res.data,
          success:res=>{
            this.selectComponent('#xfCard').show();
            //this.GetCheckout()
            this.setData({
              [`checkout.status`]:3,
              [`checkout.pay_time`]:parseInt(new Date()/1000),
            })
          },
        })
      }else{
        util.toast(res.msg,'none')
        this.GetCheckout()
      }
    }).catch(()=>{
      util.toast('出错了,请重新尝试','none')
      this.GetCheckout()
    })
  },
  displayOrderImages(){
    if(this.data.checkout.bill_list.length>0){
      wx.previewImage({
        urls:this.data.checkout.bill_list,
      })
    }
  },
  discountShow(){
    if(this.data.checkout.discount.length>0){
      this.selectComponent('#discount').show();
    }
  },
  discountEvent(e){
    console.log('discountEvent',e)
    this.GetCheckout()
  },
  xfCardEvent(e){
    this.setData({
      [`checkout.status`]:3,
    })
  },
})