第一步,获取session_key,用来解密必须用到的

第二步,获取encryptedData和iv,就是一些,微信步数加密后的数据

第三步,根据appid,session_key,iv来解密encryptedData,则会获取到解密之后的数据

实战:

首先去这里下载官方提供的demo
https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/demo/aes-sample.zip

解压后把 errorCode.php 和 wxBizDataCrypt.php 放到 服务器目录里。(网上大部分教程没有说这个,这两个文件要跟你解密php文件放一个目录)

开始写test.php后台解密文件

<?php 
include_once "wxBizDataCrypt.php";

$appid = '你的appid';
$appsecret = '你的appsecret';
$encryptedData = $_GET['encryptedData'];
$iv = $_GET['iv'];


$url = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$appid.'&secret='.$appsecret.'&js_code='.$_GET['code'].'&grant_type=authorization_code';

$content = file_get_contents($url);
$content = json_decode($content);

$sessionKey = $content->session_key;
$openId = $content->openid;


$pc = new WXBizDataCrypt($appid, $sessionKey);
$errCode = $pc->decryptData($encryptedData, $iv, $sp ); 

$sp = json_decode($sp); //sp就是step,步数的意思

$raw_data = array('openid' => $openId, 'sp' => $sp);
$res_data = json_encode($raw_data);

header('Content-Type:application/json');

if ($errCode == 0) {
    echo ($res_data);

} else {
print($errCode . "\n");  //同时返回 openid 和 步数回去
} 
?>

小程序 index.js

Page({
  data: {
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    openId:0,
    stepList:{},
    syncText:'',
    startDate:'',
    finalDate:'',
    totalsteps:'',
    totalSteps_hidden:0,
    syncText_hidden:0,
    syncData_hidden:0,
    notice_hidden:0,
    noticeText:"",

  },

get3rdSession: function () {
    let that = this
    wx.request({
      url: 'https://www.tuziang.com/test.php',
      data: {
        encryptedData: that.data.encryptedData,
        iv: that.data.iv,
        code: that.data.code,
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      success: function (res) {

        // 更新openId的值
        that.setData({
          openId: res.data.openid
        }); 
        

        //输出今天步数
        
        let stepList = res.data.sp.stepInfoList
        
        //console.log(stepList)
        that.setData({
          stepList: stepList,
        }); 

        console.log(stepList)
      },         
    }) 
  },

 onLoad: function () {
    let that = this


    wx.login({
      success: function (res) {
        let code = res.code
        that.setData({ code: code })

            wx.getWeRunData({//解密微信运动
              success(res) {
                const wRunEncryptedData = res.encryptedData
                that.setData({
                  encryptedData: wRunEncryptedData
                })
                that.setData({ iv: res.iv })
                that.get3rdSession()//解密请求函数 
              },
              
              fail: function (res) {
                if (res.errMsg == 'getWeRunData:fail auth deny'){
                  wx.showModal({
                    title: '提示',
                    content: '获取微信运动步数,需要开启计步权限',
                    success: function (res) {
                      if (res.confirm) {
                        //跳转去设置
                        wx.openSetting({
                          success: function (res) {
                          }
                        })
                      } else {
                        //不设置
                      }
                    }
                  })
                }
                else {
                  console.log(res.errMsg)
                  wx.showModal({
                    title: '提示',
                    content: '未开通微信运动,请关注“微信运动”公众号后重试',
                    showCancel: false,
                    confirmText: '知道了'
                  })
                  }
              }
              
            })
      }
    })
  },

这里没有对stepList的时间戳转日期。 我是在后台php转换的。

PHP 小程序 时间戳 转日期 https://www.tuziang.com/combat/283.html

或者使用小程序支持的js
JavaScript 小程序 时间戳 转日期 https://www.tuziang.com/combat/281.html