增加 通知
This commit is contained in:
parent
8cd9cce185
commit
9562a05520
66
app/Notifications/ModuleEarnings.php
Normal file
66
app/Notifications/ModuleEarnings.php
Normal file
@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notifications;
|
||||
|
||||
use App\Models\Module;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Notifications\Notification;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Notifications\Messages\MailMessage;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class ModuleEarnings extends Notification
|
||||
{
|
||||
use Queueable;
|
||||
|
||||
protected Module $module;
|
||||
|
||||
/**
|
||||
* Create a new notification instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Module $module)
|
||||
{
|
||||
$this->module = $module;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the mail representation of the notification.
|
||||
*
|
||||
* @param mixed $notifiable
|
||||
* @return \Illuminate\Notifications\Messages\MailMessage
|
||||
*/
|
||||
public function toGroup($notifiable)
|
||||
{
|
||||
if (!isset($notifiable['transactions'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
$module = $this->module;
|
||||
|
||||
$view = 'notifications.module.earnings';
|
||||
|
||||
// make wecom_key visible
|
||||
$wecom_key = $module->wecom_key ?? config('settings.wecom.robot_hook.billing');
|
||||
|
||||
$resp = Http::post('https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=' . $wecom_key, [
|
||||
'msgtype' => 'markdown',
|
||||
'markdown' => [
|
||||
'content' => view($view, [
|
||||
'module' => $module,
|
||||
'data' => $notifiable,
|
||||
])->render(),
|
||||
],
|
||||
]);
|
||||
|
||||
if ($resp->failed()) {
|
||||
Log::error('发送模块盈利到企业微信时失败', [
|
||||
'module' => $module->id,
|
||||
'data' => $notifiable,
|
||||
'resp' => $resp->json(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
69
app/Notifications/UserBalanceNotification.php
Normal file
69
app/Notifications/UserBalanceNotification.php
Normal file
@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notifications;
|
||||
|
||||
use App\Models\User\Balance;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use App\Broadcasting\WeComRobotChannel;
|
||||
use Illuminate\Notifications\Notification;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
|
||||
class UserBalanceNotification extends Notification implements ShouldQueue
|
||||
{
|
||||
use Queueable;
|
||||
|
||||
/**
|
||||
* Create a new notification instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the notification's delivery channels.
|
||||
*
|
||||
* @param mixed $notifiable
|
||||
* @return array
|
||||
*/
|
||||
public function via($notifiable)
|
||||
{
|
||||
return [WeComRobotChannel::class];
|
||||
}
|
||||
|
||||
public function toGroup($notifiable)
|
||||
{
|
||||
if ($notifiable instanceof Balance) {
|
||||
|
||||
if ($notifiable->paid_at !== null) {
|
||||
$view = 'notifications.user.balance';
|
||||
$notifiable->load('user');
|
||||
$user = $notifiable->user;
|
||||
|
||||
|
||||
$wecom_key = config('settings.wecom.robot_hook.billing');
|
||||
|
||||
|
||||
$data = [
|
||||
'balance' => $notifiable,
|
||||
'user' => $user,
|
||||
];
|
||||
|
||||
$resp = Http::post('https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=' . $wecom_key, [
|
||||
'msgtype' => 'markdown',
|
||||
'markdown' => [
|
||||
'content' => view($view, $data)->render(),
|
||||
],
|
||||
]);
|
||||
|
||||
if (!$resp->successful()) {
|
||||
Log::error('企业微信机器人发送失败', $data);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
41
app/Notifications/WeComRobotChannel.php
Normal file
41
app/Notifications/WeComRobotChannel.php
Normal file
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notifications;
|
||||
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Notifications\Messages\MailMessage;
|
||||
use Illuminate\Notifications\Notification;
|
||||
|
||||
class WeComRobotChannel extends Notification
|
||||
{
|
||||
use Queueable;
|
||||
|
||||
/**
|
||||
* Send the given notification.
|
||||
*
|
||||
* @param mixed $notifiable
|
||||
* @param \Illuminate\Notifications\Notification $notification
|
||||
* @return void
|
||||
*/
|
||||
public function send($notifiable, Notification $notification)
|
||||
{
|
||||
// Remember that we created the toSms() methods in our notification class
|
||||
// Now is the time to use it.
|
||||
// In our example. $notifiable is an instance of a User that just signed up.
|
||||
$message = $notification->toSms($notifiable);
|
||||
|
||||
// Now we hopefully have a instance of a SmsMessage.
|
||||
// That we are ready to send to our user.
|
||||
// Let's do it :-)
|
||||
$message->send();
|
||||
|
||||
// Or use dryRun() for testing to send it, without sending it for real.
|
||||
$message->dryRun()->send();
|
||||
|
||||
// Wait.. was that it?
|
||||
// Well sort of.. :-)
|
||||
// But we need to implement this magical SmsMessage class for it to work.
|
||||
|
||||
}
|
||||
}
|
102
app/Notifications/WorkOrderNotification.php
Normal file
102
app/Notifications/WorkOrderNotification.php
Normal file
@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notifications;
|
||||
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use App\Broadcasting\WeComRobotChannel;
|
||||
use App\Models\WorkOrder\Reply;
|
||||
use App\Models\WorkOrder\WorkOrder;
|
||||
use Illuminate\Notifications\Notification;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class WorkOrderNotification extends Notification implements ShouldQueue
|
||||
{
|
||||
use Queueable;
|
||||
|
||||
/**
|
||||
* Create a new notification instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the notification's delivery channels.
|
||||
*
|
||||
* @param mixed $notifiable
|
||||
* @return array
|
||||
*/
|
||||
public function via($notifiable)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public function toGroup($notifiable)
|
||||
{
|
||||
$workOrder = $notifiable;
|
||||
|
||||
$reply = [];
|
||||
|
||||
if ($notifiable instanceof WorkOrder) {
|
||||
|
||||
$view = 'notifications.work_order.created';
|
||||
|
||||
$workOrder->load(['module', 'user']);
|
||||
|
||||
$module = $workOrder->module;
|
||||
} elseif ($notifiable instanceof Reply) {
|
||||
|
||||
$view = 'notifications.work_order.reply';
|
||||
|
||||
|
||||
$workOrder->load(['workOrder', 'user']);
|
||||
$workOrder->workOrder->load('module');
|
||||
|
||||
$reply = $workOrder;
|
||||
$workOrder = $workOrder->workOrder;
|
||||
|
||||
$module = $workOrder->module;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
// 取消隐藏字段
|
||||
$module->makeVisible(['wecom_key']);
|
||||
|
||||
if ($module->wecom_key == null) {
|
||||
$wecom_key = config('settings.wecom.robot_hook.default');
|
||||
} else {
|
||||
$wecom_key = $module->wecom_key;
|
||||
}
|
||||
|
||||
// 隐藏字段
|
||||
$module->makeHidden(['wecom_key']);
|
||||
|
||||
|
||||
$resp = Http::post('https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=' . $wecom_key, [
|
||||
'msgtype' => 'markdown',
|
||||
'markdown' => [
|
||||
'content' => view($view, [
|
||||
'workOrder' => $workOrder,
|
||||
'user' => $workOrder->user,
|
||||
'reply' => $reply,
|
||||
'module' => $module,
|
||||
])->render(),
|
||||
],
|
||||
]);
|
||||
|
||||
if (!$resp->successful()) {
|
||||
Log::error('企业微信机器人发送失败', [
|
||||
'resp' => $resp->json(),
|
||||
'workOrder' => $workOrder,
|
||||
'reply' => $reply,
|
||||
'module' => $module,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
68
app/Observers/BalanceObserve.php
Normal file
68
app/Observers/BalanceObserve.php
Normal file
@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
namespace App\Observers;
|
||||
|
||||
use App\Models\User\Balance;
|
||||
use App\Notifications\UserBalanceNotification;
|
||||
|
||||
class BalanceObserve
|
||||
{
|
||||
/**
|
||||
* Handle the Balance "created" event.
|
||||
*
|
||||
* @param \App\Models\User\Balance $balance
|
||||
* @return void
|
||||
*/
|
||||
public function created(Balance $balance)
|
||||
{
|
||||
//
|
||||
return (new UserBalanceNotification())
|
||||
->toGroup($balance);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the Balance "updated" event.
|
||||
*
|
||||
* @param \App\Models\User\Balance $balance
|
||||
* @return void
|
||||
*/
|
||||
public function updated(Balance $balance)
|
||||
{
|
||||
//
|
||||
return (new UserBalanceNotification())
|
||||
->toGroup($balance);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the Balance "deleted" event.
|
||||
*
|
||||
* @param \App\Models\User\Balance $balance
|
||||
* @return void
|
||||
*/
|
||||
public function deleted(Balance $balance)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the Balance "restored" event.
|
||||
*
|
||||
* @param \App\Models\User\Balance $balance
|
||||
* @return void
|
||||
*/
|
||||
public function restored(Balance $balance)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the Balance "force deleted" event.
|
||||
*
|
||||
* @param \App\Models\User\Balance $balance
|
||||
* @return void
|
||||
*/
|
||||
public function forceDeleted(Balance $balance)
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
66
app/Observers/WorkOrder/ReplyObserver.php
Normal file
66
app/Observers/WorkOrder/ReplyObserver.php
Normal file
@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace App\Observers\WorkOrder;
|
||||
|
||||
use App\Models\WorkOrder\Reply;
|
||||
use App\Notifications\WorkOrderNotification;
|
||||
|
||||
class ReplyObserver
|
||||
{
|
||||
/**
|
||||
* Handle the Reply "created" event.
|
||||
*
|
||||
* @param \App\Models\WorkOrder\Reply $reply
|
||||
* @return void
|
||||
*/
|
||||
public function created(Reply $reply)
|
||||
{
|
||||
//
|
||||
return (new WorkOrderNotification())
|
||||
->toGroup($reply);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the Reply "updated" event.
|
||||
*
|
||||
* @param \App\Models\WorkOrder\Reply $reply
|
||||
* @return void
|
||||
*/
|
||||
public function updated(Reply $reply)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the Reply "deleted" event.
|
||||
*
|
||||
* @param \App\Models\WorkOrder\Reply $reply
|
||||
* @return void
|
||||
*/
|
||||
public function deleted(Reply $reply)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the Reply "restored" event.
|
||||
*
|
||||
* @param \App\Models\WorkOrder\Reply $reply
|
||||
* @return void
|
||||
*/
|
||||
public function restored(Reply $reply)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the Reply "force deleted" event.
|
||||
*
|
||||
* @param \App\Models\WorkOrder\Reply $reply
|
||||
* @return void
|
||||
*/
|
||||
public function forceDeleted(Reply $reply)
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
71
app/Observers/WorkOrder/WorkOrderObserver.php
Normal file
71
app/Observers/WorkOrder/WorkOrderObserver.php
Normal file
@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
namespace App\Observers\WorkOrder;
|
||||
|
||||
use App\Models\WorkOrder\WorkOrder;
|
||||
use App\Notifications\WorkOrderNotification;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class WorkOrderObserver
|
||||
{
|
||||
/**
|
||||
* Handle the WorkOrder "created" event.
|
||||
*
|
||||
* @param \App\Models\WorkOrder\WorkOrder $workOrder
|
||||
* @return void
|
||||
*/
|
||||
public function created(WorkOrder $workOrder)
|
||||
{
|
||||
//
|
||||
return (new WorkOrderNotification())
|
||||
->toGroup($workOrder);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the WorkOrder "updated" event.
|
||||
*
|
||||
* @param \App\Models\WorkOrder\WorkOrder $workOrder
|
||||
* @return void
|
||||
*/
|
||||
public function updated(WorkOrder $workOrder)
|
||||
{
|
||||
|
||||
Log::debug('workOrder updated', ['workOrder' => $workOrder]);
|
||||
//
|
||||
return (new WorkOrderNotification())
|
||||
->toGroup($workOrder);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the WorkOrder "deleted" event.
|
||||
*
|
||||
* @param \App\Models\WorkOrder\WorkOrder $workOrder
|
||||
* @return void
|
||||
*/
|
||||
public function deleted(WorkOrder $workOrder)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the WorkOrder "restored" event.
|
||||
*
|
||||
* @param \App\Models\WorkOrder\WorkOrder $workOrder
|
||||
* @return void
|
||||
*/
|
||||
public function restored(WorkOrder $workOrder)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the WorkOrder "force deleted" event.
|
||||
*
|
||||
* @param \App\Models\WorkOrder\WorkOrder $workOrder
|
||||
* @return void
|
||||
*/
|
||||
public function forceDeleted(WorkOrder $workOrder)
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
@ -27,6 +27,10 @@ class EventServiceProvider extends ServiceProvider
|
||||
public function boot()
|
||||
{
|
||||
//
|
||||
|
||||
\App\Models\WorkOrder\WorkOrder::observe(\App\Observers\WorkOrder\WorkOrderObserver::class);
|
||||
\App\Models\WorkOrder\Reply::observe(\App\Observers\WorkOrder\ReplyObserver::class);
|
||||
\App\Models\Balance::observe(\App\Observers\BalanceObserve::class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user