input('date', date('Y-m-d'));
$channel = (int)$request->input('channel', 104); // 渠道ID,默认102
$format = $request->input('format', 'chart'); // json 或 chart
$logic = new RegisterHourlyStatsLogic();
$result = $logic->getHourlyStats($date, $channel);
if ($result === false) {
if ($format === 'chart') {
return response('
错误: ' . htmlspecialchars($logic->getError()) . '
', 400);
}
return apiReturnFail($logic->getError());
}
// 如果请求图表格式,返回 HTML 图表页面
if ($format === 'chart') {
return $this->renderChart($result, $date, $channel);
}
// 默认返回 JSON
return apiReturnSuc($result);
}
/**
* 渲染 HTML 图表页面
*
* @param array $data
* @param string $date
* @param int $channel
* @return \Illuminate\Http\Response
*/
private function renderChart($data, $date, $channel = 102)
{
// 准备图表数据
$hours = [];
$registerCounts = [];
$chargeCounts = [];
$chargeRates = [];
foreach ($data as $row) {
$hours[] = $row['hour_display'];
$registerCounts[] = $row['register_count'];
$chargeCounts[] = $row['first_charge_count'];
$chargeRates[] = $row['charge_rate'];
}
$html = '
注册用户按小时统计图表
注册用户按小时统计图表
统计日期: ' . htmlspecialchars($date) . ' |
渠道: ' . htmlspecialchars($channel) . ' |
统计时间: ' . date('Y-m-d H:i:s') . '
';
return response($html, 200)->header('Content-Type', 'text/html; charset=utf-8');
}
}