当前位置:   article > 正文

微信小程序开发-获取微信运动步数_go 通过miniprogram包获取微信运动数据

go 通过miniprogram包获取微信运动数据

                                                        微信小程序开发-获取微信运动步数

思路:小程序传参数code,encryptedData,iv给后台,后台把encryptedData数据解密即可。

步骤:
1、根据 appid,secret,code 获取到 session_key
API地址为:https://developers.weixin.qq.com/miniprogram/dev/dev_wxwork/dev-doc/qywx-api/login/code2session.html

2、获取微信运动加密数据(如果开通了微信运动权限的话) encryptedData,iv
API地址为:https://developers.weixin.qq.com/miniprogram/dev/api/open-api/werun/wx.getWeRunData.html

3、根据参数 appid,session_key,encryptedData,iv 解密微信运动数据
API地址为:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/signature.html
注:这个地址有多种编程语言解密encryptedData数据的示例代码,

地址为:https://res.wx.qq.com/wxdoc/dist/assets/media/aes-sample.eae1f364.zip
如下图所示

4、PHP代码

  1. public function getstepAction(){
  2. $post = $_POST; //POST方式传,GET传:会把参数中的+号解析成空格,会有这个问题。
  3. $code = isset( $post['code'] ) ? $post['code'] : '';
  4. $encryptedData= isset( $post['encryptedData'] ) ? $post['encryptedData'] : '';;
  5. $iv = isset( $post['iv'] ) ? $post['iv'] : '';;
  6. if( !$code ) die('缺少code参数!');
  7. //根据参数获取session_key
  8. $appid = '已知';
  9. $appsecret = '已知';
  10. $weixin = $this->curl_get_https( "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$appsecret&js_code=$code&grant_type=authorization_code" );
  11. //$weixin = file_get_contents("https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$appsecret&js_code=$code&grant_type=authorization_code");
  12. $jsondecode = json_decode($weixin, true); //json字符串转为数组
  13. //var_dump($array);die;
  14. //$openid = isset( $array['openid'] ) ? $array['openid'] : ''; //获取openid
  15. //$unionid = isset( $array['unionid'] ) ? $array['unionid'] : '';//获取
  16. $session_key = isset( $array['session_key'] ) ? $array['session_key'] : '';
  17. //if( ! $openid ) die('openid获取失败!');
  18. //根据session_key,encryptedData,iv 解析字符串,获取步数信息
  19. if($encryptedData && $iv){
  20. require_once APP_PATH."/library/Wx/wxBizDataCrypt.php"; //引入解密类,注意大小写
  21. $pc = new WXBizDataCrypt($appid, $session_key);
  22. $errCode = $pc->decryptData( $encryptedData, $iv, $stepData );
  23. if ($errCode == 0) { //解析成功,更新数据
  24. $stepData = json_decode($stepData, true); //最近30天步数信息
  25. $stepData = $stepData['stepInfoList'];
  26. $stepData = end($stepData);
  27. $step = $stepData['step']; //拿到了最近一天的步数,看你怎么处理了
  28. echo "最近一天的步数为 [ {$step} ]步";
  29. }else {
  30. print($errCode . "\n");
  31. }
  32. }
  33. die();
  34. }
  35. /**
  36. *
  37. * @todo GET方式调用接口,返回json格式数据
  38. * @param unknown $url
  39. * 请求路径
  40. * @return unknown json格式数据
  41. */
  42. function curl_get_https($url, $post_data = []) {
  43. $curl = curl_init (); // 启动一个CURL会话
  44. curl_setopt ( $curl, CURLOPT_URL, $url );
  45. curl_setopt ( $curl, CURLOPT_HEADER, 0 );
  46. curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
  47. curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, false ); // 跳过证书检查
  48. if ($post_data) {
  49. // 设置post方式提交
  50. curl_setopt ( $curl, CURLOPT_POST, 1 );
  51. // 设置post数据
  52. curl_setopt ( $curl, CURLOPT_POSTFIELDS, $post_data );
  53. }
  54. // curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true); // 从证书中检查SSL加密算法是否存在
  55. $tmpInfo = curl_exec ( $curl ); // 返回api的json对象
  56. curl_close ( $curl ); // 关闭URL请求
  57. return $tmpInfo; // 返回json对象
  58. }

5、返回结果 $stepData :用户过去三十天微信运动步数

  1. {
  2. "stepInfoList": [
  3. {
  4. "timestamp": 1557158400,
  5. "step": 0
  6. },
  7. {
  8. "timestamp": 1557244800,
  9. "step": 0
  10. },
  11. {
  12. "timestamp": 1557331200,
  13. "step": 0
  14. },
  15. {
  16. "timestamp": 1557417600,
  17. "step": 0
  18. },
  19. {
  20. "timestamp": 1557504000,
  21. "step": 0
  22. },
  23. {
  24. "timestamp": 1557590400,
  25. "step": 0
  26. },
  27. {
  28. "timestamp": 1557676800,
  29. "step": 0
  30. },
  31. {
  32. "timestamp": 1557763200,
  33. "step": 0
  34. },
  35. {
  36. "timestamp": 1557849600,
  37. "step": 0
  38. },
  39. {
  40. "timestamp": 1557936000,
  41. "step": 0
  42. },
  43. {
  44. "timestamp": 1558022400,
  45. "step": 0
  46. },
  47. {
  48. "timestamp": 1558108800,
  49. "step": 0
  50. },
  51. {
  52. "timestamp": 1558195200,
  53. "step": 0
  54. },
  55. {
  56. "timestamp": 1558281600,
  57. "step": 0
  58. },
  59. {
  60. "timestamp": 1558368000,
  61. "step": 0
  62. },
  63. {
  64. "timestamp": 1558454400,
  65. "step": 0
  66. },
  67. {
  68. "timestamp": 1558540800,
  69. "step": 0
  70. },
  71. {
  72. "timestamp": 1558627200,
  73. "step": 0
  74. },
  75. {
  76. "timestamp": 1558713600,
  77. "step": 0
  78. },
  79. {
  80. "timestamp": 1558800000,
  81. "step": 0
  82. },
  83. {
  84. "timestamp": 1558886400,
  85. "step": 0
  86. },
  87. {
  88. "timestamp": 1558972800,
  89. "step": 0
  90. },
  91. {
  92. "timestamp": 1559059200,
  93. "step": 0
  94. },
  95. {
  96. "timestamp": 1559145600,
  97. "step": 0
  98. },
  99. {
  100. "timestamp": 1559232000,
  101. "step": 0
  102. },
  103. {
  104. "timestamp": 1559318400,
  105. "step": 0
  106. },
  107. {
  108. "timestamp": 1559404800,
  109. "step": 0
  110. },
  111. {
  112. "timestamp": 1559491200,
  113. "step": 0
  114. },
  115. {
  116. "timestamp": 1559577600,
  117. "step": 0
  118. },
  119. {
  120. "timestamp": 1559664000,
  121. "step": 0
  122. },
  123. {
  124. "timestamp": 1559750400,
  125. "step": 2010
  126. }
  127. ],
  128. "watermark": {
  129. "timestamp": 1559788371,
  130. "appid": "wxd************"
  131. }
  132. }

 

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

闽ICP备14008679号