ソースを参照

为宝箱配置列表增加复制功能

notfff 7 ヶ月 前
コミット
ad6fbc48

+ 77 - 0
app/Module/GameItems/AdminControllers/Actions/DuplicateItemChestConfigAction.php

@@ -0,0 +1,77 @@
+<?php
+
+namespace App\Module\GameItems\AdminControllers\Actions;
+
+use App\Module\GameItems\Models\ItemChestConfig;
+use App\Module\GameItems\Enums\ITEM_TYPE;
+use Dcat\Admin\Grid\RowAction;
+use Illuminate\Http\Request;
+
+/**
+ * 复制宝箱配置行操作
+ */
+class DuplicateItemChestConfigAction extends RowAction
+{
+    /**
+     * 按钮标题
+     *
+     * @var string
+     */
+    protected $title = '<i class="fa fa-copy"></i> 复制';
+
+    /**
+     * 处理请求
+     *
+     * @param Request $request
+     * @return \Dcat\Admin\Actions\Response
+     */
+    public function handle(Request $request)
+    {
+        try {
+            // 获取当前行ID
+            $id = $this->getKey();
+            
+            // 查找原宝箱配置
+            $originalConfig = ItemChestConfig::with(['item'])->find($id);
+            if (!$originalConfig) {
+                return $this->response()->error('宝箱配置不存在');
+            }
+            
+            // 查找一个未被使用的宝箱物品
+            $usedItemIds = ItemChestConfig::pluck('item_id')->toArray();
+            $availableChest = \App\Module\GameItems\Models\Item::where('type', ITEM_TYPE::CHEST)
+                ->whereNotIn('id', $usedItemIds)
+                ->first();
+
+            if (!$availableChest) {
+                return $this->response()->error('没有可用的宝箱物品进行复制,所有宝箱都已有配置');
+            }
+
+            // 复制配置,使用找到的可用宝箱
+            $newConfig = $originalConfig->replicate();
+            $newConfig->item_id = $availableChest->id; // 使用可用的宝箱ID
+            $newConfig->is_active = false; // 复制的配置默认为未激活状态
+            $newConfig->save();
+
+            $chestName = $originalConfig->item ? $originalConfig->item->name : '未知宝箱';
+
+            // 跳转到编辑页面,让用户修改配置
+            return $this->response()
+                ->success("已成功复制宝箱配置 [{$chestName}] 到 [{$availableChest->name}] (ID: {$newConfig->id})")
+                ->redirect(admin_url("game-items-chest-configs/{$newConfig->id}/edit"));
+        } catch (\Exception $e) {
+            return $this->response()
+                ->error('复制失败: ' . $e->getMessage());
+        }
+    }
+
+    /**
+     * 确认信息
+     *
+     * @return array|string|void
+     */
+    public function confirm()
+    {
+        return ['确定要复制此宝箱配置吗?', '复制操作将创建一个新的宝箱配置记录,并跳转到编辑页面'];
+    }
+}

+ 4 - 0
app/Module/GameItems/AdminControllers/ItemChestConfigController.php

@@ -13,6 +13,7 @@ use Spatie\RouteAttributes\Attributes\Resource;
 use UCore\DcatAdmin\AdminController;
 use App\Module\GameItems\AdminControllers\Helper\GridHelper;
 use App\Module\GameItems\AdminControllers\Helper\ShowHelper;
+use App\Module\GameItems\AdminControllers\Actions\DuplicateItemChestConfigAction;
 
 /**
  * 宝箱配置控制器
@@ -158,6 +159,9 @@ class ItemChestConfigController extends AdminController
 
             // 行操作
             $grid->actions(function (Grid\Displayers\Actions $actions) {
+                // 添加复制按钮
+                $actions->append(new DuplicateItemChestConfigAction());
+
                 $actions->disableDelete();
             });
         });