| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- <?php
- namespace App\Module\GameItems\AdminControllers\Tools;
- use App\Module\Game\DCache\DismantleJsonConfig;
- use App\Module\GameItems\Models\ItemDismantleRule;
- use Dcat\Admin\Grid\Tools\AbstractTool;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Log;
- /**
- * 物品分解配方配置表同步工具
- *
- * 用于在后台管理界面中同步物品分解配方配置表数据到JSON文件
- */
- class SyncDismantleJsonTool 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
- $process = new \Symfony\Component\Process\Process(['php', 'artisan', 'gameitems:generate-dismantle-json']);
- $process->setWorkingDirectory(base_path());
- $process->run();
- if (!$process->isSuccessful()) {
- Log::error('Generate dismantle.json failed: ' . $process->getErrorOutput());
- return $this->response()->error('生成失败:' . $process->getErrorOutput());
- }
- // 强制刷新缓存
- DismantleJsonConfig::getData([], true);
- return $this->response()->success('生成成功')->refresh();
- } catch (\Exception $e) {
- Log::error('Generate dismantle.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 = DismantleJsonConfig::getData();
-
- // 如果没有生成时间戳,说明需要生成
- if (!isset($json['generated_ts'])) {
- return true;
- }
-
- // 获取生成时间和最后更新时间
- $generatedAt = \Carbon\Carbon::createFromTimestamp($json['generated_ts']);
-
- // 获取分解规则的最后更新时间
- $lastUpdated = \Carbon\Carbon::parse(ItemDismantleRule::max('updated_at') ?: '2000-01-01');
-
- // 如果生成时间早于最后更新时间,说明需要重新生成
- return $generatedAt->lt($lastUpdated);
- }
- }
|