消息模块完善
This commit is contained in:
@@ -19,12 +19,32 @@ class Message extends Api
|
||||
$user_id = $this->auth->id ?? -1;
|
||||
$page = $this->request->get('page', 1);
|
||||
$page_size = $this->request->get('pageSize', 10);
|
||||
//消息类型筛选:0=系统消息,1=公告消息,2=需求消息,不传=全部
|
||||
$message_type = $this->request->get('message_type', '');
|
||||
$where = [
|
||||
'to_user_id' => $user_id
|
||||
];
|
||||
if ($message_type !== '' && $message_type !== null) {
|
||||
$where['message_type'] = $message_type;
|
||||
}
|
||||
//总未读数(兼容旧字段)+ 分类未读数(前端tab角标用)
|
||||
$unreadCount = MessageService::where($where + ['is_read' => CommonService::STATUS_OFF])->count();
|
||||
$unreadList = MessageService::where(['to_user_id' => $user_id, 'is_read' => CommonService::STATUS_OFF])
|
||||
->field('message_type, count(*) as total')
|
||||
->group('message_type')
|
||||
->select()->toArray();
|
||||
$unreadMap = [
|
||||
MessageService::TYPE_SYSTEM => 0,
|
||||
MessageService::TYPE_NOTICE => 0,
|
||||
MessageService::TYPE_DEMAND => 0,
|
||||
];
|
||||
foreach ($unreadList as $item) {
|
||||
$unreadMap[$item['message_type']] = (int)$item['total'];
|
||||
}
|
||||
|
||||
$list = MessageService::getList($where, $page_size, 'id desc')->toArray();
|
||||
$list['unread_count'] = $unreadCount;
|
||||
$list['unread_map'] = $unreadMap;
|
||||
|
||||
$this->success('', $list);
|
||||
} catch (\Exception $e) {
|
||||
@@ -36,6 +56,27 @@ class Message extends Api
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 未读消息数(供底部tabbar红点等轻量场景)
|
||||
* @return void
|
||||
*/
|
||||
public function unreadCount()
|
||||
{
|
||||
try {
|
||||
$user_id = $this->auth->id ?? -1;
|
||||
$count = MessageService::where([
|
||||
'to_user_id' => $user_id,
|
||||
'is_read' => CommonService::STATUS_OFF
|
||||
])->count();
|
||||
$this->success('', ['unread_count' => (int)$count]);
|
||||
} catch (\Exception $e) {
|
||||
if ($e instanceof HttpResponseException) {
|
||||
throw $e;
|
||||
}
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 详情
|
||||
* @return void
|
||||
@@ -50,6 +91,7 @@ class Message extends Api
|
||||
}
|
||||
|
||||
/**
|
||||
* 设为已读
|
||||
* @return void
|
||||
*/
|
||||
public function setRead()
|
||||
@@ -58,9 +100,14 @@ class Message extends Api
|
||||
$user_id = $this->auth->id ?? -1;
|
||||
$id = $this->request->post('id', 0);
|
||||
$isAll = $this->request->post('isAll', 0);
|
||||
//isAll 时可携带 message_type,仅将该类型全部设为已读
|
||||
$message_type = $this->request->post('message_type', '');
|
||||
$where = ['id' => $id, 'to_user_id' => $user_id];
|
||||
if ($isAll) {
|
||||
unset($where['id']);
|
||||
if ($message_type !== '' && $message_type !== null) {
|
||||
$where['message_type'] = $message_type;
|
||||
}
|
||||
}
|
||||
$res = MessageService::where($where)->update(['is_read' => CommonService::STATUS_ON]);
|
||||
if ($res === false) {
|
||||
|
||||
Reference in New Issue
Block a user