pathum4u / api_notification
微服务API通知服务
v3.0.0
2023-09-18 05:26 UTC
Requires
- pathum4u/api_request: ^2.0
This package is auto-updated.
Last update: 2024-09-18 07:54:37 UTC
README
简单的Laravel\Lumen微服务API通知请求包(服务间通知服务)。
需求
"pathum4u/api_request"
安装
composer require pathum4u/api_notification
请求
$notification = new RequisitionApprovalNotification($requisition_data);
$notification->toUser($this->get_users($permission, $requisition_data['requisition']['department_id']));
return $notification->send('/api/send_notification');
通知
<?php
namespace App\Notifications\Procurement;
use Pathum4u\ApiNotification\ApiNotification;
use Illuminate\Notifications\Messages\MailMessage;
class RequisitionApprovalNotification extends ApiNotification
{
/**
*
*
*/
public $data;
public $user;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($requisition_data)
{
//
$this->data = $requisition_data;
$this->user = $requisition_data['users'][0];
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via()
{
return ['mail', 'database'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail()
{
return (new MailMessage)
->subject('New Requisition')
->line($this->user->name .' submitted new requisition for approval' )
->action('Show Requisition', url(env('FRONTEND_SERVICE_URI').'/requisition/conversation/'.$this->data['requisition']['slug']))
->line('Thank you for using our application!');
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray()
{
return [
'subject' => 'New Requisition',
'action' => url(env('FRONTEND_SERVICE_URI') . '/requisition/conversation/' . $this->data['requisition']['slug']),
'body' => $this->user->name . ' Submitted new requisition for approval',
];
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toSms()
{
return [
//
];
}
}
对于多个服务
请使用pathumu4/api_request(config/services.php)配置文件中的服务名称,或默认使用'notification'。
->send('/send_notification', 'email_notification');
另一端(通知服务)
在另一端创建并注册中间件以使用令牌验证每个请求。两端使用相同的密钥。
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$allowedSecrets = explode(',', env('MY_SECRETS_TOKEN'));
if (in_array($request->header('Authorization'), $allowedSecrets)) {
return $next($request);
}
//
return response()->json(['message' => 'unauthorized token'], 401);
}
创建新的邮件类
<?php
namespace App\Notifications;
use Illuminate\Notifications\Messages\MailMessage;
class MailNotification extends MailMessage
{
/**
*
*
*/
public function push($data)
{
//
foreach($data as $key => $value){
$this->$key = $data->$key;
}
return $this;
}
}
在控制器中获取请求时
/**
* Notification Handler
*
*
*/
public function sendNotification(Request $request)
{
//
$data = json_decode($request->data);
Notification::route('mail', $data->to)->notify(new SendNotification($data));
return response()->json(['success'], 200);
}
在您的通知中使用新的MailNotification类
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
//
$mail = new MailNotification();
$mail->push($this->data);
return $mail;
}
致谢
此项目为我的一个项目创建了一些特定的要求,这可能不适用于所有人。
工作与测试
Laravel/Lumen
许可
Composer遵循MIT许可 - 有关详细信息,请参阅LICENSE 文件。