From 557d70b0aae5cb5bf781ca94d9b66ed0cf3d1607 Mon Sep 17 00:00:00 2001 From: zhang zhuo Date: Mon, 17 Nov 2025 16:19:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Controller/Admin/Base.php | 17 +++- app/Controller/Admin/System.php | 145 ++++++++++++++++++++++++++------ app/Model/GenTable.php | 67 +++++++++++++++ app/Model/GenTableColumn.php | 44 ++++++++++ app/Model/Online.php | 2 +- app/Utils/AppInfoHelper.php | 6 +- app/Utils/Crud.php | 11 ++- composer.json | 1 - 8 files changed, 256 insertions(+), 37 deletions(-) create mode 100644 app/Model/GenTable.php create mode 100644 app/Model/GenTableColumn.php diff --git a/app/Controller/Admin/Base.php b/app/Controller/Admin/Base.php index 1df7f2d..ff38f07 100644 --- a/app/Controller/Admin/Base.php +++ b/app/Controller/Admin/Base.php @@ -72,6 +72,21 @@ abstract class Base extends AbstractController return $this->response(1, $msg, $data, $count, $summary); } + /** + * @param int|bool $res + * @return ResponseInterface + */ + public function toAjax(int|bool $res) + { + if (is_int($res)) { + return $res > 0 ? $this->success("操作成功") : $this->error("操作失败"); + } + if (is_bool($res)) { + return $res ? $this->success("操作成功") : $this->error("操作失败"); + } + return $this->error("操作失败"); + } + /** * 响应 * @param int $code @@ -89,6 +104,4 @@ abstract class Base extends AbstractController if ($summary === null) unset($body['summary']); return $this->response->json($body); } - - } \ No newline at end of file diff --git a/app/Controller/Admin/System.php b/app/Controller/Admin/System.php index a4522cd..a54c102 100644 --- a/app/Controller/Admin/System.php +++ b/app/Controller/Admin/System.php @@ -11,10 +11,10 @@ use App\Model\Crontab as cModel; use App\Model\CrontabLog as clModel; use App\Model\Dept as dModel; use App\Model\Menu as mModel; -use App\Model\Online; use App\Model\Online as oModel; use App\Model\Post as pModel; use App\Model\Role as rModel; +use App\Model\GenTable as gtModel; use App\Request\Account as aRequest; use App\Request\Dept as dRequest; use App\Request\Menu as mRequest; @@ -29,11 +29,13 @@ use App\Utils\Param; use App\Utils\RedisInfoHelper; use App\Utils\SystemHelper; use Hyperf\Context\ApplicationContext; +use Hyperf\DbConnection\Db; use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\DeleteMapping; use Hyperf\HttpServer\Annotation\GetMapping; use Hyperf\HttpServer\Annotation\PostMapping; use Hyperf\HttpServer\Annotation\PutMapping; +use function Hyperf\Config\config; #[Controller(prefix: "admin")] class System extends Base @@ -83,10 +85,8 @@ class System extends Base #[Auth(needAuth: false, auth: "menu:del")] public function menuDel() { - $ids = $this->request->input("ids", ""); - if (!$ids) return $this->error("请选择要删除的菜单"); - $res = mModel::del($ids); - return $res ? $this->success("操作成功") : $this->error("操作失败"); + $ids = $this->request->input("ids"); + return $this->toAjax(mModel::del($ids)); } #[PostMapping(path: "menu/quick")] @@ -143,10 +143,8 @@ class System extends Base #[Auth(auth: "dept:del")] public function deptDel() { - $ids = $this->request->input("ids", ""); - if (!$ids) return $this->error("请选择要删除的菜单"); - $res = dModel::del($ids); - return $res ? $this->success("操作成功") : $this->error("操作失败"); + $ids = $this->request->input("ids"); + return $this->toAjax(dModel::del($ids)); } // 角色列表 @@ -197,10 +195,8 @@ class System extends Base #[Auth(auth: "role:del")] public function roleDel() { - $ids = $this->request->input("ids", ""); - if (!$ids) return $this->error("请选择要删除的角色"); - $res = rModel::del($ids); - return $res ? $this->success("操作成功") : $this->error("操作失败"); + $ids = $this->request->input("ids"); + return $this->toAjax(rModel::del($ids)); } #[GetMapping(path: "post/list")] @@ -246,10 +242,8 @@ class System extends Base #[Auth(auth: "post:del")] public function postDel() { - $ids = $this->request->input("ids", ""); - if (!$ids) return $this->error("请选择要删除的岗位"); - $res = pModel::del($ids); - return $res ? $this->success("操作成功") : $this->error("操作失败"); + $ids = $this->request->input("ids"); + return $this->toAjax(pModel::del($ids)); } // 账号列表 @@ -376,7 +370,7 @@ class System extends Base { $param = Param::only(['session_id' => '']); // 退出登录状态 - $online = Online::getByUuid($param['session_id']); + $online = oModel::getByUuid($param['session_id']); if (empty($online) || $online['status'] == 0) { return $this->success("用户已不在线"); } @@ -385,7 +379,7 @@ class System extends Base $redis->del("USER:" . $param['session_id']); $redis->del("AUTH:" . $online['account_id']); // 设置离线 - Online::leave($param['session_id']); + oModel::leave($param['session_id']); return $this->success("强退成功"); } @@ -432,13 +426,8 @@ class System extends Base #[Auth(auth: "crontab:del")] public function crontabDel() { - $ids = $this->request->input("ids", ""); - if (!$ids) return $this->error("操作失败"); - $res = cModel::del($ids); - if ($res) { - return $this->success("操作成功"); - } - return $this->error("操作失败"); + $ids = $this->request->input("ids"); + return $this->toAjax(cModel::del($ids)); } #[GetMapping(path: "crontab_log/list")] @@ -454,8 +443,7 @@ class System extends Base public function crontabLogDel() { $param = Param::only(['ids']); - $res = clModel::del($param['ids']); - return $res ? $this->success("操作成功") : $this->error("操作失败"); + return $this->toAjax(clModel::del($param['ids'])); } #[DeleteMapping(path: "crontab_log/remove_all")] @@ -466,4 +454,105 @@ class System extends Base $res = clModel::removeAll($param); return $res ? $this->success("操作成功") : $this->error("操作失败"); } + + #[GetMapping(path: "gen_table/list")] + #[Auth(auth: "gen_table:list")] + public function genTableList() + { + $param = Param::only(['table_name', 'table_comment', 'limit' => 10]); + return $this->success("表列表", gtModel::list($param)); + } + + #[DeleteMapping(path: "gen_table/del")] + #[Auth(auth: "gen_table:del")] + public function genTableDel() + { + $param = Param::only(['ids' => []]); + return $this->toAjax(gtModel::del($param['ids'])); + } + + #[GetMapping(path: "gen_table/select")] + #[Auth(auth: "gen_table:select")] + public function genTableSelect() + { + $param = Param::only(['table_name', 'table_comment', 'limit' => 10, 'page' => 1]); + $offset = ($param['page'] - 1) * $param['limit']; + // 获取所有表 + $rows = Db::select("SHOW TABLE STATUS"); + // 获取已经导入的表 + $tables = gtModel::pluck("table_name")->toArray(); + // 表前缀 + $prefix = config("databases.default.prefix"); + // 构建表不导入 gen_table gen_table_column + $tables = array_merge([$prefix . "gen_table", $prefix . "gen_table_column"], $tables); + $rows = array_filter($rows, function ($item) use ($tables, $param) { + // 过滤表 + if (in_array($item->Name, $tables)) { + return false; + } + // 表名筛选 + if (isset($param['table_name']) && $param['table_name'] != "" && !str_contains($item->Name, $param['table_name'])) { + return false; + } + // 表描述筛选 + if (isset($param['table_comment']) && $param['table_comment'] != "" && !str_contains($item->Comment, $param['table_comment'])) { + return false; + } + return true; + }); + $count = count($rows); + $rows = array_slice($rows, $offset, $param['limit']); + // 过滤返回数据 + $data = []; + foreach ($rows as $row) { + $data[] = [ + 'table_name' => $row->Name, + 'table_comment' => $row->Comment, + 'create_time' => $row->Create_time, + 'update_time' => $row->Update_time + ]; + } + return $this->success("表列表", $data, $count); + } + + #[PostMapping(path: "gen_table/build")] + #[Auth(auth: "gen_table:build")] + public function genTableBuild() + { + // 生成表数据 + $param = Param::only(['names' => []]); + if (empty($param['names'])) { + return $this->error("数据表不为空"); + } + // 获取已经导入的表 + $tables = gtModel::pluck("table_name")->toArray(); + // 表前缀 + $prefix = config("databases.default.prefix"); + // 构建表不导入 gen_table gen_table_column + $tables = array_merge([$prefix . "gen_table", $prefix . "gen_table_column"], $tables); + // 获取所有表 + $rows = Db::select("SHOW TABLE STATUS"); + // 去重 + $rows = array_filter($rows, function ($item) use ($tables, $param) { + // 过滤表 + if (in_array($item->Name, $tables)) { + return false; + } + // 表名筛选 + if (isset($param['table_name']) && $param['table_name'] != "" && !str_contains($item->Name, $param['table_name'])) { + return false; + } + // 表描述筛选 + if (isset($param['table_comment']) && $param['table_comment'] != "" && !str_contains($item->Comment, $param['table_comment'])) { + return false; + } + return true; + }); + if (empty($rows)) { + return $this->error("数据表不为空"); + } + // 生成数据 + $res = gtModel::genTable($rows); + return $res ? $this->success("操作成功") : $this->error("操作失败"); + } } \ No newline at end of file diff --git a/app/Model/GenTable.php b/app/Model/GenTable.php new file mode 100644 index 0000000..41824af --- /dev/null +++ b/app/Model/GenTable.php @@ -0,0 +1,67 @@ + 'integer']; + + /** + * @param array $param + */ + public static function list(array $param) + { + return (new self())->when(isset($param['table_name']) && $param['table_name'] != "", function ($query) use ($param) { + $query->where('table_name', "like", "%{$param['table_name']}%"); + })->when(isset($param['table_comment']) && $param['table_comment'] != "", function ($query) use ($param) { + $query->where('table_comment', "like", "%{$param['table_comment']}%"); + })->select(['table_id', 'table_name', 'table_comment', 'controller_name', 'module_name', 'remark', 'create_time', 'update_time']) + ->orderByDesc('table_id')->paginate((int)$param['limit']); + } + + public static function genTable(array $rows) + { + Db::beginTransaction(); + foreach ($rows as $row) { + var_dump($row); + // 添加主表 +// $res = self::insert([ +// 'table_name' => $row, +// ]); +// $columns = Db::select("SHOW FULL COLUMNS FROM `{$row}`"); +// var_dump($columns); + } + Db::commit(); + return true; + } +} diff --git a/app/Model/GenTableColumn.php b/app/Model/GenTableColumn.php new file mode 100644 index 0000000..3edda37 --- /dev/null +++ b/app/Model/GenTableColumn.php @@ -0,0 +1,44 @@ + 'integer', 'table_id' => 'integer', 'is_insert' => 'integer', 'is_edit' => 'integer', 'is_list' => 'integer', 'is_query' => 'integer', 'rank' => 'integer']; +} diff --git a/app/Model/Online.php b/app/Model/Online.php index 771c026..b99abca 100644 --- a/app/Model/Online.php +++ b/app/Model/Online.php @@ -49,7 +49,7 @@ class Online extends Model $q->where('username', 'like', "%{$param['username']}%"); }) ->orderByDesc("online_id") - ->select(['session_id', 'username', 'ip', 'ua', 'online_time', 'account_id']) + ->select(['session_id', 'username', 'ip', 'ua', 'online_time', 'account_id', 'update_time']) ->paginate((int)$param['limit']); } diff --git a/app/Utils/AppInfoHelper.php b/app/Utils/AppInfoHelper.php index 0f163cb..a4a75ac 100644 --- a/app/Utils/AppInfoHelper.php +++ b/app/Utils/AppInfoHelper.php @@ -74,9 +74,9 @@ class AppInfoHelper private static function formatDuration(int $seconds): string { - $h = floor($seconds / 3600); + $d = floor($seconds / 86400); + $h = floor(($seconds % 86400) / 3600); $m = floor(($seconds % 3600) / 60); - $s = $seconds % 60; - return sprintf('%2d天%2d小时%2d分钟', $h, $m, $s); + return sprintf('%d天%d小时%d分钟', $d, $h, $m); } } \ No newline at end of file diff --git a/app/Utils/Crud.php b/app/Utils/Crud.php index c436d63..569a771 100644 --- a/app/Utils/Crud.php +++ b/app/Utils/Crud.php @@ -21,9 +21,16 @@ trait Crud return $model->where($model->primaryKey, $data[$model->primaryKey])->update($data); } - public static function del(string $ids): int + public static function del(array $ids = null): int { - return self::destroy(explode(",", $ids)); + if (empty($ids)) { + $param = Param::only(['ids' => []]); + $ids = $param['ids']; + } + if (count($ids) <= 0) { + return false; + } + return self::destroy($ids); } public static function getById(int $id, array $field = ['*']): array diff --git a/composer.json b/composer.json index c60f776..8468f87 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,6 @@ "hyperf/process": "~3.1.0", "hyperf/rate-limit": "^3.1", "hyperf/redis": "~3.1.0", - "hyperf/swagger": "^3.1", "hyperf/validation": "^3.1", "hyperf/websocket-server": "^3.1", "phpoffice/phpspreadsheet": "^4.5",