update.js 3.71 KB
// pages/book/smsbatch/update.js
import util from '../../../../utils/util';
import App from '../../../../utils/api';
const {regeneratorRuntime} = global
const form={
  send_total:0,
  send_msg:'',
  name:'',
  phone:'',
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    form:JSON.parse(JSON.stringify(form)),
    options:{
      hasScene:0,
      seller_id:'',
    },
    totalChar:0,
    totalSms:0,
    buttonStatus:false,
  },
  reservePhonesMap:[],
  reservePhones:[],
  note_name:'',
  note_phone:'',
  /**
   * 生命周期函数--监听页面加载
   */
  options:{},
  onLoad: async function (options) {
    this.options=options;
    let enter_params=util.cache('enter_params');
    if(enter_params){
      try{
        let si=await App.Reserve.SellerInfo(enter_params);
        this.note_name=si.data.seller_user.nick_name||si.data.seller_user.nickname;
        this.note_phone=si.data.seller_user.phone;
      }catch (e){}
    }
    App.Reserve.GetReservePhones({sid:options.seller_id}).then(res=>{
      //this.reservePhones=res.data;
      if(res.data.length>0){
        let reservePhonesMap=[];
        let reservePhones=[];
        res.data.forEach(m=>{
          if(/1[3-9]{1}[\d]{9}/.test(m.phone)){
            reservePhonesMap.push(m);
            reservePhones.push(m.phone);
          }
        })
        this.reservePhonesMap=reservePhonesMap;
        this.reservePhones=reservePhones.toString();
        this.setData({
          options:options,
          [`form.send_total`]:reservePhonesMap.length,
          [`form.name`]:this.note_name,
          [`form.phone`]:this.note_phone,
        })
      }
    })
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  formValue(e){
    let key=util.attr(e,'key');
    let form=this.data.form;
    form[key]=util.val(e).trim();
    let obj={
      form:form,
    }
    if(key=='send_msg'){
      obj.totalChar=form[key].length;
      obj.totalSms=Math.ceil((form[key].length+3)/67);
    }
    obj.buttonStatus=false;
    if(this.data.form.send_msg && this.data.form.name && this.data.form.phone){
      obj.buttonStatus=true
    }
    this.setData(obj)
  },
  submit(e){
    if(!this.data.buttonStatus) return;
    if(this.data.form.send_total<1) {
      return util.toast('活动人数为空','none');
    }
    if(!this.data.form.send_msg){
      return util.toast('内容必须填写','none');
    }
    if(!this.data.form.name){
      return util.toast('姓名必须填写','none');
    }
    if(!/^1[3-9]{1}[\d]{9}$/.test(this.data.form.phone)){
      return util.toast('请填写正确手机号码','none');
    }
    let param={
      ...this.data.form,
      send_phones:this.reservePhones,
      sid:this.options.seller_id,
      note_name:this.note_name,
    }
    if(this.options.hasScene==1){
      param.note_name=e.detail.nickName
    }
    App.Reserve.SmsBatchUpdate(param).then(res=>{
      this.options.id=res.data.id;
      console.log('this.options.total',this.data.totalSms,this.reservePhonesMap.length)
      this.options.total=this.data.totalSms*this.reservePhonesMap.length
      wx.redirectTo({
        url:util.http_build_query(this.options,'./result'),
      });
    });
  },
})