// +---------------------------------------------------------------------- declare(strict_types=1); namespace addon\iot\controller\backend; use think\facade\Db; use think\facade\View; use ywxapp\controller\BackendBase; use addon\iot\service\AlertService; /** * 告警中心控制器(后台查全部会员告警) */ class Alerts extends BackendBase { /** * 告警中心页(layui.table 前端拉取) */ public function alertsPage() { return View::fetch('admin/alerts'); } /** * 告警列表(layui.table 分页 JSON,支持只看未读) */ public function alerts() { $page = max(1, (int) input('get.page', 1)); // layui.table 传 limit,兼容原手工分页的 size 命名 $size = max(1, (int) input('get.limit', (int) input('get.size', 20))); $size = min(100, $size); $only = (int) input('get.unread', 0) === 1; $data = AlertService::pageAll($page, $size, $only); // 兼容 layui.table parseData:count 字段 $this->result->success(['list' => $data['list'], 'count' => (int) $data['total']]); } /** 后台:标记告警已读(ids 空=全部) */ public function readAlertAll() { $ids = input('post.ids/a', []); $n = 0; try { $q = Db::name('iot_alert')->where('is_read', 0); if ($ids) { $q->whereIn('id', array_map('intval', $ids)); } $n = (int) $q->update(['is_read' => 1]); } catch (\Throwable $e) { $this->result->error('操作失败:' . $e->getMessage()); } $this->result->success(['affected' => $n]); } }