官方文檔地址:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/getPhoneNumber.html
功能 | 介紹 | 使用方法 |
獲取手機號 |
獲取微信用戶綁定的手機號,需先調用
wx.login接口。
因為需要用戶主動觸發才能發起獲取手機號接口,所以該功能不由 API 來調用,需用
button 組件的點擊來觸發。
注意:目前該接口針對非個人開發者,且完成了認證的小程序開放(不包含海外主體)。需謹慎使用,若用戶舉報較多或被發現在不必要場景下使用,微信有權永久回收該小程序的該接口權限。
|
wx.login獲取的code有效期為5分鍾,5分鍾后需要重新獲取。
<button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber"></button>
上面是獲取手機號五部曲
|
獲取手機號一共需要五步
第一步:使用button組件;設置open-type屬性值為getPhoneNumber,設置bindgetphonenumber事件的方法
(ps:截至到目前必須使用button,官方的按鈕,我使用的第三方Vantn按鈕也支持,有的人覺得按鈕跟小程序里的UI組件有差異不美觀,我是這樣做的,去掉官方按鈕的默認樣式,自己根據第三發的組件樣式來改並不是改的一模一樣,只要不影響美觀就好了)
1 <button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber"></button>
第二步:通過上一步綁定的方法getPhoneNumber獲取iv和encryptedData,此時還缺少session_key
1 getPhoneNumber (e) { 2 console.log(e.detail.iv) //加密算法的初始向量 3 console.log(e.detail.encryptedData) //包括敏感數據在內的完整用戶信息的加密數據 4 }
第三步:使用wx.login獲取code
1 wx.login({ 2 success(res) { 3 if (res.code) { 4 app.globalData.code = res.code //res.code重點為了獲取session_key 5 } else { 6 } 7 } 8 })
第四步:獲取到code之后,傳入自己的API接口中獲取session_key
(ps:接口是VB.NET webservice。個人覺得不會VB沒有關系,只要有基礎不管你用的JAVA、C#均可看懂以下代碼)
1 //https://api.weixin.qq.com/sns/jscode2session 微信官方API 2 //需要三個參數:appid:小程序ID(AppID);secret:小程序密鑰(AppSecret);grant_type:授權類型,此處只需填寫 authorization_code;js_code:通過 wx.login 接口獲得臨時登錄憑證 code 3 Public Function GetSessionKey() As Boolean 4 Dim relust As Boolean = False 5 Try 6 Dim urlStr As String = "https://api.weixin.qq.com/sns/jscode2session" 7 Dim currStr As String = "appid=" & iWeiChatInfo.appid & "&secret=" & iWeiChatInfo.secret & "&js_code=" & iWeiChatInfo.js_code & "&grant_type=" + iWeiChatInfo.grant_type 8 Dim jsonStr As String = httpHelper.GetData(urlStr, currStr) 9 Dim ijson As New JavaScriptSerializer 10 iSessionKeyInfo = ijson.Deserialize(Of SessionKeyInfo)(jsonStr) 11 Catch ex As Exception 12 End Try 13 End Function
1 Imports System.Net 2 Imports System.IO 3 Imports System.Text 4 Public Class httpHelper 5 Public Shared Function GetData(ByVal url As String, ByVal data As String) As String 6 Dim request As HttpWebRequest = WebRequest.Create(url + "?" + data) 7 request.Method = "GET" 8 Dim sr As StreamReader = New StreamReader(request.GetResponse().GetResponseStream) 9 Return sr.ReadToEnd 10 End Function 11 Public Shared Function PostData(ByVal url As String, ByVal data As String) As String 12 ServicePointManager.Expect100Continue = False 13 Dim request As HttpWebRequest = WebRequest.Create(url) 14 'Post請求方式 15 request.Method = "POST" 16 '內容類型 17 request.ContentType = "application/x-www-form-urlencoded" 18 '將URL編碼后的字符串轉化為字節 19 Dim encoding As New UTF8Encoding() 20 Dim bys As Byte() = encoding.GetBytes(data) 21 '設置請求的 ContentLength 22 request.ContentLength = bys.Length 23 '獲得請 求流 24 Dim newStream As Stream = request.GetRequestStream() 25 newStream.Write(bys, 0, bys.Length) 26 newStream.Close() 27 '獲得響應流 28 Dim sr As StreamReader = New StreamReader(request.GetResponse().GetResponseStream) 29 Return sr.ReadToEnd 30 End Function 31 End Class
第五步:此時iv、encryptedData、session_key三個參數我們有了值使用Encrypt.DecryptAesForWeChart方法進行AES解密
1 Public Class Encrypt 2 Public Shared Function DecryptAesForWeChart(ByVal SourceStr As String, ByVal myKey As String, ByVal myIV As String) As String '使用標准AES對稱解密 3 Try 4 Dim aes As New System.Security.Cryptography.AesCryptoServiceProvider 'DES算法 5 Dim buffer As Byte() = Convert.FromBase64String(SourceStr) 6 aes.Key = Convert.FromBase64String(myKey) 7 aes.IV = Convert.FromBase64String(myIV) 8 aes.Mode = System.Security.Cryptography.CipherMode.CBC 9 aes.Padding = System.Security.Cryptography.PaddingMode.PKCS7 10 Dim ms As New System.IO.MemoryStream(buffer) 11 Dim cs As New System.Security.Cryptography.CryptoStream(ms, aes.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Read) 12 Dim sr As New System.IO.StreamReader(cs) 13 Return sr.ReadToEnd() 14 Catch ex As Exception 15 Return "" 16 End Try 17 End Function 18 19 Public Shared Function EncryptDes(ByVal SourceStr As String, ByVal myKey As String, ByVal myIV As String) As String '使用的DES對稱加密 20 Try 21 Dim des As New System.Security.Cryptography.DESCryptoServiceProvider 'DES算法 22 Dim inputByteArray As Byte() 23 inputByteArray = System.Text.Encoding.Default.GetBytes(SourceStr) 24 des.Key = System.Text.Encoding.UTF8.GetBytes(myKey) 'myKey DES用8個字符,TripleDES要24個字符 25 des.IV = System.Text.Encoding.UTF8.GetBytes(myIV) 'myIV DES用8個字符,TripleDES要24個字符 26 Dim ms As New System.IO.MemoryStream 27 Dim cs As New System.Security.Cryptography.CryptoStream(ms, des.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write) 28 Dim sw As New System.IO.StreamWriter(cs) 29 sw.Write(SourceStr) 30 sw.Flush() 31 cs.FlushFinalBlock() 32 ms.Flush() 33 EncryptDes = Convert.ToBase64String(ms.GetBuffer(), 0, ms.Length) 34 Catch ex As Exception 35 Return "" 36 End Try 37 End Function 38 39 Public Shared Function DecryptDes(ByVal SourceStr As String, ByVal myKey As String, ByVal myIV As String) As String '使用標准DES對稱解密 40 Try 41 Dim des As New System.Security.Cryptography.DESCryptoServiceProvider 'DES算法 42 des.Key = System.Text.Encoding.UTF8.GetBytes(myKey) 'myKey DES用8個字符,TripleDES要24個字符 43 des.IV = System.Text.Encoding.UTF8.GetBytes(myIV) 'myIV DES用8個字符,TripleDES要24個字符 44 Dim buffer As Byte() = Convert.FromBase64String(SourceStr) 45 Dim ms As New System.IO.MemoryStream(buffer) 46 Dim cs As New System.Security.Cryptography.CryptoStream(ms, des.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Read) 47 Dim sr As New System.IO.StreamReader(cs) 48 Return sr.ReadToEnd() 49 Catch ex As Exception 50 Return "" 51 End Try 52 End Function 53 End Class
最后通過接口返回就OK