'Recommended Articles from {#email_subject#}',
'email_content' => 'Dear {#author_name#},
I hope this message finds you well!
We would like to extend our sincere thanks for your recent contribution to {#journal_title#} with your article titled "{#article_title#}".
In light of the themes explored in your paper, we would like to share with you a selection of recently published articles that cover similar or complementary topics. We believe these may be of interest to you:
{#article_info#}
We hope these articles prove useful for your research or teaching. Should you have any thoughts or feedback, we would be delighted to hear from you.
Thank you for your continued engagement with {#journal_title#}
Best regards,
{#editorinchief#}
{#journal_title#}
Website:{#journal_usx#}
X: @{#journal_sx#}Journals'
];
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'];
/**
* 查询文章所关联的文章并发送邮件提醒
*
* @return void
*/
static function get($aParam = []) {
//文章ID
$iArticleId = empty($aParam['article_id']) ? '' : $aParam['article_id'];
if(empty($iArticleId)){
return json_encode(array('status' => 2,'msg' => 'Please select an article'.json_encode($aParam) ));
}
//接口获取信息
$sApiUrl = self::$sApiUrl.'wechat/Article/getRelatedArticles';
$aResult = object_to_array(json_decode(myPost($sApiUrl, $aParam),true));
//获取接口状态
$iStatus = empty($aResult['status']) ? 0 : $aResult['status'];
if($iStatus != 1){
return json_encode($aResult);
}
//处理数据
$aData = empty($aResult['data']) ? [] : $aResult['data'];
//文章基本信息
$aArticle = empty($aData['article']) ? [] : $aData['article'];
if(empty($aArticle)){
return json_encode(array('status' => 3,'msg' => 'Article not found' ));
}
//关联文章基本信息
$aRelatedArticle = empty($aData['related_article']) ? [] : array_column($aData['related_article'], null,'article_id');
if(empty($aRelatedArticle)){
return json_encode(['status' => 4,'msg' => 'No information was found for the associated article']);
}
$aRelatedArticle += [$iArticleId => $aArticle];
//期刊数据
$aJournal = empty($aData['journal']) ? [] : array_column($aData['journal'], null,'journal_id');
if(empty($aJournal)){
return json_encode(['status' => 5,'msg' => 'The journal to which the article belongs does not exist']);
}
//获取数据-文章通讯作者数据
$aAuthor = empty($aData['author']) ? [] : $aData['author'];
if(empty($aAuthor)){
return json_encode(['status' => 5,'msg' => '未查询到引用文章作者数据']);
}
//查询邮件发送日志
$aWhere = ['article_id' => $iArticleId,'is_success' => 1];
$aEmailLog = Db::name('email_related_article')->field('related_article_id,email')->where($aWhere)->select();
$aLog = [];
if(!empty($aEmailLog)){
foreach ($aEmailLog as $key => $value) {
$aLog[$value['related_article_id']][] = $value['email'];
}
}
//邮件模版
$aEmailInfo =self::$aEmailTemplate;
//数据处理
$aEmailLog = [];
$sErrorMsg = '';
$aSendEmail = [];//['2101' => ['mohammadalipour_z@yahoo.com']];
foreach ($aAuthor as $key => $value) {
if(empty($value['is_report']) || (!empty($value['is_report']) && $value['is_report'] != 1)){
continue;
}
//作者邮箱-发送信息
$email = empty($value['email']) ? '' : $value['email'];//'tmr@tmrjournals.com';//'1172937051@qq.com';//
if(empty($email)){
continue;
}
//判断同一个邮箱是否重复发送
if(!empty($aSendEmail[$value['article_id']]) && in_array($email, $aSendEmail[$value['article_id']])){
continue;
}
//判断是否发送过邮件
$aEmailLogInfo = empty($aLog[$value['article_id']]) ? [] : $aLog[$value['article_id']];//邮件日志
if(in_array($value['email'], $aEmailLogInfo)){
continue;
}
//关联文章信息
$aRelatedArticleInfo = empty($aRelatedArticle[$value['article_id']]) ? [] : $aRelatedArticle[$value['article_id']];
if(empty($aRelatedArticleInfo)){
continue;
}
//期刊信息
$aJournalInfo = empty($aJournal[$aRelatedArticleInfo['journal_id']]) ? [] : $aJournal[$aRelatedArticleInfo['journal_id']];
if(empty($aJournalInfo)){
continue;
}
//期刊标题
$from_name = empty($aJournalInfo['title']) ? '' : $aJournalInfo['title'];
//邮件标题
$title = str_replace('{#email_subject#}', $from_name, $aEmailInfo['email_subject']);
//邮件内容
$aSearch = [
'{#author_name#}' => empty($value['author_name']) ? $email : $value['author_name'],
'{#article_title#}' => empty($aRelatedArticleInfo['title']) ? '' : strip_tags($aRelatedArticleInfo['title']),//文章标题
'{#editorinchief#}' => 'Editorial Office',//empty($aJournalInfo['editorinchief']) ? '' : $aJournalInfo['editorinchief'],//总编辑
'{#journal_title#}' => $from_name,//期刊名
'{#journal_usx#}' => empty($aJournalInfo['usx']) ? '' : self::$sJournalUsx.$aJournalInfo['usx'],//官网地址
'{#journal_sx#}' => empty($aJournalInfo['sx']) ? '' : $aJournalInfo['sx'],//期刊sx
];
//关联文章说明
$sArticleInfo = '';
$i = 1;
foreach ($aRelatedArticle as $v) {
if($v['article_id'] == $value['article_id']){
continue;
}
$sDoi = empty($v['doi']) ? '' : self::$sDoiUrl.$v['doi'];
//作者
$aAuthorInfo = empty($v['abbr']) ? [] : explode(', ', str_replace([', ',','], ', ', $v['abbr']));
if(count($aAuthorInfo) > 3){
$sAuthorInfo = implode(', ', array_slice($aAuthorInfo,0,3)).", et al.";
}else{
$sAuthorInfo = empty($aAuthorInfo) ? '' : implode(', ', $aAuthorInfo).'.';
}
$sArticleInfo .= $i.'. Article Title: '.$v['title'].'
Author(s): '.$sAuthorInfo.'
Link or DOI: '.$sDoi.'
';
$i++;
}
$aSearch['{#article_info#}'] = empty($sArticleInfo) ? '' : $sArticleInfo;
$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);
// $iStatus = empty($aResult['status']) ? 1 : $aResult['status'];
// $iIsSuccess = 2;
// $sMsg = empty($aResult['data']) ? '失败' : $aResult['data'];
// if($iStatus == 1){
// $iIsSuccess = 1;
// $sMsg = '成功';
// }
$aEmailParam = ['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'],'title' => $title,'from_name' => $from_name,'memail' => $memail,'mpassword' => $mpassword];
//邮件发送记录
$aSendEmail[$value['article_id']][] = $email;
//调用邮件发送队列
Queue::push('app\api\job\SendRelatedArticleEmail@fire', $aEmailParam, 'SendRelatedArticleEmail');
}
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;
}
/**
* 查询邮件日志是否发送
*
* @return void
*/
static function getLog($aParam = []) {
//查询条件判断
if(empty($aParam['article_id']) || empty($aParam['email']) || empty($aParam['related_article_id'])){
return json_encode(['status' => 2,'msg' => 'Missing parameter']);
}
//数据处理
$aField = self::$aField;
$aWhere = [];
foreach ($aField as $key => $value) {
if(!empty($aParam[$value])){
$aWhere[$value] = is_array($aParam[$value]) ? ['in',$aParam[$value]] : $aParam[$value];
}
}
$aResult = DB::name('email_related_article')->field('article_id,related_article_id,article_author_id,is_success,create_time')->where($aWhere)->find();
return json_encode(['status' => 1,'msg' => 'success','data' => $aResult]);
}
}
?>