Files
tougao/application/common/JournalArticle.php
2025-06-05 13:49:32 +08:00

207 lines
9.2 KiB
PHP

<?php
namespace app\common;
use think\Db;
use think\Env;
class JournalArticle
{
//官网接口地址
protected static $sApiUrl = '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/';
// 记录任务开始
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) ));
}
//关联文章ID
$iRelatedArticleId = empty($aParam['related_article_id']) ? '' :$aParam['related_article_id'] ;
if(empty($iRelatedArticleId)){
return json_encode(array('status' => 2,'msg' => 'Please select an associated article'));
}
//接口获取信息
$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']);
}
//查询发送人信息
$aEmail = array_unique(array_column($aAuthor, 'email'));
$aWhere = ['email' => ['in',$aEmail]];
$aUser = Db::name('user')->where($aWhere)->column('email,realname,localname');
//数据处理-获取文章作者
$aArticleAuthor = [];
foreach ($aAuthor as $key => $value) {
if(empty($value['email'])){
continue;
}
//用户信息
$aUserInfo = empty($aUser[$value['email']]) ? [] : $aUser[$value['email']];
$realname = empty($aUserInfo['realname']) ? '' : $aUserInfo['realname'];
$localname = empty($aUserInfo['localname']) ? '' : $aUserInfo['localname'];
$realname = empty($realname) ? $localname : $realname;
$realname = empty($value['author_name']) ? $realname : $value['author_name'];
$aArticleAuthor[$value['article_id']][$value['email']] = $realname;
}
if(empty($aArticleAuthor)){
return json_encode(['status' => 7,'msg' => 'No author information found for the article']);
}
//邮件模版
$aEmailInfo =self::$aEmailTemplate;
//数据处理
foreach ($aArticleAuthor as $key => $value) {
if(empty($value)){
continue;
}
//关联文章信息
$aRelatedArticleInfo = empty($aRelatedArticle[$key]) ? [] : $aRelatedArticle[$key];
if(empty($aRelatedArticleInfo)){
continue;
}
//期刊信息
$aJournalInfo = empty($aJournal[$aRelatedArticleInfo['journal_id']]) ? [] : $aJournal[$aRelatedArticleInfo['journal_id']];
if(empty($aJournalInfo)){
continue;
}
//用户信息
$realname = empty($val) ? $realname : $val;
//期刊标题
$from_name = empty($aJournalInfo['title']) ? '' : $aJournalInfo['title'];
//邮件标题
$title = str_replace('{#email_subject#}', $from_name, $aEmailInfo['email_subject']);
//邮件内容
$aSearch = [
'{#article_title#}' => empty($aRelatedArticleInfo['title']) ? '' : strip_tags($aRelatedArticleInfo['title']),//文章标题
'{#editorinchief#}' => 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'] == $key){
continue;
}
$sDoi = empty($v['doi']) ? '' : self::$sDoiUrl.$v['doi'];
//作者
$aAuthorInfo = empty($aArticleAuthor[$v['article_id']]) ? [] : array_values($aArticleAuthor[$v['article_id']]);
if(count($aAuthorInfo) > 3){
$sAuthorInfo = implode(',', array_slice($aAuthorInfo,0,3))."";
}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;
//作者邮箱-发送信息
foreach ($value as $k => $val) {
$email = '1172937051@qq.com';//$k;
$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);
$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];
}
}
//插入日志记录
if(!empty($aEmailLog)){
$result = Db::name('email_related_article')->insertAll($aEmailLog);
if($result === false){
return json_encode(['status' => 8,'msg' => 'Email sending log insertion failed:'.json_encode($aEmailLog)]);
}
}
return json_encode(['status' => 1,'msg' => 'Email sent successfully']);
}
}
?>