init
- 框架初始化 - 安装插件 - 修复PHP8.4报错
This commit is contained in:
43
addons/shopro/channel/Database.php
Normal file
43
addons/shopro/channel/Database.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace addons\shopro\channel;
|
||||
|
||||
use addons\shopro\notification\Notification;
|
||||
use app\admin\model\shopro\notification\Notification as NotificationModel;
|
||||
|
||||
class Database
|
||||
{
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 发送 模板消息
|
||||
*
|
||||
* @param mixed $notifiable // 通知用户
|
||||
* @param 通知内容
|
||||
* @return void
|
||||
*/
|
||||
public function send($notifiable, Notification $notification)
|
||||
{
|
||||
$data = [];
|
||||
|
||||
if (method_exists($notification, 'toDatabase')) {
|
||||
$data = $notification->toDatabase($notifiable);
|
||||
|
||||
$notificationModel = new NotificationModel();
|
||||
$notificationModel->id = \fast\Random::uuid();
|
||||
$notificationModel->notification_type = $notification->notification_type;
|
||||
$notificationModel->type = $notification->event;
|
||||
$notificationModel->notifiable_id = $notifiable['id'];
|
||||
$notificationModel->notifiable_type = $notifiable->getNotifiableType();
|
||||
$notificationModel->data = $data;
|
||||
|
||||
$notificationModel->save();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
60
addons/shopro/channel/Email.php
Normal file
60
addons/shopro/channel/Email.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace addons\shopro\channel;
|
||||
|
||||
use addons\shopro\notification\Notification;
|
||||
use think\Validate;
|
||||
use app\common\library\Email as SendEmail;
|
||||
|
||||
class Email
|
||||
{
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 发送 微信模板消息
|
||||
*
|
||||
* @param mixed $notifiable // 通知用户
|
||||
* @param 通知内容
|
||||
* @return void
|
||||
*/
|
||||
public function send($notifiable, Notification $notification)
|
||||
{
|
||||
$data = [];
|
||||
|
||||
if (method_exists($notification, 'toEmail')) {
|
||||
$data = $notification->toEmail($notifiable);
|
||||
|
||||
if ($data && isset($notifiable['email']) && Validate::is($notifiable['email'], "email")) {
|
||||
try {
|
||||
$email = new SendEmail;
|
||||
$result = $email
|
||||
->to($notifiable['email'], $notifiable['nickname'])
|
||||
->subject(($data['data'] ? $data['data']['template'] : '邮件通知'))
|
||||
->message('<div style="min-height:550px; padding: 50px 20px 100px;">' . $data['content'] . '</div>')
|
||||
->send();
|
||||
if ($result) {
|
||||
// 发送成功
|
||||
$notification->sendOk('Email');
|
||||
} else {
|
||||
// 邮件发送失败
|
||||
\think\Log::error('邮件消息发送失败:用户:' . $notifiable['id'] . ';类型:' . get_class($notification) . ";发送类型:" . $notification->event . ";错误信息:" . json_encode($email->getError()));
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
// 因为配置较麻烦,这里捕获异常防止因为缺少字段,导致队列一直执行不成功
|
||||
format_log_error($e, 'email_notification', '用户:' . $notifiable['id'] . ';类型:' . get_class($notification) . ";发送类型:" . $notification->event);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// 没有openid
|
||||
\think\Log::error('邮件消息发送失败,没有 email,或 email 格式不正确:用户:' . $notifiable['id'] . ';类型:' . get_class($notification) . ";发送类型:" . $notification->event);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
62
addons/shopro/channel/Sms.php
Normal file
62
addons/shopro/channel/Sms.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace addons\shopro\channel;
|
||||
|
||||
use addons\shopro\notification\Notification;
|
||||
|
||||
class Sms
|
||||
{
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 发送 模板消息
|
||||
*
|
||||
* @param mixed $notifiable // 通知用户
|
||||
* @param 通知内容
|
||||
* @return void
|
||||
*/
|
||||
public function send($notifiable, Notification $notification)
|
||||
{
|
||||
$data = [];
|
||||
|
||||
if (method_exists($notification, 'toSms')) {
|
||||
$data = $notification->toSms($notifiable);
|
||||
|
||||
if ($data && $data['mobile'] && isset($data['template_id'])) {
|
||||
$mobile = $data['mobile'];
|
||||
$sendData = $data['data'] ?? [];
|
||||
|
||||
$params = [
|
||||
'mobile' => $mobile,
|
||||
'msg' => $sendData,
|
||||
'template' => $data['template_id'],
|
||||
'default_content' => $notification->template['MessageDefaultContent'] ?? null // 短信宝使用
|
||||
];
|
||||
|
||||
if (in_array('smsbao', get_addonnames())) {
|
||||
// 如果是短信宝,msg 就是 default_content 的内容
|
||||
$params['msg'] = $params['default_content'];
|
||||
}
|
||||
$result = \think\Hook::listen('sms_notice', $params, null, true);
|
||||
|
||||
if (!$result) {
|
||||
// 短信发送失败
|
||||
\think\Log::error('短信发送失败:用户:'. $notifiable['id'] . ';类型:' . get_class($notification) . ";发送类型:" . $notification->event);
|
||||
} else {
|
||||
// 发送成功
|
||||
$notification->sendOk('Sms');
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
// 没有手机号
|
||||
\think\Log::error('短信发送失败,没有手机号:用户:' . $notifiable['id'] . ';类型:' . get_class($notification) . ";发送类型:" . $notification->event);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
62
addons/shopro/channel/Websocket.php
Normal file
62
addons/shopro/channel/Websocket.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace addons\shopro\channel;
|
||||
|
||||
use addons\shopro\notification\Notification;
|
||||
use addons\shopro\library\Websocket as WebsocketSend;
|
||||
|
||||
class Websocket
|
||||
{
|
||||
|
||||
/**
|
||||
* 发送 Websocket 通知
|
||||
* @param Notifiable $notifiable
|
||||
* @param Notification $notification
|
||||
*/
|
||||
public function send($notifiable, Notification $notification)
|
||||
{
|
||||
$data = [];
|
||||
|
||||
if (method_exists($notification, 'toSms')) {
|
||||
$data = $notification->toWebsocket($notifiable);
|
||||
|
||||
if ($notification->receiver_type != 'admin') {
|
||||
// 目前只有 admin 消息类型发送 socket
|
||||
return true;
|
||||
}
|
||||
|
||||
// 发送数据
|
||||
$requestData = [
|
||||
'notifiable' => $notifiable->toArray(),
|
||||
'notification_type' => $notification->notification_type,
|
||||
'type' => $notification->event,
|
||||
'data' => $data,
|
||||
'read_time' => null,
|
||||
'createtime' => date('Y-m-d H:i:s')
|
||||
];
|
||||
// 接收人
|
||||
$receiver = [
|
||||
'ids' => $notifiable->id,
|
||||
'type' => $notifiable->getNotifiableType()
|
||||
];
|
||||
|
||||
try {
|
||||
$websocket = new WebsocketSend();
|
||||
$result = $websocket->notification([
|
||||
'receiver' => $receiver,
|
||||
'data' => $requestData
|
||||
]);
|
||||
|
||||
if ($result !== true) {
|
||||
// 发送失败
|
||||
\think\Log::error('websocket 通知发送失败:用户:' . $notifiable['id'] . ';类型:' . get_class($notification) . ";发送类型:" . $notification->event . ";错误信息:" . json_encode($result, JSON_UNESCAPED_UNICODE));
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
// 因为配置较麻烦,这里捕获异常防止因为缺少字段,导致队列一直执行不成功
|
||||
format_log_error($e, 'websocket_notification', '用户:' . $notifiable['id'] . ';类型:' . get_class($notification) . ";发送类型:" . $notification->event);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
59
addons/shopro/channel/WechatMiniProgram.php
Normal file
59
addons/shopro/channel/WechatMiniProgram.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace addons\shopro\channel;
|
||||
|
||||
use addons\shopro\notification\Notification;
|
||||
use addons\shopro\facade\Wechat;
|
||||
|
||||
class WechatMiniProgram
|
||||
{
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 发送 微信模板消息
|
||||
*
|
||||
* @param mixed $notifiable // 通知用户
|
||||
* @param 通知内容
|
||||
* @return void
|
||||
*/
|
||||
public function send($notifiable, Notification $notification)
|
||||
{
|
||||
$data = [];
|
||||
|
||||
if (method_exists($notification, 'toWechatMiniProgram')) {
|
||||
$data = $notification->toWechatMiniProgram($notifiable);
|
||||
|
||||
if ($data && isset($data['openid']) && isset($data['template_id']) && $data['template_id']) {
|
||||
$data['touser'] = $data['openid'];
|
||||
unset($data['openid']);
|
||||
|
||||
try {
|
||||
// 发送模板消息
|
||||
$result = Wechat::miniProgram()->subscribe_message->send($data);
|
||||
|
||||
if ($result['errcode'] != 0) {
|
||||
// 小程序模板发送失败
|
||||
\think\Log::error('小程序模板消息发送失败:用户:'. $notifiable['id'] . ';类型:' . get_class($notification) . ";发送类型:" . $notification->event . ";错误信息:" . json_encode($result, JSON_UNESCAPED_UNICODE));
|
||||
} else {
|
||||
// 发送成功
|
||||
$notification->sendOk('WechatMiniProgram');
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
// 因为配置较麻烦,这里捕获异常防止因为缺少字段,导致队列一直执行不成功
|
||||
format_log_error($e, 'WechatMiniProgram_notification', '用户:' . $notifiable['id'] . ';类型:' . get_class($notification) . ";发送类型:" . $notification->event);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// 没有openid
|
||||
\think\Log::error('小程序模板消息发送失败,没有 openid:用户:' . $notifiable['id'] . ';类型:' . get_class($notification) . ";发送类型:" . $notification->event);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
59
addons/shopro/channel/WechatOfficialAccount.php
Normal file
59
addons/shopro/channel/WechatOfficialAccount.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace addons\shopro\channel;
|
||||
|
||||
use addons\shopro\notification\Notification;
|
||||
use addons\shopro\facade\Wechat;
|
||||
|
||||
class WechatOfficialAccount
|
||||
{
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 发送 微信模板消息
|
||||
*
|
||||
* @param mixed $notifiable // 通知用户
|
||||
* @param 通知内容
|
||||
* @return void
|
||||
*/
|
||||
public function send($notifiable, Notification $notification)
|
||||
{
|
||||
$data = [];
|
||||
|
||||
if (method_exists($notification, 'toWechatOfficialAccount')) {
|
||||
$data = $notification->toWechatOfficialAccount($notifiable);
|
||||
|
||||
if ($data && isset($data['openid']) && isset($data['template_id']) && $data['template_id']) {
|
||||
$data['touser'] = $data['openid'];
|
||||
unset($data['openid']);
|
||||
|
||||
try {
|
||||
// 发送模板消息
|
||||
$result = Wechat::officialAccount()->template_message->send($data);
|
||||
|
||||
if ($result['errcode'] != 0) {
|
||||
// 短信发送失败
|
||||
\think\Log::error('公众号模板消息发送失败:用户:' . $notifiable['id'] . ';类型:' . get_class($notification) . ";发送类型:" . $notification->event . ";错误信息:" . json_encode($result, JSON_UNESCAPED_UNICODE));
|
||||
} else {
|
||||
// 发送成功
|
||||
$notification->sendOk('WechatOfficialAccount');
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
// 因为配置较麻烦,这里捕获异常防止因为缺少字段,导致队列一直执行不成功
|
||||
format_log_error($e, 'WechatOfficialAccount_notification', '用户:' . $notifiable['id'] . ';类型:' . get_class($notification) . ";发送类型:" . $notification->event);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// 没有openid
|
||||
\think\Log::error('公众号模板消息发送失败,没有 openid:用户:' . $notifiable['id'] . ';类型:' . get_class($notification) . ";发送类型:" . $notification->event);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user