add_address.js 2.8 KB
const a = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: '',
    tel: '',
    area: '',
    address: '',
    mr: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  // 获取名字
  nameFun(e) {
    this.setData({
      name: e.detail.value.replace(/\s+/g, '')
    })
  },

  // 获取手机号
  telFun(e) {
    this.setData({
      tel: e.detail.value.replace(/\s+/g, '')
    })
  },

  // 获取详细地址
  getAddressFun(e) {
    this.setData({
      address: e.detail.value
    })
  },

  bindRegionChange(e) {
    this.setData({
      area: e.detail.value[0] + ' ' + e.detail.value[1] + ' ' + e.detail.value[2]
    })
  },

  // 设置默认地址
  switchChange(e) {
    if (e.detail.value) {
      this.setData({
        mr: 1
      })
    } else {
      this.setData({
        mr: 0
      })
    }
  },

  // 保存地址
  addFun() {
    let t = this;
    let AuglyTest_phone = /^1(3|4|5|6|7|8|9)\d{9}$/;
    let u = a.interface.addaddress;
    let d = {
      tel: t.data.tel,
      name: t.data.name,
      area: t.data.area,
      address: t.data.address,
      mo: t.data.mr
    }
    if (d.name == '') {
      wx.showToast({
        title: '请输入收货人名称',
        icon: 'none',
        duration: 1300
      })
    } else if (d.tel == '') {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none',
        duration: 1300
      })
    } else if (!AuglyTest_phone.test(d.tel)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none',
        duration: 1300
      })
    } else if (d.area == '') {
      wx.showToast({
        title: '请选择省市区',
        icon: 'none',
        duration: 1300
      })
    } else if (d.address == '') {
      wx.showToast({
        title: '请输入详细地址',
        icon: 'none',
        duration: 1300
      })
      
    } else {
      a.post(u, d).then((r) => {
        if (r.code == 200) {
          wx.showToast({
            title: '保存成功',
            icon: 'none',
            duration: 1300
          })
          setTimeout(() => {
            wx.navigateBack()
          }, 1300)
        }
      })
    }
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})