1.修复接口/api/notice/noticeDetail报错跨域

This commit is contained in:
2026-05-29 11:39:59 +08:00
parent be781504db
commit 0aa0809ad1
2 changed files with 62 additions and 13 deletions

View File

@@ -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<int, int> $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,
];
}
}

View File

@@ -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 路由 ====================