Files
code-bang/backend/application/api/controller/Message.php
T

77 lines
2.1 KiB
PHP
Raw Normal View History

2026-09-17 16:25:31 +08:00
<?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);
$where = [
'to_user_id' => $user_id
];
$unreadCount = MessageService::where($where + ['is_read' => CommonService::STATUS_OFF])->count();
$list = MessageService::getList($where, $page_size, 'id desc')->toArray();
$list['unread_count'] = $unreadCount;
$this->success('', $list);
} 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);
$where = ['id' => $id, 'to_user_id' => $user_id];
if ($isAll) {
unset($where['id']);
}
$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());
}
}
}