feat(message): 增加消息分类功能并优化消息列表

- 新增 msgGroup 方法实现消息分类功能
- 优化 msg 方法,支持分页并解析消息内容
- 修改 Circle 控制器中的消息发送逻辑
This commit is contained in:
2025-05-31 18:00:13 +08:00
parent a44f364914
commit 681af2a1ca
2 changed files with 36 additions and 8 deletions

View File

@@ -318,18 +318,45 @@ class User extends Common
} }
// 分类消息
public function msgGroup()
{
$params = $this->request->param();
$query1 = Message::field('*,count(*) as num')->where('user_id', $this->auth->id);
$query2 = Message::field('*,count(*) as num')->where('user_id', $this->auth->id);
if (isset($params['type'])) {
$query1->where('type', $params['type']);
$query2->where('type', $params['type']);
}
$query1->group('from_id')->order('update_time', 'desc');
$query2->group('from_id')->order('update_time', 'desc');
$num = $query2->where('status', 0)->column('count(*) as num','from_id');// ->column('count(*) as num', 'gender');
// dd($num);
$res = $query1->paginate($params['pageSize'] ?? 10);
$list = $res->items();
foreach ($list as &$r) {
$r['content'] = json_decode($r['content'], true);
$r['num'] = $num[$r['from_id']] ?? 0;
}
$this->success('Success', ['list' => $list, 'count' => $res->total()]);
}
// 用户消息 // 用户消息
public function msg() public function msg()
{ {
$params = $this->request->param(); $params = $this->request->param();
$model = Message::where('user_id', $this->auth->id); $query = Message::where('user_id', $this->auth->id);
if (isset($params['type'])) { if (isset($params['type'])) {
$model->where('type', $params['type']); $query->where('type', $params['type']);
} }
$res = $model->select(); $res = $query->paginate($params['pageSize'] ?? 10);
$list = $res->items();
$this->success('Success', $res); foreach ($list as &$r) {
$r['content'] = json_decode($r['content'], true);
}
$this->success('Success', ['list' => $list, 'count' => $res->total()]);
} }
// 消息读取 // 消息读取
@@ -341,6 +368,7 @@ class User extends Common
$this->error(__('No rows were found')); $this->error(__('No rows were found'));
} }
$model->save(['status' => 1]); //已读 $model->save(['status' => 1]); //已读
$model['content'] = json_decode($model['content'], true);
$this->success('Success', $model); $this->success('Success', $model);
} }

View File

@@ -139,7 +139,7 @@ class Circle extends Base
'name' => '互动消息', 'name' => '互动消息',
'avatar' => '', 'avatar' => '',
'from_id' => 0, 'from_id' => 0,
'target_id' => $circle->user_id, 'user_id' => $circle->user_id,
'content' => json_encode([ 'content' => json_encode([
'topic' => '点赞', 'topic' => '点赞',
'content' => $user['nickname'] . '点赞了你的帖子', 'content' => $user['nickname'] . '点赞了你的帖子',
@@ -233,9 +233,9 @@ class Circle extends Base
$model->save(['status' => $params['status']]); $model->save(['status' => $params['status']]);
(new Message())->allowField(true)->save([ // 消息通知 (new Message())->allowField(true)->save([ // 消息通知
'type' => 3, 'type' => 3,
'name' => '系统通知', 'name' => '通知消息',
'avatar' => '', 'avatar' => '',
'from_id' => 0, 'from_id' => 1,
'target_id' => $model->user_id, 'target_id' => $model->user_id,
'content' => json_encode([ 'content' => json_encode([
'topic' => '影圈审核', 'topic' => '影圈审核',