1
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
namespace app\queue\redis;
|
||||
|
||||
use Webman\RedisQueue\Consumer;
|
||||
|
||||
class Email implements Consumer
|
||||
{
|
||||
// 要消费的队列名
|
||||
public $queue = 'Email';
|
||||
|
||||
// 连接名,对应 plugin/webman/redis-queue/redis.php 里的连接`
|
||||
public $connection = 'default';
|
||||
|
||||
// 消费
|
||||
public function consume($data)
|
||||
{
|
||||
\support\Log::channel('mail')->alert("开始发送邮件");
|
||||
$config = Config('site');
|
||||
if(isset($data['config'])){
|
||||
$config = $data['config'];
|
||||
unset($data['config']);
|
||||
}
|
||||
try {
|
||||
$mail = new \Nette\Mail\Message;
|
||||
$mailer = new \Nette\Mail\SmtpMailer(
|
||||
host: $config['mail_smtp_host'],
|
||||
username: $config['mail_smtp_user'],
|
||||
password: $config['mail_smtp_pass'],
|
||||
encryption: \Nette\Mail\SmtpMailer::EncryptionSSL,
|
||||
);
|
||||
$mail->setFrom($config['mail_from'])
|
||||
->addTo($data['email'])
|
||||
->setSubject($data['title']);
|
||||
if($data['body']){
|
||||
$mail->setHtmlBody($data['body']);
|
||||
}else{
|
||||
$mail->setHtmlBody($this->getTemplate($data));
|
||||
}
|
||||
$mailer->send($mail);
|
||||
\support\Log::channel('mail')->alert($data['email']."邮件已经发送");
|
||||
} catch (\Throwable $th) {
|
||||
\support\Log::channel('mail')->alert('发送邮件出错1:'.$th->getMessage());
|
||||
\support\Log::channel('mail')->alert(json_encode($data));
|
||||
}
|
||||
|
||||
// 无需反序列化
|
||||
//var_export($data); // 输出 ['to' => 'tom@gmail.com', 'content' => 'hello']
|
||||
}
|
||||
function getTemplate($data){
|
||||
$config = Config('site');
|
||||
$mail_from = $config['mail_from'];
|
||||
$str = '<div style="background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(46, 48, 50) !important; padding: 40px 8px; font-family: Poppins, serif, EmojiFont; text-align: center;height: 100%;display: flex;align-items: center;justify-content: center;" data-ogsb="rgb(239, 242, 244)"><table style="border-collapse: collapse; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(41, 41, 41) !important; max-width: 420px; margin: 0px auto; border-radius: 8px;" width="100%" role="presentation" cellspacing="0" cellpadding="40" border="0" data-ogsb="rgb(255, 255, 255)"><tbody><tr><td style="border-collapse: collapse; color: rgb(177, 180, 202) !important;" align="center" data-ogsc="rgb(89, 92, 112)"><img style="width: 96px; height: 96px; margin-top: 20px; color: rgb(177, 180, 202) !important;" title="logo" src="'.domain().'/static/img/mail_logo.png" data-imagetype="External" data-ogsc=""><div style="font-size: 24px; font-weight: 500; margin-top: 32px; color: rgb(177, 180, 202) !important;" data-ogsc="">Thanks for your request!</div><div style="font-size:12px; margin-top:8px; color:#a1a5b7">Do not share this code with anyone, even if they claim to be a Codify employee.</div><div style="padding:8px 20px; margin:32px 0; border-radius:100px; color:#00b2ff; border:2px #00b2ff dotted; display:inline-block; font-size:24px">{$code}</div><div style="margin-bottom:24px; color:#a1a5b7; font-size:12px">Valid within 15 minutes and <br>can only be used once.</div><div style="height: 1px; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(46, 48, 50) !important; width: 100%; color: rgb(177, 180, 202) !important;" data-ogsc="" data-ogsb="rgb(239, 242, 245)"></div><div style="font-size:12px; text-align:center; margin-top:24px; padding-bottom:20px; color:#a1a5b7">Need help with anything? connect <a style="color:#00b2ff" href="mailto: '.$mail_from.'" title="mailto: '.$mail_from.'" data-linkindex="0" id="LPlnk280375">support</a></div></td></tr></tbody></table></div>';
|
||||
foreach($data as $k=>$v){
|
||||
$str = str_replace('{$'.$k.'}',$v,$str);
|
||||
}
|
||||
return $str ;
|
||||
}
|
||||
// 消费失败回调
|
||||
/*
|
||||
$package = [
|
||||
'id' => 1357277951, // 消息ID
|
||||
'time' => 1709170510, // 消息时间
|
||||
'delay' => 0, // 延迟时间
|
||||
'attempts' => 2, // 消费次数
|
||||
'queue' => 'send-mail', // 队列名
|
||||
'data' => ['to' => 'tom@gmail.com', 'content' => 'hello'], // 消息内容
|
||||
'max_attempts' => 5, // 最大重试次数
|
||||
'error' => '错误信息' // 错误信息
|
||||
]
|
||||
*/
|
||||
public function onConsumeFailure(\Throwable $e, $package)
|
||||
{
|
||||
echo "consume failure\n";
|
||||
echo $e->getMessage() . "\n";
|
||||
// 无需反序列化
|
||||
//var_export($package);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace app\queue\redis;
|
||||
|
||||
use Webman\RedisQueue\Consumer;
|
||||
|
||||
class Job implements Consumer
|
||||
{
|
||||
// 要消费的队列名
|
||||
public $queue = 'Default';
|
||||
|
||||
// 连接名,对应 plugin/webman/redis-queue/redis.php 里的连接`
|
||||
public $connection = 'default';
|
||||
|
||||
// 消费
|
||||
public function consume($data)
|
||||
{
|
||||
// 无需反序列化
|
||||
var_export($data); // 输出 ['to' => 'tom@gmail.com', 'content' => 'hello']
|
||||
}
|
||||
// 消费失败回调
|
||||
/*
|
||||
$package = [
|
||||
'id' => 1357277951, // 消息ID
|
||||
'time' => 1709170510, // 消息时间
|
||||
'delay' => 0, // 延迟时间
|
||||
'attempts' => 2, // 消费次数
|
||||
'queue' => 'send-mail', // 队列名
|
||||
'data' => ['to' => 'tom@gmail.com', 'content' => 'hello'], // 消息内容
|
||||
'max_attempts' => 5, // 最大重试次数
|
||||
'error' => '错误信息' // 错误信息
|
||||
]
|
||||
*/
|
||||
public function onConsumeFailure(\Throwable $e, $package)
|
||||
{
|
||||
echo "consume failure\n";
|
||||
echo $e->getMessage() . "\n";
|
||||
// 无需反序列化
|
||||
var_export($package);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
namespace app\queue\redis;
|
||||
|
||||
use Webman\RedisQueue\Consumer;
|
||||
|
||||
class Sms implements Consumer
|
||||
{
|
||||
// 要消费的队列名
|
||||
public $queue = 'Sms';
|
||||
|
||||
// 连接名,对应 plugin/webman/redis-queue/redis.php 里的连接`
|
||||
public $connection = 'default';
|
||||
|
||||
// 消费
|
||||
public function consume($data)
|
||||
{
|
||||
\support\Log::channel('mail')->alert("开始发送短信:".json_encode($data));
|
||||
$body = '';
|
||||
if($data['body']){
|
||||
$body = $data['body'];
|
||||
}else{
|
||||
$body = $this->getTemplate($data);
|
||||
}
|
||||
$url = 'http://api.smsbao.com/sms?u=aisiaisi8899&p='.md5('Aaa123123').'&m='.$data['mobile'].'&c='.urlencode($body);
|
||||
$statusStr = array(
|
||||
"0" => "短信发送成功",
|
||||
"-1" => "参数不全",
|
||||
"-2" => "服务器空间不支持,请确认支持curl或者fsocket,联系您的空间商解决或者更换空间!",
|
||||
"30" => "密码错误",
|
||||
"40" => "账号不存在",
|
||||
"41" => "余额不足",
|
||||
"42" => "帐户已过期",
|
||||
"43" => "IP地址限制",
|
||||
"50" => "内容含有敏感词"
|
||||
);
|
||||
try {
|
||||
$res = get($url);
|
||||
log_alert($res.$statusStr[$res]);
|
||||
\support\Log::channel('mail')->alert($data['email']."短信已经发送");
|
||||
} catch (\Throwable $th) {
|
||||
\support\Log::channel('mail')->alert('发送短信出错:'.$th->getMessage());
|
||||
\support\Log::channel('mail')->alert(json_encode($data));
|
||||
}
|
||||
|
||||
// 无需反序列化
|
||||
//var_export($data); // 输出 ['to' => 'tom@gmail.com', 'content' => 'hello']
|
||||
}
|
||||
function getTemplate($data){
|
||||
$str = '【问析科技】验证码:{$code},该验证码在30分钟内输入有效,若非您本人操作请尽快修改登录密码';
|
||||
foreach($data as $k=>$v){
|
||||
$str = str_replace('{$'.$k.'}',$v,$str);
|
||||
}
|
||||
return $str ;
|
||||
}
|
||||
// 消费失败回调
|
||||
/*
|
||||
$package = [
|
||||
'id' => 1357277951, // 消息ID
|
||||
'time' => 1709170510, // 消息时间
|
||||
'delay' => 0, // 延迟时间
|
||||
'attempts' => 2, // 消费次数
|
||||
'queue' => 'send-mail', // 队列名
|
||||
'data' => ['to' => 'tom@gmail.com', 'content' => 'hello'], // 消息内容
|
||||
'max_attempts' => 5, // 最大重试次数
|
||||
'error' => '错误信息' // 错误信息
|
||||
]
|
||||
*/
|
||||
public function onConsumeFailure(\Throwable $e, $package)
|
||||
{
|
||||
echo "consume failure\n";
|
||||
echo $e->getMessage() . "\n";
|
||||
// 无需反序列化
|
||||
//var_export($package);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace app\queue\redis;
|
||||
|
||||
use Webman\RedisQueue\Consumer;
|
||||
/**
|
||||
* 延迟写入日志sql
|
||||
*/
|
||||
class Sql implements Consumer
|
||||
{
|
||||
// 要消费的队列名
|
||||
public $queue = 'Sql';
|
||||
|
||||
// 连接名,对应 plugin/webman/redis-queue/redis.php 里的连接`
|
||||
public $connection = 'default';
|
||||
|
||||
// 消费
|
||||
public function consume($data)
|
||||
{
|
||||
if(isset($data['model'])){
|
||||
switch($data['model']){
|
||||
case "RecordModel":
|
||||
unset($data['model']);
|
||||
\app\model\Record::create($data);
|
||||
break;
|
||||
case "RecordModel12":
|
||||
break;
|
||||
default:
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
// 无需反序列化
|
||||
//var_export($data); // 输出 ['to' => 'tom@gmail.com', 'content' => 'hello']
|
||||
}
|
||||
// 消费失败回调
|
||||
/*
|
||||
$package = [
|
||||
'id' => 1357277951, // 消息ID
|
||||
'time' => 1709170510, // 消息时间
|
||||
'delay' => 0, // 延迟时间
|
||||
'attempts' => 2, // 消费次数
|
||||
'queue' => 'send-mail', // 队列名
|
||||
'data' => ['to' => 'tom@gmail.com', 'content' => 'hello'], // 消息内容
|
||||
'max_attempts' => 5, // 最大重试次数
|
||||
'error' => '错误信息' // 错误信息
|
||||
]
|
||||
*/
|
||||
public function onConsumeFailure(\Throwable $e, $package)
|
||||
{
|
||||
if($package['attempts'] >= $package['max_attempts']){
|
||||
\support\Log::error(json_encode($package['data']));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user