通过云开发获取运动步数

阅读量: 199 编辑

通过云开发获取微信运动步数

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

云开发官方文档:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/signature.html#method-cloud

index.js

wx.getWeRunData({
  success: res => {
    wx.showLoading({
      title: '数据加载中',
    })
    const cloudID = res.cloudID
    wx.cloud.callFunction({
      name: "getWeRunData",
      data: {
        weRunData: wx.cloud.CloudID(cloudID)
      },
      success: runRes => {
        if (runRes.errMsg.includes('ok')) {
          //  console.log(runRes.result)
          let result = runRes.result.weRunData.data;
          this.initCalendar(result.stepInfoList)
        }
      },
      complete: res => {
        wx.hideLoading()
      }
    })
  }
})

cloudfunctions/quickstartFunctions/getWeRunData/index.js

const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

exports.main = async(event, context) =>{
  const wxContext = cloud.getWXContext()
  return {
    event,
    openid:wxContext.openid,
    appid:wxContext.appid,
    unionid:wxContext.unionid
  }
}

cloudfunctions/quickstartFunctions/index.js

const getWeRunData = require('./getWeRunData/index')

// 云函数入口函数
exports.main = async (event, context) => {
  switch (event.type) {
    case 'getWeRunData':
      return await getWeRunData.main(event, context)
  }
}

云开发平台中/云函数

  • 创建云函数名称:getWeRunData