myaddcompile.js 4.1 KB
const app = getApp()
Page({

  data: {
    region: [],
    name: '',
    address: '',
    phone: '',
    wx_id: '',
    mail: '',
    isDefault: '',
    id: ''
  },
  onLoad: function (options) {
    var that = this
    if (options.id) {
      that.setData({
        id: options.id
      })
      let head = {
        'XX-Token': wx.getStorageSync('token')
      }

      let url = '/index/second/getAddress'
      let params = {

        id: options.id
      }
      app.post(url, params, head).then((res) => {
        console.log(res)
        // 将对象添加到数组中
        let answer = this.data.region;
        answer.push(res.province,res.city,res.district);
        
        that.setData({
          region: answer,
          id: res.is,
          name: res.name,
          address: res.address,
          phone: res.tel,
          wx_id: res.wx_id,
          mail: res.mail,
          isDefault: res.is_default == 0 ? false : true
        })
      })

    }
  },
  // 设为默认
  switchChange(e) {
    var that = this
    that.setData({
      isDefault: e.detail.value
    })
  },

  //保存 
  bindsave(e) {
    let that = this;

    let head = {
      'XX-Token': wx.getStorageSync('token')
    }
    let params = {
      name: that.data.name, //姓名
      province: that.data.region[0], //省
      city: that.data.region[1], //市
      district: that.data.region[2], //区
      address: that.data.address, //详细地址
      tel: that.data.phone, //电话
      wx_id: that.data.wx_id, //微信号
      mail: that.data.mail, //邮箱
      is_default: that.data.isDefault ? 1 : 0 //默认
    }

    wx.showModal({
      title: '提示',
      content: '请仔细核对信息,确认无误!',
      confirmText: '确认提交',
      cancelText: '我再想想',
      success(res) {
        if (res.confirm) {
          if (that.data.id) {
            let url = '/index/second/editAddress';
            app.post(url, params, head).then((res) => {
              wx.showToast({
                title: '修改成功',
                mask: true,
                icon: 'none',
                // duration: 2000
              })
              setTimeout(function () {
                wx.reLaunch({
                  url: '/pages/myaddress/myaddress'
                })
              }, 1000)

            })
          } else {
            let url = '/index/second/addAddress';
            app.post(url, params, head).then((res) => {
              wx.showToast({
                title: '保存成功',
                mask: true,
                icon: 'none',
                // duration: 2000
              })
              setTimeout(function () {
                wx.reLaunch({
                  url: '/pages/myaddress/myaddress'
                })
              }, 1000)

            })
          }
          console.log('用户点击确定')
        } else if (res.cancel) {

          console.log('用户点击取消')
        }
      }
    })

  },



  // 姓名
  bindName(e) {
    this.setData({
      name: e.detail.value
    })
    
  },
  // 地区
  bindRegionChange: function (e) {
    console.log(e)
    this.setData({
      region: e.detail.value
    })
  },

  //详细地址
  bindAddress(e) {
    this.setData({
      address: e.detail.value
    })

  },
  //联系方式
  bindPhone(e) {
    this.setData({
      phone: e.detail.value
    })

  },
  //微信号
  bindwx_id(e) {
    console.log(e)
    this.setData({
      wx_id: e.detail.value
    })
  },
  //邮箱号
  bindmail(e) {
    console.log(e)
    this.setData({
      mail: e.detail.value
    })
  },






  onReady: function () {

  },


  onShow: function () {

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})