diff --git a/app/api/controller/Notice.php b/app/api/controller/Notice.php index ddea833..7f703a9 100644 --- a/app/api/controller/Notice.php +++ b/app/api/controller/Notice.php @@ -11,10 +11,10 @@ use support\Response; class Notice extends MobileBase { - /** 公告列表:公开接口,无需 auth-token / user-token */ - protected array $noNeedLogin = ['noticeList']; + /** 公告列表/详情:公开接口,无需 auth-token / user-token */ + protected array $noNeedLogin = ['noticeList', 'noticeDetail']; - protected array $noNeedAuthToken = ['noticeList']; + protected array $noNeedAuthToken = ['noticeList', 'noticeDetail']; public function noticeList(Request $request): Response { @@ -45,21 +45,38 @@ class Notice extends MobileBase $list = []; foreach ($paginate->items() as $row) { - $isPopout = $this->intValue($row->notice_type, 0) === 1; - $list[] = [ - 'notice_id' => $row->id, - 'title' => $row->title, - 'content' => $row->content, - 'notice_type' => $isPopout ? 'popout' : 'silent', - 'must_confirm' => $isPopout, - 'is_read' => $isPopout && isset($readMap[$row->id]), - 'publish_time' => $row->publish_at, - ]; + $list[] = $this->formatNoticeItem($row, $readMap); } return $this->mobileSuccess(['list' => $list]); } + public function noticeDetail(Request $request): Response + { + $response = $this->initializeMobile($request); + if ($response !== null) { + return $response; + } + $noticeId = $this->resolveNoticeId($request); + if ($noticeId < 1) { + return $this->mobileError(1001, 'Missing parameters'); + } + $notice = OperationNotice::where('id', $noticeId)->where('status', 1)->find(); + if (!$notice) { + return $this->mobileError(2004, 'Notice does not exist'); + } + + $readMap = []; + $userId = ($this->auth && $this->auth->isLogin()) ? $this->auth->id : 0; + $isPopout = $this->intValue($notice->notice_type, 0) === 1; + if ($userId > 0 && $isPopout) { + $readRow = UserNoticeRead::where('user_id', $userId)->where('notice_id', $noticeId)->column('notice_id'); + $readMap = array_flip($readRow); + } + + return $this->mobileSuccess($this->formatNoticeItem($notice, $readMap)); + } + public function noticeConfirm(Request $request): Response { $response = $this->initializeMobile($request); @@ -110,5 +127,36 @@ class Notice extends MobileBase } return $result; } + + /** + * 兼容 notice_id 与部分客户端使用的 n 参数。 + */ + private function resolveNoticeId(Request $request): int + { + $noticeId = $this->intValue($request->input('notice_id', 0), 0); + if ($noticeId > 0) { + return $noticeId; + } + + return $this->intValue($request->input('n', 0), 0); + } + + /** + * @param array $readMap notice_id => index(来自 array_flip) + */ + private function formatNoticeItem(OperationNotice $row, array $readMap = []): array + { + $isPopout = $this->intValue($row->notice_type, 0) === 1; + + return [ + 'notice_id' => $row->id, + 'title' => $row->title, + 'content' => $row->content, + 'notice_type' => $isPopout ? 'popout' : 'silent', + 'must_confirm' => $isPopout, + 'is_read' => $isPopout && isset($readMap[$row->id]), + 'publish_time' => $row->publish_at, + ]; + } } diff --git a/config/route.php b/config/route.php index 04db670..d84d9de 100644 --- a/config/route.php +++ b/config/route.php @@ -153,6 +153,7 @@ Route::add(['GET', 'POST'], '/api/finance/withdrawDetail', [\app\api\controller\ Route::add(['GET', 'POST'], '/api/finance/withdrawList', [\app\api\controller\Finance::class, 'withdrawList']); Route::get('/api/notice/noticeList', [\app\api\controller\Notice::class, 'noticeList']); +Route::get('/api/notice/noticeDetail', [\app\api\controller\Notice::class, 'noticeDetail']); Route::get('/api/notice/noticeConfirm', [\app\api\controller\Notice::class, 'noticeConfirm']); // ==================== Admin 路由 ====================