增加生成模型字段注解

This commit is contained in:
2023-03-29 15:30:37 +08:00
parent 877867df80
commit 45509df381
3 changed files with 145 additions and 54 deletions

View File

@@ -5,7 +5,9 @@ namespace app\admin\model;
use app\common\model\TimeModel; use app\common\model\TimeModel;
/**
*
*/
class MallCate extends TimeModel class MallCate extends TimeModel
{ {

View File

@@ -265,13 +265,14 @@ class BuildCurdService
// 获取表列注释 // 获取表列注释
$colums = Db::query("SHOW FULL COLUMNS FROM {$this->tablePrefix}{$this->table}"); $colums = Db::query("SHOW FULL COLUMNS FROM {$this->tablePrefix}{$this->table}");
foreach ($colums as $vo) {
foreach ($colums as $vo) {
$colum = [ $colum = [
'type' => $vo['Type'], 'type' => $vo['Type'],
'comment' => !empty($vo['Comment']) ? $vo['Comment'] : $vo['Field'], 'comment' => !empty($vo['Comment']) ? $vo['Comment'] : $vo['Field'],
'required' => $vo['Null'] == "NO" ? true : false, 'required' => $vo['Null'] == "NO" ? true : false,
'default' => $vo['Default'], 'default' => $vo['Default'],
'field' => $vo['Field']
]; ];
// 格式化列数据 // 格式化列数据
@@ -297,6 +298,11 @@ class BuildCurdService
// 初始化默认模型名 // 初始化默认模型名
$this->modelFilename = Str::studly($this->table); $this->modelFilename = Str::studly($this->table);
// 主表模型命名
$modelArray = explode($this->DS, $this->modelFilename);
$this->modelName = array_pop($modelArray);
$this->buildViewJsUrl(); $this->buildViewJsUrl();
// 构建数据 // 构建数据
@@ -359,9 +365,10 @@ class BuildCurdService
continue; continue;
} }
$colum = [ $colum = [
'type' => $vo['Type'], 'type' => $vo['Type'],
'comment' => $vo['Comment'], 'comment' => $vo['Comment'],
'default' => $vo['Default'], 'default' => $vo['Default'],
'field' => $vo['Field']
]; ];
$this->buildColum($colum); $this->buildColum($colum);
@@ -377,13 +384,13 @@ class BuildCurdService
$modelName = array_pop($modelArray); $modelName = array_pop($modelArray);
$relation = [ $relation = [
'modelFilename' => $modelFilename, 'modelFilename' => $modelFilename,
'modelName' => $modelName, 'modelName' => $modelName,
'foreignKey' => $foreignKey, 'foreignKey' => $foreignKey,
'primaryKey' => $primaryKey, 'primaryKey' => $primaryKey,
'bindSelectField' => $bindSelectField, 'bindSelectField' => $bindSelectField,
'delete' => $delete, 'delete' => $delete,
'tableColumns' => $formatColums, 'tableColumns' => $formatColums,
]; ];
if (!empty($bindSelectField)) { if (!empty($bindSelectField)) {
$relationArray = explode('\\', $modelFilename); $relationArray = explode('\\', $modelFilename);
@@ -608,10 +615,7 @@ class BuildCurdService
$namespaceSuffix = implode('\\', $namespaceArray); $namespaceSuffix = implode('\\', $namespaceArray);
$this->controllerNamespace = empty($namespaceSuffix) ? "app\admin\controller" : "app\admin\controller\\{$namespaceSuffix}"; $this->controllerNamespace = empty($namespaceSuffix) ? "app\admin\controller" : "app\admin\controller\\{$namespaceSuffix}";
// 主表模型命名
$modelArray = explode($this->DS, $this->modelFilename);
$this->modelName = array_pop($modelArray);
return $this; return $this;
} }
@@ -691,6 +695,29 @@ class BuildCurdService
$colum['comment'] = trim($colum['comment']); $colum['comment'] = trim($colum['comment']);
$colum['property_type'] = $this->fieldTypeToVarType($colum['type']);
$colum['property_name'] = $colum['field'];
$colum['data_list'] = '';
if (isset($colum['formType'])) {
if ($colum['formType'] == 'relation') {
$relation_model_name = '\\app\\admin\\model\\' . Str::studly($colum['define']['table']);
$colum['property_type'] = $relation_model_name;
$colum['property_name'] = Str::camel($colum['define']['table']);
} else if (in_array($colum['formType'], ['select', 'switch', 'radio', 'checkbox',])) {
$data_list = '';
foreach ($colum['define'] as $define_key => $define_value) {
$data_list .= $define_key . ':' . $define_value . ',';
}
$data_list = substr($data_list, 0, -1);
$colum['data_list'] = $data_list;
}
}
return $colum; return $colum;
} }
@@ -734,7 +761,7 @@ class BuildCurdService
$selectCode = $this->replaceTemplate( $selectCode = $this->replaceTemplate(
$this->getTemplate("model{$this->DS}select"), $this->getTemplate("model{$this->DS}select"),
[ [
'name' => $name, 'name' => $name,
'values' => $values, 'values' => $values,
] ]
); );
@@ -753,7 +780,7 @@ class BuildCurdService
$optionCode = $this->replaceTemplate( $optionCode = $this->replaceTemplate(
$this->getTemplate("view{$this->DS}module{$this->DS}option"), $this->getTemplate("view{$this->DS}module{$this->DS}option"),
[ [
'name' => $name, 'name' => $name,
'select' => $select, 'select' => $select,
] ]
); );
@@ -816,10 +843,12 @@ class BuildCurdService
protected function buildTableView($field, $options, $value) protected function buildTableView($field, $options, $value)
{ {
$default_define = [ $default_define = [
'table' => '', // 必填 'table' => '',
// 必填
'type' => 'checkbox', 'type' => 'checkbox',
'valueField' => 'id', 'valueField' => 'id',
'fieldName' => 'title', // 必填 'fieldName' => 'title',
// 必填
'comment' => $options['comment'], 'comment' => $options['comment'],
'field' => $field, 'field' => $field,
'required' => $options['required'], 'required' => $options['required'],
@@ -859,8 +888,8 @@ class BuildCurdService
$optionCode = $this->replaceTemplate( $optionCode = $this->replaceTemplate(
$this->getTemplate("view{$this->DS}module{$this->DS}radioInput"), $this->getTemplate("view{$this->DS}module{$this->DS}radioInput"),
[ [
'field' => $field, 'field' => $field,
'name' => $name, 'name' => $name,
'select' => $select, 'select' => $select,
] ]
); );
@@ -879,8 +908,8 @@ class BuildCurdService
$optionCode = $this->replaceTemplate( $optionCode = $this->replaceTemplate(
$this->getTemplate("view{$this->DS}module{$this->DS}checkboxInput"), $this->getTemplate("view{$this->DS}module{$this->DS}checkboxInput"),
[ [
'field' => $field, 'field' => $field,
'name' => $name, 'name' => $name,
'select' => $select, 'select' => $select,
] ]
); );
@@ -1103,13 +1132,13 @@ class BuildCurdService
$controllerValue = $this->replaceTemplate( $controllerValue = $this->replaceTemplate(
$this->getTemplate("controller{$this->DS}controller"), $this->getTemplate("controller{$this->DS}controller"),
[ [
'controllerName' => $this->controllerName, 'controllerName' => $this->controllerName,
'controllerNamespace' => $this->controllerNamespace, 'controllerNamespace' => $this->controllerNamespace,
'controllerAnnotation' => $this->tableComment, 'controllerAnnotation' => $this->tableComment,
'modelFilename' => "\app\admin\model\\{$modelFilenameExtend}", 'modelFilename' => "\app\admin\model\\{$modelFilenameExtend}",
'indexMethod' => $controllerIndexMethod, 'indexMethod' => $controllerIndexMethod,
'exportMethod' => $controllerExportMethod, 'exportMethod' => $controllerExportMethod,
'selectList' => $selectList, 'selectList' => $selectList,
] ]
); );
$this->fileList[$controllerFile] = $controllerValue; $this->fileList[$controllerFile] = $controllerValue;
@@ -1134,9 +1163,9 @@ class BuildCurdService
$this->getTemplate("model{$this->DS}relation"), $this->getTemplate("model{$this->DS}relation"),
[ [
'relationMethod' => $relation, 'relationMethod' => $relation,
'relationModel' => "\app\admin\model\\{$val['modelFilename']}", 'relationModel' => "\app\admin\model\\{$val['modelFilename']}",
'foreignKey' => $val['foreignKey'], 'foreignKey' => $val['foreignKey'],
'primaryKey' => $val['primaryKey'], 'primaryKey' => $val['primaryKey'],
] ]
); );
$relationList .= $relationCode; $relationList .= $relationCode;
@@ -1145,12 +1174,19 @@ class BuildCurdService
$selectList = ''; $selectList = '';
$doc_content = '';
foreach ($this->tableColumns as $field => $val) { foreach ($this->tableColumns as $field => $val) {
if (isset($val['formType']) && in_array($val['formType'], ['select', 'switch', 'radio', 'checkbox']) && isset($val['define'])) { if (isset($val['formType']) && in_array($val['formType'], ['select', 'switch', 'radio', 'checkbox']) && isset($val['define'])) {
$selectList .= $this->buildSelectModel($field, $val['define']); $selectList .= $this->buildSelectModel($field, $val['define']);
} }
$doc_content .= " * @property {$val['property_type']} \${$val['property_name']} {$val['comment']} {$val['data_list']}\n";
} }
$doc_content = substr($doc_content, 0, -1);
$extendNamespaceArray = explode($this->DS, $this->modelFilename); $extendNamespaceArray = explode($this->DS, $this->modelFilename);
$extendNamespace = null; $extendNamespace = null;
if (count($extendNamespaceArray) > 1) { if (count($extendNamespaceArray) > 1) {
@@ -1161,12 +1197,13 @@ class BuildCurdService
$modelValue = $this->replaceTemplate( $modelValue = $this->replaceTemplate(
$this->getTemplate("model{$this->DS}model"), $this->getTemplate("model{$this->DS}model"),
[ [
'modelName' => $this->modelName, 'modelName' => $this->modelName,
'modelNamespace' => "app\admin\model{$extendNamespace}", 'modelNamespace' => "app\admin\model{$extendNamespace}",
'table' => $this->table, 'table' => $this->table,
'deleteTime' => $this->delete ? '"delete_time"' : 'false', 'deleteTime' => $this->delete ? '"delete_time"' : 'false',
'relationList' => $relationList, 'relationList' => $relationList,
'selectList' => $selectList, 'selectList' => $selectList,
'doc_content' => $doc_content,
] ]
); );
$this->fileList[$modelFile] = $modelValue; $this->fileList[$modelFile] = $modelValue;
@@ -1194,12 +1231,12 @@ class BuildCurdService
$relationModelValue = $this->replaceTemplate( $relationModelValue = $this->replaceTemplate(
$this->getTemplate("model{$this->DS}model"), $this->getTemplate("model{$this->DS}model"),
[ [
'modelName' => $val['modelName'], 'modelName' => $val['modelName'],
'modelNamespace' => "app\admin\model{$extendNamespace}", 'modelNamespace' => "app\admin\model{$extendNamespace}",
'table' => $key, 'table' => $key,
'deleteTime' => $val['delete'] ? '"delete_time"' : 'false', 'deleteTime' => $val['delete'] ? '"delete_time"' : 'false',
'relationList' => '', 'relationList' => '',
'selectList' => '', 'selectList' => '',
] ]
); );
$this->fileList[$relationModelFile] = $relationModelValue; $this->fileList[$relationModelFile] = $relationModelValue;
@@ -1302,12 +1339,12 @@ class BuildCurdService
$addFormList .= $this->replaceTemplate( $addFormList .= $this->replaceTemplate(
$this->getTemplate($templateFile), $this->getTemplate($templateFile),
[ [
'comment' => $val['comment'], 'comment' => $val['comment'],
'field' => $field, 'field' => $field,
'required' => $this->buildRequiredHtml($val['required']), 'required' => $this->buildRequiredHtml($val['required']),
'required_text' => $val['required'] ? '1' : '', 'required_text' => $val['required'] ? '1' : '',
'value' => $val['default'], 'value' => $val['default'],
'define' => $define, 'define' => $define,
] ]
); );
} }
@@ -1400,12 +1437,12 @@ class BuildCurdService
$editFormList .= $this->replaceTemplate( $editFormList .= $this->replaceTemplate(
$this->getTemplate($templateFile), $this->getTemplate($templateFile),
[ [
'comment' => $val['comment'], 'comment' => $val['comment'],
'field' => $field, 'field' => $field,
'required' => $this->buildRequiredHtml($val['required']), 'required' => $this->buildRequiredHtml($val['required']),
'required_text' => $val['required'] ? '1' : '', 'required_text' => $val['required'] ? '1' : '',
'value' => $value, 'value' => $value,
'define' => $define, 'define' => $define,
] ]
); );
} }
@@ -1508,7 +1545,7 @@ class BuildCurdService
$this->getTemplate("static{$this->DS}js"), $this->getTemplate("static{$this->DS}js"),
[ [
'controllerUrl' => $this->controllerUrl, 'controllerUrl' => $this->controllerUrl,
'indexCols' => $indexCols, 'indexCols' => $indexCols,
] ]
); );
$this->fileList[$jsFile] = $jsValue; $this->fileList[$jsFile] = $jsValue;
@@ -1648,4 +1685,53 @@ class BuildCurdService
} }
return $string; return $string;
} }
}
public function fieldTypeToVarType($type)
{
$type_prefix_map = [
'BIT' => 'bool',
'TINYINT' => 'int',
'BOOL' => 'bool',
'BOOLEAN' => 'bool',
'SMALLINT' => 'int',
'MEDIUMINT' => 'int',
'INT' => 'int',
'INTEGER' => 'int',
'BIGINT' => 'int',
'FLOAT' => 'float|double',
'DOUBLE' => 'float|double',
'DECIMAL' => 'float|double',
'DATE' => 'string',
'DATETIME' => 'string',
'TIMESTAMP' => 'int',
'TIME' => 'string',
'YEAR' => 'int',
'CHAR' => 'string',
'VARCHAR' => 'string',
'BINARY' => 'string',
'VARBINARY' => 'string',
'TINYBLOB' => 'string',
'BLOB' => 'string',
'MEDIUMBLOB' => 'string',
'LONGBLOB' => 'string',
'TINYTEXT' => 'string',
'TEXT' => 'string',
'MEDIUMTEXT' => 'string',
'LONGTEXT' => 'string',
'ENUM' => 'string',
'SET' => 'string',
'JSON' => 'string',
];
foreach ($type_prefix_map as $sql_type => $var_type) {
if (Str::startsWith(strtolower($type), strtolower($sql_type))) {
return $var_type;
}
}
return 'mixed';
}
}

View File

@@ -4,6 +4,9 @@ namespace {{modelNamespace}};
use app\common\model\TimeModel; use app\common\model\TimeModel;
/**
{{doc_content}}
*/
class {{modelName}} extends TimeModel class {{modelName}} extends TimeModel
{ {