diff --git a/app/admin/controller/Gen/Gen.php b/app/admin/controller/Gen/Gen.php index 3e3a9e7..e8e86e8 100644 --- a/app/admin/controller/Gen/Gen.php +++ b/app/admin/controller/Gen/Gen.php @@ -8,7 +8,9 @@ use app\Request; use app\exception\ErrorMsg; use think\facade\Db; use app\common\exception\Tool; -use app\admin\controller\Gen\GenApi\GenModel\Model; +use app\admin\controller\Gen\GenApi\GenModel\Model as GenModel; +use app\admin\controller\Gen\GenApi\GenController\Admin as GenAdminController; +use app\admin\controller\Gen\GenApi\GenController\Api as GenApiController; class Gen extends BaseController { @@ -343,14 +345,12 @@ class Gen extends BaseController // return $table; try { + (new GenModel($columns, $table))->createNewModel(); + (new GenAdminController($columns, $table))->createAdminController(); if ($table['tplCategory'] == "web_static") { - $this->createAdminController($columns, $table); - (new Model($columns, $table))->createNewModel(); $this->createJsWebStaticIndex($columns, $table); $this->createJsApi($columns, $table); } else if ($table['tplCategory'] == "crud") { - $this->createAdminController($columns, $table); - (new Model($columns, $table))->createNewModel(); $this->createJsVue($columns, $table); $this->createJsAdd($columns, $table); $this->createJsEdit($columns, $table); @@ -393,7 +393,7 @@ class Gen extends BaseController $columns[$key]['isInit'] = $this->intToBool($value['isInit']); } - $this->createApiController($columns, $table, 2); + (new GenApiController($columns, $table))->createApiController(); return [ 'code' => 200, diff --git a/app/admin/controller/Gen/GenApi/GenApi.php b/app/admin/controller/Gen/GenApi/GenApi.php index ed57b9b..1522f01 100644 --- a/app/admin/controller/Gen/GenApi/GenApi.php +++ b/app/admin/controller/Gen/GenApi/GenApi.php @@ -9,12 +9,6 @@ use app\admin\controller\Gen\Gen; */ class GenApi { - /** - * 模型层模板文件字符串 - * @var string - */ - protected $model_temp; - /** * 项目根目录路径 * @var string @@ -57,6 +51,23 @@ class GenApi */ protected $function_name; + /** + * 后端生成模板文件路径配置 + * @var array + */ + protected const TEMP_PATH_CONFIG = [ + 'crud' => [ + 'api' => 'resources/view/api/controller.tpl', + 'admin' => 'resources/view/admin/controller.tpl', + 'model' => 'resources/view/admin/model.tpl', + ], + 'web_static' => [ + 'api' => 'resources/view/business/webApiController.tpl', + 'admin' => 'resources/view/business/webController.tpl', + 'model' => 'resources/view/admin/model.tpl', + ], + ]; + /** * 模型层模板读取路径 * @var string @@ -75,36 +86,84 @@ class GenApi */ protected $model_path; + /** + * 后台控制器模板读取路径 + * @var string + */ + protected $admin_con_temp_path; + + /** + * 后台控制器模块生成路径 + * @var string + */ + protected $admin_con_module_path; + + /** + * 后台控制器生成路径 + * @var string + */ + protected $admin_con_path; + + /** + * 前台控制器模板读取路径 + * @var string + */ + protected $api_con_temp_path; + + /** + * 前台控制器生成路径 + * @var string + */ + protected $api_con_path; + + /** + * 前台控制器模块生成路径 + * @var string + */ + protected $api_con_module_path; + /** * 新增功能必要字段 * @var array */ protected $add_required_fields = ['guid', 'create_user_guid', 'update_user_guid']; + /** + * 新增允许字段模板字符串 + * @var string 例:['user_name','user_id',...] + */ + protected $add_fields_temp; + + /** + * 新增必填验证模板字符串 + * @var string 例: ['user_name|用户名称' => 'require',...] + */ + protected $add_required_verify_temp; + /** * 修改功能必要字段 * @var array */ protected $edit_required_fields = ['update_user_guid']; + /** + * 编辑允许字段模板字符串 + * @var string 例:['user_name','user_id',...] + */ + protected $edit_fields_temp; + + /** + * 编辑必填验证模板字符串 + * @var string 例: ['user_name|用户名称' => 'require',...] + */ + protected $edit_required_verify_temp; + /** * 业务字段 * @var array 例:[ 'user_name' => '用户名称',...] */ protected $business_fields = []; - /** - * 新增允许字段模板字符 - * @var string 例:['user_name','user_id',...] - */ - protected $add_fields_temp; - - /** - * 编辑允许字段模板字符 - * @var string 例:['user_name','user_id',...] - */ - protected $edit_fields_temp; - /** * 是否导入 * @var bool @@ -123,6 +182,36 @@ class GenApi */ protected $img_fields = []; + /** + * 生成后端模板类型 + * @var string crud:: 单表(增删改查) | web_static: 门户静态模块(查询修改) + */ + protected $gen_api_type; + + /** + * 查询列表显示字段 + * @var array 例:['user_name','user_id',...] + */ + protected $query_list_fields = []; + + /** + * 排序字段 + * @var string + */ + protected $sort_field; + + /** + * 排序类型 + * @var string + */ + protected $sort_type; + + /** + * 查询列表条件模板字符串 + * @var string 例子:Tool::getOptionalQuery(['user_name', 'LIKE']) + */ + protected $query_list_where_temp; + /** * 构造器 * @@ -131,6 +220,7 @@ class GenApi */ public function __construct(array $fields, array $table) { + //基本参数初始化 $this->root = base_path(); $this->fields = $fields; $this->table = $table; @@ -138,11 +228,7 @@ class GenApi $this->module_name = $this->table['moduleName']; $this->business_name = $this->table['businessName']; $this->function_name = $this->table['functionName']; - $this->model_temp_path = self::initFilePath("{$this->root}resources/view/admin/model.tpl"); - $this->model_temp = self::getTempStr($this->model_temp_path); - $this->model_module_path = self::initFilePath("{$this->root}common/model/{$this->module_name}"); - $this->model_path = self::initFilePath("{$this->model_module_path}/{$this->class_name}.php"); - $this->mkdirModelModule(); + //后端生成通用数据初始化 $this->initBusinessFields(); $this->initRequiredFields('add_required_fields'); $this->initRequiredFields('edit_required_fields'); @@ -150,6 +236,98 @@ class GenApi $this->initImgFields(); $this->buildAddFieldsTemp(); $this->buildEditFieldsTemp(); + $this->initGenApiType(); + $this->initQueryListDisplayFields(); + $this->initSortInfo(); + $this->initRequiredVerifyFieldsTemp(); + $this->buildQueryWhereContentTemp(); + //后端生成所需路径初始化 + $this->initModelGenPath(); + $this->initAdminControllerGenPath(); + $this->initApiControllerGenPath(); + } + + /** + * 构建查询列表条件模板字符串 + */ + public function buildQueryWhereContentTemp(): void + { + $where_content_arr = []; + foreach ($this->fields as $val) { + if ($val['isQuery']) { + $where_content_arr[] = [$val['columnName'], $val['queryType']]; + } + }; + $this->query_list_where_temp = self::toFormTempStr($where_content_arr, 3); + } + + /** + * 初始化排序信息 + */ + private function initSortInfo(): void + { + $this->sort_field = $this->table['options']->SortField ?? "{$this->business_name}_update_time"; + $this->sort_type = $this->table['options']->SortType ?? 'desc'; + } + + /** + * 初始化查询列表可显字段 + */ + private function initQueryListDisplayFields(): void + { + foreach ($this->fields as $val) { + if ($val['isList']) { + $this->query_list_fields[] = $val['columnName']; + } + }; + } + + /** + * 初始化后端模板类型 + */ + private function initGenApiType(): void + { + $this->gen_api_type = $this->table['tplCategory']; + } + + /** + * 初始化模型层生成所需路径 + */ + private function initModelGenPath(): void + { + $this->model_temp_path = self::initFilePath($this->getTempPath('model')); + $this->model_module_path = self::initFilePath("{$this->root}common/model/{$this->module_name}"); + $this->model_path = self::initFilePath("{$this->model_module_path}/{$this->class_name}.php"); + } + + /** + * 初始化后台控制器生成所需路径 + */ + private function initAdminControllerGenPath(): void + { + $this->admin_con_temp_path = self::initFilePath($this->getTempPath('admin')); + $this->admin_con_module_path = self::initFilePath("{$this->root}admin/controller/{$this->module_name}"); + $this->admin_con_path = self::initFilePath("{$this->admin_con_module_path}/{$this->class_name}.php"); + } + + /** + * 初始化前台控制器生成所需路径 + */ + private function initApiControllerGenPath(): void + { + $this->api_con_temp_path = self::initFilePath($this->getTempPath('api')); + $this->api_con_module_path = self::initFilePath("{$this->root}api/controller/{$this->module_name}"); + $this->api_con_path = self::initFilePath("{$this->api_con_module_path}/{$this->class_name}.php"); + } + + /** + * 获取生成模板文件路径 + * + * @param string $type 生成模板文件类型 admin:后台控制器 | api:前台控制器 | model:模型层 + */ + private function getTempPath(string $type): string + { + return $this->root . self::TEMP_PATH_CONFIG[$this->gen_api_type][$type]; } /** @@ -181,12 +359,7 @@ class GenApi private function buildAddFieldsTemp(): void { $field_arr = array_merge(array_keys($this->business_fields), $this->add_required_fields); - - foreach ($field_arr as $val) { - $this->add_fields_temp .= "'{$val}',"; - } - - $this->add_fields_temp = "[{$this->add_fields_temp}]"; + $this->add_fields_temp = self::toFormTempStr($field_arr); } /** @@ -195,12 +368,31 @@ class GenApi private function buildEditFieldsTemp(): void { $field_arr = array_merge(array_keys($this->business_fields), $this->edit_required_fields); + $this->edit_fields_temp = self::toFormTempStr($field_arr); + } - foreach ($field_arr as $val) { - $this->edit_fields_temp .= "'{$val}',"; - } - - $this->edit_fields_temp = "[{$this->edit_fields_temp}]"; + /** + * 初始化功能必填验证模板字符 + */ + private function initRequiredVerifyFieldsTemp(): void + { + $edit_require_fields = []; + $add_require_fields = []; + foreach ($this->fields as $val) { + $column_name = $val['columnName']; + if ($val['isEdit']) { + if ($val['isRequired']) { + $edit_require_fields[] = $column_name; + } + } + if ($val['isInsert']) { + if ($val['isRequired']) { + $add_require_fields[] = $column_name; + } + } + }; + $this->edit_required_verify_temp = self::toFormTempStr($edit_require_fields, 2); + $this->add_required_verify_temp = self::toFormTempStr($add_require_fields, 2); } /** @@ -216,11 +408,13 @@ class GenApi } /** - * 生成模型层模块文件夹 + * 生成模块文件夹 + * + * @param string $path 模块文件夹路径 */ - private function mkdirModelModule() + protected function createModuleMkdir(string $module_path): void { - self::mkdir($this->model_module_path); + self::mkdir($module_path); } /** @@ -294,4 +488,4 @@ class GenApi { return (new Gen(new \think\App()))->toFormTempStr($arr, $this->business_fields, $type); } -} \ No newline at end of file +} diff --git a/app/admin/controller/Gen/GenApi/GenController/Admin.php b/app/admin/controller/Gen/GenApi/GenController/Admin.php index 4982e2d..6432085 100644 --- a/app/admin/controller/Gen/GenApi/GenController/Admin.php +++ b/app/admin/controller/Gen/GenApi/GenController/Admin.php @@ -10,4 +10,115 @@ use app\common\exception\Tool; */ class Admin extends GenApi { + /** + * 生成后台控制器 + */ + public function createAdminController(): void + { + //创建后台控制器层模块目录 + self::createModuleMkdir($this->admin_con_module_path); + //模板文件字符串替换 + $temp_str = Tool::strReplacePlus( + self::getTempStr($this->admin_con_temp_path), + [ + //实体类名 + '{$className}' => $this->class_name, + //模块名 + '{$moduleName}' => $this->module_name, + //业务名 + '{$businessName}' => $this->business_name, + //方法名 + '{$functionName}' => $this->function_name, + //查询列表显示字段 + '{$queryFields}' => self::toFormTempStr($this->query_list_fields), + //排序字段 + '{$orderField}' => $this->sort_field, + //排序类型 + '{$orderMode}' => $this->sort_type, + //新增允许字段 + '{$addAllowFields}' => $this->add_fields_temp, + //编辑允许字段 + '{$editAllowFields}' => $this->edit_fields_temp, + //列表查询条件 + '{$whereContent}' => $this->query_list_where_temp, + //导出接口 + '{$exportExcelContent}' => $this->buildExportExcelApiTemp(), + //导入接口 + '{$importExcelContent}' => $this->buildImportExcelApiTemp(), + //下载模板接口 + '{$downloadTempContent}' => $this->buildDownloadTemplateApiTemp(), + //编辑必填验证 + '{$editRequireFields}' => $this->edit_required_verify_temp, + //新增必填验证 + '{$addRequireFields}' => $this->add_required_verify_temp, + ] + ); + //文件写入 + self::writeFile($this->admin_con_path, $temp_str); + } + + /** + * 构建Excel导出接口模板字符串 + */ + private function buildExportExcelApiTemp(): string + { + 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); + }"; + } + + /** + * 构建Excel导入接口模板字符串 + */ + private function buildImportExcelApiTemp(): string + { + if (!$this->is_import) return ''; + + //模板字符串构建并返回 + return "/**\n* 导入excel\n*/ + public function importExcel(Request \$request):array + { + \$file = new UploadFile('uploads', 'fileExt:xlsx'); + \$file->putFile('{$this->business_name}'); + \$msg = Model{$this->class_name}::importExcel(\$file); + return [ + 'code' => 0, + 'msg' => \$msg + ]; + }"; + } + + /** + * 构建Excel下载模板接口模板字符串 + */ + private function buildDownloadTemplateApiTemp(): string + { + if (!$this->is_import) return ''; + + //模板字符串构建并返回 + return "/**\n* 下载导入模板\n*/ + public function downloadTemplate(Request \$request):void + { + \$params = \$request->param(); + \$data = array_values(Model{$this->class_name}::EXCELFIELD); + \$excel = (new Excel())->exporTsheet(\$data); + \$excel->save('{$this->function_name}导入模板.xlsx'); + }"; + } } diff --git a/app/admin/controller/Gen/GenApi/GenController/Api.php b/app/admin/controller/Gen/GenApi/GenController/Api.php index 61b4190..f3fb6f7 100644 --- a/app/admin/controller/Gen/GenApi/GenController/Api.php +++ b/app/admin/controller/Gen/GenApi/GenController/Api.php @@ -10,4 +10,40 @@ use app\common\exception\Tool; */ class Api extends GenApi { + /** + * 生成前台控制器 + */ + public function createApiController(): void + { + //创建前台控制器层模块目录 + self::createModuleMkdir($this->api_con_module_path); + //模板文件字符串替换 + $temp_str = Tool::strReplacePlus( + self::getTempStr($this->api_con_temp_path), + [ + //实体类名 + '{$className}' => $this->class_name, + //模块名 + '{$moduleName}' => $this->module_name, + //业务名 + '{$businessName}' => $this->business_name, + //方法名 + '{$functionName}' => $this->function_name, + //查询列表显示字段 + '{$queryFields}' => self::toFormTempStr($this->query_list_fields), + //排序字段 + '{$orderField}' => $this->sort_field, + //排序类型 + '{$orderMode}' => $this->sort_type, + //新增允许字段 + '{$addAllowFields}' => $this->add_fields_temp, + //编辑允许字段 + '{$editAllowFields}' => $this->edit_fields_temp, + //列表查询条件 + '{$whereContent}' => $this->query_list_where_temp, + ] + ); + //文件写入 + self::writeFile($this->api_con_path, $temp_str); + } } diff --git a/app/admin/controller/Gen/GenApi/GenModel/Model.php b/app/admin/controller/Gen/GenApi/GenModel/Model.php index b6e830f..217d0f4 100644 --- a/app/admin/controller/Gen/GenApi/GenModel/Model.php +++ b/app/admin/controller/Gen/GenApi/GenModel/Model.php @@ -8,38 +8,43 @@ use app\common\exception\Tool; /** * 后端模型层生成类 */ - class Model extends GenApi +class Model extends GenApi { /** * 生成模型层 */ public function createNewModel(): void { + //创建模型层模块目录 + self::createModuleMkdir($this->model_module_path); //模板文件字符串替换 - $temp_str = Tool::strReplacePlus($this->model_temp, [ - //实体类名 - '{$className}' => $this->class_name, - //模块名 - '{$moduleName}' => $this->module_name, - //业务名 - '{$businessName}' => $this->business_name, - //模型层字段信息 - '{$fields}' => $this->buildFieldsInfoTemp(), - //模型层导出Excel方法 - '{$exportExcelContent}' => $this->buildExportFun(), - //模型层导入/下载模板Excel表头 - '{$importExcelField}' => $this->buildImportDownloadFieldsTemp(), - //模型层导入Excel方法 - '{$importExcelContent}' => $this->buildImportFunTemp(), - //模型层导入Excel初始化方法 - '{$importExcelInitContent}' => $this->buildImportInitFunTemp(), - ]); + $temp_str = Tool::strReplacePlus( + self::getTempStr($this->model_temp_path), + [ + //实体类名 + '{$className}' => $this->class_name, + //模块名 + '{$moduleName}' => $this->module_name, + //业务名 + '{$businessName}' => $this->business_name, + //模型层字段信息 + '{$fields}' => $this->buildFieldsInfoTemp(), + //模型层导出Excel方法 + '{$exportExcelContent}' => $this->buildExportFun(), + //模型层导入/下载模板Excel表头 + '{$importExcelField}' => $this->buildImportDownloadFieldsTemp(), + //模型层导入Excel方法 + '{$importExcelContent}' => $this->buildImportFunTemp(), + //模型层导入Excel初始化方法 + '{$importExcelInitContent}' => $this->buildImportInitFunTemp(), + ] + ); //文件写入 - self::writeFile($this->model_path,$temp_str); + self::writeFile($this->model_path, $temp_str); } /** - * 构建模型层字段信息模板字符 + * 构建模型层字段信息模板字符串 */ public function buildFieldsInfoTemp(): string { @@ -51,13 +56,13 @@ use app\common\exception\Tool; } /** - * 构建模型层导出方法模板字符 + * 构建模型层导出方法模板字符串 */ private function buildExportFun(): string { if (!$this->is_export) return ''; - //excel表头字符构建 + //excel表头字符串构建 $excel_header = []; foreach ($this->business_fields as $name) { $excel_header[] = $name; @@ -76,7 +81,7 @@ use app\common\exception\Tool; } $data_str = "[\n{$data_str}]"; - //模板字符构建并返回 + //模板字符串构建并返回 return " /** * 导出Excel @@ -95,7 +100,7 @@ use app\common\exception\Tool; } /** - * 构建导入/下载模板表头模板字符 + * 构建导入/下载模板表头模板字符串 */ private function buildImportDownloadFieldsTemp(): string { @@ -110,13 +115,13 @@ use app\common\exception\Tool; } /** - * 构建模型层导入方法模板字符 + * 构建模型层导入方法模板字符串 */ private function buildImportFunTemp(): string { if (!$this->is_import) return ''; - //模板字符构建并返回 + //模板字符串构建并返回 return " /** * 导入excel @@ -132,9 +137,7 @@ use app\common\exception\Tool; \$excel = new Excel(\$file); \$data = \$excel->parseExcel( Tool::getExcelRule(self::EXCELFIELD), - [ - 'titleLine' => [1] - ]); + ['titleLine' => [1]]); if (!\$data) throwErrorMsg('excel无数据', 1); \$msg = []; foreach (\$data as \$line => \$value) { @@ -155,14 +158,14 @@ use app\common\exception\Tool; } /** - * 构建模型层导入初始化方法模板字符 + * 构建模型层导入初始化方法模板字符串 */ private function buildImportInitFunTemp(): string { if (!$this->is_import) return ''; - + /** - * (匿名函数)获取excel每行导入数据变量分配-模板字符 + * (匿名函数)获取excel每行导入数据变量分配-模板字符串 */ $getImportAllocationTemp = function () { $str = ""; @@ -174,7 +177,7 @@ use app\common\exception\Tool; $import_allocation_temp = $getImportAllocationTemp(); /** - * (匿名函数)获取新增的字段值们-模板字符 + * (匿名函数)获取新增的字段值们-模板字符串 */ $getAddFieldsTemp = function () { $str = ""; @@ -185,7 +188,7 @@ use app\common\exception\Tool; }; $add_allocation_temp = $getAddFieldsTemp(); - //模板字符构建并返回 + //模板字符串构建并返回 return " /** * 导入excel初始化 diff --git a/app/resources/view/admin/controller.tpl b/app/resources/view/admin/controller.tpl index f1d5aef..da78c5a 100644 --- a/app/resources/view/admin/controller.tpl +++ b/app/resources/view/admin/controller.tpl @@ -29,7 +29,7 @@ class {$className} extends BaseController $query = Model{$className}::where($con) ->field({$queryFields}) - ->order({$orderField}, {$orderMode}); + ->order('{$orderField}', '{$orderMode}'); return msg("获取{$functionName}列表成功!",$query); } diff --git a/app/resources/view/api/controller.tpl b/app/resources/view/api/controller.tpl index 7db4d94..f680230 100644 --- a/app/resources/view/api/controller.tpl +++ b/app/resources/view/api/controller.tpl @@ -29,11 +29,9 @@ class {$className} extends BaseController $query = Model{$className}::where($con) ->field({$queryFields}) - ->order({$orderField}, {$orderMode}) + ->order('{$orderField}', '{$orderMode}') ->select(); - {$imgUrlPrefixPadding} - return msg(0, "获取{$functionName}列表成功!", [ 'data' => $query, 'count' => count($query)