// +---------------------------------------------------------------------- declare(strict_types=1); namespace addon\iot\controller\backend; use think\facade\Db; use think\facade\View; use ywxapp\controller\BackendBase; /** * ACL 规则控制器 */ class Acl extends BackendBase { /** * ACL 规则页(layui.table 前端拉取) */ public function acl() { return View::fetch('admin/acl'); } /** * ACL 列表(layui.table 分页 JSON,按 sort 升序) */ public function aclTable() { $page = max(1, (int) input('get.page', 1)); $limit = (int) input('get.limit', 20); if ($limit < 1 || $limit > 100) { $limit = 20; } $q = Db::name('iot_acl')->order('sort', 'asc')->order('id', 'asc'); $count = (int) (clone $q)->count(); $rows = $q->page($page, $limit)->select()->toArray(); $this->result->success(['list' => $rows, 'count' => $count]); } public function saveAcl() { $id = (int) input('post.id', 0); $data = [ 'target_type' => (string) input('post.target_type', 'all'), 'target' => (string) input('post.target', ''), 'topic' => (string) input('post.topic', ''), 'access' => (int) input('post.access', 3), 'allow' => (int) input('post.allow', 1), 'sort' => (int) input('post.sort', 0), 'remark' => (string) input('post.remark', ''), ]; if ($data['topic'] === '') { $this->result->error('主题过滤器不能为空'); } try { if ($id > 0) { Db::name('iot_acl')->where('id', $id)->update($data); } else { $data['create_at'] = time(); Db::name('iot_acl')->insert($data); } } catch (\Throwable $e) { $this->result->error('保存失败:' . $e->getMessage()); } $this->result->success('保存成功(最长 30s 后在 Broker 生效)'); } public function deleteAcl() { $id = (int) input('post.id', 0); Db::name('iot_acl')->where('id', $id)->delete(); $this->result->success('已删除'); } }