130 lines
4.5 KiB
PHP
130 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace app\api\controller;
|
|
|
|
use app\common\controller\Api;
|
|
use app\common\service\CommonService;
|
|
use app\common\service\MessageService;
|
|
use think\exception\HttpResponseException;
|
|
use think\Validate;
|
|
|
|
class Message extends Api
|
|
{
|
|
protected $noNeedLogin = [];
|
|
protected $noNeedRight = ['*'];
|
|
|
|
public function list()
|
|
{
|
|
try {
|
|
$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();
|
|
//where() 走的是 Query 对象,select() 返回普通数组而非 Collection,需兼容处理
|
|
$unreadList = MessageService::where(['to_user_id' => $user_id, 'is_read' => CommonService::STATUS_OFF])
|
|
->field('message_type, count(*) as total')
|
|
->group('message_type')
|
|
->select();
|
|
$unreadList = is_object($unreadList) && method_exists($unreadList, 'toArray')
|
|
? $unreadList->toArray()
|
|
: (array)$unreadList;
|
|
$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; //分类未读数(tab角标)
|
|
//全量未读总数:由后端统计所有类型,避免前端只累加 0/1/2 导致未知类型漏算
|
|
$list['unread_total'] = (int)array_sum($unreadMap);
|
|
|
|
$this->success('', $list);
|
|
} catch (\Exception $e) {
|
|
if ($e instanceof HttpResponseException) {
|
|
throw $e;
|
|
}
|
|
$this->error($e->getMessage());
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* 未读消息数(供底部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
|
|
*/
|
|
public function detail()
|
|
{
|
|
$user_id = $this->auth->id ?? -1;
|
|
$id = $this->request->get('id', 0);
|
|
$info = MessageService::getOne(['id' => $id, 'to_user_id' => $user_id]);
|
|
|
|
$this->success('success', $info);
|
|
}
|
|
|
|
/**
|
|
* 设为已读
|
|
* @return void
|
|
*/
|
|
public function setRead()
|
|
{
|
|
try {
|
|
$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) {
|
|
exception('操作失败');
|
|
}
|
|
$this->success('操作成功');
|
|
} catch (\Exception $e) {
|
|
if ($e instanceof HttpResponseException) {
|
|
throw $e;
|
|
}
|
|
$this->error($e->getMessage());
|
|
}
|
|
}
|
|
}
|