diff --git a/app/common.php b/app/common.php index 19805c3..42ad207 100644 --- a/app/common.php +++ b/app/common.php @@ -62,6 +62,43 @@ function post(string $url, $data = [], bool $useCert = false, array $sslCert = [ return $result; } +/** + * 模拟POST请求 + * @param string $url 请求地址 + * @param mixed $data 请求数据 + * @param false $useCert 是否引入微信支付证书 + * @param array $sslCert 证书路径 + * @return mixed|bool|string + */ +function postToken(string $url, $data = [], bool $useCert = false, array $sslCert = [],string $token) +{ + $header = [ + 'Content-type: application/json;', + "access_token: {$token}" + ]; + $curl = curl_init(); + curl_setopt($curl, CURLOPT_URL, $url); + curl_setopt($curl, CURLOPT_HTTPHEADER, $header); + curl_setopt($curl, CURLOPT_HEADER, false); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($curl, CURLOPT_POST, TRUE); + curl_setopt($curl, CURLOPT_POSTFIELDS, $data); + if ($useCert == true) { + // 设置证书:cert 与 key 分别属于两个.pem文件 + curl_setopt($curl, CURLOPT_SSLCERTTYPE, 'PEM'); + curl_setopt($curl, CURLOPT_SSLCERT, $sslCert['certPem']); + curl_setopt($curl, CURLOPT_SSLKEYTYPE, 'PEM'); + curl_setopt($curl, CURLOPT_SSLKEY, $sslCert['keyPem']); + } + $result = curl_exec($curl); + if ($result === false) { + curl_error($curl); + } + curl_close($curl); + return $result; +} + /** * 模拟POST请求 [第二种方式,] * @param $url diff --git a/app/controller/Index.php b/app/controller/Index.php index b3ff201..8f9f2c5 100644 --- a/app/controller/Index.php +++ b/app/controller/Index.php @@ -16,14 +16,121 @@ class Index extends BaseController return 'hello,' . $name; } - public function mm(){ - $host = "https://192.168.66.13:443"; - $cid = "29940726"; - $cskey = "SkTCnQdFbNW4Z2suNj8P"; + public function getAccessToken(){ + $config = config('app'); // - $url = $host."/artemis/oauth/token?client_id=".$cid."&client_secret=".$cskey; + $url = $config['host_url']."/oauth/token?client_id=".$config['cid']."&client_secret=".$config['cskey']; // do post $ret = post($url); + return $ret; } + + /** + * 总览总数据接口 + * @return array + */ + public function allGroupNum() { + + $config = config('app'); + + $dayStatTime = date('c',strtotime(date("Y-m-d 00:00:00"))); + $monday = date('Y-m-d 00:00:00', strtotime("last Monday", strtotime(date('Y-m-d')))); + $weekStatTime = date('c',strtotime($monday)); + $monthStatTime = date('c',strtotime(date("Y-m-01 00:00:00"))); + $dateArr = ['day' => $dayStatTime, 'week' => $weekStatTime, 'month' => $monthStatTime]; + + $returnData = []; + foreach ($dateArr as $date => $statTime) { + + $url = $config['host_url'] . "/api/cfas/v2/passengerFlow/allGroup"; + + $granularity = $date == 'month' ? 'monthly' : 'daily'; + + $data = [ + 'granularity' => $granularity, + 'statTime' => $statTime + ]; + + $json_data = json_encode($data); + $result = postToken($url,$json_data,false,[],'60NDtZnTUiPtXEFC2MAszjZ9fbedfBTQ'); + $res_data = json_decode($result,true); + + if ($res_data['code'] != 0) { + return ['code' => 404, 'msg' => $res_data['msg']]; + } + + $list = $res_data['data']['list']; + + $returnData[$date]['flowInNum'] = 0; + foreach ($list as $value) { + $returnData[$date]['flowInNum'] += $value['flowInNum']; + } + } + + + return ['code' => 0, 'data' => $returnData]; + } + + /** + * 进馆人数趋势 + * @return array|void + */ + public function groups() { + + $config = config('app'); + + $dayStatTime = date('c',strtotime(date("Y-m-d 00:00:00"))); + $monday = date('Y-m-d 00:00:00', strtotime("last Monday", strtotime(date('Y-m-d')))); + $weekStatTime = date('c',strtotime($monday)); + $monthStatTime = date('c',strtotime(date("Y-m-01 00:00:00"))); + $dateArr = [ + 'day' => [ + 'startTime' => $dayStatTime,// 当天开始时间 + 'endTime' => date('c',strtotime(date("Y-m-d 23:59:59"))),// 当天结束时间 + ], + 'week' => [ + 'startTime' => $weekStatTime, + 'endTime' => date('c',strtotime(date("Y-m-d 23:59:59"))), + ], + 'month' =>[ + 'startTime' => $monthStatTime, + 'endTime' => date('c',strtotime(date("Y-m-t 23:59:59"))) + ] + ]; + + $returnData = []; + foreach ($dateArr as $date => $dateTime) { + + $url = $config['host_url'] . "/api/cfas/v2/passengerFlow/groups"; + + $granularity = $date == 'month' ? 'monthly' :'daily'; + + $data = [ + 'granularity' => $granularity, + 'startTime' => $dateTime['startTime'], + 'endTime' => $dateTime['endTime'], + 'ids' => '01' + ]; + + $json_data = json_encode($data); + $result = postToken($url,$json_data,false,[],'60NDtZnTUiPtXEFC2MAszjZ9fbedfBTQ'); + $res_data = json_decode($result,true); + if ($res_data['code'] != 0) { + return ['code' => 404, 'msg' => $res_data['msg']]; + } + + $list = $res_data['data']['list']; + + $returnData[$date]['flowInNum'] = 0; + foreach ($list as $value) { +// $createTime = date("H",$value['createTime'] /1000 + (3600*8)); + $returnData[$date]['flowInNum'] += $value['flowInNum']; + } + $returnData['sumNum'] += $returnData[$date]['flowInNum']; + } + + return $returnData; + } + } diff --git a/config/app.php b/config/app.php index 3dada4b..1169685 100644 --- a/config/app.php +++ b/config/app.php @@ -29,4 +29,9 @@ return [ 'error_message' => '页面错误!请稍后再试~', // 显示错误信息 'show_error_msg' => false, + // 调用网址地址 + 'host_url' => 'https://192.168.66.13:443/artemis', + 'port' => '443', + 'cid' => '29940726', + 'cskey' => 'SkTCnQdFbNW4Z2suNj8P', ]; diff --git a/config/database.php b/config/database.php index ba2ae8c..6382d2e 100644 --- a/config/database.php +++ b/config/database.php @@ -30,7 +30,7 @@ return [ // 用户名 'username' => env('database.username', 'root'), // 密码 - 'password' => env('database.password', ''), + 'password' => env('database.password', 'root'), // 端口 'hostport' => env('database.hostport', '3306'), // 数据库连接参数 diff --git a/route/app.php b/route/app.php index d8e09e3..260979d 100644 --- a/route/app.php +++ b/route/app.php @@ -15,3 +15,6 @@ Route::get('think', function () { }); Route::get('hello/:name', 'index/hello'); +Route::get('getAccessToken', 'index/getAccessToken'); +Route::get('allGroupNum', 'index/allGroupNum'); +Route::get('groups', 'index/groups');