refactor: trim unused smoke API fields

This commit is contained in:
nepiedg
2026-04-26 22:05:21 +08:00
parent 613e4a58a9
commit 0ad27898cf
5 changed files with 6 additions and 278 deletions
+1 -93
View File
@@ -117,11 +117,8 @@ class SmokeService
$smokeTime = Support::dateOnly();
}
$level = $resisted ? 0 : max(0, (int) ($data['level'] ?? 1));
$num = $resisted ? 0 : max(0, (int) ($data['num'] ?? 1));
if (!$resisted && $num === 0) {
throw new \RuntimeException('num=0 请使用 /smoke/logs/resisted', 400);
}
$level = ($resisted || $num === 0) ? 0 : max(0, (int) ($data['level'] ?? 1));
$insertId = Db::connect('mysql')->name('fa_smoke_log')->insertGetId([
'uid' => $userId,
@@ -195,23 +192,6 @@ class SmokeService
];
}
public function latestLogs(int $userId, int $limit = 20): array
{
$limit = min(100, max(1, $limit));
$rows = Db::connect('mysql')->name('fa_smoke_log')
->where('uid', $userId)
->whereRaw('(deletetime IS NULL OR deletetime = 0)')
->orderRaw('COALESCE(smoke_at, FROM_UNIXTIME(createtime), smoke_time) DESC')
->order('id', 'desc')
->limit($limit)
->select()
->toArray();
return ['items' => array_map(static function ($row) {
return Support::formatLog($row);
}, $rows)];
}
public function updateLog(int $userId, int $id, array $data): array
{
$this->getLog($userId, $id);
@@ -267,61 +247,6 @@ class SmokeService
return ['deleted' => true];
}
public function dashboard(int $userId, array $params = []): array
{
$now = Support::now();
[$defaultStart, $defaultEnd] = Support::weekRange($now);
$start = !empty($params['start']) ? Support::parseDate((string) $params['start'], 'start') : $defaultStart;
$end = !empty($params['end']) ? Support::parseDate((string) $params['end'], 'end') : (!empty($params['start']) ? $start->modify('+6 day') : $defaultEnd);
if ($end < $start) {
throw new \RuntimeException('end 不能早于 start', 400);
}
$rows = Db::connect('mysql')->name('fa_smoke_log')
->field('smoke_time, SUM(num) AS total')
->where('uid', $userId)
->whereBetween('smoke_time', [$start->format(Support::DATE_LAYOUT), $end->format(Support::DATE_LAYOUT)])
->whereRaw('(deletetime IS NULL OR deletetime = 0)')
->group('smoke_time')
->select()
->toArray();
$counts = [];
foreach ($rows as $row) {
$counts[(string) $row['smoke_time']] = (int) ($row['total'] ?? 0);
}
$today = Support::dateOnly($now);
$todayKey = $today->format(Support::DATE_LAYOUT);
$todayCount = (int) Db::connect('mysql')->name('fa_smoke_log')
->where('uid', $userId)
->where('smoke_time', $todayKey)
->whereRaw('(deletetime IS NULL OR deletetime = 0)')
->sum('num');
$lastSmoke = $this->findLastActualSmoke($userId);
$minutesSinceLast = null;
if ($lastSmoke) {
$minutesSinceLast = max(0, (int) floor(($now->getTimestamp() - $lastSmoke->getTimestamp()) / 60));
}
$weekly = [];
for ($cursor = $start; $cursor <= $end; $cursor = $cursor->add(new DateInterval('P1D'))) {
$key = $cursor->format(Support::DATE_LAYOUT);
$weekly[] = [
'date' => $key,
'count' => (int) ($counts[$key] ?? 0),
'is_today' => $key === $todayKey,
];
}
return [
'today_count' => $todayCount,
'minutes_since_last' => $minutesSinceLast,
'weekly' => $weekly,
];
}
public function getHomeSummary(int $userId, ?DateTimeImmutable $asOf = null): array
{
$asOf = $asOf ?: Support::now();
@@ -622,23 +547,6 @@ class SmokeService
];
}
public function revokeShare(int $userId, string $token): array
{
$share = SmokeShare::where('share_token', trim($token))->whereNull('deleted_at')->find();
if (!$share) {
throw new \RuntimeException('分享不存在', 404);
}
if ((int) $share->uid !== $userId) {
throw new \RuntimeException('无权限操作该分享', 403);
}
$share->revoked_at = Support::now()->format(Support::DATETIME_LAYOUT);
$share->updated_at = Support::now()->format(Support::DATETIME_LAYOUT);
$share->save();
return ['revoked' => true];
}
private function formatProfileRow(array $row): array
{
return [