海南旅游SAAS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
2.5 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\Listeners;
  3. use App\Common\OrderStatus;
  4. use App\Events\OrderUpdated;
  5. use App\Models\Agent;
  6. use App\Models\OrderProductItem;
  7. use App\Models\Supplier;
  8. use App\Service\SmsService;
  9. use App\Traits\SmsTraits;
  10. class OrderEventSubscriber
  11. {
  12. public function onOrderUpdated($event)
  13. {
  14. if (env('SMS_SWITCH', '') == true) {
  15. $order = $event->order;
  16. $sms = new SmsService();
  17. //退款通知
  18. if ($order->isDirty('status') && $order->status == OrderStatus::REFUNDING) {
  19. $mobile = Agent::query()->where('id',$order->agent_id)->value('contact_mobile');
  20. $sms->send('refund', [$order->order_no, SmsTraits::$systeaNameText['agent']], [$mobile]);
  21. }
  22. //付款通知
  23. if ($order->isDirty('status') && ($order->status == OrderStatus::PAY_EARNEST || $order->status == OrderStatus::PAID)) {
  24. //通知代理商
  25. $mobile = Agent::query()->where('id',$order->agent_id)->value('contact_mobile');
  26. if (!empty($mobile)) {
  27. $sms->send('order', [$order->order_no, SmsTraits::$systeaNameText['agent']], [$mobile]);
  28. }
  29. //通知供应商
  30. $supplierIds = OrderProductItem::query()->with('supplier')->where('order_id', $order->id)->distinct()->pluck('supplier_id');
  31. $phone = Supplier::query()->whereIn('id', $supplierIds)->pluck('contact_mobile')->toArray();
  32. if (!empty($phone)) {
  33. $sms->send('order', [$order->order_no, SmsTraits::$systeaNameText['supplier']], $phone);//供应商
  34. }
  35. }
  36. //核销
  37. if ($order->isDirty('status') && $order->status == OrderStatus::SUCCESS) {
  38. //通知用户
  39. if (!empty($order->mobile)) {
  40. $sms->send('verify', [$order->order_no, SmsTraits::$systeaNameText['user']], [$order->mobile]);//用户
  41. }
  42. //通知供应商
  43. $supplierIds = OrderProductItem::query()->with('supplier')->where('order_id', $order->id)->distinct()->pluck('supplier_id');
  44. $phone = Supplier::query()->whereIn('id', $supplierIds)->pluck('contact_mobile')->toArray();
  45. if(!empty($phone)) {
  46. $sms->send('verify', [$order->order_no, SmsTraits::$systeaNameText['supplier']], $phone);//供应商
  47. }
  48. //通知代理商
  49. $mobile = Agent::query()->where('id',$order->agent_id)->value('contact_mobile');
  50. if (!empty($mobile)) {
  51. $sms->send('verify', [$order->order_no, SmsTraits::$systeaNameText['agent']], [$mobile]);//代理商
  52. }
  53. }
  54. }
  55. }
  56. /**
  57. * 为订阅者注册监听器
  58. *
  59. * @param Illuminate\Events\Dispatcher $events
  60. */
  61. public function subscribe($events)
  62. {
  63. $events->listen(
  64. OrderUpdated::class,
  65. OrderEventSubscriber::class . '@onOrderUpdated'
  66. );
  67. }
  68. }