This commit is contained in:
walkor 2022-12-25 16:29:06 +08:00
parent da6b3cc18e
commit 1c1b2fb2c2
12 changed files with 48 additions and 27 deletions

View File

@ -89,7 +89,7 @@ class Crud extends Base
$where = $request->get(); $where = $request->get();
$page = (int)$request->get('page'); $page = (int)$request->get('page');
$page = $page > 0 ? $page : 1; $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`"); $allow_column = Util::db()->select("desc `$table`");
if (!$allow_column) { if (!$allow_column) {
@ -267,7 +267,7 @@ class Crud extends Base
*/ */
protected function inputFilter(array $data): array 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`"); $allow_column = Util::db()->select("desc `$table`");
if (!$allow_column) { if (!$allow_column) {
throw new BusinessException('表不存在', 2); throw new BusinessException('表不存在', 2);

View File

@ -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)) { if (!is_file($sql_file)) {
return $this->json(1, '数据库SQL文件不存在'); return $this->json(1, '数据库SQL文件不存在');
} }
@ -119,7 +119,7 @@ return [
'password' => '$password', 'password' => '$password',
'charset' => 'utf8mb4', 'charset' => 'utf8mb4',
'collation' => 'utf8mb4_general_ci', 'collation' => 'utf8mb4_general_ci',
'prefix' => '', 'prefix' => 'wa_',
'strict' => true, 'strict' => true,
'engine' => null, 'engine' => null,
], ],
@ -155,7 +155,7 @@ return [
// 数据库编码默认采用utf8 // 数据库编码默认采用utf8
'charset' => 'utf8mb4', 'charset' => 'utf8mb4',
// 数据库表前缀 // 数据库表前缀
'prefix' => '', 'prefix' => 'wa_',
// 断线重连 // 断线重连
'break_reconnect' => true, 'break_reconnect' => true,
// 关闭SQL监听日志 // 关闭SQL监听日志
@ -202,13 +202,12 @@ EOF;
$config = include $config_file; $config = include $config_file;
$connection = $config['connections']['mysql']; $connection = $config['connections']['mysql'];
$pdo = $this->getPdo($connection['host'], $connection['username'], $connection['password'], $connection['port'], $connection['database']); $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'); $time = date('Y-m-d H:i:s');
$data = [ $data = [
'username' => $username, 'username' => $username,
'password' => Util::passwordHash($password), 'password' => Util::passwordHash($password),
'nickname' => '超级管理员', 'nickname' => '超级管理员',
'roles' => '1',
'created_at' => $time, 'created_at' => $time,
'updated_at' => $time 'updated_at' => $time
]; ];
@ -216,6 +215,13 @@ EOF;
$smt->bindValue($key, $value); $smt->bindValue($key, $value);
} }
$smt->execute(); $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(); $request->session()->flush();
return $this->json(0); return $this->json(0);
} }

View File

