feat: 初始化雄戈商城服务端代码与Webhook自动部署配置
这个提交包含在:
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace addons\shopro\library\notify;
|
||||
|
||||
use addons\shopro\exception\Exception;
|
||||
use think\queue\ShouldQueue;
|
||||
/**
|
||||
* 消息通知 trait
|
||||
*/
|
||||
|
||||
trait Notifiable
|
||||
{
|
||||
public function notify ($notification) {
|
||||
return \addons\shopro\library\notify\Notify::send([$this], $notification);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
namespace addons\shopro\library\notify;
|
||||
|
||||
use addons\shopro\exception\Exception;
|
||||
use think\queue\ShouldQueue;
|
||||
|
||||
class Notify
|
||||
{
|
||||
|
||||
public function sendNotify($notifiables, $notification) {
|
||||
if ($notification instanceof ShouldQueue) {
|
||||
// 队列执行
|
||||
return $this->sendQueueNotify($notifiables, $notification, $notification->delay);
|
||||
}
|
||||
|
||||
return $this->sendNowNotify($notifiables, $notification);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 立即发送
|
||||
*/
|
||||
public function sendNowNotify($notifiables, $notification) {
|
||||
foreach ($notifiables as $key => $notifiable) {
|
||||
$channels = $notification->via($notifiable);
|
||||
|
||||
if (empty($channels)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($channels as $k => $channel) {
|
||||
(new $channel)->send($notifiable, $notification);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 队列发送
|
||||
* delay 延迟时间
|
||||
*/
|
||||
public function sendQueueNotify($notifiables, $notification, $delay) {
|
||||
if ($delay > 0) {
|
||||
// 异步延迟发送
|
||||
\think\Queue::later($delay, '\addons\shopro\job\Notification@send', [
|
||||
'notifiables' => $notifiables,
|
||||
'notification' => $notification,
|
||||
'notification_name' => get_class($notification)
|
||||
], 'shopro');
|
||||
} else {
|
||||
// 异步立即发送
|
||||
\think\Queue::push('\addons\shopro\job\Notification@send', [
|
||||
'notifiables' => $notifiables,
|
||||
'notification' => $notification,
|
||||
'notification_name' => get_class($notification)
|
||||
], 'shopro');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static function __callStatic($name, $arguments)
|
||||
{
|
||||
return (new self)->{$name . 'Notify'}(...$arguments);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace addons\shopro\library\notify\channel;
|
||||
|
||||
use addons\shopro\notifications\Notification;
|
||||
use addons\shopro\model\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->type = $notification->event;
|
||||
$notificationModel->notifiable_id = $notifiable['id'];
|
||||
$notificationModel->notifiable_type = $notification->notifiableType;
|
||||
$notificationModel->data = json_encode($data);
|
||||
|
||||
$notificationModel->save();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace addons\shopro\library\notify\channel;
|
||||
|
||||
use addons\shopro\notifications\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")) {
|
||||
$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::write('邮件消息发送失败:用户:' . $notifiable['id'] . ';类型:' . get_class($notification) . ";发送类型:" . $notification->event . ";错误信息:" . json_encode($email->getError()));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// 没有openid
|
||||
\think\Log::write('邮件消息发送失败,没有 email,或 email 格式不正确:用户:' . $notifiable['id'] . ';类型:' . get_class($notification) . ";发送类型:" . $notification->event);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace addons\shopro\library\notify\channel;
|
||||
|
||||
use addons\shopro\notifications\Notification;
|
||||
use app\common\library\Sms as Smslib;
|
||||
|
||||
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['phone'] && isset($data['template_id'])) {
|
||||
$mobile = $data['phone'];
|
||||
$sendData = $data['data'] ?? [];
|
||||
|
||||
$params = [
|
||||
'mobile' => $mobile,
|
||||
'msg' => $sendData,
|
||||
'template' => $data['template_id']
|
||||
];
|
||||
$result = \think\Hook::listen('sms_notice', $params, null, true);
|
||||
|
||||
if (!$result) {
|
||||
// 短信发送失败
|
||||
\think\Log::write('短信发送失败:用户:'. $notifiable['id'] . ';类型:' . get_class($notification) . ";发送类型:" . $notification->event);
|
||||
} else {
|
||||
// 发送成功
|
||||
$notification->sendOk('sms');
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
// 没有手机号
|
||||
\think\Log::write('短信发送失败,没有手机号:用户:' . $notifiable['id'] . ';类型:' . get_class($notification) . ";发送类型:" . $notification->event);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace addons\shopro\library\notify\channel;
|
||||
|
||||
use addons\shopro\notifications\Notification;
|
||||
|
||||
class WxMiniProgram
|
||||
{
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 发送 微信模板消息
|
||||
*
|
||||
* @param mixed $notifiable // 通知用户
|
||||
* @param 通知内容
|
||||
* @return void
|
||||
*/
|
||||
public function send($notifiable, Notification $notification)
|
||||
{
|
||||
$data = [];
|
||||
|
||||
if (method_exists($notification, 'toWxMiniProgram')) {
|
||||
$data = $notification->toWxMiniProgram($notifiable);
|
||||
|
||||
if ($data && isset($data['openid']) && isset($data['template_id']) && $data['template_id']) {
|
||||
$data['touser'] = $data['openid'];
|
||||
unset($data['openid']);
|
||||
|
||||
try {
|
||||
// 发送模板消息
|
||||
$result = (new \addons\shopro\library\Wechat('wxMiniProgram'))->getApp()->subscribe_message->send($data);
|
||||
|
||||
if ($result['errcode'] != 0) {
|
||||
// 小程序模板发送失败
|
||||
\think\Log::write('小程序模板消息发送失败:用户:'. $notifiable['id'] . ';类型:' . get_class($notification) . ";发送类型:" . $notification->event . ";错误信息:" . json_encode($result));
|
||||
} else {
|
||||
// 发送成功
|
||||
$notification->sendOk('wxMiniProgram');
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
// 因为配置较麻烦,这里捕获异常防止因为缺少字段,导致队列一直执行不成功
|
||||
\think\Log::write('小程序模板消息发送失败:用户:' . $notifiable['id'] . ';类型:' . get_class($notification) . ";发送类型:" . $notification->event . ";错误信息:" . $e->getMessage());
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// 没有openid
|
||||
\think\Log::write('小程序模板消息发送失败,没有 openid:用户:' . $notifiable['id'] . ';类型:' . get_class($notification) . ";发送类型:" . $notification->event);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace addons\shopro\library\notify\channel;
|
||||
|
||||
use addons\shopro\notifications\Notification;
|
||||
|
||||
class WxOfficeAccount
|
||||
{
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 发送 微信模板消息
|
||||
*
|
||||
* @param mixed $notifiable // 通知用户
|
||||
* @param 通知内容
|
||||
* @return void
|
||||
*/
|
||||
public function send($notifiable, Notification $notification)
|
||||
{
|
||||
$data = [];
|
||||
|
||||
if (method_exists($notification, 'toWxOfficeAccount')) {
|
||||
$data = $notification->toWxOfficeAccount($notifiable);
|
||||
|
||||
if ($data && isset($data['openid']) && isset($data['template_id']) && $data['template_id']) {
|
||||
$data['touser'] = $data['openid'];
|
||||
unset($data['openid']);
|
||||
|
||||
try {
|
||||
// 发送模板消息
|
||||
$result = (new \addons\shopro\library\Wechat('wxOfficialAccount'))->getApp()->template_message->send($data);
|
||||
|
||||
if ($result['errcode'] != 0) {
|
||||
// 短信发送失败
|
||||
\think\Log::write('公众号模板消息发送失败:用户:' . $notifiable['id'] . ';类型:' . get_class($notification) . ";发送类型:" . $notification->event . ";错误信息:" . json_encode($result));
|
||||
} else {
|
||||
// 发送成功
|
||||
$notification->sendOk('wxOfficialAccount');
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
// 因为配置较麻烦,这里捕获异常防止因为缺少字段,导致队列一直执行不成功
|
||||
\think\Log::write('公众号模板消息发送失败:用户:' . $notifiable['id'] . ';类型:' . get_class($notification) . ";发送类型:" . $notification->event . ";错误信息:" . $e->getMessage());
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// 没有openid
|
||||
\think\Log::write('公众号模板消息发送失败,没有 openid:用户:' . $notifiable['id'] . ';类型:' . get_class($notification) . ";发送类型:" . $notification->event);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace addons\shopro\library\notify\channel;
|
||||
|
||||
use addons\shopro\notifications\Notification;
|
||||
|
||||
class WxOfficeAccountBizsend
|
||||
{
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 发送 微信公众号订阅消息(新)
|
||||
*
|
||||
* @param mixed $notifiable // 通知用户
|
||||
* @param 通知内容
|
||||
* @return void
|
||||
*/
|
||||
public function send($notifiable, Notification $notification)
|
||||
{
|
||||
$data = [];
|
||||
|
||||
if (method_exists($notification, 'toWxOfficeAccount')) {
|
||||
$data = $notification->toWxOfficeAccount($notifiable, 'wxOfficialAccountBizsend');
|
||||
|
||||
if ($data && isset($data['openid']) && isset($data['template_id']) && $data['template_id']) {
|
||||
$data['touser'] = $data['openid'];
|
||||
unset($data['openid']);
|
||||
|
||||
try {
|
||||
// 发送模板消息
|
||||
$result = (new \addons\shopro\library\Wechat('wxOfficialAccount'))->bizsendSubscribeMessage($data);
|
||||
|
||||
if ($result['errcode'] != 0) {
|
||||
// 短信发送失败
|
||||
\think\Log::write('公众号订阅消息发送失败:用户:' . $notifiable['id'] . ';类型:' . get_class($notification) . ";发送类型:" . $notification->event . ";错误信息:" . json_encode($result));
|
||||
} else {
|
||||
// 发送成功
|
||||
$notification->sendOk('wxOfficialAccountBizsend');
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
// 因为配置较麻烦,这里捕获异常防止因为缺少字段,导致队列一直执行不成功
|
||||
\think\Log::write('公众号订阅消息发送失败:用户:' . $notifiable['id'] . ';类型:' . get_class($notification) . ";发送类型:" . $notification->event . ";错误信息:" . $e->getMessage());
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// 没有openid
|
||||
\think\Log::write('公众号订阅消息发送失败,没有 openid:用户:' . $notifiable['id'] . ';类型:' . get_class($notification) . ";发送类型:" . $notification->event);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
在新工单中引用
屏蔽一个用户