submit.js 4.06 KB
// pages/reserve/submit.js
import util from '../../utils/util';
import App from '../../utils/api';
const _u = {
  name: '',
  sex: 1,
  phone: '',
  remark: '',
}
Page({
  user: _u,
  /**
   * 页面的初始数据
   */
  data: {
    seller:null,
    detail:null,
    user:_u,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  options:null,
  onLoad: function (options) {
    this.options=options
    this.GetSellerDetail();
    App.Customer.GetCustomerInfo().then(res=>{
      if(res.data){
        this.user = res.data
        this.setData({
          user: res.data,
        })
      }
    })
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },
  GetSellerDetail() {
    let options = this.options
    App.Customer.GetSellerDetail({
      sid: options.sid,
    }).then(res => {
      if (res.ret == 200) {
        let seller = res.data
        this.setData({
          seller: seller,
          detail:{
            ddate:options.ddate,
            pnum:options.pnum,
          }
        })
      }
    })
  },
  changeInput(e) {
    let key = util.attr(e, 'key');
    let val = ''
    if (key == 'sex') {
      val = util.attr(e, 'val');
    } else {
      val = util.val(e);
    }
    this.user[key] = val;
    if (key == 'sex') {
      this.setData({
        user: this.user
      })
    }
  },
  saved() {
    if(!this.user.name){
      return util.toast('请填写姓名','none')
    }
    if(!this.user.phone){
      return util.toast('请填写手机','none')
    }
    App.Customer.SetReserveInfo({
      sid:this.options.sid,
      pnum:this.options.pnum,
      ddate:this.options.ddate,
      rdate:this.getRdate(),
      rname:this.user.name,
      sex:this.user.sex,
      phone:this.user.phone,
      cusdem:this.user.remark||'',
    }).then(res=>{
      if(res.ret!=200){
        return util.toast(res.msg,'none');
      }
      if(res.data.hasSetUserInfo){
        App.Customer.GetCustomerInfo({},'clear')
      }
      let firstShowMsg='预计10分钟左右餐厅将告知您结果(请留意短息或微信通知)'
      if(!this.hasInWorkTime()){//非营业时间预订
        firstShowMsg='商家未营业,营业后将第一时间告知您预订结果'
      }
      wx.redirectTo({
        url:util.http_build_query({
          id:res.data.rid,
          firstShowMsg:firstShowMsg,
        },'/pages/customer/detail_owner')
      })
    })
  },
  getRdate(){
    let daily=this.data.seller.daily;
    let ddate=util.unixtoTime(this.options.ddate,'YYYY-MM-DD,HH:II').split(',');
    let rd=['00:00','00:00'];
    Object.keys(daily).forEach((k)=>{
      let t=daily[k].split('-')
      if(t[0]<=ddate[1] && ddate[1]<=t[1]){
        rd=t;
      }
    })
    return util.timetouUnix(ddate[0]+' '+rd[0]+':00')+'-'+util.timetouUnix(ddate[0]+' '+rd[1]+':00')
  },
  editorReserve(){
    wx.navigateBack()
  },
  getphone(e){
    console.log('getphone',e);
    if(e.detail.ret!=200){
      return util.toast('获取失败,请重新获取','none');
    }
    this.user.phone=e.detail.phone
    this.setData({
      user: this.user
    })
  },
  hasInWorkTime(){
    let daily=this.data.seller.daily;
    let wt=['morning','noon','night'];
    let stime='',etime='',time=parseInt(new Date()/1000)
    let today=util.unixtoTime(time,"YYYY-MM-DD")
    wt.forEach(k=>{
      if(!daily[k])return;
      let xt=daily[k].split('-');
      if(!stime){
        stime=util.timetouUnix(today+' '+xt[0]+':00')
      }
      etime=util.timetouUnix(today+' '+xt[1]+':00')
    })
    if(time>=stime && time<=etime){
      return true;
    }
    return false;
  },
  cardEvent(e){
    this.editorReserve()
  },
})