ViewSourceDetailAction.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace App\Module\UrsPromotion\AdminControllers\Actions;
  3. use App\Module\UrsPromotion\Models\UrsProfit;
  4. use Illuminate\Http\Request;
  5. use UCore\DcatAdmin\RowActionHandler;
  6. /**
  7. * 查看收益来源详情操作
  8. *
  9. * 根据收益来源类型跳转到对应的详情页面
  10. */
  11. class ViewSourceDetailAction extends RowActionHandler
  12. {
  13. /**
  14. * 操作按钮标题
  15. *
  16. * @var string
  17. */
  18. public $title = '查看来源';
  19. /**
  20. * 检查是否允许显示此操作
  21. *
  22. * @return bool
  23. */
  24. public function allowed()
  25. {
  26. $row = $this->getRow();
  27. // 只有有来源ID的记录才显示此操作
  28. return !empty($row->source_id) && !empty($row->source_type);
  29. }
  30. /**
  31. * 处理请求
  32. *
  33. * @param Request $request
  34. * @return mixed
  35. */
  36. public function handle(Request $request)
  37. {
  38. $id = $this->getKey();
  39. $profit = UrsProfit::find($id);
  40. if (!$profit) {
  41. return $this->response()->error('收益记录不存在');
  42. }
  43. if (empty($profit->source_id) || empty($profit->source_type)) {
  44. return $this->response()->error('收益来源信息不完整');
  45. }
  46. // 根据来源类型确定跳转URL
  47. $url = $this->getSourceUrl($profit->source_type, $profit->source_id);
  48. if (!$url) {
  49. return $this->response()->error('不支持的来源类型:' . $profit->source_type);
  50. }
  51. return $this->response()->redirect($url);
  52. }
  53. /**
  54. * 根据来源类型获取详情页面URL
  55. *
  56. * @param string $sourceType
  57. * @param int $sourceId
  58. * @return string|null
  59. */
  60. private function getSourceUrl(string $sourceType, int $sourceId): ?string
  61. {
  62. switch ($sourceType) {
  63. case 'land_harvest':
  64. // 土地收获记录
  65. return admin_url("land/harvest-logs/{$sourceId}");
  66. case 'user_register':
  67. // 用户注册记录
  68. return admin_url("users/{$sourceId}");
  69. case 'promotion_reward':
  70. // 推广奖励记录
  71. return admin_url("urs-promotion/profits/{$sourceId}");
  72. case 'planting_reward':
  73. // 种植奖励记录
  74. return admin_url("urs-promotion/profits/{$sourceId}");
  75. default:
  76. return null;
  77. }
  78. }
  79. }