fix:后端代码生成模块修改
This commit is contained in:
parent
d9774da0f1
commit
71caf80d45
@ -9,6 +9,12 @@ use app\admin\controller\Gen\Gen;
|
|||||||
*/
|
*/
|
||||||
class GenApi
|
class GenApi
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* 模型层模板文件字符串
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $model_temp;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 项目根目录路径
|
* 项目根目录路径
|
||||||
* @var string
|
* @var string
|
||||||
@ -69,17 +75,35 @@ class GenApi
|
|||||||
*/
|
*/
|
||||||
protected $model_path;
|
protected $model_path;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 新增功能必要字段
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $add_required_fields = ['guid', 'create_user_guid', 'update_user_guid'];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 修改功能必要字段
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $edit_required_fields = ['update_user_guid'];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 业务字段
|
* 业务字段
|
||||||
* @var array [ 'user_name' => '用户名称',...]
|
* @var array 例:[ 'user_name' => '用户名称',...]
|
||||||
*/
|
*/
|
||||||
protected $business_fields = [];
|
protected $business_fields = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 新增允许字段模板字符
|
* 新增允许字段模板字符
|
||||||
* @var string
|
* @var string 例:['user_name','user_id',...]
|
||||||
*/
|
*/
|
||||||
protected $add_fields_temp = "";
|
protected $add_fields_temp;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 编辑允许字段模板字符
|
||||||
|
* @var string 例:['user_name','user_id',...]
|
||||||
|
*/
|
||||||
|
protected $edit_fields_temp;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 是否导入
|
* 是否导入
|
||||||
@ -115,13 +139,17 @@ class GenApi
|
|||||||
$this->business_name = $this->table['businessName'];
|
$this->business_name = $this->table['businessName'];
|
||||||
$this->function_name = $this->table['functionName'];
|
$this->function_name = $this->table['functionName'];
|
||||||
$this->model_temp_path = self::initFilePath("{$this->root}resources/view/admin/model.tpl");
|
$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_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->model_path = self::initFilePath("{$this->model_module_path}/{$this->class_name}.php");
|
||||||
$this->mkdirModelModule();
|
$this->mkdirModelModule();
|
||||||
$this->buildBusinessFields();
|
$this->initBusinessFields();
|
||||||
$this->buildAddFieldsTemp();
|
$this->initRequiredFields('add_required_fields');
|
||||||
|
$this->initRequiredFields('edit_required_fields');
|
||||||
$this->initImportExportStatus();
|
$this->initImportExportStatus();
|
||||||
$this->initImgFields();
|
$this->initImgFields();
|
||||||
|
$this->buildAddFieldsTemp();
|
||||||
|
$this->buildEditFieldsTemp();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -133,7 +161,7 @@ class GenApi
|
|||||||
if ($val['htmlType'] == 'imageUpload') {
|
if ($val['htmlType'] == 'imageUpload') {
|
||||||
$this->img_fields[] = $val['columnName'];
|
$this->img_fields[] = $val['columnName'];
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -152,23 +180,39 @@ class GenApi
|
|||||||
*/
|
*/
|
||||||
private function buildAddFieldsTemp(): void
|
private function buildAddFieldsTemp(): void
|
||||||
{
|
{
|
||||||
$this->add_fields_temp .= '[';
|
$field_arr = array_merge(array_keys($this->business_fields), $this->add_required_fields);
|
||||||
foreach ($this->business_fields as $key => $val) {
|
|
||||||
$this->add_fields_temp .= "'{$key}',";
|
foreach ($field_arr as $val) {
|
||||||
};
|
$this->add_fields_temp .= "'{$val}',";
|
||||||
$this->add_fields_temp .= ']';
|
}
|
||||||
|
|
||||||
|
$this->add_fields_temp = "[{$this->add_fields_temp}]";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构建编辑允许字段模板字符
|
||||||
|
*/
|
||||||
|
private function buildEditFieldsTemp(): void
|
||||||
|
{
|
||||||
|
$field_arr = array_merge(array_keys($this->business_fields), $this->edit_required_fields);
|
||||||
|
|
||||||
|
foreach ($field_arr as $val) {
|
||||||
|
$this->edit_fields_temp .= "'{$val}',";
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->edit_fields_temp = "[{$this->edit_fields_temp}]";
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构建业务字段名
|
* 构建业务字段名
|
||||||
*/
|
*/
|
||||||
private function buildBusinessFields()
|
private function initBusinessFields()
|
||||||
{
|
{
|
||||||
foreach ($this->fields as $val) {
|
foreach ($this->fields as $val) {
|
||||||
if (!$val['isInit']) {
|
if (!$val['isInit']) {
|
||||||
$this->business_fields[$val['columnName']] = $val['columnComment'];
|
$this->business_fields[$val['columnName']] = $val['columnComment'];
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -191,6 +235,44 @@ class GenApi
|
|||||||
return str_replace('/', DIRECTORY_SEPARATOR, $path);
|
return str_replace('/', DIRECTORY_SEPARATOR, $path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取模板文件字符串
|
||||||
|
*
|
||||||
|
* @param string $temp_path 模板文件路径
|
||||||
|
*/
|
||||||
|
protected static function getTempStr(string $temp_path): string
|
||||||
|
{
|
||||||
|
//打开该文件资源(r只读方式打开),读取模板文件内容
|
||||||
|
return fread(fopen($temp_path, "r"), filesize($temp_path));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 写入文件
|
||||||
|
*
|
||||||
|
* @param string $path 文件路径
|
||||||
|
* @param string $content 写入内容
|
||||||
|
*/
|
||||||
|
protected static function writeFile(string $path, string $content): void
|
||||||
|
{
|
||||||
|
//打开文件资源(w写入方式打开),进行写入
|
||||||
|
fwrite(fopen($path, 'w'), $content);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 初始化必要字段
|
||||||
|
*
|
||||||
|
* @param string 必要字段变量名
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
protected function initRequiredFields(string $op_name): void
|
||||||
|
{
|
||||||
|
$init_data = [];
|
||||||
|
foreach ($this->$op_name as $val) {
|
||||||
|
$init_data[] = "{$this->business_name}_{$val}";
|
||||||
|
}
|
||||||
|
$this->$op_name = $init_data;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建文件夹
|
* 创建文件夹
|
||||||
*
|
*
|
||||||
@ -200,7 +282,7 @@ class GenApi
|
|||||||
{
|
{
|
||||||
if (!(new Gen(new \think\App()))->mkdir($path)) {
|
if (!(new Gen(new \think\App()))->mkdir($path)) {
|
||||||
throwErrorMsg(__METHOD__ . "创建文件夹失败!");
|
throwErrorMsg(__METHOD__ . "创建文件夹失败!");
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -15,12 +15,8 @@ class Model extends GenApi
|
|||||||
*/
|
*/
|
||||||
public function createNewModel(): void
|
public function createNewModel(): void
|
||||||
{
|
{
|
||||||
//打开模型层模板文件,拿到该文件资源(r只读方式打开)
|
|
||||||
$tem_f = fopen($this->model_temp_path, "r");
|
|
||||||
//读取模型层模板文件,拿到模板文件的全部字符串
|
|
||||||
$temp_str = fread($tem_f, filesize($this->model_temp_path));
|
|
||||||
//模板文件字符串替换
|
//模板文件字符串替换
|
||||||
$temp_str = Tool::strReplacePlus($temp_str, [
|
$temp_str = Tool::strReplacePlus($this->model_temp, [
|
||||||
//实体类名
|
//实体类名
|
||||||
'{$className}' => $this->class_name,
|
'{$className}' => $this->class_name,
|
||||||
//模块名
|
//模块名
|
||||||
@ -38,11 +34,8 @@ class Model extends GenApi
|
|||||||
//模型层导入Excel初始化方法
|
//模型层导入Excel初始化方法
|
||||||
'{$importExcelInitContent}' => $this->buildImportInitFunTemp(),
|
'{$importExcelInitContent}' => $this->buildImportInitFunTemp(),
|
||||||
]);
|
]);
|
||||||
|
//文件写入
|
||||||
//打开模型层文件,拿到该文件资源(w写入方式打开)
|
self::writeFile($this->model_path,$temp_str);
|
||||||
$gen_model = fopen($this->model_path, 'w');
|
|
||||||
//写入该模型层文件
|
|
||||||
fwrite($gen_model, $temp_str);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -74,7 +67,8 @@ class Model extends GenApi
|
|||||||
//导出数据分配
|
//导出数据分配
|
||||||
$data_str = '';
|
$data_str = '';
|
||||||
foreach ($this->business_fields as $field => $name) {
|
foreach ($this->business_fields as $field => $name) {
|
||||||
if (in_array($name, $this->img_fields)) {
|
//图片字段处理
|
||||||
|
if (in_array($field, $this->img_fields)) {
|
||||||
$data_str .= "Excel::ExportImgFiled(\$val['$field']),\n";
|
$data_str .= "Excel::ExportImgFiled(\$val['$field']),\n";
|
||||||
} else {
|
} else {
|
||||||
$data_str .= "\$val['{$field}'],\n";
|
$data_str .= "\$val['{$field}'],\n";
|
||||||
|
Loading…
Reference in New Issue
Block a user