SyncDismantleJsonTool.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace App\Module\GameItems\AdminControllers\Tools;
  3. use App\Module\Game\DCache\DismantleJsonConfig;
  4. use App\Module\GameItems\Models\ItemDismantleRule;
  5. use Dcat\Admin\Grid\Tools\AbstractTool;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Facades\Log;
  8. /**
  9. * 物品分解配方配置表同步工具
  10. *
  11. * 用于在后台管理界面中同步物品分解配方配置表数据到JSON文件
  12. */
  13. class SyncDismantleJsonTool extends AbstractTool
  14. {
  15. /**
  16. * 是否显示按钮
  17. *
  18. * @var bool
  19. */
  20. protected $shouldDisplay;
  21. /**
  22. * 按钮样式
  23. *
  24. * @var string
  25. */
  26. protected $style = 'btn btn-primary waves-effect';
  27. /**
  28. * 构造函数
  29. *
  30. * @param bool $shouldDisplay 是否显示按钮
  31. */
  32. public function __construct(bool $shouldDisplay = true)
  33. {
  34. $this->shouldDisplay = $shouldDisplay;
  35. }
  36. /**
  37. * 按钮标题
  38. *
  39. * @return string
  40. */
  41. public function title()
  42. {
  43. return '生成JSON';
  44. }
  45. /**
  46. * 确认提示
  47. *
  48. * @return string
  49. */
  50. public function confirm()
  51. {
  52. return '确定要生成物品分解配方配置JSON数据吗?';
  53. }
  54. /**
  55. * 处理请求
  56. *
  57. * @param Request $request
  58. * @return mixed
  59. */
  60. public function handle(Request $request)
  61. {
  62. try {
  63. // 直接调用命令生成JSON
  64. $process = new \Symfony\Component\Process\Process(['php', 'artisan', 'gameitems:generate-dismantle-json']);
  65. $process->setWorkingDirectory(base_path());
  66. $process->run();
  67. if (!$process->isSuccessful()) {
  68. Log::error('Generate dismantle.json failed: ' . $process->getErrorOutput());
  69. return $this->response()->error('生成失败:' . $process->getErrorOutput());
  70. }
  71. // 强制刷新缓存
  72. DismantleJsonConfig::getData([], true);
  73. return $this->response()->success('生成成功')->refresh();
  74. } catch (\Exception $e) {
  75. Log::error('Generate dismantle.json exception: '.$e->getMessage());
  76. return $this->response()->error('生成失败:'.$e->getMessage());
  77. }
  78. }
  79. /**
  80. * 渲染按钮
  81. *
  82. * @return string
  83. */
  84. public function render()
  85. {
  86. if (!$this->shouldDisplay) {
  87. return '';
  88. }
  89. return parent::render();
  90. }
  91. /**
  92. * 判断是否应该显示按钮
  93. *
  94. * @return bool
  95. */
  96. public static function shouldDisplay(): bool
  97. {
  98. // 获取缓存数据
  99. $json = DismantleJsonConfig::getData();
  100. // 如果没有生成时间戳,说明需要生成
  101. if (!isset($json['generated_ts'])) {
  102. return true;
  103. }
  104. // 获取生成时间和最后更新时间
  105. $generatedAt = \Carbon\Carbon::createFromTimestamp($json['generated_ts']);
  106. // 获取分解规则的最后更新时间
  107. $lastUpdated = \Carbon\Carbon::parse(ItemDismantleRule::max('updated_at') ?: '2000-01-01');
  108. // 如果生成时间早于最后更新时间,说明需要重新生成
  109. return $generatedAt->lt($lastUpdated);
  110. }
  111. }