123 lines
3.9 KiB
PHP
123 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace app\api\controller;
|
|
|
|
use app\common\enum\notice\UserNoticeEnum;
|
|
use app\common\enum\YesNoEnum;
|
|
use app\common\model\notice\NoticeRecord;
|
|
use app\common\model\user\User;
|
|
use app\common\service\chat\PrivateChatService;
|
|
use app\common\service\notice\UserNoticeService;
|
|
|
|
/**
|
|
* 用户站内消息中心。
|
|
*/
|
|
class NoticeController extends BaseApiController
|
|
{
|
|
public function lists()
|
|
{
|
|
$page = max(1, $this->request->get('page_no/d', 1));
|
|
$size = min(50, max(1, $this->request->get('page_size/d', 20)));
|
|
$query = NoticeRecord::where('user_id', $this->userId)
|
|
->whereIn('scene_id', UserNoticeEnum::getSceneIds());
|
|
|
|
$total = (clone $query)->count();
|
|
$records = $query->order('id desc')
|
|
->page($page, $size)
|
|
->select()
|
|
->toArray();
|
|
|
|
return $this->data([
|
|
'lists' => $this->formatRecords($records),
|
|
'count' => $total,
|
|
'page_no' => $page,
|
|
'page_size' => $size,
|
|
]);
|
|
}
|
|
|
|
public function summary()
|
|
{
|
|
$noticeUnreadCount = UserNoticeService::unreadCount($this->userId);
|
|
$chatUnreadCount = PrivateChatService::unreadCount($this->userId);
|
|
|
|
return $this->data([
|
|
'notice_unread_count' => $noticeUnreadCount,
|
|
'chat_unread_count' => $chatUnreadCount,
|
|
'unread_count' => $noticeUnreadCount + $chatUnreadCount,
|
|
]);
|
|
}
|
|
|
|
public function read()
|
|
{
|
|
$id = $this->request->post('id/d');
|
|
if ($id <= 0) {
|
|
return $this->fail('消息参数错误');
|
|
}
|
|
|
|
NoticeRecord::where('id', $id)
|
|
->where('user_id', $this->userId)
|
|
->whereIn('scene_id', UserNoticeEnum::getSceneIds())
|
|
->update([
|
|
'read' => YesNoEnum::YES,
|
|
'update_time' => time(),
|
|
]);
|
|
|
|
return $this->success('已读');
|
|
}
|
|
|
|
public function readAll()
|
|
{
|
|
NoticeRecord::where('user_id', $this->userId)
|
|
->whereIn('scene_id', UserNoticeEnum::getSceneIds())
|
|
->where('read', YesNoEnum::NO)
|
|
->update([
|
|
'read' => YesNoEnum::YES,
|
|
'update_time' => time(),
|
|
]);
|
|
|
|
return $this->success('全部消息已读');
|
|
}
|
|
|
|
private function formatRecords(array $records): array
|
|
{
|
|
$sourceUserIds = [];
|
|
foreach ($records as $record) {
|
|
$extra = UserNoticeService::decodeExtra($record['extra'] ?? '');
|
|
if (!empty($extra['source_user_id'])) {
|
|
$sourceUserIds[] = (int) $extra['source_user_id'];
|
|
}
|
|
}
|
|
|
|
$userMap = [];
|
|
$sourceUserIds = array_values(array_unique(array_filter($sourceUserIds)));
|
|
if (!empty($sourceUserIds)) {
|
|
$users = User::whereIn('id', $sourceUserIds)
|
|
->field('id,nickname,avatar')
|
|
->select()
|
|
->toArray();
|
|
foreach ($users as $user) {
|
|
$userMap[(int) $user['id']] = $user;
|
|
}
|
|
}
|
|
|
|
foreach ($records as &$record) {
|
|
$extra = UserNoticeService::decodeExtra($record['extra'] ?? '');
|
|
$meta = UserNoticeEnum::getMeta((int) $record['scene_id']);
|
|
$sourceUserId = (int) ($extra['source_user_id'] ?? 0);
|
|
$record = [
|
|
'id' => (int) $record['id'],
|
|
'title' => $record['title'],
|
|
'content' => $record['content'],
|
|
'is_read' => (int) $record['read'] === YesNoEnum::YES,
|
|
'create_time' => $record['create_time'],
|
|
'target_url' => UserNoticeService::sanitizeTargetUrl((string) ($extra['target_url'] ?? '')),
|
|
'source_user' => $sourceUserId > 0 ? ($userMap[$sourceUserId] ?? null) : null,
|
|
...$meta,
|
|
];
|
|
}
|
|
unset($record);
|
|
|
|
return $records;
|
|
}
|
|
}
|