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
+3 -162
View File
@@ -44,51 +44,20 @@ class Smoke extends BaseController
$homeSummary = $this->smokeService->getHomeSummary($uid, $now);
$motivation = $this->smokeService->motivation($uid, $profileView['profile'], $now);
$adviceDate = Support::dateOnly($now->modify('-1 day'));
$adviceCard = [
'title' => '智能控烟建议',
'date' => $adviceDate->format(Support::DATE_LAYOUT),
'message' => '',
'model' => '',
'status' => 'empty',
];
try {
$advice = $this->smokeAiService->getOrGenerateAdvice($user, $adviceDate, 'v2');
$adviceCard['message'] = (string) ($advice['advice'] ?? '');
$adviceCard['model'] = (string) ($advice['model'] ?? '');
$adviceCard['status'] = 'available';
} catch (\RuntimeException $e) {
if ($e->getCode() === 403) {
$adviceCard['status'] = 'locked';
} elseif ($e->getCode() === 400) {
$adviceCard['status'] = 'no_data';
} else {
$adviceCard['status'] = 'unavailable';
}
}
$timer = [
'label' => '距上次抽烟',
'last_smoke_at' => (string) ($homeSummary['last_smoke_at'] ?? ''),
'seconds_since_last' => (int) ($homeSummary['seconds_since_last'] ?? -1),
'next_suggested_at' => (string) ($defaultSuggestion['next_smoke_at'] ?? ''),
'next_suggested_clock' => Support::formatClock((string) ($defaultSuggestion['next_smoke_at'] ?? '')),
'not_before_at' => (string) ($defaultSuggestion['next_smoke_at'] ?? ''),
'suggestion_source' => 'default',
'suggestion_algorithm' => (string) ($defaultSuggestion['algorithm'] ?? ''),
];
$cachedAiNext = $this->smokeAiService->getCachedNextSmoke($user, $planDate, 'v1');
if ($cachedAiNext) {
$timer['suggestion_source'] = 'ai';
$timer['suggestion_algorithm'] = 'ai_next_smoke_v1';
$timer['next_suggested_at'] = (string) ($cachedAiNext['suggested_at'] ?? '');
$timer['next_suggested_clock'] = Support::formatClock((string) ($cachedAiNext['suggested_at'] ?? ''));
$timer['not_before_at'] = (string) ($cachedAiNext['not_before_at'] ?? '');
$timer['ai_time_nodes'] = $cachedAiNext['time_nodes'] ?? [];
$timer['ai_advice'] = (string) ($cachedAiNext['advice'] ?? '');
$timer['ai_model'] = (string) ($cachedAiNext['model'] ?? '');
}
$dailySummaryRecord = $this->smokeAiService->getCachedByType($uid, SmokeAiService::TYPE_DAILY_SUMMARY, $planDate, 'v1');
@@ -103,14 +72,6 @@ class Smoke extends BaseController
}
return Response::success([
'greeting' => $this->buildGreeting((string) ($user['nickname'] ?? ''), (string) ($user['avatar_url'] ?? ''), $now),
'profile' => $profileView,
'advice_card' => $adviceCard,
'campaign_card' => [
'title' => '绿色生活,从戒烟开始',
'subtitle' => 'BRAND CAMPAIGN',
'badge' => '广告',
],
'timer' => $timer,
'summary' => [
'today_count' => (int) ($homeSummary['today_count'] ?? 0),
@@ -118,18 +79,9 @@ class Smoke extends BaseController
'resisted_count' => (int) ($homeSummary['resisted_count'] ?? 0),
'reduced_from_yesterday' => (int) ($homeSummary['reduced_from_yesterday'] ?? 0),
'exceeded_yesterday' => (bool) ($homeSummary['exceeded_yesterday'] ?? false),
'profile_completed' => (bool) ($profileView['is_completed'] ?? false),
],
'daily_summary' => $dailySummary,
'motivation' => $motivation,
'quick_actions' => [
['type' => 'log_smoke', 'title' => '记录抽烟', 'primary' => false],
['type' => 'resist', 'title' => '想抽忍住了', 'primary' => true],
],
'data_sources' => [
'ai_advice_date' => $adviceDate->format(Support::DATE_LAYOUT),
'plan_date' => $planDate->format(Support::DATE_LAYOUT),
],
]);
} catch (\Throwable $e) {
return Response::error($e->getMessage(), $e->getCode() ?: 500);
@@ -163,24 +115,6 @@ class Smoke extends BaseController
}
}
public function createResistedLog()
{
try {
return Response::success($this->smokeService->createLog($this->getCurrentSmtUserId(), $this->request->post(), true));
} catch (\Throwable $e) {
return Response::error($e->getMessage(), $e->getCode() ?: 500);
}
}
public function readLog(int $id)
{
try {
return Response::success($this->smokeService->getLog($this->getCurrentSmtUserId(), $id));
} catch (\Throwable $e) {
return Response::error($e->getMessage(), $e->getCode() ?: 500);
}
}
public function logs()
{
try {
@@ -190,15 +124,6 @@ class Smoke extends BaseController
}
}
public function latestLogs()
{
try {
return Response::success($this->smokeService->latestLogs($this->getCurrentSmtUserId(), (int) $this->request->get('limit', 20)));
} catch (\Throwable $e) {
return Response::error($e->getMessage(), $e->getCode() ?: 500);
}
}
public function updateLog(int $id)
{
try {
@@ -217,15 +142,6 @@ class Smoke extends BaseController
}
}
public function dashboard()
{
try {
return Response::success($this->smokeService->dashboard($this->getCurrentSmtUserId(), $this->request->get()));
} catch (\Throwable $e) {
return Response::error($e->getMessage(), $e->getCode() ?: 500);
}
}
public function stats()
{
try {
@@ -236,17 +152,7 @@ class Smoke extends BaseController
}
}
public function motivation()
{
try {
$uid = $this->getCurrentSmtUserId();
return Response::success($this->smokeService->motivation($uid, $this->smokeService->getProfile($uid)));
} catch (\Throwable $e) {
return Response::error($e->getMessage(), $e->getCode() ?: 500);
}
}
public function nextSmokeTime()
public function aiNextSmokeTime()
{
try {
$user = $this->getCurrentSmtUser();
@@ -264,18 +170,11 @@ class Smoke extends BaseController
$profileView = $this->smokeService->getProfileView($uid);
$defaultSuggestion = $this->smokeService->getDefaultNextSuggestion($uid, $now, $planDate, $profileView);
$homeSummary = $this->smokeService->getHomeSummary($uid, $now);
$response = [
'source' => 'default',
'not_before_at' => (string) ($defaultSuggestion['next_smoke_at'] ?? ''),
'suggested_at' => (string) ($defaultSuggestion['next_smoke_at'] ?? ''),
'last_smoke_at' => (string) ($homeSummary['last_smoke_at'] ?? ''),
'today_count' => (int) ($homeSummary['today_count'] ?? 0),
'resisted_count' => (int) ($homeSummary['resisted_count'] ?? 0),
'reduced_from_yesterday' => (int) ($homeSummary['reduced_from_yesterday'] ?? 0),
'exceeded_yesterday' => (bool) ($homeSummary['exceeded_yesterday'] ?? false),
'default' => $defaultSuggestion,
'time_nodes' => [],
'advice' => '',
];
if ($mode !== 'default') {
@@ -285,11 +184,9 @@ class Smoke extends BaseController
if ($aiSuggestion) {
$response['source'] = 'ai';
$response['not_before_at'] = (string) ($aiSuggestion['not_before_at'] ?? '');
$response['suggested_at'] = (string) ($aiSuggestion['suggested_at'] ?? '');
$response['time_nodes'] = $aiSuggestion['time_nodes'] ?? [];
$response['advice'] = (string) ($aiSuggestion['advice'] ?? '');
$response['ai'] = $aiSuggestion;
}
}
@@ -299,30 +196,6 @@ class Smoke extends BaseController
}
}
public function aiNextSmokeTime()
{
return $this->nextSmokeTime();
}
public function aiAdvice()
{
try {
$user = $this->getCurrentSmtUser();
$date = !empty($this->request->get('date'))
? Support::parseDate((string) $this->request->get('date'), 'date')
: Support::dateOnly(Support::now()->modify('-1 day'));
$record = $this->smokeAiService->getOrGenerateAdvice($user, $date, 'v2');
return Response::success([
'date' => (string) ($record['date'] ?? $date->format(Support::DATE_LAYOUT)),
'advice' => (string) ($record['advice'] ?? ''),
'model' => (string) ($record['model'] ?? ''),
]);
} catch (\Throwable $e) {
return Response::error($e->getMessage(), $e->getCode() ?: 500);
}
}
public function unlockAiAdvice()
{
try {
@@ -372,15 +245,6 @@ class Smoke extends BaseController
}
}
public function revokeShare(string $token)
{
try {
return Response::success($this->smokeService->revokeShare($this->getCurrentSmtUserId(), $token));
} catch (\Throwable $e) {
return Response::error($e->getMessage(), $e->getCode() ?: 500);
}
}
public function generateQuitPlan()
{
try {
@@ -458,27 +322,4 @@ class Smoke extends BaseController
return Support::parseDate($value, 'date');
}
private function buildGreeting(string $nickname, string $avatarUrl, \DateTimeImmutable $now): array
{
$nickname = trim($nickname) !== '' ? trim($nickname) : '朋友';
$hour = (int) $now->format('H');
if ($hour >= 5 && $hour < 11) {
[$timeOfDay, $title, $subtitle] = ['morning', '早安', '今天也是清爽的一天'];
} elseif ($hour >= 11 && $hour < 14) {
[$timeOfDay, $title, $subtitle] = ['noon', '午安', '补充水分和能量'];
} elseif ($hour >= 14 && $hour < 19) {
[$timeOfDay, $title, $subtitle] = ['afternoon', '下午好', '把烟瘾留在昨天'];
} else {
[$timeOfDay, $title, $subtitle] = ['evening', '晚上好', '今晚早点休息'];
}
return [
'title' => $title . '' . $nickname,
'subtitle' => $subtitle,
'nickname' => $nickname,
'time_of_day' => $timeOfDay,
'avatar_url' => $avatarUrl,
];
}
}