Browse Source

短信通知

master
lemon 4 years ago
parent
commit
0da53d9bcb
  1. 3
      app/Console/Commands/BalanceDue.php
  2. 38
      app/Events/OrderUpdated.php
  3. 10
      app/Http/Controllers/Api/VerificationController.php
  4. 77
      app/Listeners/OrderEventSubscriber.php
  5. 5
      app/Models/Order.php
  6. 2
      app/Providers/EventServiceProvider.php
  7. 4
      config/sms.php

3
app/Console/Commands/BalanceDue.php

@ -8,6 +8,7 @@ use App\Models\Demand;
use App\Models\Order; use App\Models\Order;
use App\Service\SmsService; use App\Service\SmsService;
use App\Traits\DemandTraits; use App\Traits\DemandTraits;
use App\Traits\SmsTraits;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
@ -52,7 +53,7 @@ class BalanceDue extends Command
foreach ($orders as $order) { foreach ($orders as $order) {
if (!empty($order->mobile)) { if (!empty($order->mobile)) {
$type = $order->pay_type == PayType::DEPOSIT_PAY ? '订金' : '定金'; $type = $order->pay_type == PayType::DEPOSIT_PAY ? '订金' : '定金';
$sms->send('pay',['订单号:'.$order->orderNumber,$type,$order->timeout,$type,'小程序'],[$order->mobile]);
$sms->send('pay',['订单号:'.$order->orderNumber,$type,$order->timeout,$type,SmsTraits::$systeaNameText['user']],[$order->mobile]);
} }
} }
} }

38
app/Events/OrderUpdated.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');
//}
}

10
app/Http/Controllers/Api/VerificationController.php

@ -57,16 +57,6 @@ class VerificationController extends Controller
if ($order->pay_type != PayType::OFFLINE) { if ($order->pay_type != PayType::OFFLINE) {
$this->fund($order); $this->fund($order);
} }
//短信
if (env('SMS_SWITCH', '') == true) {
if (!empty($order->user->mobile)) {
(new SmsService)->send('verify', [$order->order_no, SmsTraits::$systeaNameText['user']], [$order->user->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();
(new SmsService)->send('verify', [$order->order_no, SmsTraits::$systeaNameText['supplier']], $phone);//供应商
(new SmsService)->send('verify', [$order->order_no, SmsTraits::$systeaNameText['agent']], [$order->agent->contact_phone]);//代理商
}
} }
return $this->success(); return $this->success();

77
app/Listeners/OrderEventSubscriber.php

@ -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'
);
}
}

5
app/Models/Order.php

@ -4,6 +4,7 @@ namespace App\Models;
use App\Common\OrderStatus; use App\Common\OrderStatus;
use App\Common\PayType; use App\Common\PayType;
use App\Events\OrderUpdated;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
use App\Common\OrderStatus as Status; use App\Common\OrderStatus as Status;
@ -14,6 +15,10 @@ class Order extends BaseModel
use HasFactory, SoftDeletes; use HasFactory, SoftDeletes;
protected $guarded = ['created_at', 'updated_at']; //不可批量赋值的属性 protected $guarded = ['created_at', 'updated_at']; //不可批量赋值的属性
protected $dispatchesEvents = [
'updated' => OrderUpdated::class,
];
public function scopeComplete($query) public function scopeComplete($query)
{ {
return $query->where('status',OrderStatus::SUCCESS); return $query->where('status',OrderStatus::SUCCESS);

2
app/Providers/EventServiceProvider.php

@ -2,6 +2,7 @@
namespace App\Providers; namespace App\Providers;
use App\Listeners\OrderEventSubscriber;
use App\Listeners\SupplierEventSubscriber; use App\Listeners\SupplierEventSubscriber;
use Illuminate\Auth\Events\Registered; use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification; use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
@ -33,5 +34,6 @@ class EventServiceProvider extends ServiceProvider
protected $subscribe = [ protected $subscribe = [
SupplierEventSubscriber::class, SupplierEventSubscriber::class,
OrderEventSubscriber::class,
]; ];
} }

4
config/sms.php

@ -15,6 +15,8 @@ return [
'auto_shelves' => '1120254', //供应商[{供应商名称}]在{时间}上传了一件新的产品,已为您自动上架,如需修改产品利润,请登录{系统名称}查看并修改。 'auto_shelves' => '1120254', //供应商[{供应商名称}]在{时间}上传了一件新的产品,已为您自动上架,如需修改产品利润,请登录{系统名称}查看并修改。
'verify' => '1119775', //您有一笔订单[{订单号}]已成功核销,详情请登录{系统名称}查看 'verify' => '1119775', //您有一笔订单[{订单号}]已成功核销,详情请登录{系统名称}查看
'pay' => '1120244', //您有一笔订单{1}已支付了{2},请在{3}之前支付尾款,未按时支付尾款订单将会失效,且{4}将不做退还,详情请登录{5}系统查看 'pay' => '1120244', //您有一笔订单{1}已支付了{2},请在{3}之前支付尾款,未按时支付尾款订单将会失效,且{4}将不做退还,详情请登录{5}系统查看
]
'order' => '1127625', //您有一笔新成交的订单{1},请及时登录{2}系统查看。
'refund' => '1127630', //您有一笔新的退款订单{1},请及时处理,详情请登录{2}系统查看。
]
], ],
]; ];
Loading…
Cancel
Save