Skip to content

Commit 97530a7

Browse files
committed
feat: 支持模型设计中使用UI建议,优化表格列识别
1 parent bfd613f commit 97530a7

File tree

6 files changed

+2798
-283
lines changed

6 files changed

+2798
-283
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
===============
33

44
> 运行环境要求PHP7.4+,兼容PHP8.0。
5+
> 领域驱动设计
6+
> 模型设计自动转化源码
57
68
## 依赖包
79

app/command/Init.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ protected function execute(Input $input, Output $output)
6565
$admin->save();
6666
$output->writeln('<info>Admin(id=1) Created!</info>');
6767
} else {
68-
$output->writeln('<warning>Admin(id=1) Already Exists!</warning>');
68+
//$output->writeln('<warning>Admin(id=1) Already Exists!</warning>');
6969
}
7070

7171
// 初始化管理菜单
@@ -82,7 +82,7 @@ protected function execute(Input $input, Output $output)
8282
]);
8383
$output->writeln('<info>System Menu Created!</info>');
8484
} else {
85-
$output->writeln('<warning>System Menu Already Exists!</warning>');
85+
//$output->writeln('<warning>System Menu Already Exists!</warning>');
8686
}
8787

8888
$output->writeln('<info>Init System Succeed!</info>');
@@ -99,7 +99,7 @@ protected function createDDL($entity)
9999
try {
100100
Db::execute(View::fetch('database/' . config('database.default') . '/ddl_create_table'));
101101
} catch (\Exception $e) {
102-
$this->output->writeln('<warning>' . $e->getMessage() . '</warning>');
102+
//$this->output->writeln('<warning>' . $e->getMessage() . '</warning>');
103103
}
104104

105105
foreach ($entity['indexes'] as $index) {
@@ -113,7 +113,7 @@ protected function createDDL($entity)
113113
try {
114114
Db::execute(View::fetch('database/' . config('database.default') . '/ddl_create_index'));
115115
} catch (\Exception $e) {
116-
$this->output->writeln('<warning>' . $e->getMessage() . '</warning>');
116+
//$this->output->writeln('<warning>' . $e->getMessage() . '</warning>');
117117
}
118118
}
119119

app/command/Md2c.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ class Md2c extends Command
2121
protected $entity_map = [];
2222
// 字典映射
2323
protected $dict_map = [];
24+
// UI建议映射
25+
protected $uihint_map = [];
2426

2527
// 忽略实体
2628
public const IGNORE_ENTITY = ['dict', 'dict_item', 'admin', 'config', 'menu', 'table', 'col'];
@@ -40,6 +42,7 @@ protected function execute(Input $input, Output $output)
4042
$this->models = json_decode(file_get_contents(self::MODEL_PATH), true);
4143
$this->entity_map = array_column($this->models['entities'], 'id');
4244
$this->dict_map = array_column($this->models['dicts'], 'id');
45+
$this->uihint_map = array_column($this->models['profile']['uiHint'], 'id');
4346

