feat:后端代码生成:导出功能优化、新增后台控制器、模型层排序处理功能、下载模板生成优化

This commit is contained in:
xjh 2023-05-08 22:48:27 +08:00
parent a332b3f252
commit 9669623f98
7 changed files with 226 additions and 23 deletions

View File

@ -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);
}
/**
* 构建新增允许字段模板字符
*/

View File

@ -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 ;$i<count($this->business_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');
}";

View File

@ -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};";
}
/**

View File

@ -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);
}
/**

View File

@ -34,7 +34,6 @@ class {$className} extends BaseModel
{$importExcelField}
/**
* 新增前
*/

View File

@ -0,0 +1,104 @@
<?php
namespace app\admin\controller\{$moduleName};
use app\BaseController;
use app\common\model\{$moduleName}\{$className} as Model{$className};
use app\Request;
use think\Validate;
use think\exception\ValidateException;
use think\facade\Filesystem;
use app\common\arw\adjfut\src\Excel;
use app\common\arw\adjfut\src\UploadFile;
use app\common\exception\Tool;
use think\facade\Db;
use think\facade\Env;
class {$className} extends BaseController
{
/**
* 获取{$functionName}列表
*/
public function get{$className}List(Request $request, $isExport = false): array
{
$params = $request->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}
}

View File

@ -0,0 +1,76 @@
<?php
namespace app\common\model\{$moduleName};
use app\common\arw\adjfut\src\Validate;
use app\BaseModel;
use think\model\concern\SoftDelete;
use app\common\arw\adjfut\src\Excel;
use app\Request;
use app\common\exception\Tool;
use think\facade\Db;
class {$className} extends BaseModel
{
use SoftDelete;
// 删除字段
protected $deleteTime = '{$businessName}_delete_time';
// 设置主键名
protected $pk = '{$businessName}_guid';
// 设置废弃字段
protected $disuse = [];
// 设置字段信息
protected $schema = [
{$fields}
];
// 设置json类型字段
protected $json = [''];
// 开启自动写入时间戳字段
protected $autoWriteTimestamp = 'datetime';
// 创建时间
protected $createTime = '{$businessName}_create_time';
// 修改时间
protected $updateTime = '{$businessName}_update_time';
//排序字段
public $order_field = '{$orderField}';
{$importExcelField}
/**
* 新增前
*/
public static function onBeforeInsert(self $model): void
{
// self::checkRepeatData($model);
Tool::sortInsertProc(self::class, $model->{$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}
}