receiptAddress.js 3.0 KB
import {
  request
} from "../../request/index.js"
const a = getApp()
Page({
  data: {
    imagesUrl: a.globalData.baseUrl,
    name: '',
    phone: '',
    ajxtrue: false,
    address2: '',
    msg: '',
    address: '',
  },

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

  },

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

  },

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

  },
  async formSubmit(e) {
    //  console.log('form发生了submit事件,携带数据为:', e.detail.value)
    let that = this
    //  console.log(that.data.name, that.data.phone, that.data.address, that.data.address2);
    try {
      let ajxtrue = this.data.ajxtrue
      if (ajxtrue == true) {
        //表单提交进行
        const {
          data
        } = await request({
          method: 'get',
          url: 'api/address/addAddress',
          data: {
            name: e.detail.value.name,
            phone: e.detail.value.phone,
            address: that.data.address,
            address2: e.detail.value.address2
          }
        })
        //  console.log(data);
        if (data.code == 411) {
          this.setData({
            msg: data.msg
          })
          this.popFailTest()
        } else if (data.code == 0) {
          this.setData({
            msg: data.msg
          })
          this.popFailTest()
        } else {
          this.setData({
            msg: data.msg
          })
          this.popSuccessTest()
          setTimeout(function () {
            wx.navigateBack()
          }, 2000)
        }

      } else {
        wx.showToast({
          title: '手机号有误',
          icon: 'success',
          duration: 2000
        })
      }


    } catch (err) {
      this.setData({
        msg: err.msg
      })
      this.popFailTest()
    }

  },
  // 手机号验证
  blurPhone: function (e) {
    var phone = e.detail.value;
    let that = this
    if (!(/^1[3456789]\d{9}$/.test(phone))) {

      this.setData({
        ajxtrue: false
      })
      if (phone.length >= 11) {
        wx.showToast({
          title: '手机号有误',
          icon: 'success',
          duration: 2000
        })
      }
    } else {
      this.setData({
        ajxtrue: true
      })
      //  console.log('验证成功', that.data.ajxtrue)
    }
  },
  bindRegionChange: function (e) {
    //  console.log('picker发送选择改变,携带值为', e.detail.value)
    this.setData({
      address: e.detail.value.toString()
    })
    //  console.log(this.data.address);

  },
  // 弹窗
  popSuccessTest() {
    wx.showToast({
      title: this.data.msg,
      icon: '', //默认值是success,就算没有icon这个值,就算有其他值最终也显示success
      duration: 2000, //停留时间
    })
  },
  popFailTest() {
    wx.showToast({
      title: this.data.msg,
      icon: '{{imagesUrl}}assets/static/img/cuowu.png',
      duration: 2000, //停留时间
    })
  },

})