| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- <?php
- namespace App\Module\GameItems\AdminControllers\Tools;
- use App\Module\Game\DCache\RecipeJsonConfig;
- use App\Module\GameItems\Models\ItemRecipe;
- use Dcat\Admin\Grid\Tools\AbstractTool;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Log;
- /**
- * 物品合成配方配置表同步工具
- *
- * 用于在后台管理界面中同步物品合成配方配置表数据到JSON文件
- */
- class SyncRecipeJsonTool extends AbstractTool
- {
- /**
- * 是否显示按钮
- *
- * @var bool
- */
- protected $shouldDisplay;
- /**
- * 按钮样式
- *
- * @var string
- */
- protected $style = 'btn btn-primary waves-effect';
- /**
- * 构造函数
- *
- * @param bool $shouldDisplay 是否显示按钮
- */
- public function __construct(bool $shouldDisplay = true)
- {
- $this->shouldDisplay = $shouldDisplay;
- }
- /**
- * 按钮标题
- *
- * @return string
- */
- public function title()
- {
- return '生成JSON';
- }
- /**
- * 确认提示
- *
- * @return string
- */
- public function confirm()
- {
- return '确定要生成物品合成配方配置JSON数据吗?';
- }
- /**
- * 处理请求
- *
- * @param Request $request
- * @return mixed
- */
- public function handle(Request $request)
- {
- try {
- // 直接调用缓存类刷新数据,这会同时生成JSON文件
- RecipeJsonConfig::getData([], true);
- return $this->response()->success('生成成功')->refresh();
- } catch (\Exception $e) {
- Log::error('Generate recipe.json exception: '.$e->getMessage());
- return $this->response()->error('生成失败:'.$e->getMessage());
- }
- }
- /**
- * 渲染按钮
- *
- * @return string
- */
- public function render()
- {
- if (!$this->shouldDisplay) {
- return '';
- }
- return parent::render();
- }
- /**
- * 判断是否应该显示按钮
- *
- * @return bool
- */
- public static function shouldDisplay(): bool
- {
- // 获取缓存数据
- $json = RecipeJsonConfig::getData();
- // 如果没有生成时间戳,说明需要生成
- if (!isset($json['generated_ts'])) {
- return true;
- }
- // 获取生成时间和最后更新时间
- $generatedAt = \Carbon\Carbon::createFromTimestamp($json['generated_ts']);
- // 获取合成配方的最后更新时间
- $lastUpdated = \Carbon\Carbon::parse(ItemRecipe::max('updated_at') ?: '2000-01-01');
- // 如果生成时间早于最后更新时间,说明需要重新生成
- return $generatedAt->lt($lastUpdated);
- }
- }
|