This commit is contained in:
walkor 2022-12-08 18:46:14 +08:00
parent 91a2d54091
commit 2f6bebb964
2 changed files with 45 additions and 30 deletions

View File

@ -400,7 +400,7 @@ class TableController extends Base
$pid = $request->post('pid', 0); $pid = $request->post('pid', 0);
$icon = $request->post('icon', ''); $icon = $request->post('icon', '');
$controller_file = '/' . trim($request->post('controller', ''), '/'); $controller_file = '/' . trim($request->post('controller', ''), '/');
$model_file = '/' . trim($request->post('model', ''), ''); $model_file = '/' . trim($request->post('model', ''), '/');
$overwrite = $request->post('overwrite'); $overwrite = $request->post('overwrite');
if ($controller_file === '/' || $model_file === '/') { if ($controller_file === '/' || $model_file === '/') {
return $this->json(1, '控制器和model不能为空'); return $this->json(1, '控制器和model不能为空');
@ -408,8 +408,8 @@ class TableController extends Base
$controller_info = pathinfo($controller_file); $controller_info = pathinfo($controller_file);
$model_info = pathinfo($model_file); $model_info = pathinfo($model_file);
$controller_path = '/' . Util::filterPath($controller_info['dirname'] ?? ''); $controller_path = Util::filterPath($controller_info['dirname'] ?? '');
$model_path = '/' . Util::filterPath($model_info['dirname'] ?? ''); $model_path = Util::filterPath($model_info['dirname'] ?? '');
$controller_file_name = Util::filterAlphaNum($controller_info['filename'] ?? ''); $controller_file_name = Util::filterAlphaNum($controller_info['filename'] ?? '');
$model_file_name = Util::filterAlphaNum($model_info['filename'] ?? ''); $model_file_name = Util::filterAlphaNum($model_info['filename'] ?? '');
@ -428,20 +428,32 @@ class TableController extends Base
if (!$overwrite) { if (!$overwrite) {
if (is_file(base_path($controller_file))) { if (is_file(base_path($controller_file))) {
return $this->json(1, "/$controller_file 已经存在"); return $this->json(1, "$controller_file 已经存在");
} }
if (is_file(base_path($model_file))) { if (is_file(base_path($model_file))) {
return $this->json(1, "/$model_file 已经存在"); return $this->json(1, "$model_file 已经存在");
} }
} }
$explode = explode('/', trim(strtolower($controller_path), '/'));
$plugin = ''; $plugin = '';
if (strpos($controller_file, '/plugin/') === 0) { if ($explode[0] === 'plugin') {
echo "innn?"; if (count($explode) < 4) {
if (!preg_match('/^\/plugin\/(.*?)\//', $controller_file, $match)) { return $this->json(2, '控制器参数非法');
return $this->json(2, '参数非法');
} }
$plugin = $match[1]; $plugin = $explode[1];
if ($explode[2] !== 'app') {
return $this->json(2, '控制器必须在app目录');
}
$app = $explode[3] !== 'controller' ? $explode[3] : '';
} else {
if (count($explode) < 3) {
return $this->json(2, '控制器参数非法');
}
if ($explode[0] !== 'app') {
return $this->json(2, '控制器必须在app目录');
}
$app = $explode[1] !== 'controller' ? $explode[1] : '';
} }
$model_class = $model_file_name; $model_class = $model_file_name;
@ -456,12 +468,14 @@ class TableController extends Base
// 创建controller // 创建controller
$controller_url_name = $controller_suffix && substr($controller_class, -strlen($controller_suffix)) === $controller_suffix ? substr($controller_class, 0, -strlen($controller_suffix)) : $controller_class; $controller_url_name = $controller_suffix && substr($controller_class, -strlen($controller_suffix)) === $controller_suffix ? substr($controller_class, 0, -strlen($controller_suffix)) : $controller_class;
$controller_url_name = str_replace('_', '-', $inflector->tableize($controller_url_name)); $controller_url_name = str_replace('_', '-', $inflector->tableize($controller_url_name));
$explode = explode('/', trim(strtolower($controller_path), '/'));
if ($plugin) { if ($plugin) {
array_splice($explode, 0, 2); array_splice($explode, 0, 2);
} }
array_shift($explode); array_shift($explode);
if ($app) {
array_shift($explode);
}
foreach ($explode as $index => $item) { foreach ($explode as $index => $item) {
if ($item === 'controller') { if ($item === 'controller') {
unset($explode[$index]); unset($explode[$index]);
@ -474,11 +488,12 @@ class TableController extends Base
$this->createController($controller_class, $controller_namespace, base_path($controller_file), $model_class, $model_namespace, $title, $template_path); $this->createController($controller_class, $controller_namespace, base_path($controller_file), $model_class, $model_namespace, $title, $template_path);
// 创建模版 // 创建模版
$template_file_path = ($plugin ? "/plugin/$plugin" : '') . "/app/view/$template_path"; $template_file_path = ($plugin ? "/plugin/$plugin" : '') . '/app/' . ($app ? "$app/" : '') . 'view/' . $template_path;
$model_class_with_namespace = "$model_namespace\\$model_class"; $model_class_with_namespace = "$model_namespace\\$model_class";
$primary_key = (new $model_class_with_namespace)->getKeyName(); $primary_key = (new $model_class_with_namespace)->getKeyName();
$url_path_base = $plugin ? "/app/$plugin/" : '/'; $url_path_base = ($plugin ? "/app/$plugin/" : '/') . ($app ? "$app/" : '') . $template_path;
$this->createTemplate(base_path($template_file_path), $table_name, $template_path, $url_path_base, $primary_key, "$controller_namespace\\$controller_class"); $this->createTemplate(base_path($template_file_path), $table_name, $url_path_base, $primary_key, "$controller_namespace\\$controller_class");
$menu = Rule::where('key', $controller_class_with_namespace)->first(); $menu = Rule::where('key', $controller_class_with_namespace)->first();
if (!$menu) { if (!$menu) {
@ -488,7 +503,7 @@ class TableController extends Base
$menu->key = $controller_class_with_namespace; $menu->key = $controller_class_with_namespace;
$menu->title = $title; $menu->title = $title;
$menu->icon = $icon; $menu->icon = $icon;
$menu->href = "$url_path_base$template_path/index"; $menu->href = "$url_path_base/index";
$menu->save(); $menu->save();
$roles = admin('roles'); $roles = admin('roles');
@ -687,7 +702,7 @@ EOF;
* @param $controller_class_with_namespace * @param $controller_class_with_namespace
* @return void * @return void
*/ */
protected function createTemplate($template_file_path, $table, $template_path, $url_path_base, $primary_key, $controller_class_with_namespace) protected function createTemplate($template_file_path, $table, $url_path_base, $primary_key, $controller_class_with_namespace)
{ {
$this->mkdir($template_file_path . '/index.html'); $this->mkdir($template_file_path . '/index.html');
$code_base = Util::controllerToUrlPath($controller_class_with_namespace); $code_base = Util::controllerToUrlPath($controller_class_with_namespace);
@ -767,11 +782,11 @@ EOF
// 相关常量 // 相关常量
const PRIMARY_KEY = "$primary_key"; const PRIMARY_KEY = "$primary_key";
const SELECT_API = "$url_path_base$template_path/select"; const SELECT_API = "$url_path_base/select";
const UPDATE_API = "$url_path_base$template_path/update"; const UPDATE_API = "$url_path_base/update";
const DELETE_API = "$url_path_base$template_path/delete"; const DELETE_API = "$url_path_base/delete";
const INSERT_URL = "$url_path_base$template_path/insert"; const INSERT_URL = "$url_path_base/insert";
const UPDATE_URL = "$url_path_base$template_path/update"; const UPDATE_URL = "$url_path_base/update";
$js $js
// 表格渲染 // 表格渲染
layui.use(["table", "form", "common", "popup", "util"], function() { layui.use(["table", "form", "common", "popup", "util"], function() {
@ -950,7 +965,7 @@ EOF;
<script> <script>
// 相关接口 // 相关接口
const INSERT_API = "$url_path_base$template_path/insert"; const INSERT_API = "$url_path_base/insert";
$js $js
//提交事件 //提交事件
layui.use(["form", "popup"], function () { layui.use(["form", "popup"], function () {
@ -1026,8 +1041,8 @@ EOF;
// 相关接口 // 相关接口
const PRIMARY_KEY = "$primary_key"; const PRIMARY_KEY = "$primary_key";
const SELECT_API = "$url_path_base$template_path/select" + location.search; const SELECT_API = "$url_path_base/select" + location.search;
const UPDATE_API = "$url_path_base$template_path/update"; const UPDATE_API = "$url_path_base/update";
// 获取数据库记录 // 获取数据库记录
layui.use(["form", "util"], function () { layui.use(["form", "util"], function () {

View File

@ -20,9 +20,9 @@
<div class="main-container"> <div class="main-container">
<input type="hidden" name="table" value="<?=htmlspecialchars($table)?>"> <input type="hidden" name="table" value="<?=htmlspecialchars($table)?>">
<div class="layui-form-item"> <div class="layui-form-item">
<label class="layui-form-label">菜单名</label> <label class="layui-form-label required">菜单名</label>
<div class="layui-input-block"> <div class="layui-input-block">
<input type="text" name="title" value="" class="layui-input"> <input type="text" name="title" value="" required lay-verify="required" class="layui-input">
</div> </div>
</div> </div>
<div class="layui-form-item"> <div class="layui-form-item">
@ -38,15 +38,15 @@
</div> </div>
</div> </div>
<div class="layui-form-item"> <div class="layui-form-item">
<label class="layui-form-label">控制器</label> <label class="layui-form-label required">控制器</label>
<div class="layui-input-block"> <div class="layui-input-block">
<input type="text" name="controller" value="<?=$controller?>" class="layui-input" placeholder="自动生成请留空"> <input type="text" name="controller" value="<?=$controller?>" class="layui-input" required lay-verify="required">
</div> </div>
</div> </div>
<div class="layui-form-item"> <div class="layui-form-item">
<label class="layui-form-label">模型</label> <label class="layui-form-label required">模型</label>
<div class="layui-input-block"> <div class="layui-input-block">
<input type="text" name="model" value="<?=$model?>" class="layui-input" placeholder="自动生成请留空"> <input type="text" name="model" value="<?=$model?>" class="layui-input" required lay-verify="required">
</div> </div>
</div> </div>
<div class="layui-form-item"> <div class="layui-form-item">