209 lines
9.3 KiB
PHP
209 lines
9.3 KiB
PHP
<?php
|
||
namespace app\api\controller;
|
||
use app\api\controller\Base;
|
||
use think\Controller;
|
||
use think\Db;
|
||
use think\Env;
|
||
class Cronmonitor extends Controller
|
||
{
|
||
|
||
//期刊接口地址
|
||
protected $sJournalUrl = 'http://journalapi.tmrjournals.com/public/index.php/';//'http://zmzm.journal.dev.com/'; //
|
||
|
||
//期刊官网
|
||
protected $sJournalUsx = 'https://www.tmrjournals.com/';
|
||
protected $aEmailConfig = [
|
||
|
||
'cite' => [
|
||
|
||
'email_subject' => 'Congratulations! Your article published in {#email_subject#} has recently been cited',
|
||
'email_content' => '
|
||
Dear {#author_name#},<br><br>
|
||
I hope this message finds you well! I would like to take this opportunity to extend my warmest congratulations to you. Your article, "{#article_name#}" has been cited in "{#journal_name#}"{#factor#}. This is an incredible achievement that truly reflects your outstanding contributions to the field.<br><br>
|
||
Once again, congratulations! I look forward to seeing more of your remarkable research in the future!<br><br>
|
||
Best regards,<br>
|
||
{#editorinchief#}<br>
|
||
{#journal_title#}<br>
|
||
Website:{#journal_usx#}<br>
|
||
X: @{#journal_sx#}Journals',
|
||
]
|
||
];
|
||
|
||
public function __construct(\think\Request $request = null)
|
||
{
|
||
parent::__construct($request);
|
||
}
|
||
|
||
/**
|
||
* 查询文章是否被引用 并发送邮件给通讯作者
|
||
*
|
||
* @return void
|
||
*/
|
||
public function articleCite(){
|
||
|
||
$aParam = $this->request->post();
|
||
|
||
$sDate = empty($aParam['date']) ? date('Y-m-d', strtotime('-1 day')) : $aParam['date'];
|
||
|
||
//接口请求地址
|
||
$sUrl = $this->sJournalUrl.'crontab/Cite/getList';
|
||
|
||
//请求接口获取数据
|
||
$aParam['date'] = $sDate;
|
||
$aResult = object_to_array(json_decode(myPost($sUrl, $aParam)));
|
||
$aContent = empty($aResult['data']) ? [] : $aResult['data'];
|
||
if(empty($aContent)){
|
||
$this->showMessage($aResult['msg'] ?? '接口异常',2);
|
||
exit;
|
||
}
|
||
|
||
//获取数据-文章引用数据
|
||
$aArticleCite = empty($aContent['article_cite']) ? [] : $aContent['article_cite'];
|
||
if(empty($aArticleCite)){
|
||
$this->showMessage('未查询到引用文章数据:'.$sDate,2);
|
||
exit;
|
||
}
|
||
$aCite = [];
|
||
foreach ($aArticleCite as $key => $value) {
|
||
$aCite[$value['article_id']][] = $value;
|
||
}
|
||
//获取数据-文章通讯作者数据
|
||
$aArticleAuthor = empty($aContent['article_author']) ? [] : $aContent['article_author'];
|
||
if(empty($aArticleAuthor)){
|
||
$this->showMessage('未查询到引用文章通讯作者数据:'.$sDate,2);
|
||
exit;
|
||
}
|
||
|
||
//获取数据-文章数据
|
||
$aArticle = empty($aContent['article']) ? [] : $aContent['article'];
|
||
//期刊数据
|
||
$aJournal = empty($aContent['journal']) ? [] : $aContent['journal'];
|
||
|
||
//查询发送人信息
|
||
$aEmail = array_column($aArticleAuthor, 'email');
|
||
$aWhere = ['email' => ['in',$aEmail]];
|
||
$aUser = Db::name('user')->where($aWhere)->column('email,realname,localname');
|
||
|
||
//查询邮件发送日志
|
||
$aArticleCiteId = array_column($aArticleCite, 'article_cite_id');
|
||
$aWhere = ['article_cite_id' => ['in',$aArticleCiteId],'is_success' => 1];
|
||
$aEmailLog = Db::name('email_article_cite')->field('article_cite_id,email')->where($aWhere)->select();
|
||
$aLog = [];
|
||
if(!empty($aEmailLog)){
|
||
foreach ($aEmailLog as $key => $value) {
|
||
$aLog[$value['email']][] = $value['article_cite_id'];
|
||
}
|
||
}
|
||
//邮件发送
|
||
//获取邮件模版
|
||
$aEmailCite= $this->aEmailConfig['cite'];
|
||
//邮件发送日志记录数组
|
||
$aEmailLog = [];
|
||
foreach ($aArticleAuthor as $key => $value) {
|
||
if(empty($value['email'])){//邮箱为空
|
||
continue;
|
||
}
|
||
$aArticleCite = empty($aCite[$value['article_id']]) ? [] : $aCite[$value['article_id']];
|
||
if(empty($aArticleCite)){
|
||
$this->showMessage('未查询到文章引用信息为空,文章ID:'.$value['article_id']."\n\n",2);
|
||
continue;
|
||
}
|
||
//数据组装-接收邮箱
|
||
// $email = 'tmr@tmrjournals.com';//$value['email'];
|
||
$email = $value['email'];
|
||
|
||
//用户信息
|
||
$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'];
|
||
|
||
//邮件日志
|
||
$aEmailLogInfo = empty($aLog[$email]) ? [] : $aLog[$email];
|
||
foreach ($aArticleCite as $val) {
|
||
if(in_array($val['article_cite_id'], $aEmailLogInfo)){
|
||
$this->showMessage('文章标题为:'.$val['article_name'].'邮箱账号为:'.$email."已发送过邮件\n\n",2);
|
||
continue;
|
||
}
|
||
//期刊数据
|
||
$aJournalInfo = empty($aJournal[$val['journal_id']]) ? [] : $aJournal[$val['journal_id']];
|
||
if(empty($aJournalInfo)){
|
||
$this->showMessage('未查询到文章标题为:'.$val['article_name'].'期刊信息'."\n\n",2);
|
||
continue;
|
||
}
|
||
//期刊标题
|
||
$from_name = empty($aJournalInfo['journal_title']) ? '' : $aJournalInfo['journal_title'];
|
||
//邮件标题
|
||
$title = str_replace('{#email_subject#}', $from_name, $aEmailCite['email_subject']);
|
||
|
||
//邮件内容
|
||
$aSearch = [
|
||
'{#author_name#}' => empty($realname) ? $email : $realname,
|
||
'{#article_name#}' => empty($val['article_name']) ? '' : $val['article_name'],//文章标题
|
||
'{#journal_name#}' => empty($val['journal_name']) ? '' : $val['journal_name'],//期刊名
|
||
'{#factor#}' => empty($val['factor']) ? '' : ' (Impact Factor: '.$val['factor'].')',//影响因子
|
||
'{#editorinchief#}' => empty($aJournalInfo['editorinchief']) ? '' : $aJournalInfo['editorinchief'],//总编辑
|
||
'{#journal_title#}' => $from_name,//期刊名
|
||
'{#journal_usx#}' => empty($aJournalInfo['usx']) ? '' : $this->sJournalUsx.$aJournalInfo['usx'],//官网地址
|
||
'{#journal_sx#}' => empty($aJournalInfo['sx']) ? '' : $aJournalInfo['sx'],//期刊sx
|
||
];
|
||
$content = str_replace(array_keys($aSearch), array_values($aSearch), $aEmailCite['email_content']);
|
||
//判断标题和内容是否为空
|
||
if(empty($title) || empty($content)){
|
||
$this->showMessage('未查询到文章标题为:'.$val['article_name'].'标题为'.$title.'为空或邮件内容为'.$content."为空\n\n",2);
|
||
continue;
|
||
}
|
||
|
||
//发送邮件
|
||
$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 = '失败';
|
||
if($iStatus == 1){
|
||
$iIsSuccess = 1;
|
||
$sMsg = '成功';
|
||
}
|
||
$this->showMessage('给邮箱:'.$email.'发送邮件'.$sMsg."\n\n",2);
|
||
$pre = Env::get('emailtemplete.pre');
|
||
$net = Env::get('emailtemplete.net');
|
||
$net1 = str_replace("{{email}}",trim($email),$net);
|
||
$content=$pre.$content.$net1;
|
||
$aEmailLog[] = ['article_id' => $val['article_id'],'article_cite_id' => $val['article_cite_id'],'email' => $email,'content' => $content,'create_tiem' => time(),'is_success' => $iIsSuccess,'journal_id' => $val['journal_id'],'journal_issn' => $aJournalInfo['issn']];
|
||
}
|
||
}
|
||
|
||
//插入日志记录
|
||
if(!empty($aEmailLog)){
|
||
$result = Db::name('email_article_cite')->insertAll($aEmailLog);
|
||
if($result === false){
|
||
$this->showMessage('邮件发送日志插入失败:'.json_encode($aEmailLog),2);
|
||
exit;
|
||
}
|
||
}
|
||
$this->showMessage('批量处理文章引用发送邮件通知通讯作者成功:'.$sDate."\n",1);
|
||
}
|
||
|
||
/**
|
||
*
|
||
* 格式化信息输出
|
||
*
|
||
* @access public
|
||
* @return void
|
||
* @author huangpu
|
||
* @date 2018.09.28
|
||
* @param $[message] [<显示信息>]
|
||
* @param $[status] [<输出信息1成功,2失败>]
|
||
*/
|
||
private function showMessage($message, $status = 1) {
|
||
if ($status == 1) {
|
||
echo "[SUCCESS]";
|
||
} else {
|
||
echo "[ERROR]";
|
||
}
|
||
echo date("Y-m-d H:i:s") . " " . $message . "\n";
|
||
}
|
||
}
|