add-address.js 5.0 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: {
    username: '',
    phone: '',
    value: '',
    checked: 0,
    show: false,
    selectBox: true,
    location: '',
    latitude: "",
    longitude: "",
    latitudes: "", //传值
    longitudes: "",
    flag: true
  },
  //姓名
  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
    });
  },
  //地区
  map() {
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        const latitude = res.latitude
        const longitude = res.longitude
        const speed = res.speed
        const accuracy = res.accuracy
      }
    })
    let that = this;
    const key = 'C4QBZ-UYQLI-S45G2-5ETMC-LQYWE-L6FJU';
    const referer = '春晓世纪华联超市';
    const category = '房产小区,其它';
    wx.getLocation({
      type: 'gcj02',
      success(res) {
        console.log(res, 'rs')
        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, '确认')
  },
  //开关
  onChange() {
    this.setData({
      checked: !this.data.checked
    })
  },
  //点击保存
  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.checked == true ? 1 : 0;
      let postData = {
        address: this.data.value,
        area: this.data.location,
        delFlag: delFlags,
        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'
          })
          // wx.redirectTo({
          //   url: '/pages/select-address/select-address?edit=1',
          // })
          wx.navigateBack({
            delta: 1,
          })
        } else {
          wx.showToast({
            title: '地址超出配送费范围请重新选择',
            icon: 'none'
          })
        }
      })
    }
  },
  onLoad: function (options) {},

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

  },

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

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

  },

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

  },

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

  },

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

  },

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

  }
})