Files
webman-buildadmin/app/api/controller/Notice.php

163 lines
5.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
declare(strict_types=1);
namespace app\api\controller;
use app\common\model\OperationNotice;
use app\common\model\UserNoticeRead;
use Webman\Http\Request;
use support\Response;
class Notice extends MobileBase
{
/** 公告列表/详情:公开接口,无需 auth-token / user-token */
protected array $noNeedLogin = ['noticeList', 'noticeDetail'];
protected array $noNeedAuthToken = ['noticeList', 'noticeDetail'];
public function noticeList(Request $request): Response
{
$response = $this->initializeMobile($request);
if ($response !== null) {
return $response;
}
$page = $this->intValue($request->input('page', 1), 1);
$pageSize = $this->intValue($request->input('page_size', 20), 20);
$paginate = OperationNotice::where('status', 1)->order('id', 'desc')->paginate([
'page' => $page,
'list_rows' => $pageSize,
]);
$popoutNoticeIds = [];
foreach ($paginate->items() as $row) {
if ($this->intValue($row->notice_type, 0) === 1) {
$popoutNoticeIds[] = $row->id;
}
}
$readMap = [];
$userId = ($this->auth && $this->auth->isLogin()) ? $this->auth->id : 0;
if ($userId > 0 && $popoutNoticeIds !== []) {
$readRows = UserNoticeRead::where('user_id', $userId)->whereIn('notice_id', $popoutNoticeIds)->column('notice_id');
$readMap = array_flip($readRows);
}
$list = [];
foreach ($paginate->items() as $row) {
$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);
if ($response !== null) {
return $response;
}
$noticeId = $this->intValue($request->input('notice_id', 0), 0);
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');
}
if ($this->intValue($notice->notice_type, 0) !== 1) {
return $this->mobileError(2004, 'Notice does not require confirmation');
}
$readRow = UserNoticeRead::where('user_id', $this->auth->id)->where('notice_id', $noticeId)->find();
$now = time();
if ($readRow) {
$readRow->read_at = $now;
if ($this->intValue($readRow->confirmed, 0) !== 1) {
$readRow->confirmed = 1;
}
$readRow->save();
} else {
UserNoticeRead::create([
'user_id' => $this->auth->id,
'notice_id' => $noticeId,
'confirmed' => 1,
'read_at' => $now,
'create_time' => $now,
]);
}
return $this->mobileSuccess([
'notice_id' => $noticeId,
'confirmed' => true,
'confirm_time' => $now,
]);
}
private function intValue($value, int $default): int
{
$result = filter_var($value, FILTER_VALIDATE_INT);
if ($result === false) {
return $default;
}
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,
];
}
}