微信小程序获取手机号

微信小程序获取手机号

微信小程序:
wxml:
 
 js:
      Page({
      getPhoneNumber: function (e) {
    var that=this;
    //获取用户信息(code)
    wx.login({
      success: res => {
        
        if(res.code)
        {
          console.log(res);
          that.globalData.code = res.code;
        }
        else
        {
          console.log('获取用户登录态失败!' + res.errMsg) 
        }
        //请求用户信息
        wx.getUserInfo({
          success: function (r) {
            console.log(r.userInfo);
            that.globalData.userInfo=r.userInfo;
          }
        })
      }
    })
    //获取IV
    console.log(e.detail.iv)
    //获取encryptedData
    console.log(e.detail.encryptedData)
    //获取全局变量的UserInfo信息
    console.log(app.globalData.userInfo)
    wx.request({
      url: '请求路径',
      method:"post",//请求方式
      data: { name: app.globalData.userInfo.nickName, iv: e.detail.iv, encryptedData:e.detail.encryptedData,code:app.globalData.code},//传递参数
      success:function(res){
        console.log(res);
        //回调
        that.setData({
          Role:res.data
        })
      }
    })
  },
      })
Vs:
后端:
返回的对象:
public class PostData
    {
        public string iv { get; set; }
        public string code { get; set; }
        public string encryptedData { get; set; }
    }
    //
    //API控制器
    [HttpPost]//微信用户添加
        public string AddWcReader(PostData data)
        {
            //微信小程序的APPID
                var appId = "wx4314bac4bb229f37";
                //微信小程序的APPsecret
                var secret = "15ad68c5fdca8b22b56c0f4c7eb80883";
                //获取sessionKey
                var code = data.code;
                HttpClient http = new HttpClient();
                string result = string.Empty;
                HttpResponseMessage response = http.GetAsync($"https://api.weixin.qq.com/sns/jscode2session?appid={appId}&secret={secret}&js_code={code}&grant_type=authorization_code").Result;
                if (response.IsSuccessStatusCode)
                {
                    result = response.Content.ReadAsStringAsync().Result;
                }
                if (string.IsNullOrEmpty(result))
                {
                    throw new Exception("未经授权的操作");
                }
                var apiResult = JsonConvert.DeserializeObject(result);
                string sessionKey = apiResult.session_key;
                //获取手机号
                string tel = Mi.Jie(data.iv, sessionKey, data.encryptedData);
        }
        //获取用户的解密信息
        using System.Security.Cryptography;
        public class Mi
    {
        public static string Jie(string iv, string sessionKey, string encryptedData)
        {
            byte[] encDatas = Convert.FromBase64String(encryptedData);
            RijndaelManaged rijndaelCipher = new RijndaelManaged();
            rijndaelCipher.Key = Convert.FromBase64String(sessionKey); // Encoding.UTF8.GetBytes(AesKey);
            rijndaelCipher.IV = Convert.FromBase64String(iv);// Encoding.UTF8.GetBytes(AesIV);
            rijndaelCipher.Mode = CipherMode.CBC;
            rijndaelCipher.Padding = PaddingMode.PKCS7;
            ICryptoTransform transform = rijndaelCipher.CreateDecryptor();
            byte[] plainText = transform.TransformFinalBlock(encDatas, 0, encDatas.Length);
            string result1 = Encoding.Default.GetString(plainText);
            dynamic model = Newtonsoft.Json.Linq.JToken.Parse(result1) as dynamic;
            return model.phoneNumber;
        }

    }

你可能感兴趣的:(微信小程序)