Browse Source

接口数据取数据库

master
wanghongjun 3 years ago
parent
commit
93872e29c8
  1. 90
      app/command/passengerFlowAll.php
  2. 63
      app/command/passengerFlowReal.php
  3. 15
      app/controller/Index.php
  4. 1
      config/console.php

90
app/command/passengerFlowAll.php

@ -0,0 +1,90 @@
<?php
namespace app\command;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Db;
class passengerFlowAll extends Command
{
protected function configure()
{
$this->setName('passengerFlow')
->addArgument('name', Argument::OPTIONAL, "your name")
->addOption('city', null, Option::VALUE_REQUIRED, 'city name')
->setDescription('Say Hello');
}
protected function execute(Input $input, Output $output)
{
$config = config('app');
$dateData = getTargetDate();
$token = getAccessToken();
$dateArr = [
'day' => [
'startTime' => $dateData['day']['c_start_time'],// 当天开始时间
'endTime' => date("c"),// 当天结束时间
'granularity' => 'daily'
],
'week' => [
'startTime' => $dateData['week']['c_start_time'],
'endTime' => date("c"),
'granularity' => 'daily'
],
'month' =>[
'startTime' => $dateData['month']['c_start_time'],
'endTime' => date("c"),
'granularity' => 'monthly'
]
];
try {
foreach ($dateArr as $date => $value) {
$url = $config['host_url'] . "/api/cfas/v2/passengerFlow/groups";
$granularity = $value['granularity'];
$dataArr = [
'granularity' => $granularity,
'startTime' => $value['startTime'],
'endTime' => $value['endTime'],
'ids' => '01' // --- <replace> --- //
];
$json_data = json_encode($dataArr);
$result = postToken($url,$json_data,false,[],$token);
$res_data = json_decode($result,true);
if ($res_data['code'] != 0) throw new \Exception('请求失败');
$list = (array)$res_data['data']['list'];
foreach ($list as $val) {
$insert_arr = [
'groupId' => $val['groupId'],
'groupName' => $val['groupName'],
'flowInNum' => $val['flowInNum'],
'flowOutNum' => $val['flowOutNum'],
'noRepeatInNum' => $val['noRepeatInNum'],
'noRepeatOutNum' => $val['noRepeatOutNum'],
'holdValue' => $val['holdValue'],
'createTime' => $val['createTime'],
'updateTime' => $val['updateTime'],
'netValue' => $val['netValue'],
'statTime' => $val['statTime'],
'granularity' => $granularity,
'date' => $date,
];
Db::table('tp_passenger_flow_all')->insert($insert_arr);
}
}
$output->writeln('完成');
} catch (\Exception $e) {
$output->writeln($e->getMessage());
}
}
}

63
app/command/passengerFlowReal.php

@ -0,0 +1,63 @@
<?php
namespace app\command;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Db;
class passengerFlowReal extends Command
{
protected function configure()
{
$this->setName('passengerFlow')
->addArgument('name', Argument::OPTIONAL, "your name")
->addOption('city', null, Option::VALUE_REQUIRED, 'city name')
->setDescription('Say Hello');
}
protected function execute(Input $input, Output $output)
{
$config = config('app');
try{
$url = $config['host_url'] . "/api/cfas/v3/passenger/realTime";
$dataArr = [
'01' // --- <replace> --- //
];
$token = getAccessToken();
$json_data = json_encode($dataArr);
$result = postToken($url,$json_data,false,[],$token);
$res_data = json_decode($result,true);
$list = (array) $res_data['data'];
foreach ($list as $value) {
$insert_arr = [
'groupId' => $value['groupId'],
'regionId' => $value['regionId'],
'statisticsTime' => $value['statisticsTime'],
'enter' => $value['enter'],
'exit' => $value['exit'],
'pass' => $value['pass'],
'holdValue' => $value['holdValue'],
'allEnter' => $value['allEnter'],
'allExit' => $value['allExit']
];
Db::table('tp_passenger_flow_real')->insert($insert_arr);
}
$output->writeln('完成');
} catch (\Exception $e) {
$output->writeln($e->getMessage());
}
}
}

15
app/controller/Index.php

@ -190,23 +190,12 @@ class Index extends BaseController
* @return void
*/
public function realTimeData() {
$config = config('app');
$url = $config['host_url'] . "/api/cfas/v3/passenger/realTime";
$dataArr = [
'01' // --- <replace> --- //
];
$token = getAccessToken();
$json_data = json_encode($dataArr);
$result = postToken($url,$json_data,false,[],$token);
$res_data = json_decode($result,true);
if ($res_data['code'] != 0) return json_encode(['code' => 403, 'msg' => $res_data['msg']],JSON_UNESCAPED_UNICODE);
$list = Db::table('tp_passenger_flow_real')->select()->toArray();
$returnData = [];
$sumAllEnter = 0;
foreach ($res_data['data'] as $value) {
foreach ($list as $value) {
if (!isset($returnData[$value['groupId']]['allEnter'])) $returnData[$value['groupId']]['allEnter'] = 0;
$returnData[$value['groupId']]['allEnter'] += $value['allEnter'];
$sumAllEnter += $value['allEnter'];

1
config/console.php

@ -7,5 +7,6 @@ return [
'commands' => [
'passengerFlow' => 'app\command\passengerFlow',
'passengerFlowAll' => 'app\command\passengerFlowAll',
'passengerFlowReal' => 'app\command\passengerFlowReal',
],
];

Loading…
Cancel
Save