Quellcode durchsuchen

refactor(fund): 重构资金模块的表单和网格辅助方法

- 移除了 FormHelperTrait 中的 textBalance 和 radioStatus 方法
- 更新了 FundConfigController 中的表单和网格定义
- 删除了 FundController 中对移除方法的调用
- 优化了 FundOrderController 中的命名空间引用
- 更新了 GenerateFundCurrencyConfigJson 命令中的 JSON 数据结构
- 调整了 FundCurrencyModel 中的注释和关联关系
- 更新了 GameConfigController 中对资金配置信息的获取
- 在 FormHelper 中添加了 selectModelOption 方法,用于生成模型选项
notfff vor 7 Monaten
Ursprung
Commit
5444830bfd

+ 67 - 40
UCore/DcatAdmin/FormHelper.php

@@ -15,6 +15,7 @@ use Dcat\Admin\Form;
  * $formHelper->embedClassFields('user', '用户信息', [User::class]);
  *
  * 类属性注释示例:
+ *
  * @label 用户名
  */
 class FormHelper
@@ -35,12 +36,13 @@ class FormHelper
     public function __construct($form, $controller)
     {
 
-        $this->form = $form;
+        $this->form       = $form;
         $this->controller = $controller;
     }
 
-    public function enableBack(){
-        $this->form->tools(function (Form\Tools $tools){
+    public function enableBack()
+    {
+        $this->form->tools(function (Form\Tools $tools) {
             $tools->prepend();
         });
     }
@@ -56,23 +58,24 @@ class FormHelper
 
     /**
      * 嵌套表单
+     *
      * @param $field
      * @param $lable
      * @return Form\Field\Embeds
      * @throws \ReflectionException
      */
-    public function embedsCats($field,$lable)
+    public function embedsCats($field, $lable)
     {
         $modeClass = get_class($this->form->repository()->model());
-        $cates = $this->form->repository()->model()->getCasts();
+        $cates     = $this->form->repository()->model()->getCasts();
 //        dump($cates);
-        $class  = $cates[$field] ?? "";
+        $class           = $cates[$field] ?? "";
         $reflectionClass = new \ReflectionClass($class);
 
-        return $this->form->embeds($field,$lable,function (\Dcat\Admin\Form\EmbeddedForm $form) use ($class,$reflectionClass){
+        return $this->form->embeds($field, $lable, function (\Dcat\Admin\Form\EmbeddedForm $form) use ($class, $reflectionClass) {
 
             // 从注释中提取字段标签
-            $getFieldLabel = function($name, $comment) {
+            $getFieldLabel = function ($name, $comment) {
                 if ($comment) {
                     // 优先提取@label标签
                     if (preg_match('/@label\s+([^\r\n]+)/', $comment, $matches)) {
@@ -99,13 +102,14 @@ class FormHelper
                         }
                     }
                 }
+
                 // 默认使用字段名转换
-                return ucwords(str_replace(['_', '-'], ' ', $name));
+                return ucwords(str_replace([ '_', '-' ], ' ', $name));
             };
             foreach ($reflectionClass->getProperties() as $property) {
                 // 设置不同类型的表单
-                $type = $property->getType() ? $property->getType()->getName() : null;
-                $name = $property->getName();
+                $type    = $property->getType() ? $property->getType()->getName() : null;
+                $name    = $property->getName();
                 $comment = $property->getDocComment();
 
                 // 根据类型设置不同表单字段
@@ -126,6 +130,7 @@ class FormHelper
                         $form->text($name, $getFieldLabel($name, $comment));
                 }
             }
+
             // 返回表单对象
             return $form;
 
@@ -140,7 +145,7 @@ class FormHelper
      * @param $default
      * @return Form\Field\SwitchField
      */
-    public function switch($field ,  $label = null)
+    public function switch($field, $label = null)
     {
         return $this->form->switch($field, $label);
     }
@@ -151,7 +156,7 @@ class FormHelper
      * @param $field
      * @return Form\Field\Number
      */
-    public function number($field,  $label = null)
+    public function number($field, $label = null)
     {
         return $this->form->number($field, $label);
     }
@@ -162,12 +167,12 @@ class FormHelper
      * @param $field
      * @return Form\Field\Text
      */
-    public function text($field,  $label = null)
+    public function text($field, $label = null)
     {
         return $this->form->text($field, $label);
     }
 
-    public function hidden($field,  $label = null)
+    public function hidden($field, $label = null)
     {
         return $this->form->hidden($field, $label);
     }
@@ -178,7 +183,7 @@ class FormHelper
      * @param $field
      * @return Form\Field\Select
      */
-    public function select($field,  $label = null)
+    public function select($field, $label = null)
     {
         return $this->form->select($field, $label);
     }
@@ -190,23 +195,23 @@ class FormHelper
      * @param array $enmu 枚举
      * @return Form\Field\Select|mixed
      */
-    public function selectOption($field, $enmu,  $label = null)
+    public function selectOption($field, $enmu, $label = null)
     {
         return $this->form->select($field, $label)->options($this->useing($field, $enmu));
     }
 
-    public function selectOptionCast($field,   $label = null)
+    public function selectOptionCast($field, $label = null)
     {
         $modeClass = get_class($this->form->repository()->model());
-        $cates = $this->form->repository()->model()->getCasts();
+        $cates     = $this->form->repository()->model()->getCasts();
 //        dump($cates);
-        $enmu  = $cates[$field] ?? "";
+        $enmu = $cates[$field] ?? "";
         if ($enmu === '') {
             throw new \Exception("$field is not a model $modeClass casts");
         }
-        $values  = $enmu::getValueDescription();
+        $values = $enmu::getValueDescription();
 
-        return $this->form->select($field, $label)->options( $values);
+        return $this->form->select($field, $label)->options($values);
     }
 
     /**
@@ -215,7 +220,7 @@ class FormHelper
      * @param $field
      * @return Form\Field\MultipleSelect
      */
-    public function multipleSelect($field,  $label = null)
+    public function multipleSelect($field, $label = null)
     {
         return $this->form->multipleSelect($field, $label);
     }
@@ -226,7 +231,7 @@ class FormHelper
      * @param $field
      * @return Form\Field\MultipleSelectTable
      */
-    public function multipleSelectTable($field,  $label = null)
+    public function multipleSelectTable($field, $label = null)
     {
         return $this->form->multipleSelectTable($field, $label);
     }
@@ -238,7 +243,7 @@ class FormHelper
      * @param $field
      * @return Form\Field\SelectTable
      */
-    public function selectTable($field,$label = null)
+    public function selectTable($field, $label = null)
     {
         return $this->form->selectTable($field, $label);
     }
@@ -250,7 +255,7 @@ class FormHelper
      * @param $field
      * @return Form\Field\Checkbox
      */
-    public function checkbox($field,  $label = null)
+    public function checkbox($field, $label = null)
     {
         return $this->form->checkbox($field, $label);
     }
@@ -262,10 +267,11 @@ class FormHelper
      * @param array $enmu
      * @return Form\Field\Radio
      */
-    public function radio($field, array $enmu,   $label = null )
+    public function radio($field, array $enmu, $label = null)
     {
         $lable = $lable ?? $field;
-        return $this->form->radio($field, $label)->options( $enmu);
+
+        return $this->form->radio($field, $label)->options($enmu);
     }
 
 
@@ -276,7 +282,7 @@ class FormHelper
      *
      * @return Form\Field\ListField
      */
-    public function list($field,  $label = null)
+    public function list($field, $label = null)
     {
         return $this->form->list($field, $label);
     }
@@ -299,14 +305,14 @@ class FormHelper
      * @param $field
      * @return $this
      */
-    public function editor($field,  $label = null)
+    public function editor($field, $label = null)
     {
         $this->form->editor($field, $label);
 
         return $this;
     }
 
-    public function multipleImage($field,  $label = null)
+    public function multipleImage($field, $label = null)
     {
         $this->form->multipleImage($field, $label);
 
@@ -333,7 +339,7 @@ class FormHelper
      * @param $field
      * @return Form\Field\Display
      */
-    public function display($field,  $label = null)
+    public function display($field, $label = null)
     {
         return $this->form->display($field, $label);
     }
@@ -342,11 +348,12 @@ class FormHelper
      * @param $field
      * @return Form\Field\Display
      */
-    public function displayOptions($field,  $label = null)
+    public function displayOptions($field, $label = null)
     {
-        $c   = $this->controller;
-        return $this->form->display($field, $label)->with(function($value) use ($field,$c) {
-            return $c->_option($field.'-'.$value);
+        $c = $this->controller;
+
+        return $this->form->display($field, $label)->with(function ($value) use ($field, $c) {
+            return $c->_option($field . '-' . $value);
 
         });
     }
@@ -358,20 +365,21 @@ class FormHelper
      * @return $this
      *
      */
-    public function textarea($field,  $label = null)
+    public function textarea($field, $label = null)
     {
         return $this->form->textarea($field, $label);
     }
 
     /**
      * 增加表格
+     *
      * @param $field
      * @param $call
      * @return Form\Field\Table
      */
-    public function table($field,$call,  $label = null)
+    public function table($field, $call, $label = null)
     {
-        return $this->form->table($field, $label,$call);
+        return $this->form->table($field, $label, $call);
     }
 
 
@@ -381,13 +389,14 @@ class FormHelper
      * @param $field
      * @return Form\Field\Tags
      */
-    public function tags($field,  $label = null)
+    public function tags($field, $label = null)
     {
         return $this->form->tags($field, $label);
     }
 
     /**
      * 关闭所有的 多余
+     *
      * @return void
      */
     public function disableAll()
@@ -409,4 +418,22 @@ class FormHelper
         $this->form->disableEditingCheck();
     }
 
+
+    /**
+     * select模型选项
+     * @param $field
+     * @param $label
+     * @param $modelClass
+     * @param $name
+     * @param $id
+     * @return Form\Field\Select|mixed
+     */
+    public function selectModelOption($field, $label, $modelClass,$name = 'name',  $id = 'id')
+    {
+        $options = $modelClass::all()->pluck($name, $id);
+
+        return $this->form->select($field, $label)->options($options);
+
+    }
+
 }

+ 4 - 0
UCore/DcatAdmin/Traits/Options.php

@@ -2,6 +2,10 @@
 
 namespace UCore\DcatAdmin\Traits;
 
+
+/**
+ *
+ */
 trait Options
 {
 

+ 18 - 24
app/Module/Fund/AdminControllers/FundConfigController.php

@@ -6,6 +6,7 @@ use App\Module\Fund\AdminControllers\Helper\FormHelper;
 use App\Module\Fund\AdminControllers\Helper\GridHelper;
 use App\Module\Fund\AdminControllers\Helper\ShowHelper;
 use App\Module\Fund\AdminControllers\Tools\SyncFundCurrencyJsonTool;
+use App\Module\Fund\Models\FundCurrencyModel;
 use App\Module\Fund\Repositorys\FundConfigRepository;
 use App\Module\Fund\Repositorys\FundCurrencyRepository;
 use Dcat\Admin\Form;
@@ -36,21 +37,21 @@ class FundConfigController extends AdminController
     {
         return Grid::make(new FundConfigRepository(), function (Grid $grid) {
             $helper = new GridHelper($grid, $this);
-            
+
             // 添加工具按钮
             $grid->tools([
                 new SyncFundCurrencyJsonTool()
             ]);
-            
+
             $helper->columnId();
             $grid->column('name', '账户种类名称')->sortable();
-            
+
             $grid->column('currency.name', '关联币种')->sortable();
-            
+
             $grid->column('create_time', '创建时间')->display(function ($value) {
                 return date('Y-m-d H:i:s', $value);
             })->sortable();
-            
+
             $grid->column('update_time', '更新时间')->display(function ($value) {
                 return date('Y-m-d H:i:s', $value);
             })->sortable();
@@ -59,11 +60,9 @@ class FundConfigController extends AdminController
             $grid->filter(function (Grid\Filter $filter) {
                 $filter->equal('id', 'ID');
                 $filter->like('name', '账户种类名称');
-                
-                // 获取所有币种作为筛选选项
-                $currencyRepository = new FundCurrencyRepository();
-                $currencies = $currencyRepository->all()->pluck('name', 'id')->toArray();
-                $filter->equal('currency_id', '关联币种')->select($currencies);
+
+
+
             });
         });
     }
@@ -78,7 +77,7 @@ class FundConfigController extends AdminController
     {
         return Show::make($id, new FundConfigRepository(), function (Show $show) {
             $helper = new ShowHelper($show, $this);
-            
+
             $show->field('id', 'ID');
             $show->field('name', '账户种类名称');
             $show->field('currency.name', '关联币种');
@@ -100,23 +99,18 @@ class FundConfigController extends AdminController
     {
         return Form::make(new FundConfigRepository(), function (Form $form) {
             $helper = new FormHelper($form, $this);
-            
+
             $form->display('id', 'ID');
-            
+
             $form->text('name', '账户种类名称')
                 ->required()
                 ->maxLength(30)
                 ->help('账户种类的名称,如"可用美元账户"、"冻结美元账户"等');
-            
-            // 获取所有币种作为选项
-            $currencyRepository = new FundCurrencyRepository();
-            $currencies = $currencyRepository->all()->pluck('name', 'id')->toArray();
-            
-            $form->select('currency_id', '关联币种')
-                ->options($currencies)
-                ->required()
-                ->help('该账户种类关联的币种');
-            
+
+
+
+//            $helper->selectTable();
+            $helper->selectModelOption('currency_id', '关联币种',FundCurrencyModel::class, 'name');
             // 保存前处理
             $form->saving(function (Form $form) {
                 // 设置时间戳
@@ -125,7 +119,7 @@ class FundConfigController extends AdminController
                 }
                 $form->update_time = time();
             });
-            
+
             // 保存后处理
             $form->saved(function (Form $form) {
                 // 提示用户更新JSON配置

+ 2 - 2
app/Module/Fund/AdminControllers/FundController.php

@@ -152,8 +152,8 @@ class FundController extends AdminController
                 ->help('用户ID,必须是有效的用户');
 
             $helper->selectFundId(); // 资金账户选择,使用AccountService
-            $helper->textBalance(); // 余额输入,自动单位转换
-            $helper->radioStatus(); // 状态选择,使用枚举
+
+
             $helper->selectFundType(); // 资金类型选择,使用枚举
 
             $form->display('create_time', '创建时间');

+ 2 - 1
app/Module/Fund/AdminControllers/FundOrderController.php

@@ -2,9 +2,10 @@
 
 namespace App\Module\Fund\AdminControllers;
 
+use App\Module\Fund\AdminControllers\Helper\FilterHelper;
+use App\Module\Fund\AdminControllers\Helper\GridHelper;
 use Spatie\RouteAttributes\Attributes\Resource;
 use UCore\DcatAdmin\AdminController;
-use UCore\DcatAdmin\GridHelper;
 use Dcat\Admin\Form;
 use Dcat\Admin\Grid;
 use Dcat\Admin\Show;

+ 4 - 35
app/Module/Fund/AdminControllers/Helper/FormHelperTrait.php

@@ -34,40 +34,9 @@ trait FormHelperTrait
             ->required();
     }
 
-    /**
-     * 添加资金余额输入(元转毫)
-     *
-     * 复用价值:高 - 统一处理余额的输入,包括单位转换和验证
-     *
-     * @param string $field 字段名
-     * @param string $label 标签名
-     * @return Field\Text
-     */
-    public function textBalance(string $field = 'balance', string $label = '余额'): Field\Text
-    {
-        return $this->form->text($field, $label)
-            ->required()
-            ->help('余额,单位为元,系统会自动转换为毫')
-            ->saving(function ($value) {
-                return $value * 1000;
-            });
-    }
 
-    /**
-     * 添加资金状态选择
-     *
-     * 复用价值:高 - 统一处理资金状态的选择,使用枚举类型
-     *
-     * @param string $field 字段名
-     * @param string $label 标签名
-     * @return Field\Radio
-     */
-    public function radioStatus(string $field = 'status', string $label = '状态'): Field\Radio
-    {
-        return $this->form->radio($field, $label)
-            ->options(FUND_STATUS::getNames())
-            ->default(FUND_STATUS::NORMAL);
-    }
+
+
 
     /**
      * 添加操作类型选择
@@ -81,7 +50,7 @@ trait FormHelperTrait
     public function selectOperateType(string $field = 'operate_type', string $label = '操作类型'): Field\Select
     {
         return $this->form->select($field, $label)
-            ->options(LOG_TYPE::getNames())
+            ->options(LOG_TYPE::getValueDescription())
             ->required();
     }
 
@@ -97,7 +66,7 @@ trait FormHelperTrait
     public function selectFundType(string $field = 'fund_type', string $label = '资金类型'): Field\Select
     {
         return $this->form->select($field, $label)
-            ->options(FUND_TYPE::getNames())
+            ->options(FUND_TYPE::getValueDescription())
             ->required();
     }
 

+ 17 - 10
app/Module/Fund/Commands/GenerateFundCurrencyConfigJson.php

@@ -4,6 +4,7 @@ namespace App\Module\Fund\Commands;
 
 use App\Module\Fund\Models\FundConfigModel;
 use App\Module\Fund\Models\FundCurrencyModel;
+use App\Module\Game\DCache\FundCurrencyJsonConfig;
 use Illuminate\Console\Command;
 use Illuminate\Support\Facades\DB;
 
@@ -16,6 +17,7 @@ use Illuminate\Support\Facades\DB;
  */
 class GenerateFundCurrencyConfigJson extends Command
 {
+
     /**
      * 命令名称
      *
@@ -47,17 +49,17 @@ class GenerateFundCurrencyConfigJson extends Command
             // 准备JSON数据
             $jsonData = [
                 'generated_ts' => time(),
-                'currencies' => [],
-                'fund_configs' => []
+                'currencies'   => [],
+                'fund' => []
             ];
 
             // 处理币种数据
             foreach ($currencies as $currency) {
                 $currencyData = [
-                    'id' => $currency->id,
+                    'id'             => $currency->id,
                     'identification' => $currency->identification,
-                    'name' => $currency->name,
-                    'icon' => $currency->icon,
+                    'name'           => $currency->name,
+                    'icon'           => $currency->icon,
                 ];
 
                 // 如果有额外数据,解析并添加
@@ -75,12 +77,12 @@ class GenerateFundCurrencyConfigJson extends Command
             foreach ($fundConfigs as $fundConfig) {
                 // 直接使用数据库中存储的currency_id字段
                 $fundConfigData = [
-                    'id' => $fundConfig->id,
-                    'name' => $fundConfig->name,
+                    'id'          => $fundConfig->id,
+                    'name'        => $fundConfig->name,
                     'currency_id' => $fundConfig->currency_id, // 关联的币种ID
                 ];
 
-                $jsonData['fund_configs'][] = $fundConfigData;
+                $jsonData['fund'][] = $fundConfigData;
             }
 
             return $jsonData;
@@ -104,20 +106,25 @@ class GenerateFundCurrencyConfigJson extends Command
 
         try {
             // 直接调用静态方法生成JSON,而不通过缓存类
-            $result = self::generateJson();
+            $result = FundCurrencyJsonConfig::getData([], true);
+
 
             if ($result !== false) {
                 $this->info('货币配置JSON文件生成成功');
                 $this->info('共生成 ' . count($result['currencies']) . ' 条币种配置数据');
-                $this->info('共生成 ' . count($result['fund_configs']) . ' 条账户种类配置数据');
+                $this->info('共生成 ' . count($result['fund']) . ' 条账户种类配置数据');
+
                 return Command::SUCCESS;
             } else {
                 $this->error('生成货币配置JSON文件失败');
+
                 return Command::FAILURE;
             }
         } catch (\Exception $e) {
             $this->error('生成货币配置JSON文件失败: ' . $e->getMessage());
+
             return Command::FAILURE;
         }
     }
+
 }

+ 7 - 3
app/Module/Fund/Models/FundCurrencyModel.php

@@ -10,13 +10,13 @@ use UCore\ModelCore;
  * 用于存储系统支持的各种币种信息,如美元、人民币、比特币等。
  * 一个币种可以对应多个账户种类(FundConfigModel)。
  *
- * field start 
+ * field start
  * @property   int  $id  自增
  * @property   string  $identification  资金标识
  * @property   string  $icon  资金标识
  * @property   string  $name  资金名字
  * @property   string  $data1  数据
- * @property   int  $create_time  
+ * @property   int  $create_time
  * @property   int  $update_time  更新时间
  * field end
  */
@@ -24,7 +24,7 @@ class FundCurrencyModel extends ModelCore
 {
     protected $table = 'fund_currency';
 
-    // attrlist start 
+    // attrlist start
     protected $fillable = [
         'id',
         'identification',
@@ -35,6 +35,7 @@ class FundCurrencyModel extends ModelCore
         'update_time',
     ];
     // attrlist end
+
     public $timestamps = false;
 
     /**
@@ -48,4 +49,7 @@ class FundCurrencyModel extends ModelCore
     {
         return $this->hasMany(FundConfigModel::class, 'currency_id');
     }
+
+
+
 }

+ 5 - 5
app/Module/Game/AdminControllers/GameConfigController.php

@@ -429,12 +429,12 @@ class GameConfigController extends AdminController
      */
     protected function getFundCurrencyConfigInfo()
     {
-        $data = FundCurrencyJsonConfig::getData();
-
+        $data = FundCurrencyJsonConfig::getData([],true);
+//        dd($data);
         // 计算有关联币种的账户种类数量
         $linkedAccountsCount = 0;
-        if (isset($data['fund_configs'])) {
-            foreach ($data['fund_configs'] as $fundConfig) {
+        if (isset($data['fund'])) {
+            foreach ($data['fund'] as $fundConfig) {
                 if (!empty($fundConfig['currency_id'])) {
                     $linkedAccountsCount++;
                 }
@@ -444,7 +444,7 @@ class GameConfigController extends AdminController
         $info = [
             '生成时间'  => Datetime::ts2string($data['generated_ts']),
             '币种数量' => isset($data['currencies']) ? count($data['currencies']) : 0,
-            '账户种类数量' => isset($data['fund_configs']) ? count($data['fund_configs']) : 0,
+            '账户种类数量' => isset($data['fund']) ? count($data['fund']) : 0,
             '已关联币种的账户数量' => $linkedAccountsCount,
         ];