// +---------------------------------------------------------------------- declare(strict_types=1); namespace addon\iot\controller\backend; use think\facade\Db; use think\facade\View; use ywxapp\controller\BackendBase; /** * 认证账号控制器 * * 注意:本类名为 Auth,与 service\Auth 同名, * 内部引用密码哈希等需使用全限定名 \addon\iot\service\Auth::hashPassword()。 */ class Auth extends BackendBase { /** * 认证账号页(layui.table 前端拉取) */ public function auth() { return View::fetch('admin/auth'); } /** * 账号列表(layui.table 分页 JSON) */ public function authTable() { $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_auth')->order('id', 'desc'); $count = (int) (clone $q)->count(); $rows = $q->page($page, $limit)->select()->toArray(); // 密文不返回前端 foreach ($rows as &$r) { unset($r['password']); } unset($r); $this->result->success(['list' => $rows, 'count' => $count]); } public function saveAuth() { $id = (int) input('post.id', 0); $username = trim((string) input('post.username', '')); $password = (string) input('post.password', ''); $isSuper = (int) input('post.is_superuser', 0); $status = (int) input('post.status', 1); $remark = (string) input('post.remark', ''); if ($username === '') { $this->result->error('用户名不能为空'); } $data = [ 'username' => $username, 'is_superuser' => $isSuper ? 1 : 0, 'status' => $status ? 1 : 0, 'remark' => $remark, 'update_at' => time(), ]; // 仅当填写了密码才更新(编辑时留空表示不改) if ($password !== '') { $data['password'] = \addon\iot\service\Auth::hashPassword($password); } try { if ($id > 0) { Db::name('iot_auth')->where('id', $id)->update($data); } else { if ($password === '') { $this->result->error('新增账号必须设置密码'); } $data['create_at'] = time(); Db::name('iot_auth')->insert($data); } } catch (\Throwable $e) { $this->result->error('保存失败:' . $e->getMessage()); } $this->result->success('保存成功'); } public function deleteAuth() { $id = (int) input('post.id', 0); Db::name('iot_auth')->where('id', $id)->delete(); $this->result->success('已删除'); } }