回复 刷新

暂无评论

c#微信小程序登录及获取电话号码

开发前建议先阅读微信小程序官方开发文档

1.微信小程序登录

// 登录 wx.login({ success: res => { // 发送 res.code 到后台换取 openId, sessionKey, unionId if (res.code) { console.log("code========" + JSON.stringify(res)); //发起网络请求 wx.request({ url: 'http://xxxxxx/Weixin/'+res.code, method: 'GET', header: { 'content-type': 'application/x-www-form-urlencoded' }, success: function (res) { console.log(res) console.log('response.data----'+JSON.stringify(res.data)); wx.setStorageSync('sessionKey', res.data.session_key); //存储sessionKey在Storage wx.setStorageSync('app_openid', res.data.openid); //存储openid在Storage } }) } else { console.log('登录失败!' + res.errMsg) } } })

2.后端登录接口

[HttpGet("{code}")] public RepParamrep Get(string code) { string appid = "xxxxx"; string secret = "xxxxx"; return Get(code, appid, secret); } public RepParamrep Get(string code, string appid, string secret) { string cod = code; string html = string.Empty; string url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appid + "&secret=" + secret + "&js_code=" + cod + "&grant_type=authorization_code"; HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); request.Method = "GET"; request.ContentType = "text/html;charset=UTF-8"; HttpWebResponse response = request.GetResponse() as HttpWebResponse; Stream ioStream = response.GetResponseStream(); StreamReader sr = new StreamReader(ioStream, Encoding.UTF8); html = sr.ReadToEnd(); sr.Close(); ioStream.Close(); response.Close(); RepParamrep rep = JsonConvert.DeserializeObject<RepParamrep>(html); return rep; } public class RepParamrep { public string session_key { get; set; } public string openid { get; set; } }

3.前端获取用户信息

// 获取用户信息 wx.getSetting({ success: res => { if (res.authSetting['scope.userInfo']) { // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框 wx.getUserInfo({ success: res => { console.log(res); } }) } } })

4.前端获取手机号

<button type="primary" open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">获取手机号</button> getPhoneNumber: function(e) { if (e.detail.errMsg == "getPhoneNumber:ok") { wx.request({ url: 'http://xxxx/Weixin', method:'post', header:{ 'Content-type':'application/json' }, data: { encryptedData:e.detail.encryptedData, iv:e.detail.iv, session_key:wx.getStorageSync('sessionKey') }, success: function (res) { console.log(res) }, fail: function (res) { //错误处理 app.ylShowLog(res) }, complete: function (res) { //完成处理 wx.hideLoading(); } }) }},

5.后端手机号解码接口

[HttpPost] public string getPhoneNumber(UserRepParamrep userRepParamrep) { String encryptedData = userRepParamrep.encryptedData; String iv = userRepParamrep.iv; String session_key = userRepParamrep.session_key; try { byte[] encryData = Convert.FromBase64String(encryptedData); RijndaelManaged rijndaelCipher = new RijndaelManaged(); rijndaelCipher.Key = Convert.FromBase64String(session_key); rijndaelCipher.IV = Convert.FromBase64String(iv); rijndaelCipher.Mode = CipherMode.CBC; rijndaelCipher.Padding = PaddingMode.PKCS7; ICryptoTransform transform = rijndaelCipher.CreateDecryptor(); byte[] plainText = transform.TransformFinalBlock(encryData, 0, encryData.Length); string result = Encoding.Default.GetString(plainText); dynamic model = Newtonsoft.Json.Linq.JToken.Parse(result) as dynamic; string phoneNumber = model.phoneNumber; //return model.phoneNumber; if (string.IsNullOrEmpty(phoneNumber)) { return ""; } return phoneNumber; } catch (Exception ex) { return ""; } }
public class UserRepParamrep { public string session_key { get; set; } public string iv { get; set; } public string encryptedData { get; set; } }
  • 218
  • 0
  • 0