当前位置:   article > 正文

微信运动接口(获取自己的微信运动数据)_wx.getwerundata

wx.getwerundata

@toc

1、微信运动接口wx.getWeRunData()

1.1 实现步骤

  该接口获取用户过去30天微信运动步数。该接口比较复杂,具体操作步骤如下:

  • 先调用wx.login()接口获取code

  • 使用wx.request()接口向地址https://api.weixin.qq.com/sns/jscode2session?appid= appid &secret= secret &js_code= res.code &grant_type=authorization_code发送网络请求,此时需要发送appid、secret和code3个参数,返回session_key。

  • 调用wx.getWeRunData()接口返回encryptedData和iv,其中encryptedData是加密的步数信息,需要解密为明文。

  • 在小程序中使用CryptoJS组件对encryptedData进行解密,此时需要用到appid、session_key和iv,解密得到明文的步数信息是小程序的可读信息。

1.2 接口参数

属性类型默认值必填说明
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)

object.success回调函数:

属性类型说明最低版本
encryptedDatastring包括敏感数据在内的完整用户信息的加密数据,详细见加密数据解密算法。解密后得到的数据结构见后文
ivstring加密算法的初始向量,详细见加密数据解密算法
cloudIDstring敏感数据对应的云 ID,开通云开发的小程序才会返回,可通过云调用直接获取开放数据,详细见云调用直接获取开放数据2.7.0

官网示例代码:

wx.getWeRunData({
  success (res) {
    // 拿 encryptedData 到开发者后台解密开放数据
    const encryptedData = res.encryptedData
    // 或拿 cloudID 通过云调用直接获取开放数据
    const cloudID = res.cloudID
  }
})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

2、获取运动数据案例

  本例使用wx.getWeRunData()接口获取用户的运动步数并显示。解密的CryptoJS组件可以在https://www.npm.js.com/package/crypto-js下载得到。如下图圈起来的所示:

image-20220322195752366

  getWeRunData.wxml:

<view wx:for="{{stepInfoList}}">{{30-index}}天前你运动了{{item.step}}步</view>
  • 1

  getWeRunData.js:

var DataCrypt = require('../../utils/DataCrypt.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    stepInfoList:[]
  },


  onLoad: function (options) { 
    var that = this;
     wx.login({ 
       success: function (res) { 
         var appid = "你的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("seccion_key:"+session_key); 
               that.getWeRunData(appid, session_key); } 
                   }) 
                  }
             } 
             }) 
   }, 
                   //获取encryptedData(没有解密的步数)和iv(加密算法的初始向量) 
  getWeRunData: function (appid,session_key) {
      var that=this
      wx.getSetting({ success: function (res) {
         console.log(res); 
         if (!res.authSetting['scope.werun']) 
         {
           wx.authorize({
             scope: 'scope.werun',
             success:()=>{
               console.log("授权测试成功!!!")
             }
           })
            wx.getWeRunData({ 
                 success: function (res) {
                    console.log(res); 
                    var encryptedData = res.encryptedData; 
                    var iv = res.iv;
                    var pc = new DataCrypt(appid, session_key); 
                    console.log(pc); 
                    var data = pc.decryptData(encryptedData, iv) 
                   console.log("------" + data.stepInfoList[30].step)
                 that.setData({
                   stepInfoList: data.stepInfoList})
                 },
                fail: function (res) { console.log("获取数据失败") }
            })
          //  wx.showModal
          // ({ 
          //    title: '权限提示', 
          //    content: '获取微信运动步数需要开启计步权限', 
          //    success: function (res) 
          //     { 
          //      if (res.confirm) { 
          //        //跳转去设置 
          //        wx.openSetting({ 
          //          success: function (res) 
          //          {
          //            console.log("authSetting:"+res.authSetting)
          //          }
          //      })
          //      } 
          //     }
          // })
        } else {
               wx.getWeRunData({ 
                 success: function (res) {
                    console.log(res); 
                    var encryptedData = res.encryptedData; 
                    var iv = res.iv;
                    var pc = new DataCrypt(appid, session_key); 
                    console.log(pc); 
                    var data = pc.decryptData(encryptedData, iv) 
                   console.log("------" + data.stepInfoList[30].step)
                 that.setData({
                   stepInfoList: data.stepInfoList})
                 },
          fail: function (res) { console.log("获取数据失败") }
        })
      }
    }
    })
  },





  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148

   这里我并没有给出我的appid和secret,敏感数据不要给别人看,你去你小程序后台用自己的。

   本例中CryptoJS组件属于网络开源项目,下载之后引入到小程序项目中,DataCrypt.js文件的作用是吧CryptoJS组件引入到小程序项目。wx.getWeRunData()接口获取的encryptedData经过CryptoJS组件解密之后得到的明文数据data中包含了微信运动数据,具体的数据在data.stepInfoList数组中,该数组共31条记录,对应当日和前30天的运动步数数据。其中,stepInfoList[0]是30天前的运动步数数据,setpInfoList[30]是当日的运动步数数据。

2.1 真机调试

image-20220322200347839

  点击允许之后,可以看到我们的微信运动数据就出来了。

image-20220322200405912

  验证下今天的数据是否正确,查看下自己的微信运动数据:

image-20220322200601678

可以看到,今天的数据和接口获取到的第0天的数据都是14972步,演示完毕。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/308188
推荐阅读
相关标签
  

闽ICP备14008679号