From 9669623f98a95928048bb5bdaeb7ce9ba8f48e86 Mon Sep 17 00:00:00 2001 From: xjh <2423579486@qq.com> Date: Mon, 8 May 2023 22:48:27 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=90=8E=E7=AB=AF=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=94=9F=E6=88=90:=E5=AF=BC=E5=87=BA=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E3=80=81=E6=96=B0=E5=A2=9E=E5=90=8E=E5=8F=B0?= =?UTF-8?q?=E6=8E=A7=E5=88=B6=E5=99=A8=E3=80=81=E6=A8=A1=E5=9E=8B=E5=B1=82?= =?UTF-8?q?=E6=8E=92=E5=BA=8F=E5=A4=84=E7=90=86=E5=8A=9F=E8=83=BD=E3=80=81?= =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E6=A8=A1=E6=9D=BF=E7=94=9F=E6=88=90=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/controller/Gen/GenApi/GenApi.php | 32 +++++- .../Gen/GenApi/GenController/Admin.php | 24 ++-- .../controller/Gen/GenApi/GenModel/Model.php | 8 +- app/resources/view/admin/controller.tpl | 4 +- app/resources/view/admin/model.tpl | 1 - .../business/sort/sortAdminController.tpl | 104 ++++++++++++++++++ .../view/business/sort/sortModel.tpl | 76 +++++++++++++ 7 files changed, 226 insertions(+), 23 deletions(-) create mode 100644 app/resources/view/business/sort/sortAdminController.tpl create mode 100644 app/resources/view/business/sort/sortModel.tpl diff --git a/app/admin/controller/Gen/GenApi/GenApi.php b/app/admin/controller/Gen/GenApi/GenApi.php index 1522f01..5bb82c6 100644 --- a/app/admin/controller/Gen/GenApi/GenApi.php +++ b/app/admin/controller/Gen/GenApi/GenApi.php @@ -56,12 +56,17 @@ class GenApi * @var array */ protected const TEMP_PATH_CONFIG = [ - 'crud' => [ + 'crud' => [ //单表(增删改查) 'api' => 'resources/view/api/controller.tpl', 'admin' => 'resources/view/admin/controller.tpl', 'model' => 'resources/view/admin/model.tpl', ], - 'web_static' => [ + 'crud_sort' => [ //单表(增删改查+排序处理) + 'api' => 'resources/view/api/controller.tpl', + 'admin' => 'resources/view/business/sort/sortAdminController.tpl', + 'model' => 'resources/view/business/sort/sortModel.tpl', + ], + 'web_static' => [ //门户静态 'api' => 'resources/view/business/webApiController.tpl', 'admin' => 'resources/view/business/webController.tpl', 'model' => 'resources/view/admin/model.tpl', @@ -176,6 +181,12 @@ class GenApi */ protected $is_export = false; + /** + * 是否排序处理 + * @var bool + */ + protected $is_sort = false; + /** * 图片字段数组 * @var array @@ -241,6 +252,7 @@ class GenApi $this->initSortInfo(); $this->initRequiredVerifyFieldsTemp(); $this->buildQueryWhereContentTemp(); + $this->initIsSortStatus(); //后端生成所需路径初始化 $this->initModelGenPath(); $this->initAdminControllerGenPath(); @@ -327,7 +339,13 @@ class GenApi */ private function getTempPath(string $type): string { - return $this->root . self::TEMP_PATH_CONFIG[$this->gen_api_type][$type]; + //模板类型 + $temp_type = $this->gen_api_type; + //若排序处理开启,并且模板类型为crud,则使用crud_sort单表(增删改查+排序处理) + if ($this->is_sort) { + $temp_type = ($temp_type == 'crud') ? 'crud_sort' : $temp_type; + } + return $this->root . self::TEMP_PATH_CONFIG[$temp_type][$type]; } /** @@ -353,6 +371,14 @@ class GenApi $this->is_export = in_array('4', $checked_btn_arr); } + /** + * 初始化是否排序处理状态 + */ + private function initIsSortStatus(): void + { + $this->is_sort = ($this->table['isSort'] == 1); + } + /** * 构建新增允许字段模板字符 */ diff --git a/app/admin/controller/Gen/GenApi/GenController/Admin.php b/app/admin/controller/Gen/GenApi/GenController/Admin.php index 6432085..8dff2fd 100644 --- a/app/admin/controller/Gen/GenApi/GenController/Admin.php +++ b/app/admin/controller/Gen/GenApi/GenController/Admin.php @@ -64,22 +64,11 @@ class Admin extends GenApi { if (!$this->is_export) return ''; - //提取业务字段名称(模板字符串) - $business_fields = []; - foreach ($this->business_fields as $key => $value) { - $business_fields[] = $key; - }; - $business_fields_temp = self::toFormTempStr($business_fields); - //模板字符串构建并返回 return "/**\n* 导出Excel\n*/ public function exportExcel(Request \$request):void { - \$params = \$request->param(); - \$select = Model{$this->class_name}::field({$business_fields_temp}) - ->order('{$this->sort_field}', '{$this->sort_type}' ) - ->select()->toArray(); - Model{$this->class_name}::exportExcel(\$select); + Model{$this->class_name}::exportExcel(self::get{$this->class_name}List(\$request, true)); }"; } @@ -111,12 +100,21 @@ class Admin extends GenApi { if (!$this->is_import) return ''; + $initial_value_temp = ''; + for($i =0 ;$ibusiness_fields);$i++){ + $index =$i+1; + $initial_value_temp .= "'默认值{$index}',"; + } + //模板字符串构建并返回 return "/**\n* 下载导入模板\n*/ public function downloadTemplate(Request \$request):void { \$params = \$request->param(); - \$data = array_values(Model{$this->class_name}::EXCELFIELD); + \$data = [ + array_values(Model{$this->class_name}::EXCELFIELD), + [{$initial_value_temp}] + ]; \$excel = (new Excel())->exporTsheet(\$data); \$excel->save('{$this->function_name}导入模板.xlsx'); }"; diff --git a/app/admin/controller/Gen/GenApi/GenModel/Model.php b/app/admin/controller/Gen/GenApi/GenModel/Model.php index 217d0f4..df81e80 100644 --- a/app/admin/controller/Gen/GenApi/GenModel/Model.php +++ b/app/admin/controller/Gen/GenApi/GenModel/Model.php @@ -37,6 +37,8 @@ class Model extends GenApi '{$importExcelContent}' => $this->buildImportFunTemp(), //模型层导入Excel初始化方法 '{$importExcelInitContent}' => $this->buildImportInitFunTemp(), + //排序字段 + '{$orderField}' => $this->sort_field, ] ); //文件写入 @@ -108,10 +110,8 @@ class Model extends GenApi $init_fields_str = $this->toFormTempStr($this->business_fields, 4); $init_fields_str = substr($init_fields_str, 0, strlen($init_fields_str) - 1); //末尾的逗号去除 - return " - // excel导入/下载模板表头 - public const EXCELFIELD = {$init_fields_str}; - "; + return " // excel导入/下载模板表头 + public const EXCELFIELD = {$init_fields_str};"; } /** diff --git a/app/resources/view/admin/controller.tpl b/app/resources/view/admin/controller.tpl index da78c5a..49f29a9 100644 --- a/app/resources/view/admin/controller.tpl +++ b/app/resources/view/admin/controller.tpl @@ -20,7 +20,7 @@ class {$className} extends BaseController /** * 获取{$functionName}列表 */ - public function get{$className}List(Request $request): array + public function get{$className}List(Request $request, $isExport = false): array { $params = $request->param(); $con = []; @@ -31,7 +31,7 @@ class {$className} extends BaseController ->field({$queryFields}) ->order('{$orderField}', '{$orderMode}'); - return msg("获取{$functionName}列表成功!",$query); + return $isExport ? $query->select()->toArray() : msg("获取{$functionName}列表成功!",$query); } /** diff --git a/app/resources/view/admin/model.tpl b/app/resources/view/admin/model.tpl index da02a09..9b084c7 100644 --- a/app/resources/view/admin/model.tpl +++ b/app/resources/view/admin/model.tpl @@ -34,7 +34,6 @@ class {$className} extends BaseModel {$importExcelField} - /** * 新增前 */ diff --git a/app/resources/view/business/sort/sortAdminController.tpl b/app/resources/view/business/sort/sortAdminController.tpl new file mode 100644 index 0000000..5d76776 --- /dev/null +++ b/app/resources/view/business/sort/sortAdminController.tpl @@ -0,0 +1,104 @@ +param(); + $con = []; + + {$whereContent} + + $query = Model{$className}::where($con) + ->field({$queryFields}) + ->order('{$orderField}', '{$orderMode}'); + + return $isExport ? $query->select()->toArray() : msg("获取{$functionName}列表成功!",$query); + } + + /** + * 编辑{$functionName} + */ + public function edit{$className}(Request $request): array + { + Db::startTrans(); + try { + $params = $request->param(); + $this->validate($params, {$editRequireFields}); + $model = Model{$className}::where('{$businessName}_guid',$params['{$businessName}_guid'])->find(); + if (!$model) throwErrorMsg("该{$functionName}不存在", 1); + $model->allowField({$editAllowFields})->save($params); + Db::commit(); + return msg('编辑成功!'); + } catch (\Throwable $th) { + Db::rollback(); + throw $th; + } + } + + /** + * 添加{$functionName} + */ + public function add{$className}(Request $request): array + { + Db::startTrans(); + try { + $params = $request->param(); + $this->validate($params, {$addRequireFields}); + $model = Model{$className}::create($params,{$addAllowFields}); + Db::commit(); + return msg('添加成功!'); + } catch (\Throwable $th) { + Db::rollback(); + throw $th; + } + } + + /** + * 删除{$functionName} + */ + public function delete{$className}(Request $request): array + { + Db::startTrans(); + try { + $params = $request->param(); + $this->validate($params, [ + '{$businessName}_guid' => 'require', + ]); + ${$businessName} = Model{$className}::where([ + '{$businessName}_guid' => explode(',', $params['{$businessName}_guid']) + ])->select(); + ${$businessName}->delete(); + Db::commit(); + return msg('删除成功!'); + } catch (\Throwable $th) { + Db::rollback(); + throw $th; + } + } + + {$exportExcelContent} + + {$downloadTempContent} + + {$importExcelContent} + +} diff --git a/app/resources/view/business/sort/sortModel.tpl b/app/resources/view/business/sort/sortModel.tpl new file mode 100644 index 0000000..9208ef5 --- /dev/null +++ b/app/resources/view/business/sort/sortModel.tpl @@ -0,0 +1,76 @@ +{$orderField}); + $model->completeCreateField(); + } + + /** + * 更新前 + */ + public static function onBeforeUpdate(self $model): void + { + // self::checkRepeatData($model); + Tool::sortEditProc(self::class, $model->{$businessName}_guid, $model->{$orderField}); + $model->completeUpdateField(); + } + + /** + * 删除前 + */ + public static function onBeforeDelete(self $model): void + { + Tool::sortDeleteProc(self::class, $model->{$businessName}_guid); + $model->completeDeleteField(); + } + + {$exportExcelContent} + + {$importExcelContent} + + {$importExcelInitContent} + + +}