RefreshPetJsonTool.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 Dcat\Admin\Grid\Tools\AbstractTool;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Support\Facades\Log;
  9. /**
  10. * 宠物配置表刷新工具
  11. *
  12. * 用于在后台管理界面中刷新宠物配置表数据
  13. */
  14. class RefreshPetJsonTool extends AbstractTool
  15. {
  16. /**
  17. * 是否显示按钮
  18. *
  19. * @var bool
  20. */
  21. protected $shouldDisplay;
  22. /**
  23. * 按钮样式
  24. *
  25. * @var string
  26. */
  27. protected $style = 'btn btn-danger waves-effect';
  28. /**
  29. * 构造函数
  30. *
  31. * @param bool $shouldDisplay 是否显示按钮
  32. */
  33. public function __construct(bool $shouldDisplay = true)
  34. {
  35. $this->shouldDisplay = $shouldDisplay;
  36. }
  37. /**
  38. * 按钮标题
  39. *
  40. * @return string
  41. */
  42. public function title()
  43. {
  44. return '立即刷新';
  45. }
  46. /**
  47. * 确认提示
  48. *
  49. * @return string
  50. */
  51. public function confirm()
  52. {
  53. return '确定要立即刷新宠物配置JSON数据吗?';
  54. }
  55. /**
  56. * 处理请求
  57. *
  58. * @param Request $request
  59. * @return mixed
  60. */
  61. public function handle(Request $request)
  62. {
  63. try {
  64. // 通过缓存类生成JSON
  65. PetConfigJsonConfig::getData([], true);
  66. PetLevelJsonConfig::getData([], true);
  67. PetSkillJsonConfig::getData([], true);
  68. return $this->response()->success('刷新成功')->refresh();
  69. } catch (\Exception $e) {
  70. Log::error('Refresh pet JSON exception: '.$e->getMessage());
  71. return $this->response()->error('刷新失败:'.$e->getMessage());
  72. }
  73. }
  74. /**
  75. * 渲染按钮
  76. *
  77. * @return string
  78. */
  79. public function render()
  80. {
  81. if (!$this->shouldDisplay) {
  82. return '';
  83. }
  84. return parent::render();
  85. }
  86. /**
  87. * 判断是否应该显示按钮
  88. *
  89. * @return bool
  90. */
  91. public static function shouldDisplay(): bool
  92. {
  93. return true;
  94. }
  95. }