Pārlūkot izejas kodu

调整字段查询和展示问题

liaofei 2 gadi atpakaļ
vecāks
revīzija
2dff6bb770

+ 1 - 1
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);
         }

+ 4 - 4
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' => '单选框',

+ 6 - 0
crmeb/crmeb/services/crud/Make.php

@@ -81,6 +81,12 @@ abstract class Make
      */
     protected $value = [];
 
+    /**
+     * 数据库获取器后缀
+     * @var string
+     */
+    protected $attrPrefix = '_label';
+
     /**
      * 后台前端模板根路径
      * @var string

+ 75 - 2
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 = <<<CONTENT
+{$tab2}\$attr = '';
+{$tab2}switch ((int)\$value){
+{$caseContent}
+{$tab2}}
+{$tab2}return \$attr;
+CONTENT;
+        return $content;
+    }
+
     /**
      * @param string $path
      * @param string $name
@@ -83,8 +149,15 @@ class Model extends Make
      * @email 136327134@qq.com
      * @date 2023/3/13
      */
-    protected function getStub(string $type = '')
+    protected function getStub(string $type = 'model')
     {
-        return __DIR__ . DS . 'stubs' . DS . 'model' . DS . 'crudModel.stub';
+        $routePath = __DIR__ . DS . 'stubs' . DS . 'model' . DS;
+
+        $stubs = [
+            'model' => $routePath . 'crudModel.stub',
+            'attr' => $routePath . 'getattr.stub',
+        ];
+
+        return $type ? $stubs[$type] : $stubs['model'];
     }
 }

+ 12 - 4
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);
             }

+ 5 - 2
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'];

+ 1 - 1
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);

+ 1 - 1
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));
     }

+ 1 - 1
crmeb/crmeb/services/crud/stubs/model/crudModel.stub

@@ -41,5 +41,5 @@ class {%nameCamel%} extends BaseModel
      * @var string
      */
     protected $pk = '{%key%}';
-
+{%attr-php%}
 }

+ 6 - 0
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%}

+ 1 - 1
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()];
     }