SyncPetJsonTool.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace App\Module\Pet\AdminControllers\Tools;
  3. use App\Module\Game\DCache\PetConfigJsonConfig;
  4. use App\Module\Game\DCache\PetLevelJsonConfig;
  5. use App\Module\Game\DCache\PetSkillJsonConfig;
  6. use App\Module\Pet\Models\PetConfig;
  7. use App\Module\Pet\Models\PetLevelConfig;
  8. use App\Module\Pet\Models\PetSkill;
  9. use Dcat\Admin\Grid\Tools\AbstractTool;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Support\Facades\Log;
  12. /**
  13. * 宠物配置表同步工具
  14. *
  15. * 用于在后台管理界面中同步宠物配置表数据到JSON文件
  16. */
  17. class SyncPetJsonTool extends AbstractTool
  18. {
  19. /**
  20. * 是否显示按钮
  21. *
  22. * @var bool
  23. */
  24. protected $shouldDisplay;
  25. /**
  26. * 按钮样式
  27. *
  28. * @var string
  29. */
  30. protected $style = 'btn btn-primary waves-effect';
  31. /**
  32. * 构造函数
  33. *
  34. * @param bool $shouldDisplay 是否显示按钮
  35. */
  36. public function __construct(bool $shouldDisplay = true)
  37. {
  38. $this->shouldDisplay = $shouldDisplay;
  39. }
  40. /**
  41. * 按钮标题
  42. *
  43. * @return string
  44. */
  45. public function title()
  46. {
  47. return '生成JSON';
  48. }
  49. /**
  50. * 确认提示
  51. *
  52. * @return string
  53. */
  54. public function confirm()
  55. {
  56. return '确定要生成宠物配置JSON数据吗?';
  57. }
  58. /**
  59. * 处理请求
  60. *
  61. * @param Request $request
  62. * @return mixed
  63. */
  64. public function handle(Request $request)
  65. {
  66. try {
  67. // 直接调用缓存类刷新数据,这会同时生成JSON文件
  68. PetConfigJsonConfig::getData([], true);
  69. PetLevelJsonConfig::getData([], true);
  70. PetSkillJsonConfig::getData([], true);
  71. return $this->response()->success('生成成功')->refresh();
  72. } catch (\Exception $e) {
  73. Log::error('Generate pet JSON exception: '.$e->getMessage());
  74. return $this->response()->error('生成失败:'.$e->getMessage());
  75. }
  76. }
  77. /**
  78. * 渲染按钮
  79. *
  80. * @return string
  81. */
  82. public function render()
  83. {
  84. if (!$this->shouldDisplay) {
  85. return '';
  86. }
  87. return parent::render();
  88. }
  89. /**
  90. * 判断是否应该显示按钮
  91. *
  92. * @return bool
  93. */
  94. public static function shouldDisplay(): bool
  95. {
  96. // 获取缓存数据
  97. $petConfigJson = PetConfigJsonConfig::getData();
  98. $petLevelJson = PetLevelJsonConfig::getData();
  99. $petSkillJson = PetSkillJsonConfig::getData();
  100. // 如果没有生成时间戳,说明需要生成
  101. if (!isset($petConfigJson['generated_ts']) ||
  102. !isset($petLevelJson['generated_ts']) ||
  103. !isset($petSkillJson['generated_ts'])) {
  104. return true;
  105. }
  106. // 获取生成时间
  107. $configGeneratedAt = \Carbon\Carbon::createFromTimestamp($petConfigJson['generated_ts']);
  108. $levelGeneratedAt = \Carbon\Carbon::createFromTimestamp($petLevelJson['generated_ts']);
  109. $skillGeneratedAt = \Carbon\Carbon::createFromTimestamp($petSkillJson['generated_ts']);
  110. // 获取各表的最后更新时间
  111. $configLastUpdated = \Carbon\Carbon::parse(PetConfig::max('updated_at') ?: '2000-01-01');
  112. $levelLastUpdated = \Carbon\Carbon::parse(PetLevelConfig::max('updated_at') ?: '2000-01-01');
  113. $skillLastUpdated = \Carbon\Carbon::parse(PetSkill::max('updated_at') ?: '2000-01-01');
  114. // 如果任何一个生成时间早于对应表的最后更新时间,说明需要重新生成
  115. return $configGeneratedAt->lt($configLastUpdated) ||
  116. $levelGeneratedAt->lt($levelLastUpdated) ||
  117. $skillGeneratedAt->lt($skillLastUpdated);
  118. }
  119. }