detail_owner.js 8.74 KB
// pages/customer/detail_owner.js
import util from '../../utils/util';
import App from '../../utils/api';
import RS from '../../utils/reserveStatus';
import PubSub from '../../utils/PubSub'
const {regeneratorRuntime} = global
import TP from '../../utils/tpls';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    detail: null,
    hasOnlineReserve: false,
    firstShowMsg:'',
    rsl:[],//推荐商家
    hasCancel:false, //取消预定效果
    hasLoadedCheckout:false,
    subscribeHide:false,//顶部提示
    subscribeYDHide:true,//顶部订阅引导
  },
  id: null,
  tpls: TP.tpls(),
  hasGetSellerDetail:false,
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.id = options.id;
    if(!options.firstShowMsg){
      this.hasGetSellerDetail=true
    }
    this.setData({
      firstShowMsg:options.firstShowMsg,
      hasSubscribeFail:options.hasSubscribeFail,
    })
    PubSub.on('detailPub',(res)=>{
      console.log(res)
      if(res.type=='cancel'){
        this.setData({
          hasCancel:true,
        })
        setTimeout(()=>{
          wx.pageScrollTo({
            scrollTop:0,
            success:()=>{
              setTimeout(()=>{
                this.setData({
                  hasCancel:false,
                })
              },3000)
            },
          })
        }, 800)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.selectComponent('#getpos').getPos();
  },

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

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

  },

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
    console.log('onShareAppMessage', arguments)
    return this.shareSetting(e);
  },
  getDetail(){
    App.Reserve.Detail({
      openid: getApp().openid,
      rid: this.id
    }).then(async res => {
      this.detail=this.formatDatail(res.data)
      this.detail.ddate_solt=Number(this.detail.ddate_solt);
      this.setData({
        detail: this.detail,
        hasOnlineReserve:res.data.type==1,
      })
      if(this.hasGetSellerDetail){
        let sellerRes=await App.Customer.GetSellerDetail({sid:this.detail.sid})
        let seller=sellerRes.data
        let flag=false,stime='',etime='',time=parseInt(new Date()/1000)
        let today=util.unixtoTime(time,"YYYY-MM-DD")
        stime=util.timetouUnix(today+' '+seller.rec_stime+':00')
        etime=util.timetouUnix(today+' '+seller.rec_etime+':00')
        if(etime<=stime){
          if(time<=etime){
            flag=true
          }
          etime+=86400
        }
        if(!flag){
          if(time>=stime && time<=etime){
            flag=true;
          }
        }
        let firstShowMsg='预计10分钟左右餐厅将告知您结果(请留意短息或微信通知)'
        if(!flag){
          firstShowMsg=seller.rec_stime
        }
        this.setData({
          firstShowMsg:firstShowMsg,
        })
      }
      //this.GetBalance()//余额
      this.GetCheckout()//账单
    })
  },
  formatDatail(detail) {
    detail.rdateStr = util.unixtoTime(detail.ddate, 'MM月DD日 WW HH:II');
    detail=RS.reserve(detail);
    detail.seller.seller_tel=detail.seller.seller_tel.split(',');
    return detail;
  },
  showManager() {
    this.selectComponent('#manager').show();
  },
  lookLocation: function () {
    wx.openLocation({
      latitude: Number(this.data.detail.seller.lat),
      longitude: Number(this.data.detail.seller.long),
      scale: 17,
      address: this.data.detail.seller.map_address,
      success: function (res) {
        console.log(res)
      }
    })
  },
  userinfo(e) {
    console.log('userinfo', e)
    util.toast('授权成功,可以继续转发', 'none');
    // wx.navigateTo({
    //     url:'invite?id='+this.id,
    // });
  },
  shareSetting(e) {
    console.log('shareSetting', e.target.dataset);
    let hasInvitation = e.target.dataset.act == 'invitation';
    if (this.topicVal && this.data.detail.openid == getApp().openid) {
      App.Reserve.SetTopicFromShare({
        rid: this.id,
        topic: this.topicVal
      })
    }
    let nickname = "";
    try {
      nickname = util.cache('userInfo').nickName;
    } catch (e) {
    }
    let obj = {
      //title: (nickname || '') + '给你发了一个邀请,点击查看',
      title: this.tpls[0].shareTitle,
      path: '/pages/customer/index?id=' + this.id + '&fromid=' + getApp().openid + '&invite_name=' + nickname + '&hasInvitation=1&tplid=1',
      imageUrl: this.tpls[0].shareImage,
    }
    if (hasInvitation) {
      //obj.title=(nickname || '') + '给你发了一个邀请函,点击查看';
      obj.path = obj.path + '&hasInvitation=1';
    }
    return obj;
  },
  cookbook() {
    if (this.data.detail.order && this.data.detail.order.cookbooklist.length > 0) {
      wx.navigateTo({
        url: util.http_build_query({
          rid: this.id,
          oid: this.data.detail.order.id,
          identity: 1
        }, '/pack-cookbook/pages/success')
      });
    } else {
      App.Advance.CreateOrder({
        rid: this.id,
        openid: getApp().openid,
      }).then(res => {
        if (res.ret != 200) {
          return util.toast(res.msg, 'none')
        }
        wx.navigateTo({
          url: util.http_build_query({
            rid: this.id,
            oid: res.data.id,
            identity: 1,
            sid: res.data.sellerid
          }, '/pack-cookbook/pages/index')
        });
      })
    }

  },
  wineGo() {
    util.toast('本区域暂无配送人员', 'none');
  },
  callSeller(){
    if(this.data.detail.seller.is_online_reserve==1 && this.data.detail.seller.is_tele==0){
      //在线预定没有话机
      return wx.makePhoneCall({
        phoneNumber:'400-000-9690'
      })
    }
    let seller_tel=this.data.detail.seller.seller_tel
    if(seller_tel.length<1) return util.toast('商家电话错误','none')
    if(seller_tel.length==1){//只有一个电话
      let phone=seller_tel[0].replace(/[^\d]/,'')
      wx.makePhoneCall({
        phoneNumber:phone
      })
    }else{
      this.selectComponent('#as').show();
    }
  },
  callSellerPhone(e){
    if(e.detail.value){
      let phone=e.detail.value.replace(/[^\d]/,'')
      wx.makePhoneCall({
        phoneNumber:phone
      })
    }
  },
  cardEvent(e){
    console.log(e)
  },
  GetRecommendSellerList(sid){
    let param={
      sid:sid,
    }
    if(this.location){
      param.lat=this.location.latitude
      param.lng=this.location.longitude
    }
    App.Customer.GetRecommendSellerList(param).then(res=>{
        this.setData({
          rsl:res.data
        })
    })
  },
  hasGoto:false,
  bindtransition(e){
    if('change'==e.type){
      this.hasGoto=false
    }
    if('animationfinish'==e.type){
      if(this.hasGoto){
        wx.switchTab({
          url: '/pages/reserve/index'
        })
        return;
      }else{
        this.hasGoto=true
      }
    }
  },
  location:null,
  onpos(e){
    console.log('onpos',e);
    this.location=e.detail
    if(!this.detail){
      return setTimeout(()=>{
        this.onpos(e)
      },500)
    }
    if(this.detail.type==1 && this.detail.status==6){
      this.GetRecommendSellerList(this.detail.seller.id)
    }
  },
  gotoSellerDetail(e){
    console.log(e)
    let url='/pages/reserve/detail?id='+e.currentTarget.id
    wx.navigateTo({
      url:url,
    })
  },
	onUnload(){
		let pages = getCurrentPages();
		if(pages.length>2 && pages[pages.length-2].route=='pages/reserve/detail'){
			wx.setStorageSync('order', 'complete')
		}
		console.log(pages)
	},
  GetBalance(){//获得余额
    App.User.GetBalance().then(res=>{
      this.setData({
        user:res.data
      })
    })
  },
  GetCheckout(){
    App.Customer.GetOrderInfo({rid:this.options.id}).then(res=>{
      this.setData({
        checkout:res.data,
        hasLoadedCheckout:true,
      })
    })
  },
  checkout(){
	  if(this.data.detail.status==0){
      this.selectComponent('#showModal').options({
        header: '商家未确认',
        body: '确认后,到店消费可在线买单'
      }).show();
    }else{
      wx.navigateTo({
        url:util.http_build_query({
          rid:this.data.detail.id,
          sid:this.data.detail.sid
        },'/pack-checkout/pages/index')
      })
    }
  },
  subscribeHide(){
    this.setData({
      subscribeHide:true,
    })
  },
  subscribeYDHide(){
    this.setData({
      subscribeYDHide:true,
    })
  },
  subscribeFailHide(){
    this.setData({
      hasSubscribeFail:false,
    })
  },
  subscribeEvent(e){
    if(e.detail.type!='success'){
      this.setData({
        subscribeYDHide:false,
      })
    }
  },
})