Pārlūkot izejas kodu

feat(game-items): 优化宝箱开启消耗配置模块

- 优化宝箱名称链接,增加物品详情页面跳转
- 改进消耗ID显示逻辑,增加物品名称和链接
- 调整表单输入方式,使用数字输入框替代选择框
- 重构物品关系方法,提高代码可读性
- 引入 ImgService 服务,优化图片处理逻辑
Your Name 8 mēneši atpakaļ
vecāks
revīzija
bc073c3586

+ 2 - 1
UCore/DcatAdmin/ShowHelper.php

@@ -2,6 +2,7 @@
 
 namespace UCore\DcatAdmin;
 
+use App\Module\File\Services\ImgService;
 use UCore\DcatAdmin\Show\Divider;
 use UCore\DcatAdmin\Show\Expand;
 use UCore\DcatAdmin\Traits\Options;
@@ -58,7 +59,7 @@ class ShowHelper
     {
         return $this->show->field($name,  $label)->as(function ($pp){
 //            dump($pp);
-            return Img::img2imgurl($pp);
+            return ImgService::img2imgurl($pp);
         })->image();
     }
     public function fieldTimes($name,$label='')

+ 17 - 23
app/Module/GameItems/AdminControllers/ItemChestOpenCostController.php

@@ -42,19 +42,23 @@ class ItemChestOpenCostController extends AdminController
 
             $helper->columnId();
             $grid->column('chest.name', '宝箱名称')->link(function () {
-                return admin_url('game-items/items/' . $this->chest_id);
+                return admin_url('game-items/' . $this->chest_id);
             });
 
             $grid->column('cost_type', '消耗类型')->display(function ($value) {
                 return CHEST_COST_TYPE::getAll()[$value] ?? '未知';
             });
 
-            $grid->column('cost_id', '消耗ID')->display(function ($value) {
+            $grid->column('cost_id', '消耗ID')->as(function ($value) {
                 if ($this->cost_type == CHEST_COST_TYPE::ITEM->value) {
-                    return $this->costItem ? "{$this->costItem->name} (ID: {$value})" : $value;
+                    if ($this->costItem) {
+                        $itemUrl = admin_url('game-items-items/' . $value);
+                        return "<a href='{$itemUrl}' target='_blank'>{$this->costItem->name} <i class='fa fa-external-link'></i></a> (ID: {$value})";
+                    }
+                    return $value;
                 }
                 return $value;
-            });
+            })->unescape();
 
             $grid->column('cost_quantity', '消耗数量');
             $grid->column('is_active', '是否激活')->switch();
