| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php
- namespace App\Module\Pet\AdminControllers\Tools;
- use App\Module\Game\DCache\PetConfigJsonConfig;
- use App\Module\Game\DCache\PetLevelJsonConfig;
- use App\Module\Game\DCache\PetSkillJsonConfig;
- use App\Module\Pet\Models\PetConfig;
- use App\Module\Pet\Models\PetLevelConfig;
- use App\Module\Pet\Models\PetSkill;
- use Dcat\Admin\Grid\Tools\AbstractTool;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Log;
- /**
- * 宠物配置表同步工具
- *
- * 用于在后台管理界面中同步宠物配置表数据到JSON文件
- */
- class SyncPetJsonTool 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文件
- PetConfigJsonConfig::getData([], true);
- PetLevelJsonConfig::getData([], true);
- PetSkillJsonConfig::getData([], true);
- return $this->response()->success('生成成功')->refresh();
- } catch (\Exception $e) {
- Log::error('Generate pet 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
- {
- // 获取缓存数据
- $petConfigJson = PetConfigJsonConfig::getData();
- $petLevelJson = PetLevelJsonConfig::getData();
- $petSkillJson = PetSkillJsonConfig::getData();
- // 如果没有生成时间戳,说明需要生成
- if (!isset($petConfigJson['generated_ts']) ||
- !isset($petLevelJson['generated_ts']) ||
- !isset($petSkillJson['generated_ts'])) {
- return true;
- }
- // 获取生成时间
- $configGeneratedAt = \Carbon\Carbon::createFromTimestamp($petConfigJson['generated_ts']);
- $levelGeneratedAt = \Carbon\Carbon::createFromTimestamp($petLevelJson['generated_ts']);
- $skillGeneratedAt = \Carbon\Carbon::createFromTimestamp($petSkillJson['generated_ts']);
- // 获取各表的最后更新时间
- $configLastUpdated = \Carbon\Carbon::parse(PetConfig::max('updated_at') ?: '2000-01-01');
- $levelLastUpdated = \Carbon\Carbon::parse(PetLevelConfig::max('updated_at') ?: '2000-01-01');
- $skillLastUpdated = \Carbon\Carbon::parse(PetSkill::max('updated_at') ?: '2000-01-01');
- // 如果任何一个生成时间早于对应表的最后更新时间,说明需要重新生成
- return $configGeneratedAt->lt($configLastUpdated) ||
- $levelGeneratedAt->lt($levelLastUpdated) ||
- $skillGeneratedAt->lt($skillLastUpdated);
- }
- }
|