ValidateReferralAction.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace App\Module\UrsPromotion\AdminControllers\Actions;
  3. use App\Module\UrsPromotion\Models\UrsUserReferral;
  4. use App\Module\UrsPromotion\Services\UrsReferralService;
  5. use Illuminate\Http\Request;
  6. use UCore\DcatAdmin\RowActionHandler;
  7. /**
  8. * 验证推荐关系操作
  9. *
  10. * 验证推荐关系的有效性并更新状态
  11. */
  12. class ValidateReferralAction extends RowActionHandler
  13. {
  14. /**
  15. * 操作按钮标题
  16. *
  17. * @var string
  18. */
  19. public $title = '验证关系';
  20. /**
  21. * 检查是否允许显示此操作
  22. *
  23. * @return bool
  24. */
  25. public function allowed()
  26. {
  27. // 所有记录都允许验证
  28. return true;
  29. }
  30. /**
  31. * 处理请求
  32. *
  33. * @param Request $request
  34. * @return mixed
  35. */
  36. public function handle(Request $request)
  37. {
  38. $id = $this->getKey();
  39. $referral = UrsUserReferral::find($id);
  40. if (!$referral) {
  41. return $this->response()->error('推荐关系记录不存在');
  42. }
  43. try {
  44. // 验证推荐关系
  45. $result = UrsReferralService::validateReferral($referral);
  46. if ($result['valid']) {
  47. $message = "推荐关系验证通过!";
  48. if ($result['updated']) {
  49. $message .= " 状态已更新为有效。";
  50. } else {
  51. $message .= " 状态保持有效。";
  52. }
  53. // 如果有验证详情,添加到消息中
  54. if (!empty($result['details'])) {
  55. $message .= "\n验证详情:" . implode(', ', $result['details']);
  56. }
  57. return $this->response()
  58. ->success($message)
  59. ->refresh();
  60. } else {
  61. $message = "推荐关系验证失败!";
  62. if ($result['updated']) {
  63. $message .= " 状态已更新为无效。";
  64. }
  65. // 添加失败原因
  66. if (!empty($result['reasons'])) {
  67. $message .= "\n失败原因:" . implode(', ', $result['reasons']);
  68. }
  69. return $this->response()
  70. ->warning($message)
  71. ->refresh();
  72. }
  73. } catch (\Exception $e) {
  74. return $this->response()->error('验证失败:' . $e->getMessage());
  75. }
  76. }
  77. /**
  78. * 确认对话框
  79. *
  80. * @return string
  81. */
  82. public function confirm()
  83. {
  84. return '确定要验证此推荐关系吗?系统将检查推荐关系的有效性并可能更新状态。';
  85. }
  86. }