diff --git a/crmeb/app/adminapi/controller/v1/setting/SystemCrud.php b/crmeb/app/adminapi/controller/v1/setting/SystemCrud.php index 3812ce2c1..1185a5697 100644 --- a/crmeb/app/adminapi/controller/v1/setting/SystemCrud.php +++ b/crmeb/app/adminapi/controller/v1/setting/SystemCrud.php @@ -360,7 +360,7 @@ class SystemCrud extends AuthController if (config('filesystem.password') != $pwd) { return app('json')->fail('文件管理密码错误'); } - + if (empty($filepath) || !$id) { return app('json')->fail(410087); } diff --git a/crmeb/app/services/system/SystemCrudServices.php b/crmeb/app/services/system/SystemCrudServices.php index ae21a526e..60fc8ce66 100644 --- a/crmeb/app/services/system/SystemCrudServices.php +++ b/crmeb/app/services/system/SystemCrudServices.php @@ -172,10 +172,10 @@ class SystemCrudServices extends BaseServices // 'value' => 'dateTimeRange', // 'label' => '日期时间区间选择', // ], - [ - 'value' => 'checkbox', - 'label' => '多选框', - ], +// [ +// 'value' => 'checkbox', +// 'label' => '多选框', +// ], [ 'value' => 'radio', 'label' => '单选框', diff --git a/crmeb/crmeb/services/crud/Make.php b/crmeb/crmeb/services/crud/Make.php index 2156caa26..9d9ff3812 100644 --- a/crmeb/crmeb/services/crud/Make.php +++ b/crmeb/crmeb/services/crud/Make.php @@ -81,6 +81,12 @@ abstract class Make */ protected $value = []; + /** + * 数据库获取器后缀 + * @var string + */ + protected $attrPrefix = '_label'; + /** * 后台前端模板根路径 * @var string diff --git a/crmeb/crmeb/services/crud/Model.php b/crmeb/crmeb/services/crud/Model.php index 5795dd7cb..a90611002 100644 --- a/crmeb/crmeb/services/crud/Model.php +++ b/crmeb/crmeb/services/crud/Model.php @@ -14,6 +14,8 @@ namespace crmeb\services\crud; +use think\helper\Str; + /** * Class Model * @author 等风来 @@ -57,9 +59,73 @@ class Model extends Make $this->value['content-php'] = $this->tab() . "use SoftDelete;\n"; } $this->value['modelName'] = $options['modelName'] ?? $name; + $field = $options['fromField'] ?? []; + + $attrFnContent = []; + foreach ($field as $item) { + if (in_array($item['type'], ['radio', 'select']) && !empty($item['option'])) { + $attrFnContent[] = $this->getAttrFnContent($item['field'], $item['name'], $item['option']); + } + } + if ($attrFnContent) { + $this->value['attr-php'] = implode("\n", $attrFnContent); + } + return parent::handle($name, $options); } + /** + * + * @param string $key + * @param array $options + * @return array|false|string|string[] + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/5/11 + */ + protected function getAttrFnContent(string $key, string $comment, array $options) + { + $attrFnStub = file_get_contents($this->getStub('attr')); + $var = ['{%field%}', '{%date%}', '{%name%}', '{%content-php%}']; + $value = [ + Str::studly($key . $this->attrPrefix), + date('Y-m-d'), + $comment, + $this->getSwithAndSelectPhpContent($options) + ]; + + return str_replace($var, $value, $attrFnStub); + } + + /** + * 获取开关和下拉框获取器内容 + * @param array $options + * @return string + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/5/11 + */ + protected function getSwithAndSelectPhpContent(array $options) + { + if (!$options) { + return ''; + } + $case = []; + foreach ($options as $option) { + $case[] = $this->tab(3) . "case " . $option['value'] . ":\n" . $this->tab(4) . "\$attr = '$option[label]';\n" . $this->tab(4) . "break;"; + } + $caseContent = implode("\n", $case); + $tab2 = $this->tab(2); + $content = << $routePath . 'crudModel.stub', + 'attr' => $routePath . 'getattr.stub', + ]; + + return $type ? $stubs[$type] : $stubs['model']; } } diff --git a/crmeb/crmeb/services/crud/Service.php b/crmeb/crmeb/services/crud/Service.php index 88d91c0ee..315b9434f 100644 --- a/crmeb/crmeb/services/crud/Service.php +++ b/crmeb/crmeb/services/crud/Service.php @@ -59,12 +59,20 @@ class Service extends Make $contentAction .= $stub . "\n"; } - //生成form表单 - if (in_array('save', $action) || in_array('update', $action)) { - $var = ['{%key%}', '{%date%}', '{%route%}', '{%form-php%}', '{%modelName%}']; + if ($field) { + //生成form表单 + $var = ['{%key%}', '{%date%}', '{%route%}', '{%form-php%}', '{%modelName%}', '{%field%}']; $value = [$options['key'] ?? 'id', $this->value['date'], Str::snake($options['route'] ?? $name)]; $from = []; + $select = []; foreach ($field as $item) { + //处理查询字段 + if (in_array($item['type'], ['radio', 'select', 'checkbox'])) { + $select[] = $item['field'] . ' as ' . $item['field'] . $this->attrPrefix; + } else { + $select[] = $item['field']; + } + //处理表单信息 switch ($item['type']) { case 'frameImageOne': $from[] = $this->tab(2) . $this->getframeImageOnePhpContent($item['field'], $item['name']) . ';'; @@ -95,7 +103,7 @@ class Service extends Make $value[] = ''; } $value[] = $options['modelName'] ?? $options['menus'] ?? $name; - + $value[] = implode(',', $select); if ($value && $var) { $contentAction = str_replace($var, $value, $contentAction); } diff --git a/crmeb/crmeb/services/crud/ViewPages.php b/crmeb/crmeb/services/crud/ViewPages.php index 56f089a66..8193da491 100644 --- a/crmeb/crmeb/services/crud/ViewPages.php +++ b/crmeb/crmeb/services/crud/ViewPages.php @@ -75,6 +75,7 @@ class ViewPages extends Make $contentVue = []; foreach ($field as $key => $item) { $keyName = 'key'; + $fieldValue = $item['field']; if (isset($item['type'])) { switch ($item['type']) { case 'frameImageOne': @@ -88,9 +89,11 @@ class ViewPages extends Make $contentVue[] = str_replace(['{%field%}'], [$item['field']], $templateContent); break; } + if (in_array($item['type'], ['radio', 'select', 'checkbox'])) { + $fieldValue = $fieldValue . $this->attrPrefix; + } } - - $columnStr[] = ($key == 0 ? $this->tab(2) : '') . "{\n" . $this->tab(3) . "title:\"{$item['name']}\"," . $this->tab(2) . "\n" . $this->tab(3) . "$keyName:\"{$item['field']}\"\n" . $this->tab(2) . "}\n"; + $columnStr[] = ($key == 0 ? $this->tab(2) : '') . "{\n" . $this->tab(3) . "title:\"{$item['name']}\"," . $this->tab(2) . "\n" . $this->tab(3) . "$keyName:\"{$fieldValue}\"\n" . $this->tab(2) . "}\n"; } $this->value['auth'] = Str::snake($name); $this->value['componentName'] = $this->value['auth']; diff --git a/crmeb/crmeb/services/crud/stubs/controller/delete.stub b/crmeb/crmeb/services/crud/stubs/controller/delete.stub index 293b74fbb..cbe7de7a4 100644 --- a/crmeb/crmeb/services/crud/stubs/controller/delete.stub +++ b/crmeb/crmeb/services/crud/stubs/controller/delete.stub @@ -10,7 +10,7 @@ return app('json')->fail(100100); } - if ($this->service->destroy($id)) { + if ($this->service->destroy((int)$id)) { return app('json')->success(100002); } else { return app('json')->success(100008); diff --git a/crmeb/crmeb/services/crud/stubs/controller/edit.stub b/crmeb/crmeb/services/crud/stubs/controller/edit.stub index 6e01c891f..fbb378b70 100644 --- a/crmeb/crmeb/services/crud/stubs/controller/edit.stub +++ b/crmeb/crmeb/services/crud/stubs/controller/edit.stub @@ -6,5 +6,5 @@ */ public function edit($id) { - return app('json')->success($this->service->getCrudForm($id)); + return app('json')->success($this->service->getCrudForm((int)$id)); } diff --git a/crmeb/crmeb/services/crud/stubs/model/crudModel.stub b/crmeb/crmeb/services/crud/stubs/model/crudModel.stub index 29eaf0230..738ee2aa5 100644 --- a/crmeb/crmeb/services/crud/stubs/model/crudModel.stub +++ b/crmeb/crmeb/services/crud/stubs/model/crudModel.stub @@ -41,5 +41,5 @@ class {%nameCamel%} extends BaseModel * @var string */ protected $pk = '{%key%}'; - +{%attr-php%} } diff --git a/crmeb/crmeb/services/crud/stubs/model/getattr.stub b/crmeb/crmeb/services/crud/stubs/model/getattr.stub index c445665cb..96e3568f6 100644 --- a/crmeb/crmeb/services/crud/stubs/model/getattr.stub +++ b/crmeb/crmeb/services/crud/stubs/model/getattr.stub @@ -1,3 +1,9 @@ + /** + * {%name%}获取器 + * @date {%date%} + * @param string $value + * @return string + */ public function get{%field%}Attr($value) { {%content-php%} diff --git a/crmeb/crmeb/services/crud/stubs/service/crudListIndex.stub b/crmeb/crmeb/services/crud/stubs/service/crudListIndex.stub index 4761b01d2..ea7de17ca 100644 --- a/crmeb/crmeb/services/crud/stubs/service/crudListIndex.stub +++ b/crmeb/crmeb/services/crud/stubs/service/crudListIndex.stub @@ -7,7 +7,7 @@ public function getCrudListIndex(array $where = []) { [$page, $limit] = $this->getPageValue(); - $model = $this->dao->selectModel($where, '*', 0, 0, '{%key%} desc'); + $model = $this->dao->selectModel($where, '{%field%}', 0, 0, '{%key%} desc'); return ['count' => $model->count(), 'list' => $model->page($page ?: 1, $limit ?: 10)->select()->toArray()]; }