@ -74,7 +74,7 @@ class RoleController extends Crud
if ($request->method() === 'POST') { if ($request->method() === 'POST') {
$data = $this->insertInput($request); $data = $this->insertInput($request);
$pid = $data['pid'] ?? null; $pid = $data['pid'] ?? null;
if ($pid) { if (!$pid) {
return $this->json(1, '请选择父级角色组'); return $this->json(1, '请选择父级角色组');
} }
if (!Auth::isSupperAdmin() && !in_array($pid, Auth::getScopeRoleIds(true))) { if (!Auth::isSupperAdmin() && !in_array($pid, Auth::getScopeRoleIds(true))) {

View File

@ -73,7 +73,7 @@ class TableController extends Base
$table_names = array_column($tables, 'TABLE_NAME'); $table_names = array_column($tables, 'TABLE_NAME');
$table_rows_count = []; $table_rows_count = [];
foreach ($table_names as $table_name) { 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) { foreach ($tables as $key => $table) {
$tables[$key]->TABLE_ROWS = $table_rows_count[$table->TABLE_NAME] ?? $table->TABLE_ROWS; $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::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) { foreach ($keys as $key) {
$name = $key['name']; $name = $key['name'];
$columns = is_array($key['columns']) ? $key['columns'] : explode(',', $key['columns']); $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'); $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) { foreach ($columns as $column) {
$field = $column['field']; $field = $column['field'];
// 新字段 // 新字段
@ -315,7 +315,7 @@ class TableController extends Base
} }
$old_keys = Util::getSchema($table_name, 'keys'); $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) { foreach ($keys as $key) {
$key_name = $key['name']; $key_name = $key['name'];
$old_key = $old_keys[$key_name] ?? []; $old_key = $old_keys[$key_name] ?? [];
@ -385,7 +385,8 @@ class TableController extends Base
{ {
$table_name = $request->input('table'); $table_name = $request->input('table');
Util::checkTableName($table_name); 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(); $inflector = InflectorFactory::create()->build();
$model_class = $inflector->classify($inflector->singularize($table_basename)); $model_class = $inflector->classify($inflector->singularize($table_basename));
$base_path = '/plugin/admin/app'; $base_path = '/plugin/admin/app';
@ -463,7 +464,7 @@ class TableController extends Base
$model_namespace = str_replace('/' , '\\', trim($model_path, '/')); $model_namespace = str_replace('/' , '\\', trim($model_path, '/'));
// 创建model // 创建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_suffix = $plugin ? config("plugin.$plugin.app.controller_suffix") : config('app.controller_suffix');
$controller_class = $controller_file_name; $controller_class = $controller_file_name;
@ -1158,7 +1159,7 @@ EOF;
$field = current($allow_column); $field = current($allow_column);
} }
$order = $order === 'asc' ? 'asc' : 'desc'; $order = $order === 'asc' ? 'asc' : 'desc';
$paginator = Util::db()->table($table); $paginator = Util::db()->table($this->getTableName($table));
foreach ($request->get() as $column => $value) { foreach ($request->get() as $column => $value) {
if ($value === '') { if ($value === '') {
continue; continue;
@ -1247,7 +1248,7 @@ EOF;
if (isset($columns['updated_at']) && empty($data['updated_at'])) { if (isset($columns['updated_at']) && empty($data['updated_at'])) {
$data['updated_at'] = $datetime; $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); return $this->json(0, $id);
} }
@ -1314,7 +1315,7 @@ EOF;
if (isset($columns['updated_at']) && empty($data['updated_at'])) { if (isset($columns['updated_at']) && empty($data['updated_at'])) {
$data['updated_at'] = $datetime; $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); return $this->json(0);
} }
@ -1337,7 +1338,7 @@ EOF;
} }
$primary_key = $primary_keys[0]; $primary_key = $primary_keys[0];
$value = (array)$request->post($primary_key); $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); return $this->json(0);
} }
@ -1353,14 +1354,15 @@ EOF;
if (!$tables) { if (!$tables) {
return $this->json(0, 'not found'); 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)) { if ($found = array_intersect($tables, $table_not_allow_drop)) {
return $this->json(400, implode(',', $found) . '不允许删除'); return $this->json(400, implode(',', $found) . '不允许删除');
} }
foreach ($tables as $table) { foreach ($tables as $table) {
Util::schema()->drop($table); Util::schema()->drop($table);
// 删除schema // 删除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'); 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;
}
} }

View File

@ -25,7 +25,7 @@ class Admin extends Base
* *
* @var string * @var string
*/ */
protected $table = 'wa_admins'; protected $table = 'admins';
/** /**
* The primary key associated with the table. * The primary key associated with the table.

View File

@ -16,7 +16,7 @@ class AdminRole extends Base
* *
* @var string * @var string
*/ */
protected $table = 'wa_admin_roles'; protected $table = 'admin_roles';
/** /**
* The primary key associated with the table. * The primary key associated with the table.

View File

@ -17,7 +17,7 @@ class Option extends Base
* *
* @var string * @var string
*/ */
protected $table = 'wa_options'; protected $table = 'options';
/** /**
* The primary key associated with the table. * The primary key associated with the table.

View File

@ -18,7 +18,7 @@ class Role extends Base
* *
* @var string * @var string
*/ */
protected $table = 'wa_roles'; protected $table = 'roles';
/** /**
* The primary key associated with the table. * The primary key associated with the table.

View File

@ -23,7 +23,7 @@ class Rule extends Base
* *
* @var string * @var string
*/ */
protected $table = 'wa_rules'; protected $table = 'rules';
/** /**
* The primary key associated with the table. * The primary key associated with the table.

View File

@ -27,7 +27,7 @@ class Upload extends Base
* *
* @var string * @var string
*/ */
protected $table = 'wa_uploads'; protected $table = 'uploads';
/** /**
* The primary key associated with the table. * The primary key associated with the table.

View File

@ -34,7 +34,7 @@ class User extends Base
* *
* @var string * @var string
*/ */
protected $table = 'wa_users'; protected $table = 'users';
/** /**
* The primary key associated with the table. * The primary key associated with the table.