【小程序+ thinkphp5】 获取微信运动数据
配置。请参看上篇文章。这里直接上代码
PHP 代码:
//获取微信运动数据: public function test(){ $code = input("code"); $signature = input("signature"); //数据签名 $rawDate = $_GET['rawData']; //记住不应该用TP中的input方法,会过滤掉必要的数据 $encryptedData = $_GET['encryptedData']; //微信运动数据 $iv = $_GET['iv']; //初始向量 $nickname = input("nickname"); //用户昵称 $avatarurl = input("avatarurl"); //用户头像 if(empty($code) || empty($signature) || empty($rawDate) ||empty($encryptedData) || empty($iv) || empty($nickname) || empty($avatarurl) ){ return json(['code'=>'2','message'=>"参数错误","result"=>null]); } //开发者使用登陆凭证 code 获取 session_key 和 openid $APPID = config::get("config.wx_appid"); $AppSecret = config::get("config.wx_appsecret"); $wx_request_url = config::get("wechat.wx_request_url"); $param = array( 'appid' => $APPID, 'secret' => $AppSecret, 'js_code' => $code, 'grant_type' => 'authorization_code' ); // 一个使用curl实现的get方法请求 $arr = http_send($wx_request_url, $param, 'post'); $arr = json_decode($arr,true); if(isset($arr['errcode']) && !empty($arr['errcode'])){ return json(['code'=>'2','message'=>$arr['errmsg'],"result"=>null]); } $openid = $arr['openid']; $session_key = $arr['session_key']; // 数据签名校验 $signature2 = sha1($rawDate.$session_key); if ($signature != $signature2) { $msg = "shibai 1"; return json(['code'=>'2','message'=>'获取失败',"result"=>$msg]); } //开发者如需要获取敏感数据,需要对接口返回的加密数据( encryptedData )进行对称解密 include_once (EXTEND_PATH. 'Wxdev/wxBizDataCrypt.php'); $pc = new \WXBizDataCrypt($APPID, $session_key); $errCode = $pc->decryptData($encryptedData, $iv, $data); //其中$data包含用户的所有数据 if ($errCode != 0) { return json(['code'=>'2','message'=>'获取失败',"result"=>null]); } if(empty($data)){ return json(['code'=>'2','message'=>'暂无数据',"result"=>null]); } $data = json_decode($data,true); $list = $data["stepInfoList"]; foreach ($list as $key => $value) { $list[$key]['time'] = date("Y-m-d H:i:s",$value['timestamp']); } return json(['code'=>'1','message'=>'获取成功',"result"=>$list]); }
小程序代码:
//获取微信运动
wx.login({
success: loginres=>{
let code = loginres.code;
if(code){
wx.getWeRunData({
success: runres => {
let encryptedData = runres.encryptedData
let iv = runres.iv
wx.getUserInfo({
success: infores => {
let userinfo = infores.userInfo
let signature = infores.signature;
let rawData = infores.rawData;
wx.request({
url: 'https://www.test.com/wechat/index/test',
data: {
"code": code,
"signature": signature,
"rawData": rawData,
"nickname": userinfo.nickName,
"avatarurl": userinfo.avatarUrl,
'iv': iv,
'encryptedData': encryptedData
},
header:{
"content-type" : "application/x-www-form-urlencoded"
},
success: function (info) {
console.log(info);
},
fail:function(res){
console.log(res);
},
complete:function(res){
console.log(res);
}
})//request结束
}
}) //userinfo结束
}
})//微信运动结束
}else{
that.alert("获取用户登陆失败" + loginres.errMsg)
}
}
})
授权代码:
// 获取用户信息
wx.getSetting({
success: res => {
var code = res.code; //获取code
//访问用户权限
if (res.authSetting['scope.userInfo']) {
// 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
}
//访问微信运动
if (!res.authSetting['scope.werun']) {
wx.authorize({
scope: 'scope.werun',
success() {
// 用户已经同意小程序使用微信运动,后续调用 wx.getWeRunData 接口不会弹窗询问
}
})
}
欢迎做技术的朋友一起沟通交流!伸手党请绕道,
代码改变世界QQ群:453011886