1.修复接口/api/notice/noticeDetail报错跨域
This commit is contained in:
@@ -11,10 +11,10 @@ use support\Response;
|
|||||||
|
|
||||||
class Notice extends MobileBase
|
class Notice extends MobileBase
|
||||||
{
|
{
|
||||||
/** 公告列表:公开接口,无需 auth-token / user-token */
|
/** 公告列表/详情:公开接口,无需 auth-token / user-token */
|
||||||
protected array $noNeedLogin = ['noticeList'];
|
protected array $noNeedLogin = ['noticeList', 'noticeDetail'];
|
||||||
|
|
||||||
protected array $noNeedAuthToken = ['noticeList'];
|
protected array $noNeedAuthToken = ['noticeList', 'noticeDetail'];
|
||||||
|
|
||||||
public function noticeList(Request $request): Response
|
public function noticeList(Request $request): Response
|
||||||
{
|
{
|
||||||
@@ -45,21 +45,38 @@ class Notice extends MobileBase
|
|||||||
|
|
||||||
$list = [];
|
$list = [];
|
||||||
foreach ($paginate->items() as $row) {
|
foreach ($paginate->items() as $row) {
|
||||||
$isPopout = $this->intValue($row->notice_type, 0) === 1;
|
$list[] = $this->formatNoticeItem($row, $readMap);
|
||||||
$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,
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->mobileSuccess(['list' => $list]);
|
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
|
public function noticeConfirm(Request $request): Response
|
||||||
{
|
{
|
||||||
$response = $this->initializeMobile($request);
|
$response = $this->initializeMobile($request);
|
||||||
@@ -110,5 +127,36 @@ class Notice extends MobileBase
|
|||||||
}
|
}
|
||||||
return $result;
|
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,
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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::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/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']);
|
Route::get('/api/notice/noticeConfirm', [\app\api\controller\Notice::class, 'noticeConfirm']);
|
||||||
|
|
||||||
// ==================== Admin 路由 ====================
|
// ==================== Admin 路由 ====================
|
||||||
|
|||||||
Reference in New Issue
Block a user