submit.js 6.6 KB
// pages/reserve/submit.js
import util from '../../utils/util';
import App from '../../utils/api';
const _u = {
  name: '',
  sex: 1,
  phone: '',
  remark: '',
}
let dur=['morning','noon','night','midnight']
Page({
  paddingBottom:0,
  user: _u,
  /**
   * 页面的初始数据
   */
  data: {
    seller:null,
    detail:null,
    user:_u,
    loading:0,
  },
  hasSubmit:true,
  /**
   * 生命周期函数--监听页面加载
   */
  options:null,
  onLoad: function (options) {
    //section=noon&time=11:00&date=2020-12-27&sid=1&ddate=1609038000&room_demand=[{"id":"30","name":"6人间"},{"id":"31","name":"10人间"}]
	  console.log('options',options)
    options.room_demand=JSON.parse(decodeURIComponent(options.room_demand))
    this.options=options
    this.GetSellerDetail();
    App.Customer.GetCustomerInfo().then(res=>{
      if(res.data.length>0){
			res.data.forEach((v,i)=>{
				if(v.is_default==1){
					this.user = v
					v.remark=wx.getStorageSync('remark') ? wx.getStorageSync('remark') : '';
					this.setData({
						user: v,
					})
					return;						
				}
			})
      }
    })
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
	this.setData({
		'user.remark':wx.getStorageSync('remark') ? wx.getStorageSync('remark') : ''
	})
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  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:options,
          loading:1,
        })
      }
    })
  },
	selectconcat(){
		wx.navigateTo({
			url:'/pages/me/concat?action=select'
		})
	},
  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(e) {
    if(!this.hasSubmit) return;
    let hasSubscribeFail=e.detail.type!='success'
    if(!this.user.name){
      return util.toast('请填写姓名','none')
    }
    if(!this.user.phone){
      return util.toast('请填写手机号','none')
    }
    if(!/^[1][3-9][0-9]{9}$/.test(this.user.phone)){
      return util.toast('请填写正确的手机号','none')
    }
    this.hasSubmit=false;
		let roomids=[];
		this.options.room_demand.forEach((v,i)=>{
			roomids.push(v.id)
		})
        console.log('ceshi',roomids);	
    App.Customer.SetReserveInfo({
      sid:this.options.sid,
      ddate:this.options.ddate,
      rdate:this.getRdate(),
      rname:this.user.name,
      sex:this.user.sex,
      phone:this.user.phone,
      cusdem:this.user.remark||'',
      ddate_solt:this.options.ddate_solt,
        roomids:roomids.toString(),
      pnum: this.options.pnum ? this.options.pnum : 4,
    }).then(res=>{
			console.log('ceshi',this.hasSubmit,res);
      if(res.ret!=200){
        this.hasSubmit=true;
        return util.toast(res.msg,'none');
      }
      if(res.data.hasSetUserInfo){
        App.Customer.GetCustomerInfo({},'clear')
      }
	  if(this.data.user.remark!=''){
	  	this.setremark();
	  }
      let firstShowMsg='预计10分钟左右餐厅将告知您结果(请留意短息或微信通知)'
      if(!this.hasInWorkTime()){//非营业时间预订
        firstShowMsg=this.data.seller.rec_stime
      }
	  wx.removeStorageSync('remark')
      wx.redirectTo({
        url:util.http_build_query({
          id:res.data.rid,
          firstShowMsg:firstShowMsg,
          hasSubscribeFail:hasSubscribeFail,
        },'/pages/customer/detail_owner')
      })
    }).catch((res)=>{
      this.hasSubmit=true//出错后可再次提交
      if(!res.msgSecCheck){
        return util.toast('提交失败,请重试','none');
      }
    })
  },
  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(){
    console.log(this.options)
    let params={
      act:'update',
      ...this.options,
    }
    params.room_demand=JSON.stringify(params.room_demand)
    params.id=params.sid,
    wx.redirectTo({
      url:util.http_build_query(params,'/pages/reserve/reserve'),
    })
  },
  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 stime='',etime='',time=parseInt(new Date()/1000)
    let today=util.unixtoTime(time,"YYYY-MM-DD")
    stime=util.timetouUnix(today+' '+this.data.seller.rec_stime+':00')
    etime=util.timetouUnix(today+' '+this.data.seller.rec_etime+':00')
    if(etime<=stime){
      if(time<=etime){
        return true
      }
      etime+=86400
    }
    if(time>=stime && time<=etime){
      return true;
    }
    return false;
  },
  cardEvent(e){
    this.editorReserve()
  },
  keyboardheight:0,
  keyboardheightchange(e){
    console.log(e);
    if(e.detail.height==0 || e.detail.height>this.keyboardheight){
      this.keyboardheight=e.detail.height
    }
    this.setData({
      //paddingBottom:e.detail.height?(parseInt(e.detail.height)+100):0
      paddingBottom:this.keyboardheight>0?(parseInt(this.keyboardheight)+20):0
    })
    setTimeout(()=>{
      this.pageScrollToBottom()
    },0)
  },
  pageScrollToBottom: function () {
    wx.createSelectorQuery().select('.pages').boundingClientRect(function (rect) {
      // 使页面滚动到底部
      wx.pageScrollTo({
        scrollTop: rect.bottom
      })
    }).exec()
  },
  toremark(){
	wx.navigateTo({
		url:'remark?remark='+this.data.user.remark,
	})  
  },
  setremark(){
	  App.Customer.SetUserRemarkes({remark:this.data.user.remark}).then((json)=>{
		console.log(json)
		if(json.ret==200){
			console.log(json)
			App.Customer.GetUserRemarkes({},'clear')
		}
	  })
  },
})