官方文档地址: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