宝体数据调用接口
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

235 lines
9.5 KiB

<?php
namespace app\controller;
use app\BaseController;
use think\facade\Db;
use think\facade\View;
class Index extends BaseController
{
public function index()
{
return view("index");
//return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> ThinkPHP V' . \think\facade\App::version() . '<br/><span style="font-size:30px;">14载初心不改 - 你值得信赖的PHP框架</span></p><span style="font-size:25px;">[ V6.0 版本由 <a href="https://www.yisu.com/" target="yisu">亿速云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=64890268" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="ee9b1aa918103c4fc"></think>';
}
public function hello($name = 'ThinkPHP6')
{
return 'hello,' . $name;
}
public function getAccessToken(){
$config = config('app');
//
$url = $config['host_url']."/oauth/token?client_id=".$config['cid']."&client_secret=".$config['cskey'];
// do post
$ret = post($url);
return $ret;
}
protected $token = 'y871T5okLEegC6WrcKN259Mhy4AGDcjm';
/**
* 分时时间段接口 <首页-客流趋势>
* @return false|string
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function allGroupTimeNum() {
$request = $this->request;
$granularity = $request['granularity'];
if (empty($granularity)) return json_encode(['code' => 403, 'date参数不能为空'],JSON_UNESCAPED_UNICODE);
$dateData = getTargetDate();
$allTimeData = [];
$groupsData = [];
if ($granularity == 'daily') {
$list = Db::table('tp_passenger_flow')->where(['granularity' => 'hourly'])->select()->toArray();
$data = $this->handleGroupData($list);
$allTimeData = $data['allTimeData'];
$groupsData = $data['groupsData'];
} elseif ($granularity == 'monthly') {
$list = Db::table('tp_passenger_flow')->where(['granularity' => 'daily'])->select()->toArray();
$data = $this->handleGroupData($list,'d');
$allTimeData = $data['allTimeData'];
$groupsData = $data['groupsData'];
} elseif ($granularity == 'yearly') {
$list = Db::table('tp_passenger_flow')->where(['granularity' => 'monthly'])->select()->toArray();
$data = $this->handleGroupData($list,'m');
$allTimeData = $data['allTimeData'];
$groupsData = $data['groupsData'];
}
return json_encode(['code' => 0, 'msg' => '成功', 'data' => [
'allTimeData' => $allTimeData,
'groupsData' => $groupsData
]],JSON_UNESCAPED_UNICODE);
}
/**
* 查询时间范围内的多个统计组的客流统计数据
* @param $granularity // 可选 hourly | daily | monthly | yearly,分别为按小时、按天、按月和按年统计
* @param $s_time // 开始时间
* @param $e_time // 结束时间
* @return false|mixed|string
*/
protected function getGroupData($granularity,$s_time,$e_time) {
$config = config('app');
$token = getAccessToken();
$url = $config['host_url'] . "/api/cfas/v2/passengerFlow/groups";
$dataArr = [
'granularity' => $granularity,
'startTime' => $s_time,
'endTime' => $e_time,
'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) return json_encode(['code' => 403, 'msg' => $res_data['msg']],JSON_UNESCAPED_UNICODE);
return json_encode(['code' => 403, 'msg' => $res_data['msg'],'data' => $res_data['data']['list']],JSON_UNESCAPED_UNICODE);
}
/**
* 特殊处理
* @param $list
* @param $date_str
* @return array
*/
protected function handleGroupData($list,$date_str = 'H:00') {
$allTimeData = [];
$groupsData = [];
if ($date_str == 'H:00') {
for ($i = 9; $i < 23; $i++) {
$str = strlen($i) == 1 ? '0'.$i.':00' : $i.':00';
$allTimeData[$str]['num'] = 0;
$allTimeData[$str]['title'] = $str;
}
} elseif ($date_str == 'd') {
$d = date('d') * 1;
$date = $d - 12 > 0 ? $d - 12 : 1;
for ($i = $date; $i <= $d; $i++) {
$str = strlen($i) == 1 ? '0'.$i : $i;
$allTimeData[$str]['num'] = 0;
$allTimeData[$str]['title'] = $str;
}
} elseif ($date_str == 'm') {
$m = date('m') * 1;
$date = $m - 12 > 0 ? $m - 12 : 1;
for ($i = $date; $i <= $m; $i++) {
$str = strlen($i) == 1 ? '0'.$i : $i;
$allTimeData[$str]['num'] = 0;
$allTimeData[$str]['title'] = $str;
}
}
$sumNoRepeatInNum = 0;
foreach ($list as $value) {
$sumNoRepeatInNum += $value['noRepeatInNum']; // 进馆总人数
if (!isset($groupsData[$value['groupName']]['noRepeatInNum'])) $groupsData[$value['groupName']]['noRepeatInNum'] = 0;
$groupsData[$value['groupName']]['noRepeatInNum'] += $value['noRepeatInNum']; // 分租进馆人数
$date = date($date_str,ceil($value['createTime'] / 1000) + (8*3600)); // 各时间端进馆人数
if (!isset($allTimeData[$date])) continue;
$allTimeData[$date]['num'] += $value['noRepeatInNum'];
}
ksort($allTimeData);
// 计算各组占比
foreach ($groupsData as &$val) {
$val['proportion'] = (round($val['noRepeatInNum']/$sumNoRepeatInNum,2) * 100) ."%";
}
return [
'allTimeData' => $allTimeData, // 总客流趋势时间段
'groupsData' => $groupsData // 各组总数及占比
];
}
/**
* 总览总数据接口 <首页-中间部分总览>
* @return false|string
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function allGroupNum() {
$dateArr = ['day' => 'daily', 'week' => 'daily', 'month' => 'monthly'];
$returnData = [];
foreach ($dateArr as $date => $granularity) {
$list = Db::table('tp_passenger_flow_all')
->where(['granularity' => $granularity, 'date' => $date])->select()->toArray();
$returnData[$date]['noRepeatInNum'] = 0;
foreach ($list as $value) {
$returnData[$date]['noRepeatInNum'] += $value['noRepeatInNum'];
}
}
foreach ($returnData as &$val) {
$val['noRepeatInNum'] = formatNumber($val['noRepeatInNum']);
}
$returnArr = [
'code' => 0,
'msg' => '成功',
'data' => $returnData
];
return json_encode($returnArr,JSON_UNESCAPED_UNICODE);
}
/**
* 查询最新客流统计组实时数据 <各场管实施实时进馆人数>
* @return void
*/
public function realTimeData() {
$list = Db::table('tp_passenger_flow_real')->select()->toArray();
$returnData = [];
$sumAllEnter = 0;
foreach ($list as $value) {
if (!isset($returnData[$value['groupId']]['allEnter'])) $returnData[$value['groupId']]['allEnter'] = 0;
$returnData[$value['groupId']]['allEnter'] += $value['allEnter'];
$sumAllEnter += $value['allEnter'];
}
foreach ($returnData as &$val) {
$val['proportion'] = (round($val['allEnter']/$sumAllEnter,2) * 100) ."%";
}
return json_encode(['code' => 0, 'msg' => '成功', 'data' => $returnData],JSON_UNESCAPED_UNICODE);
}
/**
* 今日各场馆人数趋势
* @return false|string
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function toDayGroupsEnterNum()
{
$list = Db::table('tp_passenger_flow')->where(['granularity' => 'hourly'])->select()->toArray();
$allTimeData = [];
for ($i = 9; $i < 23; $i++) {
$str = strlen($i) == 1 ? '0'.$i.':00' : $i.':00';
$allTimeData[$str] = 0;
}
$returnData = [];
foreach ($list as $value) {
$date = date('H:00',ceil($value['createTime'] / 1000)); // 各时间端进馆人数
if (!isset($allTimeData[$date])) continue;
if (!isset($returnData[$value['groupName']][$allTimeData[$date]])) $returnData[$value['groupName']][$date] = 0;
$returnData[$value['groupName']][$date] += $value['flowInNum'];
}
return json_encode(['code' => 0, 'msg' => '成功', 'data' => $returnData],JSON_UNESCAPED_UNICODE);
}
}