@@ -65,14 +69,9 @@ class ItemChestOpenCostController extends AdminController
             $grid->filter(function (Grid\Filter $filter) {
                 $helper = new FilterHelper($filter, $this);
                 $helper->equal('id', 'ID');
-                $filter->equal('chest_id', '宝箱')->select(
-                    Item::where('type', 3)->pluck('name', 'id')
-                );
-                $filter->where('chest_name', function ($query) {
-                    $query->whereHas('chest', function ($query) {
-                        $query->where('name', 'like', "%{$this->input}%");
-                    });
-                }, '宝箱名称');
+                $helper->equalSelectModelChestItem('chest_id', '宝箱ID');
+
+
                 $filter->equal('cost_type', '消耗类型')->select(CHEST_COST_TYPE::getAll());
                 $filter->equal('is_active', '是否激活')->radio([
                     1 => '是',
@@ -116,7 +115,10 @@ class ItemChestOpenCostController extends AdminController
             // 根据消耗类型显示不同的关联信息
             $show->field('costItem.name', '消耗物品名称')->unescape()->as(function ($name) {
                 if ($this->cost_type == CHEST_COST_TYPE::ITEM->value && $name) {
-                    return "<span class='badge badge-primary'>{$name}</span>";
+                    $itemUrl = admin_url('game-items/items/' . $this->cost_id);
+                    return "<span class='badge badge-primary'>{$name}</span> "
+                        . "<a href='{$itemUrl}' target='_blank' class='btn btn-sm btn-primary ml-1'>"
+                        . "<i class='fa fa-external-link'></i> 查看物品详情</a>";
                 }
                 return '-';
             });
@@ -144,19 +146,11 @@ class ItemChestOpenCostController extends AdminController
             // 只显示宝箱类型的物品
             $chests = Item::where('type', 3)->pluck('name', 'id');
 
-            $form->select('chest_id', '宝箱')->options($chests)->required();
+            $helper->selectModelChestItem('chest_id', '宝箱');
             $form->select('cost_type', '消耗类型')->options(CHEST_COST_TYPE::getAll())->required();
 
             // 根据消耗类型显示不同的选择器
-            $form->select('cost_id', '消耗ID')->options(function ($id) {
-                $costType = request()->get('cost_type');
-
-                if ($costType == CHEST_COST_TYPE::ITEM->value) {
-                    return Item::pluck('name', 'id');
-                }
-
-                return [];
-            })->required();
+            $form->number('cost_id', '消耗ID')->required();
 
             $helper->number('cost_quantity', '消耗数量')->min(1)->default(1)->required();
             $form->switch('is_active', '是否激活')->default(1);

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

@@ -192,7 +192,7 @@ class ItemController extends AdminController
             $show->relation('chest_costs', '宝箱消耗',
                 function (\App\Module\GameItems\Models\Item $item) {
 //                dd($item);
-                    $grid = new Grid(new ItemChestOpenCostRepository(['cost_item']));
+                    $grid = new Grid(new ItemChestOpenCostRepository(['costItem']));
 
                     $grid->model()->where('chest_id', $item->id);
 
@@ -200,7 +200,7 @@ class ItemController extends AdminController
                     $grid->setResource('game-items-chest-costs');
 
                     $grid->id();
-                    $grid->column('cost_item.name', '物品名称');
+                    $grid->column('costItem.name', '物品名称');
 
 
 

+ 18 - 16
app/Module/GameItems/Models/ItemChestOpenCost.php

@@ -10,18 +10,20 @@ use UCore\ModelCore;
  * 宝箱开启消耗配置
  *
  * field start
- * @property   int  $id  记录ID,主键
- * @property   int  $chest_id  宝箱ID,外键关联item_items表
- * @property   int  $cost_type  消耗类型(1:物品, 2:货币, 3:其他资源)
- * @property   int  $cost_id  消耗的物品/货币/资源ID
- * @property   int  $cost_quantity  消耗数量
- * @property   int  $is_active  是否激活(0:否, 1:是)
- * @property   \Carbon\Carbon  $created_at  创建时间
- * @property   \Carbon\Carbon  $updated_at  更新时间
+ *
+ * @property   int $id  记录ID,主键
+ * @property   int $chest_id  宝箱ID,外键关联item_items表
+ * @property   int $cost_type  消耗类型(1:物品, 2:货币, 3:其他资源)
+ * @property   int $cost_id  消耗的物品/货币/资源ID
+ * @property   int $cost_quantity  消耗数量
+ * @property   int $is_active  是否激活(0:否, 1:是)
+ * @property   \Carbon\Carbon $created_at  创建时间
+ * @property   \Carbon\Carbon $updated_at  更新时间
  * field end
  */
 class ItemChestOpenCost extends ModelCore
 {
+
     /**
      * 与模型关联的表名
      *
@@ -46,10 +48,10 @@ class ItemChestOpenCost extends ModelCore
      * @var array
      */
     protected $casts = [
-        'cost_type' => 'integer',
-        'cost_id' => 'integer',
+        'cost_type'     => 'integer',
+        'cost_id'       => 'integer',
         'cost_quantity' => 'integer',
-        'is_active' => 'boolean',
+        'is_active'     => 'boolean',
     ];
 
     /**
@@ -59,7 +61,7 @@ class ItemChestOpenCost extends ModelCore
      */
     public function chest(): BelongsTo
     {
-        return $this->belongsTo(Item::class, 'chest_id');
+        return $this->belongsTo(Item::class, 'chest_id', 'id');
     }
 
     /**
@@ -69,11 +71,10 @@ class ItemChestOpenCost extends ModelCore
      */
     public function costItem(): ?BelongsTo
     {
-        if ($this->cost_type === CHEST_COST_TYPE::ITEM->value) {
-            return $this->belongsTo(Item::class, 'cost_id');
-        }
 
-        return null;
+        return $this->belongsTo(Item::class, 'cost_id');
+
+
     }
 
     /**
@@ -85,4 +86,5 @@ class ItemChestOpenCost extends ModelCore
     {
         return CHEST_COST_TYPE::getAll()[$this->cost_type] ?? '未知';
     }
+
 }