E企盈小程序营销工具技术服务商 热线:4006-838-530

小程序获取微信运动步数

E企盈小程序直播系统

第一步,获取session_key,用来解密必须用到的代码为:wx.login({    success: function (res) {      var appid = “wx1b4e5e75***48af1”;      var secret = “8eb5b7c6af68****af2995b6a8758”;      if (res.code) {        wx.request({          url: ‘https://api.weixin.qq.com/sns/jscode2session?appid=’ + appid + ‘&secret=’ + secret + ‘&js_code=’ + res.code + ‘&grant_type=authorization_code’,          header: {            ‘content-type’: ‘json’          },          success: function (res) {            var session_key = res.data.session_key;            console.log(session_key);            that.getData(appid, session_key);          }        })      }    }  }) 注意:这里要注意的是,在体验版中,使用体验版的appid和secret,微信小程序端是可以获取到seesion_key和openid的,而在上线的正式版本中,使用正式的appid和secret,则会出现错误。第二步,获取encryptedData和iv,就是一些,微信步数加密后的数据代码为:wx.getWeRunData({          success: function (res) {            console.log(res);            console.log(“appid:” + appid + “session_key:” + session_key + “encryptedData:” + res.encryptedData + “iv:” + res.iv);            var encryptedData = res.encryptedData;            var iv = res.iv;            //使用解密工具,链接地址:            //https://codeload.github.com/gwjjeff/cryptojs/zip/master            var pc = new WXBizDataCrypt(appid, session_key);            console.log(pc);            var data = pc.decryptData(encryptedData, iv)            console.log(data)          },          fail: function (res) {            wx.showModal({              title: ‘提示’,              content: ‘开发者未开通微信运动,请关注“微信运动”公众号后重试’,              showCancel: false,              confirmText: ‘知道了’            })          }        }) 第三步,根据appid,session_key,iv来解密encryptedData,则会获取到解密之后的数据https://codeload.github.com/gwjjeff/cryptojs/zip/master使用CryptoJS解决微信小程序用户信息解密[https://codeload.github.com/gwjjeff/cryptojs/zip/master]详细 JS代码为:/**  * 生命周期函数–监听页面加载  */ onLoad: function (options) {   var that = this;   wx.login({     success: function(res){       var appid = “”;       var secret = “”;       if(res.code){         wx.request({           url: ‘https://api.weixin.qq.com/sns/jscode2session?appid=’ + appid + ‘&secret=’ + secret + ‘&js_code=’ + res.code + ‘&grant_type=authorization_code’,           header: {             ‘content-type’: ‘json’           },           success: function(res){             var session_key = res.data.session_key;             console.log(session_key);             that.getData(appid,session_key);           }         })       }     }   })   },  //获取encryptedData(没有解密的步数)和iv(加密算法的初始向量) getData: function (appid,session_key) {   wx.getSetting({     success: function (res) {       console.log(res);       if (!res.authSetting[‘scope.werun’]) {         wx.showModal({           title: ‘提示’,           content: ‘获取微信运动步数,需要开启计步权限’,           success: function (res) {             if (res.confirm) {               //跳转去设置               wx.openSetting({                 success: function (res) {                 }               })             } else {               //不设置             }           }         })       } else {         wx.getWeRunData({           success: function (res) {             console.log(res);             console.log(“appid:” + appid + “session_key:” + session_key + “encryptedData:” + res.encryptedData + “iv:” + res.iv);             var encryptedData = res.encryptedData;             var iv = res.iv;             //使用解密工具,链接地址:             //https://codeload.github.com/gwjjeff/cryptojs/zip/master             var pc = new WXBizDataCrypt(appid, session_key);             console.log(pc);             var data = pc.decryptData(encryptedData, iv)             console.log(data)           },           fail: function (res) {             wx.showModal({               title: ‘提示’,               content: ‘开发者未开通微信运动,请关注“微信运动”公众号后重试’,               showCancel: false,               confirmText: ‘知道了’             })           }         })       }     }   }) }, 结果为:

赞(0) 打赏
未经允许不得转载:E企盈小程序开发-热线:4006-838-530 » 小程序获取微信运动步数
分享到: 更多 (0)
E企盈小程序直播营销卖货系统

评论 抢沙发

E企盈小程序开发

联系我们联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