[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 }); }
前台index.js
//index.js var app = getApp() Page({ data: { userInfo: {}, }, onLoad: function () { var that = this app.getUserInfo(function (userInfo) { //更新数据 that.setData({ userInfo: userInfo }) }) } })
前台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('通过 code 获取第三方服务器 session 成功, session 为:' + res.data.session); //缓存起来 wx.setStorageSync('thirdSessionId', res.data.session); //wx api 获取用户信息 service.getUserInfo(function (res, success) { if (success) { console.log('获取用户信息成功, 加密数据为:' + res.encryptedData); console.log('获取用户信息成功, 加密向量为:' + res.iv); //缓存敏感的用户信息,解密向量 wx.setStorageSync('encryptedData', res.encryptedData); wx.setStorageSync('iv', res.iv); that.globalData.userInfo = res.userInfo; //解密数据 service.getDecryptionData(function (res, success) { if (success) { console.log("解密数据成功"); console.log(res.data.data); } else { console.log('解密数据失败'); } }) } else { console.log('获取用户信息失败') } }); } else { console.log('通过 code 获取第三方服务器 session 失败'); } }); } else { console.log('登录失败:'); } } }) } }, globalData: { userInfo: null } } App(appConfig)
运行输出