4447
array_walk($this->models['entities'], [$this, 'entity2class']);
4548
array_walk($this->models['entities'], [$this, 'entity2table']);
@@ -93,7 +96,7 @@ protected function entity2class($entity)
9396
}
9497
$path = $this->app->getBasePath() . 'model' . DIRECTORY_SEPARATOR . name_class($entity['defKey']) . '.php';
9598
if (file_exists($path)) {
96-
$this->output->writeln('<warning>' . $path . ' Already Exists!</warning>');
99+
//$this->output->writeln('<warning>' . $path . ' Already Exists!</warning>');
97100
} else {
98101
file_put_contents($path, (new PsrPrinter())->printFile($file));
99102
$this->output->writeln('<info>' . $path . ' Created!</info>');
@@ -109,7 +112,7 @@ protected function entity2class($entity)
109112
$class->addProperty('rule', [])->setProtected()->addComment('验证规则');
110113
$path = $this->app->getBasePath() . 'validate' . DIRECTORY_SEPARATOR . name_class($entity['defKey']) . '.php';
111114
if (file_exists($path)) {
112-
$this->output->writeln('<warning>' . $path . ' Already Exists!</warning>');
115+
//$this->output->writeln('<warning>' . $path . ' Already Exists!</warning>');
113116
} else {
114117
file_put_contents($path, (new PsrPrinter())->printFile($file));
115118
$this->output->writeln('<info>' . $path . ' Created!</info>');
@@ -124,7 +127,7 @@ protected function entity2class($entity)
124127
$class->addMethod('initialize')->setProtected()->setBody('$this->model = new \app\model\\' . name_class($entity['defKey']) . '();');
125128
$path = $this->app->getBasePath() . 'controller' . DIRECTORY_SEPARATOR . 'admin' . DIRECTORY_SEPARATOR . name_class($entity['defKey']) . '.php';
126129
if (file_exists($path)) {
127-
$this->output->writeln('<warning>' . $path . ' Already Exists!</warning>');
130+
//$this->output->writeln('<warning>' . $path . ' Already Exists!</warning>');
128131
} else {
129132
file_put_contents($path, (new PsrPrinter())->printFile($file));
130133
$this->output->writeln('<info>' . $path . ' Created!</info>');
@@ -167,20 +170,33 @@ protected function entity2table($entity)
167170
// 属性转列
168171
protected function field2col($field, $index, $entity)
169172
{
170-
if (in_array(strtolower($field['defKey']), ['update_time', 'delete_time', 'revision'])) {
173+
if (in_array(strtolower($field['defKey']), ['delete_time', 'revision'])) {
171174
return;
172175
}
173176

174177
$data = [
175178
'table_code' => strtolower($entity['defKey']),
176179
'data_index' => strtolower($field['defKey']),
180+
'value_type' => 'text',
177181
'title' => $field['defName'],
178182
'tip' => $field['comment'],
183+
'hide_in_table' => $field['hideInGraph'] ? 1 : 0,
184+
'hide_in_form' => ($field['hideInGraph'] || $field['primaryKey']) ? 1 : 0,
179185
'sort' => $index
180186
];
181187
if (!empty($field['refDict'])) {
182188
$dict = map_array_value($this->dict_map, $this->models['dicts'], $field['refDict']);
183-
$data['value_enum_dict_key'] = $dict ? $dict['defKey'] : '';
189+
if ($dict) {
190+
$data['value_enum_dict_key'] = $dict['defKey'];
191+
$data['value_type'] = 'select';
192+
$data['filters'] = 1;
193+
}
194+
}
195+
if (!empty($field['uiHint'])) {
196+
$uihint = map_array_value($this->uihint_map, $this->models['profile']['uiHint'], $field['uiHint']);
197+
if ($uihint) {
198+
$data['value_type'] = $uihint['defKey'];
199+
}
184200
}
185201

186202
Col::create($data);

app/controller/admin/Table.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ public function delete($names)
7979
}
8080

8181
foreach ($models as $model) {
82-
$model->together(['cols'])->delete();
82+
$model->cols()->delete();
83+
$model->delete();
8384
}
8485
return $this->success();
8586
}

app/model/Col.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,48 @@ public function getFormilySchemaAttr($value, $data)
4646
'timeRange' => 'string[]',
4747
];
4848
$mapComponent = [
49+
'text' => 'Input',
4950
'select' => 'Select',
50-
'textarea' => 'Input.TextArea',
51-
'password' => 'Password',
51+
'switch' => 'Switch',
52+
'digit' => 'NumberPicker',
5253
'money' => 'NumberPicker',
54+
'password' => 'Password',
55+
'treeSelect' => 'TreeSelect',
56+
'cascader' => 'Cascader',
57+
'textarea' => 'Input.TextArea',
58+
'code' => 'Input.TextArea',
59+
'jsonCode' => 'Input.TextArea',
60+
'radio' => 'Radio.Group',
61+
'checkbox' => 'Checkbox.Group',
62+
'rate' => 'Rate',
63+
'percent' => 'Slider',
64+
'progress' => 'Slider',
65+
'avatar' => 'Upload',
66+
'image' => 'Upload',
67+
//'color' => '',
68+
'date' => 'DatePicker',
69+
'dateTime' => 'DatePicker',
70+
'dateWeek' => 'DatePicker',
71+
'dateMonth' => 'DatePicker',
72+
'dateQuarter' => 'DatePicker',
73+
'dateYear' => 'DatePicker',
5374
'dateRange' => 'DatePicker.RangePicker',
5475
'dateTimeRange' => 'DatePicker.RangePicker',
76+
'time' => 'TimePicker',
5577
'timeRange' => 'TimePicker.RangePicker',
78+
//'second' => '',
79+
//'fromNow' => '',
80+
//'customRichText' => '',
5681
];
5782

5883
$schema = [
5984
'name' => $data['data_index'],
6085
'type' => $mapType[$data['value_type']] ?? 'string',
6186
'title' => $data['title'],
62-
//'required' => true,
6387
'x-decorator' => 'FormItem',
6488
'x-component' => $mapComponent[$data['value_type']] ?? 'Input',
6589
];
90+
!empty($data['required']) && $schema['required'] = true;
6691
!empty($data['value_enum_dict_key']) && $schema['enum'] = array_map(
6792
fn($key, $value) => ['value' => $key, 'label' => $value],
6893
array_keys(system_dict($data['value_enum_dict_key'])),

0 commit comments

Comments
 (0)