From 18cd4d550d5203001ad0b0b350c051564f5b599a Mon Sep 17 00:00:00 2001 From: chengxl Date: Tue, 20 May 2025 11:45:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E7=AB=A0=E8=A2=AB=E5=BC=95=E7=94=A8?= =?UTF-8?q?=E5=AE=9A=E6=97=B6=E5=8F=91=E9=80=81=E9=82=AE=E4=BB=B6=E6=8F=90?= =?UTF-8?q?=E9=86=92=E7=BB=99=E9=80=9A=E8=AE=AF=E4=BD=9C=E8=80=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/api/controller/Cronmonitor.php | 202 ++++++++++++++++++++ application/command/articleCiteSendEmail.sh | 22 +++ 2 files changed, 224 insertions(+) create mode 100644 application/api/controller/Cronmonitor.php create mode 100755 application/command/articleCiteSendEmail.sh diff --git a/application/api/controller/Cronmonitor.php b/application/api/controller/Cronmonitor.php new file mode 100644 index 0000000..7d7d811 --- /dev/null +++ b/application/api/controller/Cronmonitor.php @@ -0,0 +1,202 @@ + [ + + 'email_subject' => 'Congratulations! Your article published in {#email_subject#} has recently been cited', + 'email_content' => ' + Dear {#author_name#},

+ 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.

+ Once again, congratulations! I look forward to seeing more of your remarkable research in the future!

+ Best regards,
+ {#editorinchief#}
+ {#journal_title#}
+ Website:{#journal_usx#}
+ 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']; + + //用户信息 + $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); + $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"; + } +} diff --git a/application/command/articleCiteSendEmail.sh b/application/command/articleCiteSendEmail.sh new file mode 100755 index 0000000..a74423c --- /dev/null +++ b/application/command/articleCiteSendEmail.sh @@ -0,0 +1,22 @@ +#!/bin/bash +# articleCiteSendEmail.sh +# 批量处理文章引用发送邮件给通讯作者 +# 调用接口获取前一天文章引用的记录 +# 此文件需要在crontab中配置每天【凌晨3点】运行一次 +# @author chengxiaoling +# @date 2025-05-20 + +# 基础配置 +DOMAIN="http://zmzm.tougao.dev.com/" # 项目域名 +ROUTE="/api/Cronmonitor/articleCite" # 控制器路由 +BASE_PATH=$(cd `dirname $0`; pwd) +# 如果日志目录不存在则创建 +logDir=${BASE_PATH}/log/$(date "+%Y")/$(date "+%m") +if [ ! -d $logDir ];then + mkdir -p $logDir +fi + +# 执行请求并记录日志 +curl "${DOMAIN}${ROUTE}" >> ${logDir}/articleCiteSendEmail_$(date "+%Y%m%d").log 2>&1 +# 添加时间戳 +echo "[$(date '+%Y-%m-%d %H:%M:%S')] 定时任务已执行" >> ${logDir}/articleCiteSendEmail_$(date "+%Y%m%d").log \ No newline at end of file