Files
tougao/application/common/JournalArticle.php
2025-06-18 11:08:01 +08:00

243 lines
10 KiB
PHP

<?php
namespace app\common;
use think\Db;
use think\Env;
use think\Queue;
class JournalArticle
{
//官网接口地址
protected static $sApiUrl = 'http://journalapi.tmrjournals.com/public/index.php/';//'http://zmzm.journal.dev.com/';
//期刊官网
protected static $sJournalUsx = 'https://www.tmrjournals.com/';
protected static $aEmailTemplate = [
'email_subject' => 'Recommended Articles from {#email_subject#}',
'email_content' => 'Dear {#author_name#},<br><br>
I hope this message finds you well!<br><br>
We would like to extend our sincere thanks for your recent contribution to {#journal_title#} with your article titled "{#article_title#}".<br><br>
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:<br><br>
{#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.<br><br>
Thank you for your continued engagement with {#journal_title#}<br><br>
Best regards,<br>
{#editorinchief#}<br>
{#journal_title#}<br>
Website:{#journal_usx#}<br>
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' => 6,'msg' => 'No author information found for the article']);
}
//查询邮件发送日志
$aWhere = ['article_id' => $iArticleId,'is_success' => 1];
$aEmailLog = Db::name('email_related_article')->field('related_article_id,article_author_id')->where($aWhere)->select();
$aLog = [];
if(!empty($aEmailLog)){
foreach ($aEmailLog as $key => $value) {
$aLog[$value['related_article_id']][] = $value['article_author_id'];
}
}
//邮件模版
$aEmailInfo =self::$aEmailTemplate;
//数据处理
$aEmailLog = [];
$sErrorMsg = '';
foreach ($aAuthor as $key => $value) {
//作者邮箱-发送信息
$email = empty($value['email']) ? '' : $value['email'];
if(empty($email)){
continue;
}
$email = $value['email'];//'tmr@tmrjournals.com';//'1172937051@qq.com';//
//判断是否发送过邮件
//邮件日志
$aEmailLogInfo = empty($aLog[$value['article_id']]) ? [] : $aLog[$value['article_id']];
if(in_array($value['article_author_id'], $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'].'<br>Author(s): '.$sAuthorInfo.'<br>Link or DOI: '.$sDoi.'<br><br>';
$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 = '成功';
}
$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' => '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['article_author_id']) || 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]);
}
}
?>