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

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

思路:小程序传参数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代码

public function getstepAction(){
	$post = $_POST;	//POST方式传,GET传:会把参数中的+号解析成空格,会有这个问题。
	$code = isset( $post['code'] ) ? $post['code'] : '';
	$encryptedData= isset( $post['encryptedData'] ) ? $post['encryptedData'] : '';;
	$iv = isset( $post['iv'] ) ? $post['iv'] : '';;
	if( !$code )	die('缺少code参数!');
	
	//根据参数获取session_key
	$appid = '已知';
	$appsecret = '已知';
	$weixin = $this->curl_get_https( "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$appsecret&js_code=$code&grant_type=authorization_code" );
	//$weixin = file_get_contents("https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$appsecret&js_code=$code&grant_type=authorization_code");

	$jsondecode = json_decode($weixin, true); //json字符串转为数组
	//var_dump($array);die;
	//$openid = isset( $array['openid'] ) ? $array['openid'] : '';	//获取openid
	//$unionid = isset( $array['unionid'] ) ? $array['unionid'] : '';//获取
	$session_key = isset( $array['session_key'] ) ? $array['session_key'] : '';
	//if( ! $openid )	die('openid获取失败!');
	
	//根据session_key,encryptedData,iv 解析字符串,获取步数信息
	if($encryptedData && $iv){
		require_once APP_PATH."/library/Wx/wxBizDataCrypt.php";	//引入解密类,注意大小写
		$pc = new WXBizDataCrypt($appid, $session_key);
		$errCode = $pc->decryptData( $encryptedData, $iv, $stepData );
		if ($errCode == 0) {	//解析成功,更新数据
			$stepData = json_decode($stepData, true);	//最近30天步数信息
			$stepData = $stepData['stepInfoList'];
			$stepData = end($stepData);
			$step = $stepData['step'];	//拿到了最近一天的步数,看你怎么处理了
			echo "最近一天的步数为 [ {$step} ]步";
		}else {
			print($errCode . "\n");
		}
	}
	die();
}

/**
 *
 * @todo GET方式调用接口,返回json格式数据
 * @param unknown $url
 *        	请求路径
 * @return unknown json格式数据
 */
function curl_get_https($url, $post_data = []) {
	$curl = curl_init (); // 启动一个CURL会话

	curl_setopt ( $curl, CURLOPT_URL, $url );
	curl_setopt ( $curl, CURLOPT_HEADER, 0 );
	curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
	curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, false ); // 跳过证书检查

	if ($post_data) {
		// 设置post方式提交
		curl_setopt ( $curl, CURLOPT_POST, 1 );
		// 设置post数据

		curl_setopt ( $curl, CURLOPT_POSTFIELDS, $post_data );
	}
	// curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true); // 从证书中检查SSL加密算法是否存在
	$tmpInfo = curl_exec ( $curl ); // 返回api的json对象
	curl_close ( $curl ); // 关闭URL请求

	return $tmpInfo; // 返回json对象
}

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

{
    "stepInfoList": [
        {
            "timestamp": 1557158400,
            "step": 0
        },
        {
            "timestamp": 1557244800,
            "step": 0
        },
        {
            "timestamp": 1557331200,
            "step": 0
        },
        {
            "timestamp": 1557417600,
            "step": 0
        },
        {
            "timestamp": 1557504000,
            "step": 0
        },
        {
            "timestamp": 1557590400,
            "step": 0
        },
        {
            "timestamp": 1557676800,
            "step": 0
        },
        {
            "timestamp": 1557763200,
            "step": 0
        },
        {
            "timestamp": 1557849600,
            "step": 0
        },
        {
            "timestamp": 1557936000,
            "step": 0
        },
        {
            "timestamp": 1558022400,
            "step": 0
        },
        {
            "timestamp": 1558108800,
            "step": 0
        },
        {
            "timestamp": 1558195200,
            "step": 0
        },
        {
            "timestamp": 1558281600,
            "step": 0
        },
        {
            "timestamp": 1558368000,
            "step": 0
        },
        {
            "timestamp": 1558454400,
            "step": 0
        },
        {
            "timestamp": 1558540800,
            "step": 0
        },
        {
            "timestamp": 1558627200,
            "step": 0
        },
        {
            "timestamp": 1558713600,
            "step": 0
        },
        {
            "timestamp": 1558800000,
            "step": 0
        },
        {
            "timestamp": 1558886400,
            "step": 0
        },
        {
            "timestamp": 1558972800,
            "step": 0
        },
        {
            "timestamp": 1559059200,
            "step": 0
        },
        {
            "timestamp": 1559145600,
            "step": 0
        },
        {
            "timestamp": 1559232000,
            "step": 0
        },
        {
            "timestamp": 1559318400,
            "step": 0
        },
        {
            "timestamp": 1559404800,
            "step": 0
        },
        {
            "timestamp": 1559491200,
            "step": 0
        },
        {
            "timestamp": 1559577600,
            "step": 0
        },
        {
            "timestamp": 1559664000,
            "step": 0
        },
        {
            "timestamp": 1559750400,
            "step": 2010
        }
    ],
    "watermark": {
        "timestamp": 1559788371,
        "appid": "wxd************"
    }
}

 

  • 6
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
微信小程序是一款非常流行的移动应用程序,它提供了许多功能和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解析部分需要进行具体的实现。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值