request->param('category', 'all'); $page = max(1, (int)$this->request->param('page', 1)); $limit = min(50, max(1, (int)$this->request->param('limit', 20))); $mid = $this->masterId(); $build = function () use ($mid, $category) { $q = Db::name('message')->where('receive_type', 'master')->where('receive_id', $mid); if ($category !== 'all' && $category !== '') { $q->where('type', $category); } return $q; }; $total = $build()->count(); $list = $build()->order('id desc')->page($page, $limit)->select(); foreach ($list as &$row) { $row['time_text'] = date('m-d H:i', (int)$row['createtime']); $row['is_read'] = (int)$row['is_read']; } unset($row); $this->success('', [ 'list' => $list, 'total' => (int)$total, 'unread' => $this->unreadCount($mid), 'page' => $page, ]); } public function read() { $id = (int)$this->request->post('id'); $mid = $this->masterId(); $row = Db::name('message')->where('id', $id)->where('receive_type', 'master')->where('receive_id', $mid)->find(); if (!$row) { $this->error('消息不存在'); } Db::name('message')->where('id', $id)->update(['is_read' => 1]); $this->success('已读'); } public function readAll() { $mid = $this->masterId(); $affected = Db::name('message') ->where('receive_type', 'master') ->where('receive_id', $mid) ->where('is_read', 0) ->update(['is_read' => 1]); $this->success('已全部标记为已读', ['count' => (int)$affected]); } public function unread() { $this->success('', ['unread' => $this->unreadCount($this->masterId())]); } protected function unreadCount($mid) { if ($mid <= 0) { return 0; } return (int)Db::name('message') ->where('receive_type', 'master') ->where('receive_id', $mid) ->where('is_read', 0) ->count(); } }