addaddress.js 5.8 KB
// pages/addaddress/addaddress.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: '',
    mobile: '',
    province: '',
    city: '',
    county: '',
    addr: '',
    region: ['北京市', '北京市', '东城区'],
    regionname: '请选择所在地区',
    addressid: '',
    type:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    if (options.type == 1) {
      wx.setNavigationBarTitle({
        title: '新增收货地址' //页面切换,更换页面标题
      })
      this.setData({
        type:options.type
      })
    } else {
      this.setData({
        addressid: options.id,
        type: options.type
      })
      wx.setNavigationBarTitle({
        title: '编辑收货地址' //页面切换,更换页面标题
      })
      this.getaddresstail()
    }

    console.log(this.data.type)


  },

  //获取收货地址详情
  getaddresstail() {
    let that = this;
    var url = 'take/get_one';
    var params = {
      take_id: that.data.addressid,

    }
    app.post(url, params).then((res) => {
      console.log(res);
      that.setData({
        name: res.name,
        mobile: res.mobile,

        province: res.province,
        city: res.city,
        county: res.county,
        addr: res.addr,
        regionname: res.province + res.city + res.county,

      })
      console.log(that.data.name)



    }).catch((err) => {

    })
  },
  entershouhuo(e) {
    this.setData({
      name: e.detail.value
    })
  },
  enterphone(e) {
    this.setData({
      mobile: e.detail.value
    })

  },
  enteraddress(e) {
    this.setData({
      addr: e.detail.value
    })
  },
  bindRegionChange: function(e) {
    console.log('picker发送选择改变,携带值为', e.detail.value)
    this.setData({
      region: e.detail.value,
      regionname: e.detail.value[0] + e.detail.value[1] + e.detail.value[2],
      province: e.detail.value[0],
      city: e.detail.value[1],
      county: e.detail.value[2]
    })
  },

  addsave() {
    let that = this;
    console.log(that.data.regionname)
    if (that.data.name == '') {
      wx.showToast({
        title: '请填写收货人',
        icon: 'none'
      })

      return false
    }


    if (that.data.mobile == '') {
      wx.showToast({
        title: '请填写手机号',
        icon: "none"
      })

      return false
    }
    if (that.data.mobile != '') {
      var reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
      if (!reg.test(that.data.mobile)) {
        wx.showToast({
          title: '请输入正确的手机号',
          icon: "none"
        })
        return false
      }


    }

    if (that.data.regionname == '请选择所在地区') {
      wx.showToast({
        title: '请选择所在地区',
        icon: "none"
      })

      return false
    }

    if (that.data.addr == '') {
      wx.showToast({
        title: '请输入详细地址',
        icon: 'none'
      })

      return false
    }
    var url = 'take/create_take';
    var params = {
      name: that.data.name,
      mobile: that.data.mobile,
      province: that.data.province,
      city: that.data.city,
      county: that.data.county,
      addr: that.data.addr

    }
    app.post(url, params).then((res) => {
      console.log(res);

      wx.showToast({
        title: '新增收货地址成功',
        icon: 'none'
      })

      setTimeout(function() {
        // wx.navigateTo({
        //   url: '/pages/dizhibox/dizhibox',
        // })

        wx.redirectTo({
         
          url: '/pages/dizhibox/dizhibox?addrestype=' + 1,
        })
      }, 1000)



    }).catch((err) => {

    })

  },
  editsave() {
    let that = this;
    console.log(that.data.regionname)
    if (that.data.name == '') {
      wx.showToast({
        title: '请填写收货人',
        icon: 'none'
      })

      return false
    }


    if (that.data.mobile == '') {
      wx.showToast({
        title: '请填写手机号',
        icon: "none"
      })

      return false
    }
    if (that.data.mobile != '') {
      var reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
      if (!reg.test(that.data.mobile)) {
        wx.showToast({
          title: '请输入正确的手机号',
          icon: "none"
        })
        return false
      }


    }

    if (that.data.regionname == '请选择所在地区') {
      wx.showToast({
        title: '请选择所在地区',
        icon: "none"
      })

      return false
    }

    if (that.data.addr == '') {
      wx.showToast({
        title: '请输入详细地址',
        icon: 'none'
      })

      return false
    }
    var url = 'take/update_take';
    var params = {
      name: that.data.name,
      mobile: that.data.mobile,
      province: that.data.province,
      city: that.data.city,
      county: that.data.county,
      addr: that.data.addr,
      take_id:that.data.addressid

    }
    app.post(url, params).then((res) => {
      console.log(res);

      wx.showToast({
        title: '保存成功',
        icon: 'none'
      })

      setTimeout(function() {
        wx.navigateTo({
          url: '/pages/dizhibox/dizhibox',
        })
      }, 1000)



    }).catch((err) => {

    })
  },


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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})