微信小程序獲取用戶手機號


1.getPhoneNumber這個組件通過button來實現(別的標簽無效)。將button中的open-type=“getPhoneNumber”,並且綁定bindgetphonenumber事件獲取回調。

<button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber"></button>
  <input type='text' name='phone' value='{{phone}}'></input>

2.在使用這個組件之前必須先調用login接口,如果沒有調用login點擊button時會提示先調用login。 

 * 如果 后面的方法 getPhoneNumber 已添加login接口,這里的onlaunch方式就不用重復添加了。

  onLaunch: function () {
    wx.login({
      success: function (res) {
        if (res.code) {
          //發起網絡請求
          console.log(res.code)
        } else {
          console.log('獲取用戶登錄態失敗!' + res.errMsg)
        }
      }
    });
  },

3.通過bindgetphonenumber綁定的事件來獲取回調。回調的參數有三個,

errMsg:用戶點擊取消或授權的信息回調。

iv:加密算法的初始向量(如果用戶沒有同意授權則為undefined)。

encryptedData: 用戶信息的加密數據(如果用戶沒有同意授權同樣返回undefined)

getPhoneNumber:function(e){
    if(e.detail.iv&&e.detail.encryptedData){
        // console.log(e.detail.iv)
        // console.log(e.detail.encryptedData)
        wx.login({
            success:(res)=>{
                wx.request({
                    url:`https://SERVER/Api/Index/get_session_key`
                    ,header:{'content-type':'application/json'}
                    ,data:{
                        code:res.code
                    }
                    ,success:(res)=>{
                        console.log(res)
                        let session_key=res.data.session_key
                        wx.request({
                            url:`https://SERVER/Api/Index/decryptData2`
                            ,header:{'content-type':'application/json'}
                            ,data:{
                                'encryptedData':encodeURIComponent(e.detail.encryptedData)
                                ,'iv':e.detail.iv
                                ,'session_key':session_key
                            }
                            ,success:(res)=>{
                                console.log(res.data)
                                if(!res.data){
                                    console.log('獲取失敗,請重試')
                                    wx.showToast({
                                        title:'網絡錯誤,請重試…'
                                        ,icon:'none'
                                    })
                                }else{
                                    this.setData({
                                        phone:res.data.purePhoneNumber
                                    })
                                }
                            }
                        })
                    }
                })
            }
        })
    }else{
        console.log('拒絕獲取手機號碼')
    }
}

4、后端頁面代碼

function get_session_key($code){
      $url='https://api.weixin.qq.com/sns/jscode2session?appid='.$appid.'&secret='.$secret.'&js_code='.$code.'&grant_type=authorization_code';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch); 
        curl_close($ch); 
        $res = json_decode($output, true); 
      $out=[
          'session_key'=>$res[session_key],
      ];
      $this->ajaxReturn($out);
    }


function decryptData2($encryptedData,$iv,$session_key){
  $aesKey=base64_decode($session_key);
  $aesIV=base64_decode(str_replace(' ','+',$iv));
  $aesCipher=base64_decode(urldecode($encryptedData));
  $result=openssl_decrypt($aesCipher,"AES-128-CBC",$aesKey,1,$aesIV);
  $this->ajaxReturn(jsondecode($result));
}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM