SyncShopItemsJsonTool.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace App\Module\Shop\AdminControllers\Tools;
  3. use App\Module\Game\DCache\ShopItemsJsonConfig;
  4. use Dcat\Admin\Admin;
  5. use Dcat\Admin\Grid\Tools\AbstractTool;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Facades\Log;
  8. /**
  9. * 同步商店商品JSON配置表工具
  10. */
  11. class SyncShopItemsJsonTool extends AbstractTool
  12. {
  13. /**
  14. * 按钮样式
  15. *
  16. * @var string
  17. */
  18. protected $style = 'btn btn-success waves-effect';
  19. /**
  20. * 按钮标题
  21. *
  22. * @return string
  23. */
  24. public function title()
  25. {
  26. return '同步商品JSON';
  27. }
  28. /**
  29. * 确认提示
  30. *
  31. * @return string
  32. */
  33. public function confirm()
  34. {
  35. return '确定要立即同步商店商品JSON数据吗?';
  36. }
  37. /**
  38. * 处理请求
  39. *
  40. * @param Request $request
  41. * @return \Dcat\Admin\Actions\Response
  42. */
  43. public function handle(Request $request)
  44. {
  45. try {
  46. // 强制刷新商店商品JSON配置
  47. $result = ShopItemsJsonConfig::getData([], true);
  48. if ($result) {
  49. Log::info('商店商品JSON配置表同步成功', [
  50. 'operator' => Admin::user()->name ?? 'unknown',
  51. 'time' => now()->toDateTimeString()
  52. ]);
  53. return $this->response()
  54. ->success('商店商品JSON配置表同步成功!')
  55. ->refresh();
  56. } else {
  57. Log::error('商店商品JSON配置表同步失败', [
  58. 'operator' => Admin::user()->name ?? 'unknown',
  59. 'time' => now()->toDateTimeString()
  60. ]);
  61. return $this->response()
  62. ->error('商店商品JSON配置表同步失败,请检查日志!');
  63. }
  64. } catch (\Exception $e) {
  65. Log::error('商店商品JSON配置表同步异常', [
  66. 'error' => $e->getMessage(),
  67. 'trace' => $e->getTraceAsString(),
  68. 'operator' => Admin::user()->name ?? 'unknown',
  69. 'time' => now()->toDateTimeString()
  70. ]);
  71. return $this->response()
  72. ->error('商店商品JSON配置表同步异常:' . $e->getMessage());
  73. }
  74. }
  75. }