本文實(shí)例講述了微信小程序登錄數(shù)據(jù)解密及狀態(tài)維持。分享給大家供大家參考,具體如下:
專注于為中小企業(yè)提供網(wǎng)站設(shè)計(jì)制作、成都做網(wǎng)站服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)中山免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了數(shù)千家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
學(xué)習(xí)過(guò)小程序的朋友應(yīng)該知道,在小程序中是不支持cookie的,借助小程序中的緩存我們也可以存儲(chǔ)一些信息,但是對(duì)于一些比較重要的信息,我們需要通過(guò)登錄狀態(tài)維持來(lái)保存,同時(shí),為了安全起見(jiàn),用戶的敏感信息,也是需要加密在網(wǎng)絡(luò)上傳輸?shù)摹?/p>
前臺(tái),service。封裝了http請(qǐng)求,同時(shí)封裝了getSession(通過(guò)code獲取服務(wù)器生成的session)、getUserInfo(獲取用戶信息)、getDecryptionData(解密數(shù)據(jù))
//service.js //封裝了http服務(wù),getUserInfo,提供回調(diào)函數(shù) var recourse = { doMain: "http://www.domain.com/" } module.exports = { //Http Get requestGet: function (url, data, cb) { wx.request({ url: recourse.doMain + url, data: data, method: 'GET', header: {}, success: function (res) { cb(res, true) }, fail: function () { cb(data, false) } }) }, //Http POST requestPost: function (url, data, cb) { wx.request({ url: recourse.doMain + url, data: data, method: 'POST', header: {}, success: function (res) { cb(res, true) }, fail: function () { cb(data, false) } }) }, //獲取第三方sessionId getSession: function (code, cb) { wx.request({ url: recourse.doMain + 'SmallRoutine/PostCode', data: { code: code }, method: 'POST', success: function (res) { cb(res, true) }, fail: function (res) { cb(res, false) } }) }, //獲取用戶信息 getUserInfo: function (cb) { wx.getUserInfo({ success: function (res) { cb(res, true) }, fail: function (res) { cb(res, false) } }) }, //獲取解密數(shù)據(jù) getDecryptionData: function (cb) { wx.request({ url: recourse.doMain+'SmallRoutine/Decryption', data: { encryptedData: wx.getStorageSync('encryptedData'), iv: wx.getStorageSync('iv'), session: wx.getStorageSync('thirdSessionId'), }, method: 'POST', success: function (res) { cb(res, true) }, fail: function (res) { cb(res, false) } }) } }
后臺(tái),根據(jù)code獲取session,客戶端用來(lái)保持登錄狀態(tài)
[HttpPost] public ActionResult PostCode(string code) { try { if(!string.IsNullOrEmpty(code)) { HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(string.Format("https://api.weixin.qq.com/sns/jscode2session?appid={0}&secret={1}&js_code={2}&grant_type=authorization_code",appId,appSecret,code)); request.Method = "GET"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader sr = new StreamReader(response.GetResponseStream()); string content = sr.ReadToEnd(); if(response.StatusCode == HttpStatusCode.OK) { var successModel = Newtonsoft.Json.JsonConvert.DeserializeObject(content); if(null != successModel.session_key) { //session_key是微信服務(wù)器生成的針對(duì)用戶數(shù)據(jù)加密簽名的密鑰,不應(yīng)該傳輸?shù)娇蛻舳? var session_key = successModel.session_key; //3re_session用于服務(wù)器和小程序之間做登錄狀態(tài)校驗(yàn) var thirdSession = Guid.NewGuid().ToString().Replace("-",""); var now = DateTime.Now; //存到數(shù)據(jù)庫(kù)或者redis緩存,這里一小時(shí)過(guò)期 Service.AddLogin(new Domain.Login() { Code = code, Createime = now, OpenId = successModel.openid, OverdueTime = now.AddMinutes(60), SessionKey = successModel.session_key, SessionRd = thirdSession }); return Json(new { success = true,session = thirdSession,openId = successModel.openid }); } else { var errModel = Newtonsoft.Json.JsonConvert.DeserializeObject (content); return Json(new { success = false,msg = errModel.errcode + ":" + errModel.errmsg }); } } else { var errModel = Newtonsoft.Json.JsonConvert.DeserializeObject (content); return Json(new { success = false,msg = errModel.errcode + ":" + errModel.errmsg }); } } else { return Json(new { success = false,msg = "code不能為null" }); } } catch(Exception e) { return Json(new { success = false }); } }
解密敏感信息
[HttpPost] public ActionResult Decryption(string encryptedData,string iv,string session) { try { var sessionKey = Service.GetSessionKey(session); if(!string.IsNullOrEmpty(sessionKey)) { var str = AESDecrypt(encryptedData,sessionKey,iv); var data = Newtonsoft.Json.JsonConvert.DeserializeObject(str); if(null != data) { //服務(wù)器可以更新用戶信息 return Json(new { success = true,data = data }); } } } catch(Exception e) { Service.AddLog("翻譯錯(cuò)誤:"+e.ToString()); } return Json(new { success = false }); }
AES解密
public static string AESDecrypt(string encryptedData,string key,string iv) { if(string.IsNullOrEmpty(encryptedData)) return ""; byte[] encryptedData2 = Convert.FromBase64String(encryptedData); System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged { Key = Convert.FromBase64String(key), IV = Convert.FromBase64String(iv), Mode = System.Security.Cryptography.CipherMode.CBC, Padding = System.Security.Cryptography.PaddingMode.PKCS7 }; System.Security.Cryptography.ICryptoTransform ctf = rm.CreateDecryptor(); Byte[] resultArray = ctf.TransformFinalBlock(encryptedData2,0,encryptedData2.Length); return Encoding.UTF8.GetString(resultArray); }
判斷用戶是否掉線
[HttpPost] public ActionResult PostSession(string session) { if(!string.IsNullOrEmpty(session)) { var loginInfo = Service.GetLoginInfo(session); if(null != loginInfo) { return Json(new { success = true,openId = loginInfo.OpenId }); } else { return Json(new { success = false }); } } return Json(new { success = false }); }
前臺(tái)index.js
//index.js var app = getApp() Page({ data: { userInfo: {}, }, onLoad: function () { var that = this app.getUserInfo(function (userInfo) { //更新數(shù)據(jù) that.setData({ userInfo: userInfo }) }) } })
前臺(tái)app.js
var service = require('./service/service.js') var appConfig = { getUserInfo: function (cb) { var that = this if (that.globalData.userInfo) { //從緩存中用戶信息 } else { //wx api 登錄 wx.login({ success: function (res) { console.log('登錄成功 code 為:' + res.code); if (res.code) { service.getSession(res.code, function (res, success) { if (success) { console.log('通過(guò) code 獲取第三方服務(wù)器 session 成功, session 為:' + res.data.session); //緩存起來(lái) wx.setStorageSync('thirdSessionId', res.data.session); //wx api 獲取用戶信息 service.getUserInfo(function (res, success) { if (success) { console.log('獲取用戶信息成功, 加密數(shù)據(jù)為:' + res.encryptedData); console.log('獲取用戶信息成功, 加密向量為:' + res.iv); //緩存敏感的用戶信息,解密向量 wx.setStorageSync('encryptedData', res.encryptedData); wx.setStorageSync('iv', res.iv); that.globalData.userInfo = res.userInfo; //解密數(shù)據(jù) service.getDecryptionData(function (res, success) { if (success) { console.log("解密數(shù)據(jù)成功"); console.log(res.data.data); } else { console.log('解密數(shù)據(jù)失敗'); } }) } else { console.log('獲取用戶信息失敗') } }); } else { console.log('通過(guò) code 獲取第三方服務(wù)器 session 失敗'); } }); } else { console.log('登錄失敗:'); } } }) } }, globalData: { userInfo: null } } App(appConfig)
運(yùn)行輸出
希望本文所述對(duì)大家微信小程序開(kāi)發(fā)有所幫助。