edit-address.js 6.1 KB
// pages/add-address/add-address.js
let method = require("../../utils/reuqest.js");
const util = require("../../utils/util.js");
const chooseLocation = requirePlugin('chooseLocation');
Page({
  data: {
    id: '',    //地址id
    username: '',
    phone: '',
    value: '',
    switchChecked:false, //默认地址
    show: false,
    selectBox: true,
    location: '',
    latitude: "",
    longitude: "",
    latitudes: "",  //传值给后台
    longitudes: ""
  },
  //姓名
  inputName(e) {
    this.setData({
      username: e.detail
    })
  },
  //电话
  inputDel(e) {
    this.setData({
      phone: e.detail
    })
  },
  //电话失焦事件
  blurPhone(e) {
    if (this.data.phone.length == 11) {
      this.sPhoneAvailable(e.detail.value);
      return false;
    } else {
      this.isTelAvailable(e.detail.value)
    }
  },
  //校验手机号
  sPhoneAvailable(pone) {
    var myreg = /^[1][3,5,7,8][0-9]{9}$/;
    if (!myreg.test(pone)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none'
      })
      return false;
    } else {
      return true;
    }
  },
  //校验电话号码
  isTelAvailable(tel) {
    var myreg = /^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/;
    if (!myreg.test(tel)) {
      wx.showToast({
        title: '请输入正确的电话号码',
        icon: 'none'
      })
      return false;
    } else {
      alert("电话号校验成功");
      return true;
    }
  },
  // 弹出层
  showPopup() {
    this.setData({
      show: true
    });
  },
  onClose() {
    this.setData({
      show: false
    });
  },
  //地区
  bindRegionChange: function (e) {
    this.setData({
      region: e.detail.value
    })

  },
  map() {
    let that = this;
    const key = 'C4QBZ-UYQLI-S45G2-5ETMC-LQYWE-L6FJU'; 
    const referer = '春晓世纪华联超市'; 
    const category = '房产小区,其它';
    wx.getLocation({
      type: 'gcj02',
      success(res) {
        const location = JSON.stringify({
          latitude: res.latitude,
          longitude: res.longitude
        });
        wx.navigateTo({
          url: `plugin://chooseLocation/index?key=${key}&referer=${referer}&location=${location}&category=${category}`
        });
      }
    })


  },
  //详细地址
  inputAddress(e) {
    this.setData({
      value: e.detail
    })
  },
  cancelChoose() {
    this.setData({
      show: false
    });
  },
  chooseAddress(e) {
    this.setData({
      show: false,
      selectBox: false
    });
    console.log(e.detail.values, '确认')
  },

  switch1Change(e){
     console.log(e.detail.value,'value')
    this.setData({
      switchChecked:e.detail.value
    })
   },
   //保存
  save() {
    if (this.data.username == '') {
      wx.showToast({
        title: '请输入姓名',
        icon: 'none'
      })
    } else if (this.data.phone == '') {
      wx.showToast({
        title: '请输入电话',
        icon: 'none'
      })
    } else if (this.data.value == '') {
      wx.showToast({
        title: '请输入详细地址',
        icon: 'none'
      })
    } else {
      let delFlags = this.data.switchChecked == true ? 1 : 0;
      let postData = {
        address: this.data.value,
        area: this.data.location,
        delFlag: delFlags,
        id: this.data.id,
        location: this.data.longitudes + "," + this.data.latitudes,
        name: this.data.username,
        phone: this.data.phone,
        zipCode: ""
      }
      method.postRequest("/address/create", postData, data => {
        if (data.statusCode == 0) {
          console.log('11')
          wx.showToast({
            title: '保存成功',
            icon: 'none'
          })
          console.log('22')
          wx.navigateTo({
            url: '/pages/select-address/select-address',
          })
          console.log('33')
        } else {
          wx.showToast({
            title: data.msg,
            icon: 'none'
          })
        }
      })
    }
  },
  //删除
  delEvent(e) {
    let id = this.data.id;
    let obj = {};
    wx.showModal({
      title: '提示',
      content: '确定删除该地址吗?',
      success(res) {
        if (res.confirm) {
          method.postRequest("/address/delete/" + id, obj, data => {
            if (data.statusCode == 0) {
              wx.showToast({
                title: '删除成功',
                icon:'none'
              })
              setTimeout(() => {
                wx.redirectTo({
                  url: '/pages/select-address/select-address',
                })
              },1000)
            }else{
              wx.showToast({
                title: data.msg,
                icon:'none'
              })
            }
          })
        } else if (res.cancel) {
        }
      }
    })
  },
  onLoad: function (options) {
    console.log(options, 'options')
    this.setData({
      username: options.name,
      phone: options.phone,
      value: options.address,
      switchChecked: options.delFlag == 1 ?true:false,
      location: options.area,
      locations: options.locations,
      latitudes: options.locations.split(",")[0], //传值给后台
      longitudes: options.locations.split(",")[1],
      id: options.id
    })

  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    const location = chooseLocation.getLocation();
    const address = location.address
    const latitude = location.latitude
    const longitude = location.longitude
    this.setData({
      location: address,
      latitudes: latitude,
      longitudes: longitude
    })
  },

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

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    chooseLocation.setLocation(null);
  },

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

  },

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

  },

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

  }
})