【小程序+ thinkphp5】 獲取微信運動數據

配置。請參看上篇文章。這裏直接上代碼php

 

PHP 代碼:json

//獲取微信運動數據:
        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 接口不會彈窗詢問 } }) }
相關文章
相關標籤/搜索