7 changed files with 127 additions and 12 deletions
-
3app/Console/Commands/BalanceDue.php
-
38app/Events/OrderUpdated.php
-
10app/Http/Controllers/Api/VerificationController.php
-
77app/Listeners/OrderEventSubscriber.php
-
5app/Models/Order.php
-
2app/Providers/EventServiceProvider.php
-
4config/sms.php
@ -0,0 +1,38 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Events; |
||||
|
|
||||
|
use App\Models\Order; |
||||
|
use Illuminate\Broadcasting\Channel; |
||||
|
use Illuminate\Broadcasting\InteractsWithSockets; |
||||
|
use Illuminate\Broadcasting\PresenceChannel; |
||||
|
use Illuminate\Broadcasting\PrivateChannel; |
||||
|
use Illuminate\Contracts\Broadcasting\ShouldBroadcast; |
||||
|
use Illuminate\Foundation\Events\Dispatchable; |
||||
|
use Illuminate\Queue\SerializesModels; |
||||
|
|
||||
|
class OrderUpdated |
||||
|
{ |
||||
|
use Dispatchable, InteractsWithSockets, SerializesModels; |
||||
|
|
||||
|
public $order; |
||||
|
/** |
||||
|
* Create a new event instance. |
||||
|
* |
||||
|
* @return void |
||||
|
*/ |
||||
|
public function __construct(Order $order) |
||||
|
{ |
||||
|
$this->order = $order; |
||||
|
} |
||||
|
|
||||
|
///**
|
||||
|
// * Get the channels the event should broadcast on.
|
||||
|
// *
|
||||
|
// * @return \Illuminate\Broadcasting\Channel|array
|
||||
|
// */
|
||||
|
//public function broadcastOn()
|
||||
|
//{
|
||||
|
// return new PrivateChannel('channel-name');
|
||||
|
//}
|
||||
|
} |
||||
@ -0,0 +1,77 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Listeners; |
||||
|
|
||||
|
use App\Common\OrderStatus; |
||||
|
use App\Events\OrderUpdated; |
||||
|
use App\Models\Agent; |
||||
|
use App\Models\OrderProductItem; |
||||
|
use App\Models\Supplier; |
||||
|
use App\Service\SmsService; |
||||
|
use App\Traits\SmsTraits; |
||||
|
use Illuminate\Contracts\Queue\ShouldQueue; |
||||
|
use Illuminate\Queue\InteractsWithQueue; |
||||
|
use Illuminate\Support\Facades\Log; |
||||
|
|
||||
|
class OrderEventSubscriber |
||||
|
{ |
||||
|
public function onOrderUpdated($event) |
||||
|
{ |
||||
|
if (env('SMS_SWITCH', '') == true) { |
||||
|
$order = $event->order; |
||||
|
$sms = new SmsService(); |
||||
|
//退款通知
|
||||
|
if ($order->isDirty('status') && $order->status == OrderStatus::REFUNDING) { |
||||
|
$mobile = Agent::query()->where('id',$order->agent_id)->value('contact_phone'); |
||||
|
$sms->send('refund', [$order->order_no, SmsTraits::$systeaNameText['agent']], [$mobile]); |
||||
|
} |
||||
|
|
||||
|
//付款通知
|
||||
|
if ($order->isDirty('status') && ($order->status == OrderStatus::PAY_EARNEST || $order->status == OrderStatus::PAID)) { |
||||
|
//通知代理商
|
||||
|
$mobile = Agent::query()->where('id',$order->agent_id)->value('contact_phone'); |
||||
|
if (!empty($mobile)) { |
||||
|
$sms->send('order', [$order->order_no, SmsTraits::$systeaNameText['agent']], [$mobile]); |
||||
|
} |
||||
|
//通知供应商
|
||||
|
$supplierIds = OrderProductItem::query()->with('supplier')->where('order_id', $order->id)->distinct()->pluck('supplier_id'); |
||||
|
$phone = Supplier::query()->whereIn('id', $supplierIds)->pluck('contact_phone')->toArray(); |
||||
|
if (!empty($phone)) { |
||||
|
$sms->send('order', [$order->order_no, SmsTraits::$systeaNameText['supplier']], $phone);//供应商
|
||||
|
} |
||||
|
} |
||||
|
|
||||
|
//核销
|
||||
|
if ($order->isDirty('status') && $order->status == OrderStatus::SUCCESS) { |
||||
|
//通知用户
|
||||
|
if (!empty($order->mobile)) { |
||||
|
$sms->send('verify', [$order->order_no, SmsTraits::$systeaNameText['user']], [$order->mobile]);//用户
|
||||
|
} |
||||
|
//通知供应商
|
||||
|
$supplierIds = OrderProductItem::query()->with('supplier')->where('order_id', $order->id)->distinct()->pluck('supplier_id'); |
||||
|
$phone = Supplier::query()->whereIn('id', $supplierIds)->pluck('contact_phone')->toArray(); |
||||
|
if(!empty($phone)) { |
||||
|
$sms->send('verify', [$order->order_no, SmsTraits::$systeaNameText['supplier']], $phone);//供应商
|
||||
|
} |
||||
|
//通知代理商
|
||||
|
$mobile = Agent::query()->where('id',$order->agent_id)->value('contact_phone'); |
||||
|
if (!empty($mobile)) { |
||||
|
$sms->send('verify', [$order->order_no, SmsTraits::$systeaNameText['agent']], [$mobile]);//代理商
|
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 为订阅者注册监听器 |
||||
|
* |
||||
|
* @param Illuminate\Events\Dispatcher $events |
||||
|
*/ |
||||
|
public function subscribe($events) |
||||
|
{ |
||||
|
$events->listen( |
||||
|
OrderUpdated::class, |
||||
|
OrderEventSubscriber::class . '@onOrderUpdated' |
||||
|
); |
||||
|
} |
||||
|
} |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue