From 1c1b2fb2c2131d9c81a744a5c72dcc05d623cc6e Mon Sep 17 00:00:00 2001 From: walkor Date: Sun, 25 Dec 2022 16:29:06 +0800 Subject: [PATCH] save --- src/plugin/admin/app/controller/Crud.php | 4 +- .../app/controller/InstallController.php | 16 +++++--- .../admin/app/controller/RoleController.php | 2 +- .../admin/app/controller/TableController.php | 39 +++++++++++++------ src/plugin/admin/app/model/Admin.php | 2 +- src/plugin/admin/app/model/AdminRole.php | 2 +- src/plugin/admin/app/model/Option.php | 2 +- src/plugin/admin/app/model/Role.php | 2 +- src/plugin/admin/app/model/Rule.php | 2 +- src/plugin/admin/app/model/Upload.php | 2 +- src/plugin/admin/app/model/User.php | 2 +- .../admin/{webman-admin.sql => install.sql} | 0 12 files changed, 48 insertions(+), 27 deletions(-) rename src/plugin/admin/{webman-admin.sql => install.sql} (100%) diff --git a/src/plugin/admin/app/controller/Crud.php b/src/plugin/admin/app/controller/Crud.php index 5669ba7..7a255e4 100644 --- a/src/plugin/admin/app/controller/Crud.php +++ b/src/plugin/admin/app/controller/Crud.php @@ -89,7 +89,7 @@ class Crud extends Base $where = $request->get(); $page = (int)$request->get('page'); $page = $page > 0 ? $page : 1; - $table = $this->model->getTable(); + $table = config('plugin.admin.database.connections.mysql.prefix') . $this->model->getTable(); $allow_column = Util::db()->select("desc `$table`"); if (!$allow_column) { @@ -267,7 +267,7 @@ class Crud extends Base */ protected function inputFilter(array $data): array { - $table = $this->model->getTable(); + $table = config('plugin.admin.database.connections.mysql.prefix') . $this->model->getTable(); $allow_column = Util::db()->select("desc `$table`"); if (!$allow_column) { throw new BusinessException('表不存在', 2); diff --git a/src/plugin/admin/app/controller/InstallController.php b/src/plugin/admin/app/controller/InstallController.php index 31f18ee..f23aff0 100644 --- a/src/plugin/admin/app/controller/InstallController.php +++ b/src/plugin/admin/app/controller/InstallController.php @@ -88,7 +88,7 @@ class InstallController extends Base } } - $sql_file = base_path() . '/plugin/admin/webman-admin.sql'; + $sql_file = base_path() . '/plugin/admin/install.sql'; if (!is_file($sql_file)) { return $this->json(1, '数据库SQL文件不存在'); } @@ -119,7 +119,7 @@ return [ 'password' => '$password', 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_general_ci', - 'prefix' => '', + 'prefix' => 'wa_', 'strict' => true, 'engine' => null, ], @@ -155,7 +155,7 @@ return [ // 数据库编码默认采用utf8 'charset' => 'utf8mb4', // 数据库表前缀 - 'prefix' => '', + 'prefix' => 'wa_', // 断线重连 'break_reconnect' => true, // 关闭SQL监听日志 @@ -202,13 +202,12 @@ EOF; $config = include $config_file; $connection = $config['connections']['mysql']; $pdo = $this->getPdo($connection['host'], $connection['username'], $connection['password'], $connection['port'], $connection['database']); - $smt = $pdo->prepare("insert into `wa_admins` (`username`, `password`, `nickname`, `roles`, `created_at`, `updated_at`) values (:username, :password, :nickname, :roles, :created_at, :updated_at)"); + $smt = $pdo->prepare("insert into `wa_admins` (`username`, `password`, `nickname`, `created_at`, `updated_at`) values (:username, :password, :nickname, :created_at, :updated_at)"); $time = date('Y-m-d H:i:s'); $data = [ 'username' => $username, 'password' => Util::passwordHash($password), 'nickname' => '超级管理员', - 'roles' => '1', 'created_at' => $time, 'updated_at' => $time ]; @@ -216,6 +215,13 @@ EOF; $smt->bindValue($key, $value); } $smt->execute(); + $admin_id = $pdo->lastInsertId(); + + $smt = $pdo->prepare("insert into `wa_admin_roles` (`role_id`, `admin_id`) values (:role_id, :admin_id)"); + $smt->bindValue('role_id', 1); + $smt->bindValue('admin_id', $admin_id); + $smt->execute(); + $request->session()->flush(); return $this->json(0); } diff --git a/src/plugin/admin/app/controller/RoleController.php b/src/plugin/admin/app/controller/RoleController.php index f20f8db..0470de8 100644 --- a/src/plugin/admin/app/controller/RoleController.php +++ b/src/plugin/admin/app/controller/RoleController.php @@ -74,7 +74,7 @@ class RoleController extends Crud if ($request->method() === 'POST') { $data = $this->insertInput($request); $pid = $data['pid'] ?? null; - if ($pid) { + if (!$pid) { return $this->json(1, '请选择父级角色组'); } if (!Auth::isSupperAdmin() && !in_array($pid, Auth::getScopeRoleIds(true))) { diff --git a/src/plugin/admin/app/controller/TableController.php b/src/plugin/admin/app/controller/TableController.php index d1c7424..3775b38 100644 --- a/src/plugin/admin/app/controller/TableController.php +++ b/src/plugin/admin/app/controller/TableController.php @@ -73,7 +73,7 @@ class TableController extends Base $table_names = array_column($tables, 'TABLE_NAME'); $table_rows_count = []; foreach ($table_names as $table_name) { - $table_rows_count[$table_name] = Util::db()->table($table_name)->count(); + $table_rows_count[$table_name] = Util::db()->table($this->getTableName($table_name))->count(); } foreach ($tables as $key => $table) { $tables[$key]->TABLE_ROWS = $table_rows_count[$table->TABLE_NAME] ?? $table->TABLE_ROWS; @@ -160,7 +160,7 @@ class TableController extends Base Util::db()->statement("ALTER TABLE `$table_name` COMMENT $table_comment"); // 索引 - Util::schema()->table($table_name, function (Blueprint $table) use ($keys) { + Util::schema()->table($this->getTableName($table_name), function (Blueprint $table) use ($keys) { foreach ($keys as $key) { $name = $key['name']; $columns = is_array($key['columns']) ? $key['columns'] : explode(',', $key['columns']); @@ -288,7 +288,7 @@ class TableController extends Base } $old_columns = Util::getSchema($table_name, 'columns'); - Util::schema()->table($table_name, function (Blueprint $table) use ($columns, $old_columns, $keys, $table_name) { + Util::schema()->table($this->getTableName($table_name), function (Blueprint $table) use ($columns, $old_columns, $keys, $table_name) { foreach ($columns as $column) { $field = $column['field']; // 新字段 @@ -315,7 +315,7 @@ class TableController extends Base } $old_keys = Util::getSchema($table_name, 'keys'); - Util::schema()->table($table_name, function (Blueprint $table) use ($keys, $old_keys, $table_name) { + Util::schema()->table($this->getTableName($table_name), function (Blueprint $table) use ($keys, $old_keys, $table_name) { foreach ($keys as $key) { $key_name = $key['name']; $old_key = $old_keys[$key_name] ?? []; @@ -385,7 +385,8 @@ class TableController extends Base { $table_name = $request->input('table'); Util::checkTableName($table_name); - $table_basename = strpos($table_name, 'wa_') === 0 ? substr($table_name, 3) : $table_name; + $prefix = config('plugin.admin.database.connections.mysql.prefix'); + $table_basename = strpos($table_name, $prefix) === 0 ? substr($table_name, strlen($prefix)) : $table_name; $inflector = InflectorFactory::create()->build(); $model_class = $inflector->classify($inflector->singularize($table_basename)); $base_path = '/plugin/admin/app'; @@ -463,7 +464,7 @@ class TableController extends Base $model_namespace = str_replace('/' , '\\', trim($model_path, '/')); // 创建model - $this->createModel($model_class, $model_namespace, base_path($model_file), $table_name); + $this->createModel($model_class, $model_namespace, base_path($model_file), $this->getTableName($table_name)); $controller_suffix = $plugin ? config("plugin.$plugin.app.controller_suffix") : config('app.controller_suffix'); $controller_class = $controller_file_name; @@ -1158,7 +1159,7 @@ EOF; $field = current($allow_column); } $order = $order === 'asc' ? 'asc' : 'desc'; - $paginator = Util::db()->table($table); + $paginator = Util::db()->table($this->getTableName($table)); foreach ($request->get() as $column => $value) { if ($value === '') { continue; @@ -1247,7 +1248,7 @@ EOF; if (isset($columns['updated_at']) && empty($data['updated_at'])) { $data['updated_at'] = $datetime; } - $id = Util::db()->table($table)->insertGetId($data); + $id = Util::db()->table($this->getTableName($table))->insertGetId($data); return $this->json(0, $id); } @@ -1314,7 +1315,7 @@ EOF; if (isset($columns['updated_at']) && empty($data['updated_at'])) { $data['updated_at'] = $datetime; } - Util::db()->table($table)->where($primary_key, $value)->update($data); + Util::db()->table($this->getTableName($table))->where($primary_key, $value)->update($data); return $this->json(0); } @@ -1337,7 +1338,7 @@ EOF; } $primary_key = $primary_keys[0]; $value = (array)$request->post($primary_key); - Util::db()->table($table)->whereIn($primary_key, $value)->delete(); + Util::db()->table($this->getTableName($table))->whereIn($primary_key, $value)->delete(); return $this->json(0); } @@ -1353,14 +1354,15 @@ EOF; if (!$tables) { return $this->json(0, 'not found'); } - $table_not_allow_drop = ['wa_admins', 'wa_users', 'wa_options', 'wa_roles', 'wa_rules']; + $prefix = config('plugin.admin.database.connections.mysql.prefix'); + $table_not_allow_drop = ["{$prefix}admins", "{$prefix}users", "{$prefix}options", "{$prefix}roles", "{$prefix}rules", "{$prefix}admin_roles", "{$prefix}uploads"]; if ($found = array_intersect($tables, $table_not_allow_drop)) { return $this->json(400, implode(',', $found) . '不允许删除'); } foreach ($tables as $table) { Util::schema()->drop($table); // 删除schema - Util::db()->table('wa_options')->where('name', "table_form_schema_$table")->delete(); + Util::db()->table('options')->where('name', "table_form_schema_$table")->delete(); } return $this->json(0, 'ok'); } @@ -1591,4 +1593,17 @@ EOF; } } + /** + * @param string $table_name + * @return false|string + */ + protected function getTableName(string $table_name) + { + $prefix = config('plugin.admin.database.connections.mysql.prefix'); + if (strpos($table_name, $prefix) === 0) { + return substr($table_name, strlen($prefix)); + } + return $table_name; + } + } diff --git a/src/plugin/admin/app/model/Admin.php b/src/plugin/admin/app/model/Admin.php index f55e932..17d173e 100644 --- a/src/plugin/admin/app/model/Admin.php +++ b/src/plugin/admin/app/model/Admin.php @@ -25,7 +25,7 @@ class Admin extends Base * * @var string */ - protected $table = 'wa_admins'; + protected $table = 'admins'; /** * The primary key associated with the table. diff --git a/src/plugin/admin/app/model/AdminRole.php b/src/plugin/admin/app/model/AdminRole.php index 0b2c2f7..fd7e4e2 100644 --- a/src/plugin/admin/app/model/AdminRole.php +++ b/src/plugin/admin/app/model/AdminRole.php @@ -16,7 +16,7 @@ class AdminRole extends Base * * @var string */ - protected $table = 'wa_admin_roles'; + protected $table = 'admin_roles'; /** * The primary key associated with the table. diff --git a/src/plugin/admin/app/model/Option.php b/src/plugin/admin/app/model/Option.php index 97018b0..326537e 100644 --- a/src/plugin/admin/app/model/Option.php +++ b/src/plugin/admin/app/model/Option.php @@ -17,7 +17,7 @@ class Option extends Base * * @var string */ - protected $table = 'wa_options'; + protected $table = 'options'; /** * The primary key associated with the table. diff --git a/src/plugin/admin/app/model/Role.php b/src/plugin/admin/app/model/Role.php index bf77d88..874f99a 100644 --- a/src/plugin/admin/app/model/Role.php +++ b/src/plugin/admin/app/model/Role.php @@ -18,7 +18,7 @@ class Role extends Base * * @var string */ - protected $table = 'wa_roles'; + protected $table = 'roles'; /** * The primary key associated with the table. diff --git a/src/plugin/admin/app/model/Rule.php b/src/plugin/admin/app/model/Rule.php index ebc7dd1..f9b2753 100644 --- a/src/plugin/admin/app/model/Rule.php +++ b/src/plugin/admin/app/model/Rule.php @@ -23,7 +23,7 @@ class Rule extends Base * * @var string */ - protected $table = 'wa_rules'; + protected $table = 'rules'; /** * The primary key associated with the table. diff --git a/src/plugin/admin/app/model/Upload.php b/src/plugin/admin/app/model/Upload.php index c82d0c5..6abe939 100644 --- a/src/plugin/admin/app/model/Upload.php +++ b/src/plugin/admin/app/model/Upload.php @@ -27,7 +27,7 @@ class Upload extends Base * * @var string */ - protected $table = 'wa_uploads'; + protected $table = 'uploads'; /** * The primary key associated with the table. diff --git a/src/plugin/admin/app/model/User.php b/src/plugin/admin/app/model/User.php index 0fc6665..0927d2b 100644 --- a/src/plugin/admin/app/model/User.php +++ b/src/plugin/admin/app/model/User.php @@ -34,7 +34,7 @@ class User extends Base * * @var string */ - protected $table = 'wa_users'; + protected $table = 'users'; /** * The primary key associated with the table. diff --git a/src/plugin/admin/webman-admin.sql b/src/plugin/admin/install.sql similarity index 100% rename from src/plugin/admin/webman-admin.sql rename to src/plugin/admin/install.sql