相关文章发送队列任务
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
namespace app\common;
|
namespace app\common;
|
||||||
use think\Db;
|
use think\Db;
|
||||||
use think\Env;
|
use think\Env;
|
||||||
|
use think\Queue;
|
||||||
class JournalArticle
|
class JournalArticle
|
||||||
{
|
{
|
||||||
|
|
||||||
@@ -37,6 +38,9 @@ class JournalArticle
|
|||||||
|
|
||||||
protected static $sDoiUrl = 'https://doi.org/';
|
protected static $sDoiUrl = 'https://doi.org/';
|
||||||
|
|
||||||
|
//必填参数
|
||||||
|
protected static $aField = ['article_id','journal_id','journal_issn','related_article_id','article_author_id','email','content','is_success','msg','create_time'];
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询文章所关联的文章并发送邮件提醒
|
* 查询文章所关联的文章并发送邮件提醒
|
||||||
@@ -87,26 +91,13 @@ class JournalArticle
|
|||||||
return json_encode(['status' => 6,'msg' => 'No author information found for the article']);
|
return json_encode(['status' => 6,'msg' => 'No author information found for the article']);
|
||||||
}
|
}
|
||||||
|
|
||||||
//数据处理-获取文章作者
|
|
||||||
$aArticleAuthor = [];
|
|
||||||
foreach ($aAuthor as $key => $value) {
|
|
||||||
if(empty($value['email'])){
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$aArticleAuthor[$value['article_id']][$value['email']] = $value['author_name'];
|
|
||||||
}
|
|
||||||
|
|
||||||
if(empty($aArticleAuthor)){
|
|
||||||
return json_encode(['status' => 7,'msg' => 'No author information found for the article']);
|
|
||||||
}
|
|
||||||
|
|
||||||
//查询邮件发送日志
|
//查询邮件发送日志
|
||||||
$aWhere = ['article_id' => $iArticleId,'is_success' => 1];
|
$aWhere = ['article_id' => $iArticleId,'is_success' => 1];
|
||||||
$aEmailLog = Db::name('email_related_article')->field('related_article_id,email')->where($aWhere)->select();
|
$aEmailLog = Db::name('email_related_article')->field('related_article_id,article_author_id')->where($aWhere)->select();
|
||||||
$aLog = [];
|
$aLog = [];
|
||||||
if(!empty($aEmailLog)){
|
if(!empty($aEmailLog)){
|
||||||
foreach ($aEmailLog as $key => $value) {
|
foreach ($aEmailLog as $key => $value) {
|
||||||
$aLog[$value['related_article_id']][] = $value['email'];
|
$aLog[$value['related_article_id']][] = $value['article_author_id'];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -114,14 +105,23 @@ class JournalArticle
|
|||||||
$aEmailInfo =self::$aEmailTemplate;
|
$aEmailInfo =self::$aEmailTemplate;
|
||||||
//数据处理
|
//数据处理
|
||||||
$aEmailLog = [];
|
$aEmailLog = [];
|
||||||
foreach ($aArticleAuthor as $key => $value) {
|
foreach ($aAuthor as $key => $value) {
|
||||||
|
|
||||||
if(empty($value)){
|
//作者邮箱-发送信息
|
||||||
|
$email = empty($value['email']) ? '' : $value['email'];
|
||||||
|
if(empty($email)){
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$email = '1172937051@qq.com';//'tmr@tmrjournals.com';
|
||||||
|
//判断是否发送过邮件
|
||||||
|
//邮件日志
|
||||||
|
$aEmailLogInfo = empty($aLog[$value['article_id']]) ? [] : $aLog[$value['article_id']];
|
||||||
|
if(in_array($value['article_author_id'], $aEmailLogInfo)){
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
//关联文章信息
|
//关联文章信息
|
||||||
$aRelatedArticleInfo = empty($aRelatedArticle[$key]) ? [] : $aRelatedArticle[$key];
|
$aRelatedArticleInfo = empty($aRelatedArticle[$value['article_id']]) ? [] : $aRelatedArticle[$value['article_id']];
|
||||||
if(empty($aRelatedArticleInfo)){
|
if(empty($aRelatedArticleInfo)){
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -137,6 +137,7 @@ class JournalArticle
|
|||||||
$title = str_replace('{#email_subject#}', $from_name, $aEmailInfo['email_subject']);
|
$title = str_replace('{#email_subject#}', $from_name, $aEmailInfo['email_subject']);
|
||||||
//邮件内容
|
//邮件内容
|
||||||
$aSearch = [
|
$aSearch = [
|
||||||
|
'{#author_name#}' => empty($value['author_name']) ? $email : $value['author_name'],
|
||||||
'{#article_title#}' => empty($aRelatedArticleInfo['title']) ? '' : strip_tags($aRelatedArticleInfo['title']),//文章标题
|
'{#article_title#}' => empty($aRelatedArticleInfo['title']) ? '' : strip_tags($aRelatedArticleInfo['title']),//文章标题
|
||||||
'{#editorinchief#}' => 'Editorial Office',//empty($aJournalInfo['editorinchief']) ? '' : $aJournalInfo['editorinchief'],//总编辑
|
'{#editorinchief#}' => 'Editorial Office',//empty($aJournalInfo['editorinchief']) ? '' : $aJournalInfo['editorinchief'],//总编辑
|
||||||
'{#journal_title#}' => $from_name,//期刊名
|
'{#journal_title#}' => $from_name,//期刊名
|
||||||
@@ -147,7 +148,7 @@ class JournalArticle
|
|||||||
$sArticleInfo = '';
|
$sArticleInfo = '';
|
||||||
$i = 1;
|
$i = 1;
|
||||||
foreach ($aRelatedArticle as $v) {
|
foreach ($aRelatedArticle as $v) {
|
||||||
if($v['article_id'] == $key){
|
if($v['article_id'] == $value['article_id']){
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$sDoi = empty($v['doi']) ? '' : self::$sDoiUrl.$v['doi'];
|
$sDoi = empty($v['doi']) ? '' : self::$sDoiUrl.$v['doi'];
|
||||||
@@ -163,54 +164,55 @@ class JournalArticle
|
|||||||
}
|
}
|
||||||
$aSearch['{#article_info#}'] = empty($sArticleInfo) ? '' : $sArticleInfo;
|
$aSearch['{#article_info#}'] = empty($sArticleInfo) ? '' : $sArticleInfo;
|
||||||
|
|
||||||
//邮件日志
|
$content = str_replace(array_keys($aSearch), array_values($aSearch), $aEmailInfo['email_content']);
|
||||||
$aEmailLogInfo = empty($aLog[$key]) ? [] : $aLog[$key];
|
|
||||||
|
|
||||||
//作者邮箱-发送信息
|
//判断标题和内容是否为空
|
||||||
foreach ($value as $k => $val) {
|
if(empty($title) || empty($content)){
|
||||||
|
continue;
|
||||||
$email = 'tmr@tmrjournals.com';//$k;
|
|
||||||
|
|
||||||
//判断是否发送过邮件
|
|
||||||
if(in_array($email, $aEmailLogInfo)){
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$aSearch['{#author_name#}'] = empty($val) ? $email : $val;
|
|
||||||
$content = str_replace(array_keys($aSearch), array_values($aSearch), $aEmailInfo['email_content']);
|
|
||||||
|
|
||||||
//判断标题和内容是否为空
|
|
||||||
if(empty($title) || empty($content)){
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$pre = Env::get('emailtemplete.pre');
|
|
||||||
$net = Env::get('emailtemplete.net');
|
|
||||||
$net1 = str_replace("{{email}}",trim($email),$net);
|
|
||||||
$content=$pre.$content.$net1;
|
|
||||||
//发送邮件
|
|
||||||
$memail = empty($aJournalInfo['email']) ? '' : $aJournalInfo['email'];
|
|
||||||
$mpassword = empty($aJournalInfo['epassword']) ? '' : $aJournalInfo['epassword'];
|
|
||||||
$aResult = sendEmail($email,$title,$from_name,$content,$memail,$mpassword);
|
|
||||||
exit;
|
|
||||||
$iStatus = empty($aResult['status']) ? 0 : $aResult['status'];
|
|
||||||
$iIsSuccess = 2;
|
|
||||||
$sMsg = empty($aResult['data']) ? '失败' : $aResult['data'];
|
|
||||||
if($iStatus == 1){
|
|
||||||
$iIsSuccess = 1;
|
|
||||||
$sMsg = '成功';
|
|
||||||
}
|
|
||||||
$aEmailLog[] = ['article_id' => $iArticleId,'related_article_id' => $key,'email' => $email,'content' => $content,'create_time' => time(),'is_success' => $iIsSuccess,'journal_id' => $aJournalInfo['journal_id'],'journal_issn' => $aJournalInfo['issn'],'msg' => $sMsg];
|
|
||||||
}
|
}
|
||||||
|
$pre = Env::get('emailtemplete.pre');
|
||||||
}
|
$net = Env::get('emailtemplete.net');
|
||||||
//插入日志记录
|
$net1 = str_replace("{{email}}",trim($email),$net);
|
||||||
if(!empty($aEmailLog)){
|
$content=$pre.$content.$net1;
|
||||||
$result = Db::name('email_related_article')->insertAll($aEmailLog);
|
//发送邮件
|
||||||
if($result === false){
|
$memail = empty($aJournalInfo['email']) ? '' : $aJournalInfo['email'];
|
||||||
return json_encode(['status' => 8,'msg' => 'Email sending log insertion failed:'.json_encode($aEmailLog)]);
|
$mpassword = empty($aJournalInfo['epassword']) ? '' : $aJournalInfo['epassword'];
|
||||||
|
$aResult = sendEmail($email,$title,$from_name,$content,$memail,$mpassword);
|
||||||
|
$iStatus = empty($aResult['status']) ? 1 : $aResult['status'];
|
||||||
|
$iIsSuccess = 2;
|
||||||
|
$sMsg = empty($aResult['data']) ? '失败' : $aResult['data'];
|
||||||
|
if($iStatus == 1){
|
||||||
|
$iIsSuccess = 1;
|
||||||
|
$sMsg = '成功';
|
||||||
}
|
}
|
||||||
|
$aEmaiParam = ['article_id' => $iArticleId,'article_author_id' =>$value['article_author_id'],'related_article_id' => $value['article_id'],'email' => $email,'content' => $content,'create_time' => time(),'journal_id' => $aJournalInfo['journal_id'],'journal_issn' => $aJournalInfo['issn'],'email' => $email,'title' => $title,'from_name' => $from_name,'content' => $content,'memail' => $memail,'mpassword' => $mpassword];
|
||||||
|
//调用邮件发送队列
|
||||||
|
Queue::push('app\api\job\SendRelatedArticleEmail@fire', $aEmaiParam, 'SendRelatedArticleEmail');
|
||||||
}
|
}
|
||||||
return json_encode(['status' => 1,'msg' => 'Email sent successfully']);
|
|
||||||
|
return json_encode(['status' => 1,'msg' => 'Added to email sending queue']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 插入日志
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
static function addLog($aParam = []) {
|
||||||
|
|
||||||
|
//数据处理
|
||||||
|
$aField = self::$aField;
|
||||||
|
$aInsert = [];
|
||||||
|
foreach ($aField as $key => $value) {
|
||||||
|
if(isset($aParam[$value])){
|
||||||
|
$aInsert[$value] = $aParam[$value];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$result = 0;
|
||||||
|
if(!empty($aInsert)){
|
||||||
|
$result = DB::name('email_related_article')->insertGetId($aParam);
|
||||||
|
}
|
||||||
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user