微信小程序——获取微信步数

官方文档:https://developers.weixin.qq.com/miniprogram/dev/api/open-api/werun/wx.getWeRunData.html

可以通过wx.getWeRunData获取用户过去三十天的微信运动步数。

前提

使用这个接口有两个前提:

登录部分需要通过登录拿到token,在之后的请求头部带上token信息,以便在后端知道是哪个用户。

wx.getWeRunData

wx.getWeRunData成功回调的参数res有两个属性encryptedDataiv,需要传到后端进行解密。

解密后的Json结构如下:

  • timestap:时间戳,表示数据所对应的时间
  • step:当天的微信步数
{
  "stepInfoList": [
    {
      "timestamp": 1445866601,
      "step": 100
    },
    {
      "timestamp": 1445876601,
      "step": 120
    }
  ]
}

后端可以进行处理,得到类似于下图的数据,再将需要展示的数据返回给前端。

在这里插入图片描述

完整代码示例如下:

const app = getApp()

Page({
  onLoad: function () {
    const self = this;
    wx.login({
      success: function (res) {
        if (res.code) {
          wx.request({
            url: "https://www.xxx.com.cn/api/auth/login",
            method: 'POST',
            data: {
              code: res.code
            },
            success: function (res) {
              app.globalData.userInfo.token = res.data.token;
              app.globalData.userInfo.session_key = res.data.session_key;
              self.getWeRunData();
            }
          })
        }
      }
    });
  },
  getWeRunData: function(){
    wx.getSetting({
      success: function (res) {
        if(res.authSetting['scope.werun']===false){
          wx.showModal({
            title: '提示',
            content: '请开启获取微信步数权限',
            showCancel: false,
            confirmText: '知道了'
          })
        }else{
          wx.authorize({
            scope: 'scope.werun',
            success () {
              wx.getWeRunData({
                success: function (res) {
                  wx.request({
                    url: "https://www.xxx.com.cn/api/getWeRunData",
                    method: 'POST',
                    header: {
                      "accept": "application/json",
                      "Authorization": app.globalData.userInfo.token
                    },
                    data: {
                      encryptedData: res.encryptedData,
                      iv: res.iv,
                      session_key: app.globalData.userInfo.session_key
                    },
                    success: function (res) {
                      console.log("步数:" + res.data.data.steps);
                      wx.showModal({
                        title: '步数',
                        content: res.data.data.steps + '',
                      })
                    }
                  })
                },
                fail: function (res) {
                  wx.showModal({
                    title: '提示',
                    content: '请先关注“微信运动”公众号并设置数据来源,以获取并提供微信步数数据',
                    showCancel: false,
                    confirmText: '知道了'
                  })
                }
              })
            },
            fail(){
              wx.showModal({
                title: '提示',
                content: '请开启获取微信步数权限',
                showCancel: false,
                confirmText: '知道了'
              })
            }
          })
        }
      }
    })
  }
})
  • 4
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
微信小程序是一款非常流行的移动应用程序,它提供了许多功能和API供开发者使用。获取微信运动步数是其中的一个功能,以下是一个使用Java编写的微信小程序获取微信运动步数的实例代码。 ```java import java.net.URL; import java.net.HttpURLConnection; import java.io.InputStream; import java.io.InputStreamReader; import java.io.BufferedReader; public class WeChatMiniProgram { public static void main(String[] args) { // 小程序的AppID String appId = "your_app_id"; // 登录凭证code String code = "your_code"; // 小程序的AppSecret String appSecret = "your_app_secret"; // 获取access_token的URL String accessTokenUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appId + "&secret=" + appSecret + "&grant_type=authorization_code&js_code=" + code; try { // 发送HTTP请求获取access_token URL url = new URL(accessTokenUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.connect(); // 读取返回的数据 InputStream inputStream = connection.getInputStream(); InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8"); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); StringBuilder response = new StringBuilder(); String line; while ((line = bufferedReader.readLine()) != null) { response.append(line); } // 解析返回的数据 String jsonString = response.toString(); // 提取access_token和openid String accessToken = // 从jsonString中提取access_token String openId = // 从jsonString中提取openId // 获取微信运动步数的URL String stepCountUrl = "https://api.weixin.qq.com/sns/more_info?access_token=" + accessToken + "&openid=" + openId; // 发送HTTP请求获取微信运动步数 URL stepUrl = new URL(stepCountUrl); HttpURLConnection stepConnection = (HttpURLConnection) stepUrl.openConnection(); stepConnection.setRequestMethod("GET"); stepConnection.connect(); // 读取返回的数据 InputStream stepInputStream = stepConnection.getInputStream(); InputStreamReader stepInputStreamReader = new InputStreamReader(stepInputStream, "UTF-8"); BufferedReader stepBufferedReader = new BufferedReader(stepInputStreamReader); StringBuilder stepResponse = new StringBuilder(); String stepLine; while ((stepLine = stepBufferedReader.readLine()) != null) { stepResponse.append(stepLine); } // 解析返回的数据 String stepJsonString = stepResponse.toString(); // 提取微信运动步数 int stepCount = // 从stepJsonString中提取微信运动步数 // 输出微信运动步数 System.out.println("微信运动步数:" + stepCount); } catch (Exception e) { e.printStackTrace(); } } } ``` 以上代码中,通过发送HTTP请求获取微信小程序的`access_token`和`openId`,然后使用`access_token`和`openId`来发送第二个HTTP请求,获取微信运动步数。需要注意的是,你需要将代码中的`your_app_id`、`your_code`和`your_app_secret`替换为你自己的实际值。此外,代码中的JSON解析部分需要进行具体的实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值