电话号码的收取获取&&手机号验证

 <view class='right' style=' width:60%;'>
     <input type='text' placeholder='请输入联系电话' maxlength="11" placeholder-style="color:#ccc;" value='{{phone}}' bindblur="contactphone"></input>            
 </view>
            <button class="getphone" open-type='getPhoneNumber' bindgetphonenumber="getPhoneNumber">快速获取</button> 
  //监控联系电话
  contactphone:function(e){    
      var phone = e.detail.value;  //获取input输入的value
      if (!(/^1[34578]\d{9}$/.test(phone))) {   //手机号验证 正则     
        if (phone.length >= 11) {
          wx.showToast({   //弹窗提示
            title: '手机号有误',
            icon: 'success',
            duration: 2000
          })
        }
      } else {
        this.setData({
          phone: e.detail.value
        })
        wx.setStorageSync('phone', phone);  //缓存手机号
      }  
  },
//快速获取电话号码
  getPhoneNumber:function (e) {   //配合buttom   open-type='getphonenumber'使用
    this.getUserInfo(e);  //运行登录方法
    var that=this;    
    var encryptedData = e.detail.encryptedData;   获取方法返回值
    var iv = e.detail.iv;        
    if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '未授权',
        success: function (res) { }
      })
    } else {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '同意授权',
        success: function (res) { 
          wx.request({
            url: app.globalData.baseUrl + 'rest/app/hmpg/wechat/getPhoneNumber',//后台接口 获取手机号
            data: {
              code:code,  //登录后返回的code
              encryptedData: encryptedData,  // getphonenumber返回值 
              iv: iv    //getphonenumber返回值
            },
            header: {
              'content-type': 'application/json' // 默认值
            },
            success: function (res) {
              if(res.data.code==0){               
                var telephonenum = res.data.data.phoneNumber;
                that.setData({
                  phone: res.data.data.phoneNumber
                })
              }             
            }
          });
          
        }
      })
    }
  },

 

posted @ 2018-11-01 16:39  Jinsuo  阅读(204)  评论(0编辑  收藏  举报