Your Name hai 8 meses
pai
achega
1ec9c0bbd1

+ 77 - 1
UCore/DcatAdmin/FormHelper.php

@@ -7,6 +7,16 @@ use UCore\DcatAdmin\Traits\UserID;
 use Dcat\Admin\Form;
 
 
+/**
+ * 表单助手类 - 自动根据类属性生成表单字段
+ *
+ * 使用示例:
+ * $formHelper = new FormHelper($form);
+ * $formHelper->embedClassFields('user', '用户信息', [User::class]);
+ *
+ * 类属性注释示例:
+ * @label 用户名
+ */
 class FormHelper
 {
 
@@ -43,9 +53,75 @@ class FormHelper
     {
         return $this->form->field($field);
     }
-    public function embedsCats($field)
+    public function embedsCats($field,$lable)
     {
+        $modeClass = get_class($this->form->repository()->model());
+        $cates = $this->form->repository()->model()->getCasts();
+//        dump($cates);
+        $class  = $cates[$field] ?? "";
+        $reflectionClass = new \ReflectionClass($class);
+
+        return $this->form->embeds($field,$lable,function (\Dcat\Admin\Form\EmbeddedForm $form) use ($class,$reflectionClass){
+
+            // 从注释中提取字段标签
+            $getFieldLabel = function($name, $comment) {
+                if ($comment) {
+                    // 优先提取@label标签
+                    if (preg_match('/@label\s+([^\r\n]+)/', $comment, $matches)) {
+                        return trim($matches[1]);
+                    }
+
+                    // 处理多行注释,提取第一行描述
+                    $lines = explode("\n", $comment);
+                    foreach ($lines as $line) {
+                        // 匹配描述行 (以 * 开头但不包含@标签的行)
+                        if (preg_match('/^\s*\*\s+([^@*]+)/', $line, $matches)) {
+                            $desc = trim($matches[1]);
+                            if (!empty($desc) && !preg_match('/@\w+/', $desc)) {
+                                return $desc;
+                            }
+                        }
+                    }
+
+                    // 最后提取@var类型后的描述
+                    if (preg_match('/@var\s+\S+\s+([^\r\n]+)/', $comment, $matches)) {
+                        $desc = trim($matches[1]);
+                        if (!empty($desc)) {
+                            return $desc;
+                        }
+                    }
+                }
+                // 默认使用字段名转换
+                return ucwords(str_replace(['_', '-'], ' ', $name));
+            };
+            foreach ($reflectionClass->getProperties() as $property) {
+                // 设置不同类型的表单
+                $type = $property->getType() ? $property->getType()->getName() : null;
+                $name = $property->getName();
+                $comment = $property->getDocComment();
+
+                // 根据类型设置不同表单字段
+                switch ($type) {
+                    case 'string':
+                        $form->text($name, $getFieldLabel($name, $comment));
+                        break;
+                    case 'int':
+                        $form->number($name, $getFieldLabel($name, $comment));
+                        break;
+                    case 'float':
+                        $form->decimal($name, $getFieldLabel($name, $comment));
+                        break;
+                    case 'bool':
+                        $form->switch($name, $getFieldLabel($name, $comment));
+                        break;
+                    default:
+                        $form->text($name, $getFieldLabel($name, $comment));
+                }
+            }
+            // 返回表单对象
+            return $form;
 
+        });
     }
 
 

+ 1 - 1
UCore/Model/CastsAttributes.php

@@ -37,7 +37,7 @@ abstract class CastsAttributes implements \Illuminate\Contracts\Database\Eloquen
     {
 
         $ob = json_decode($value, true);
-        if (is_array($ob)) {
+        if (empty($ob)) {
             $ob = json_decode('{}',true);
         }
         $res = new static();

+ 2 - 2
app/Module/GameItems/AdminControllers/ItemController.php

@@ -201,9 +201,9 @@ class ItemController extends AdminController
             $form->number('default_expire_seconds', '默认过期时间(秒)')
                 ->default(0)
                 ->help('0表示永不过期');
-            $helper->keyValue('display_attributes', '显示属性')
+            $helper->embedsCats('display_attributes', '显示属性')
                 ->help('用于显示的属性,如:攻击力、防御力等');
-            $form->keyValue('numeric_attributes', '数值属性')
+            $helper->embedsCats('numeric_attributes', '数值属性')
                 ->help('用于计算的属性,如:宝箱掉落物品数量范围等');
 
             $form->datetime('global_expire_at', '全局过期时间')

+ 5 - 1
app/Module/GameItems/Casts/DisplayAttributesCast.php

@@ -9,7 +9,11 @@ use JsonMapper;
 class DisplayAttributesCast extends \UCore\Model\CastsAttributes
 {
 
-    public $img = ' ';
+    /**
+     * 客户端图标地址
+     * @var string $img
+     */
+    public string $img = ' ';
 
 
     /**

+ 26 - 2
app/Module/GameItems/Casts/NumericAttributesCast.php

@@ -11,13 +11,37 @@ class NumericAttributesCast extends CastsAttributes
 
     /**
      * 宝箱最小数量
+     *
      * @var int $min_drop_count
      */
-    public $min_drop_count = 0;
+    public int $min_drop_count = 0;
 
     /**
      * 宝箱最大数量
+     *
      * @var int $max_drop_count
      */
-    public $max_drop_count = 0;
+    public int $max_drop_count = 0;
+
+
+
+    /**
+     * 测试属性浮点类型
+     *
+     * @var float $fint
+     */
+    public float $fint = 0.1;
+
+    /**
+     * 测试属性整数类型
+     *
+     * @var int $intd
+     */
+    public int $intd = 1;
+    /**
+     * 测试属性字符串类型
+     * @var string $stval
+     */
+    public string $stval = ' ';
+
 }