微信公众号对接接口调整

This commit is contained in:
chengxl
2025-05-29 13:49:07 +08:00
parent c288e446e2
commit 4758357eb0

View File

@@ -7,7 +7,7 @@ use app\common\QrCodeImage;
use app\common\Wechat; use app\common\Wechat;
use app\common\Material; use app\common\Material;
use think\Cache; use think\Cache;
use think\Queue;
/** /**
* @title 数据库接口 * @title 数据库接口
* @description 数据库接口 * @description 数据库接口
@@ -32,19 +32,18 @@ class Aiarticle extends Base
/**OPENAI相关配置----------end**/ /**OPENAI相关配置----------end**/
//数据表必填字段[ai_article] //数据表必填字段[ai_article]
protected $aAiFileds = ['article_id','title_english','title_chinese','journal_issn','covered','digest','research_result','content','highlights','discussion','prospect','research_background','discussion_results','research_method']; protected $aAiFileds = ['article_id','title_english','title_chinese','journal_issn','covered','digest','research_result','content','highlights','discussion','prospect','research_background','discussion_results','research_method','overview','summary'];
//期刊接口地址 //期刊接口地址
protected $sJournalUrl = 'http://journalapi.tmrjournals.com/public/index.php';//'http://zmzm.journal.dev.com'; // 'http://zmzm.journal.dev.com'; // 'http://journalapi.tmrjournals.com/public/index.php';//'http://zmzm.journal.dev.com'; // protected $sJournalUrl = 'http://journalapi.tmrjournals.com/public/index.php';//'http://zmzm.journal.dev.com'; //
//期刊官网 //期刊官网
protected $sJournalUsx = 'https://www.tmrjournals.com'; protected $sJournalUsx = 'https://www.tmrjournals.com';
//投稿系统 //投稿系统
protected $sSubmissionUrl = 'https://submission.tmrjournals.com/'; protected $sSubmissionUrl = 'https://submission.tmrjournals.com/';
protected $aOpenAiAsk = [ protected $aOpenAiAsk = [
1 => '"将以下内容翻译为中文,仅返回翻译结果,不要解释:\n {#content#}"', 1 => '"将以下内容翻译为中文,仅返回翻译结果,不要解释:\n {#content#}"',
2 => ' 'default' => '
**核心要求** **核心要求**
注意:生成内容会以微信公众号的形式推送给学术专家,针对以下九点生成的内容要提高内容的连贯性,逻辑性,科学性,避免让学术专家产生疑问
1 内容涵盖哪些学科及方法请罗列 1 内容涵盖哪些学科及方法请罗列
2 学术规范翻译并提炼摘要更强调逻辑性、科学术语准确性和表达的严谨性并且不需要分点展示字数小于200字以便更方便读者阅读 2 学术规范翻译并提炼摘要更强调逻辑性、科学术语准确性和表达的严谨性并且不需要分点展示字数小于200字以便更方便读者阅读
3 研究背景提炼,大于500字 3 研究背景提炼,大于500字
@@ -62,7 +61,23 @@ class Aiarticle extends Base
**输出格式** **输出格式**
中文格式[英文简写忽略首字母大写] 中文格式[英文简写忽略首字母大写]
格式内容 格式内容
```json{"covered": "【总字数<=100】", "digest": "【总字数<=500】", "research_background": "【总字数>200】", "discussion_results": "【总字数>450】","research_method" => "【总字数>300】", "prospect": "", "highlights": "", "title_chinese": "", "content": "", }' ```json{"covered": "【总字数<=100】", "digest": "【总字数<=500】", "research_background": "【总字数>200】", "discussion_results": "【总字数>450】","research_method" => "【总字数>300】", "prospect": "", "highlights": "", "title_chinese": "", "content": ""}',
'Review' => '
**核心要求**
1 内容涵盖哪些学科及方法请罗列
2 学术规范翻译并提炼文章概述,更强调逻辑性、科学术语准确性和表达的严谨性,并且不需要分点展示,大于1000字
3 针对文章进行简单总结大于300字
4 请将标题翻译成中文【内容需自然流畅、口语化、连贯性、学术性】
{#title_chinese#}
5 请将文章内容翻译成中文【内容需自然流畅、口语化、连贯性、学术性】
{#content#}
**稿件关键信息**
- 稿件简介:{#abstract#}
- 稿件内容:{#content#}
**输出格式**
中文格式[英文简写忽略首字母大写]
格式内容
```json{"covered": "【总字数<=100】", "overview": "【总字数>=1000】", "summary": "【总字数>=3500】", "title_chinese": "", "content": ""}'
]; ];
//文章图片icon地址 //文章图片icon地址
@@ -103,9 +118,22 @@ class Aiarticle extends Base
$data = [ $data = [
'model' => $model, 'model' => $model,
'messages' => $messages, // 'messages' => $messages,
'temperature' => 0, 'messages' => [
'top_p' => 1, // [
// 'role' => 'system',
// 'content' => '你是一名资深医学期刊编辑,需遵循医学原则回答以下问题。回答的内容会以微信公众号的形式推送给学术专家,要提高内容的连贯性,逻辑性,科学性,避免让学术专家产生质疑'
// ],
[
'role' => 'user',
'content' => $messages
]
],
'temperature' => 0.3,// 降低随机性0-10为最确定
// 'top_p' => 9, // 控制生成多样性
// 'max_tokens' => 1500, // 防止答案截断
// 'frequency_penalty' => 0.5, // 减少重复内容
// 'presence_penalty' => 0.2 // 鼓励提及新概念
]; ];
$oCurl = curl_init(); $oCurl = curl_init();
@@ -143,23 +171,22 @@ class Aiarticle extends Base
* @param aSearch array 模版内容 * @param aSearch array 模版内容
* @param iTemplateId 模版ID * @param iTemplateId 模版ID
*/ */
private function _createContentForOpenAI($aSearch = [],$iTemplateId = 0){ private function _createContentForOpenAI($aSearch = [],$sArticleType = ''){
if(empty($aSearch) || empty($iTemplateId)){ if(empty($aSearch) || empty($sArticleType)){
return json_encode(array('status' => 2,'msg' => 'Please select a template or enter the content you want to consult'.$this->sError)); return json_encode(array('status' => 2,'msg' => 'Please select a template or enter the content you want to consult'.$this->sError));
} }
//组织参数 //组织参数
$sTemplate = $this->aOpenAiAsk[$iTemplateId]; if(in_array($sArticleType, ['Mini Review','Review'])){
$sArticleType = 'Review';
}else{
$sArticleType = 'default';
}
$sTemplate = $this->aOpenAiAsk[$sArticleType];
$sTemplate = str_replace(array_keys($aSearch), array_values($aSearch), $sTemplate); $sTemplate = str_replace(array_keys($aSearch), array_values($aSearch), $sTemplate);
$messages = [
[
'role' => 'user', //角色platform平台;developer:开发者user:用户;guideline:模型规范“指南”部分:连接https://model-spec.openai.com/2025-02-12.html#chain_of_command
'content' => $sTemplate
]
];
//请求接口 //请求接口
$sModel = empty($aParam['api_model']) ? 'gpt-4o' : $aParam['api_model']; $sModel = empty($aParam['api_model']) ? 'gpt-4o' : $aParam['api_model'];
$result = $this->curlOpenAI($messages,$sModel); $result = $this->curlOpenAI($sTemplate,$sModel);
if($result == FALSE){ if($result == FALSE){
return json_encode(array('status' => 2,'msg' => 'Interface request failed'.$this->sError)); return json_encode(array('status' => 2,'msg' => 'Interface request failed'.$this->sError));
} }
@@ -202,6 +229,8 @@ class Aiarticle extends Base
return json_encode(array('status' => 2,'msg' => 'Please select an article' )); return json_encode(array('status' => 2,'msg' => 'Please select an article' ));
} }
//媒体类型
$iMediaType = empty($aParam['media_type']) ? 1 : $aParam['media_type'];
//获取文章是否生成AI内容 //获取文章是否生成AI内容
$aResult = json_decode($this->getArticle($iArticleId),true); $aResult = json_decode($this->getArticle($iArticleId),true);
$iStatus = empty($aResult['status']) ? 0 : $aResult['status']; $iStatus = empty($aResult['status']) ? 0 : $aResult['status'];
@@ -212,13 +241,17 @@ class Aiarticle extends Base
$aArticleContent = empty($aResult['data']) ? [] : $aResult['data']; $aArticleContent = empty($aResult['data']) ? [] : $aResult['data'];
//文章数据 //文章数据
$aArticle = empty($aArticleContent['article']) ? [] : $aArticleContent['article']; $aArticle = empty($aArticleContent['article']) ? [] : $aArticleContent['article'];
if(empty($aArticle)){ if(empty($aArticle)){
return json_encode(['status' => 3,'msg' => 'The content of the article is empty']); return json_encode(['status' => 3,'msg' => 'The content of the article is empty']);
} }
//期刊数据 //期刊数据
$aJournal = empty($aArticleContent['journal']) ? [] : $aArticleContent['journal']; $aJournal = empty($aArticleContent['journal']) ? [] : $aArticleContent['journal'];
if(empty($aJournal)){
return json_encode(['status' => 3,'msg' => 'The journal to which the article belongs does not exist or has been closed']);
}
// Queue::later(10,'app\api\job\WechatMaterial@fire', ['article_id' => $iArticleId], 'uploadMaterial');
// exit;
//查询AI内容是否生成 //查询AI内容是否生成
$aAiArticle = json_decode($this->getAiArticle(['article_id' => $iArticleId]),true); $aAiArticle = json_decode($this->getAiArticle(['article_id' => $iArticleId]),true);
$aAiArticleContent = empty($aAiArticle['data']) ? [] : $aAiArticle['data']; $aAiArticleContent = empty($aAiArticle['data']) ? [] : $aAiArticle['data'];
@@ -228,14 +261,13 @@ class Aiarticle extends Base
if(empty($aAiArticle)){ if(empty($aAiArticle)){
//插入t_ai_article数据 //插入t_ai_article数据
$sIssn = empty($aJournal['issn']) ? '' : $aJournal['issn']; $sIssn = empty($aJournal['issn']) ? '' : $aJournal['issn'];
$aInsert = ['title_english' => $aArticle['title'],'article_id' => $iArticleId,'create_time' => time(),'journal_issn' => $sIssn,'journal_id' => $aArticle['journal_id']]; $aInsert = ['title_english' => $aArticle['title'],'article_id' => $iArticleId,'create_time' => time(),'journal_issn' => $sIssn,'journal_id' => $aArticle['journal_id'],'article_type' => $aArticle['type'],'media_type' => $iMediaType,'journal_issn' => empty($aJournal['issn']) ? '' :$aJournal['issn']];
$iId = Db::name('ai_article')->insertGetId($aInsert); $iId = Db::name('ai_article')->insertGetId($aInsert);
if($iId === false){ if($iId === false){
return json_encode(['status' => 4,'msg' => 'Data insertion failed']); return json_encode(['status' => 4,'msg' => 'Data insertion failed']);
} }
$aAiArticle = array_merge(['ai_article_id' => $iId,'article_id' => $iArticleId,'is_generate' => 2],$aInsert); $aAiArticle = array_merge(['ai_article_id' => $iId,'article_id' => $iArticleId,'is_generate' => 2],$aInsert);
} }
//判断是否生成 //判断是否生成
if(!empty($aAiArticle['is_generate']) && $aAiArticle['is_generate'] == 1){ if(!empty($aAiArticle['is_generate']) && $aAiArticle['is_generate'] == 1){
return json_encode(['status' => 5,'msg' => 'The data has been generated, please proceed with the next steps']); return json_encode(['status' => 5,'msg' => 'The data has been generated, please proceed with the next steps']);
@@ -283,7 +315,8 @@ class Aiarticle extends Base
$aSearch['{#journal_content#}'] = trim(trim($aSearch['{#journal_content#}'],'.'),'。'); $aSearch['{#journal_content#}'] = trim(trim($aSearch['{#journal_content#}'],'.'),'。');
//写入Redis 标识已经开始请求OPENAI处理 //写入Redis 标识已经开始请求OPENAI处理
$aResult = $this->_createContentForOpenAI($aSearch,2); $sArticleType = empty($aArticle['type']) ? 'default' : $aArticle['type'];
$aResult = $this->_createContentForOpenAI($aSearch,$sArticleType);
$aResult = json_decode($aResult,true); $aResult = json_decode($aResult,true);
if($aResult['status'] != 1){ if($aResult['status'] != 1){
return json_encode($aResult); return json_encode($aResult);
@@ -521,6 +554,9 @@ class Aiarticle extends Base
if(!isset($aParam[$val])){ if(!isset($aParam[$val])){
continue; continue;
} }
if(is_array($aParam[$val])){
$aParam[$val] = implode(";",$aParam[$val]);
}
$aUpdateParam[$val] = empty($aParam[$val]) ? '' : addslashes(strip_tags($aParam[$val])); $aUpdateParam[$val] = empty($aParam[$val]) ? '' : addslashes(strip_tags($aParam[$val]));
} }
if(empty($aUpdateParam)){ if(empty($aUpdateParam)){
@@ -616,8 +652,7 @@ class Aiarticle extends Base
//文章ID //文章ID
$iArticleId = empty($aParam['article_id']) ? '' : $aParam['article_id']; $iArticleId = empty($aParam['article_id']) ? '' : $aParam['article_id'];
//模版ID
$iTemplateId = empty($aParam['template_id']) ? '' : $aParam['template_id'];
//模版ID //模版ID
$iIsSync = empty($aParam['is_sync']) ? 2 : $aParam['is_sync']; $iIsSync = empty($aParam['is_sync']) ? 2 : $aParam['is_sync'];
@@ -625,9 +660,6 @@ class Aiarticle extends Base
if(empty($iArticleId)){ if(empty($iArticleId)){
return json_encode(['status' => 2, 'msg' => 'Please select an article']); return json_encode(['status' => 2, 'msg' => 'Please select an article']);
} }
if(empty($iTemplateId)){
return json_encode(['status' => 2, 'msg' => 'Please select a template']);
}
//获取AI生成文章内容 //获取AI生成文章内容
$aAiContent = json_decode($this->getAiArticle(['article_id' => $iArticleId,'is_select_author' => 1]),true); $aAiContent = json_decode($this->getAiArticle(['article_id' => $iArticleId,'is_select_author' => 1]),true);
@@ -637,8 +669,6 @@ class Aiarticle extends Base
if(empty($aAiArticle)){ if(empty($aAiArticle)){
return json_encode(['status' => 3, 'msg' => 'The article content of WeChat official account has not been generated']); return json_encode(['status' => 3, 'msg' => 'The article content of WeChat official account has not been generated']);
} }
//模版ID
$iTemplateId = empty($iTemplateId) ? $aAiArticle['template_id'] : $iTemplateId;
//获取文章内容 //获取文章内容
$aResult = json_decode($this->getArticle($iArticleId),true); $aResult = json_decode($this->getArticle($iArticleId),true);
@@ -668,7 +698,10 @@ class Aiarticle extends Base
//文章图片 //文章图片
$sArticleIcon = trim($this->sJournalUsx,'/').$this->sArticleIcon.$aArticle['article_icon']; $sArticleIcon = trim($this->sJournalUsx,'/').$this->sArticleIcon.$aArticle['article_icon'];
//期刊图片 //期刊图片
$sJournalIcon = trim($this->sJournalUsx,'/').$this->sJournalIcon.$aJournal['journal_icon']; $sJournalStageIcon = empty($aJournalStage['stage_icon']) ? '' : '/public/'.$aJournalStage['stage_icon'];
$sJournalIcon = empty($aJournal['journal_icon']) ? '' : $this->sJournalIcon.$aJournal['journal_icon'];
$sJournalIcon = empty($sJournalStageIcon) ? $sJournalIcon : $sJournalStageIcon;
$sJournalIcon = trim($this->sJournalUsx,'/').$sJournalIcon;
//期刊编辑二维码 //期刊编辑二维码
$sEditorQrcode = empty($aJournal['editor_qrcode']) ? '' : $aJournal['editor_qrcode']; $sEditorQrcode = empty($aJournal['editor_qrcode']) ? '' : $aJournal['editor_qrcode'];
@@ -676,10 +709,11 @@ class Aiarticle extends Base
//获取期刊二维码 //获取期刊二维码
$oMaterial = new Material; $oMaterial = new Material;
$aJournal['icon'] = $sJournalIcon;
$aJournal['journal_stage_id'] = empty($aArticle['journal_stage_id']) ? 0 : $aArticle['journal_stage_id'];
$aJournalQrCode = $oMaterial->createJournalQrCode($aJournal); $aJournalQrCode = $oMaterial->createJournalQrCode($aJournal);
$sJournalCode = empty($aJournalQrCode['qrcode_url']) ? '' : $this->sSubmissionUrl.'public/qrcode/'.$aJournalQrCode['qrcode_url']; $sJournalCode = empty($aJournalQrCode['qrcode_url']) ? '' : $this->sSubmissionUrl.'public/qrcode/'.$aJournalQrCode['qrcode_url'];
$aAiArticle['journal_qrcode'] = $sJournalCode; $aAiArticle['journal_qrcode'] = $sJournalCode;
//文章二维码 //文章二维码
$aArticleQrCode = $oMaterial->createArticleQrCode($aArticle); $aArticleQrCode = $oMaterial->createArticleQrCode($aArticle);
$sArticleCode = empty($aArticleQrCode['qrcode_url']) ? '' : $this->sSubmissionUrl.'public/qrcode/'.$aArticleQrCode['qrcode_url']; $sArticleCode = empty($aArticleQrCode['qrcode_url']) ? '' : $this->sSubmissionUrl.'public/qrcode/'.$aArticleQrCode['qrcode_url'];
@@ -761,11 +795,19 @@ class Aiarticle extends Base
$aAiArticle += $aJournal; $aAiArticle += $aJournal;
//获取模版信息 //获取模版信息
$sTemplatePath = ROOT_PATH."public/wechatTemplate/template_".$iTemplateId; $sJournalIssn = empty($aJournal['issn']) ? 'default' : $aJournal['issn'];//期刊issn
$sArticleType = empty($aArticle['type']) ? 'default' : $aArticle['type'];//文章类型
if(in_array($sArticleType, ['review','Mini Review'])){
$sArticleType = "Review";
}
$sTemplatePath = ROOT_PATH."public/ArticleTemplate/".$sJournalIssn."/".$sArticleType;
if (!is_dir($sTemplatePath)) {//默认模版信息
$sTemplatePath = ROOT_PATH."public/ArticleTemplate/default/".$sArticleType;
}
//index模版目录 //index模版目录
$sTemplateIndexPath = $sTemplatePath.'/index.html'; $sTemplateIndexPath = $sTemplatePath.'/index.html';
// 验证文件有效性 // 验证文件有效性
if (!file_exists($sTemplateIndexPath)) { if (!file_exists($sTemplateIndexPath)) {
return json_encode(['status' => 4, 'msg' => 'Article template does not exist']); return json_encode(['status' => 4, 'msg' => 'Article template does not exist']);
@@ -785,19 +827,19 @@ class Aiarticle extends Base
} }
//处理通讯作者信息数据 //处理通讯作者信息数据
$aAuthorInfo = json_decode($this->dealTemplateAuthor($aAuthor,$iTemplateId),true); $aAuthorInfo = json_decode($this->dealTemplateAuthor($aAuthor,$sTemplatePath),true);
$aAuthorInfo = empty($aAuthorInfo['data']) ? [] : $aAuthorInfo['data']; $aAuthorInfo = empty($aAuthorInfo['data']) ? [] : $aAuthorInfo['data'];
$aSearch['{###author_summary###}'] = empty($aAuthorInfo['author_info']) ? '' : $aAuthorInfo['author_info']; $aSearch['{###author_summary###}'] = empty($aAuthorInfo['author_info']) ? '' : $aAuthorInfo['author_info'];
$aSearch['{###author_string###}'] = empty($aAuthorInfo['author_string']) ? '' : ',通讯作者为'.$aAuthorInfo['author_string']; $aSearch['{###author_string###}'] = empty($aAuthorInfo['author_string']) ? '' : ',通讯作者为'.$aAuthorInfo['author_string'];
//处理往期推荐数据 //处理往期推荐数据
$aPreviousRecommend = json_decode($this->dealTemplatePreviousRecommend($aArticle,$iTemplateId),true); $aPreviousRecommend = json_decode($this->dealTemplatePreviousRecommend($aArticle,$sTemplatePath),true);
$aSearch['{###previous_recommend_summary###}'] = empty($aPreviousRecommend['data']) ? '' : $aPreviousRecommend['data']; $aSearch['{###previous_recommend_summary###}'] = empty($aPreviousRecommend['data']) ? '' : $aPreviousRecommend['data'];
//处理期刊主题数据 //处理期刊主题数据
$sJournalTopic = empty($aJournal['journal_topic']) ? '' : $aJournal['journal_topic']; $sJournalTopic = empty($aJournal['journal_topic']) ? '' : $aJournal['journal_topic'];
$aTopic = json_decode($this->dealTemplateTopic($sJournalTopic,$iTemplateId),true); $aTopic = json_decode($this->dealTemplateTopic($sJournalTopic,$sTemplatePath),true);
$aSearch['{###topic_name_summary###}'] = empty($aTopic['data']) ? '' : $aTopic['data']; $aSearch['{###topic_name_summary###}'] = empty($aTopic['data']) ? '' : $aTopic['data'];
//公众号名字处理 //公众号名字处理
@@ -832,11 +874,29 @@ class Aiarticle extends Base
} }
$aSearch['{###jabbr###}'] = trim(trim($aSearch['{###jabbr###}'],'.'),'。'); $aSearch['{###jabbr###}'] = trim(trim($aSearch['{###jabbr###}'],'.'),'。');
$aSearch['{###free_year###}'] = date('Y'); // $aSearch['{###free_year###}'] = date('Y');
$aSearch['{###covered###}'] = '内容涵盖'.str_replace('研究涵盖', '', $aSearch['{###covered###}']); // $aSearch['{###covered###}'] = '内容涵盖'.str_replace('研究涵盖', '', $aSearch['{###covered###}']);
if(!empty($aSearch['{###covered###}']) && strpos($aSearch['{###covered###}'], '涵盖') === false){
$aSearch['{###covered###}'] = '内容涵盖'.str_replace('研究涵盖', '', $aSearch['{###covered###}']);
}
//期刊封面
$aSearch['{###journal_icon_info###}'] = $aSearch['{###journal_icon_details###}'] = '';
if(!empty($sJournalIcon)){
$sTemplateJournalInfo = $sTemplatePath.'/journal_icon_info.html';
if(file_exists($sTemplateJournalInfo) && is_readable($sTemplateJournalInfo)) {
$sTemplateJournalInfo = file_get_contents($sTemplateJournalInfo);
$aSearch['{###journal_icon_info###}'] = str_replace('{###journal_icon###}', $aAiArticle['journal_icon']??'', $sTemplateJournalInfo);
}
$sTemplateJournalInfo = $sTemplatePath.'/journal_icon_details.html';
if(file_exists($sTemplateJournalInfo) && is_readable($sTemplateJournalInfo)) {
$sTemplateJournalInfo = file_get_contents($sTemplateJournalInfo);
$aSearch['{###journal_icon_details###}'] = str_replace('{###journal_icon###}', $aAiArticle['journal_icon']??'', $sTemplateJournalInfo);
}
}
$sTemplate = str_replace(array_keys($aSearch), array_values($aSearch), $sTemplate); $sTemplate = str_replace(array_keys($aSearch), array_values($aSearch), $sTemplate);
file_put_contents(ROOT_PATH.'public/template_info.html', $sTemplate); // $this->writeFile($sTemplate,ROOT_PATH.'public/template_info.html');
return json_encode(['status' => 1, 'msg' => '模版生成成功','data' => ['template' => $sTemplate]]); return json_encode(['status' => 1, 'msg' => '模版生成成功','data' => ['template' => $sTemplate]]);
} }
@@ -844,22 +904,22 @@ class Aiarticle extends Base
* 处理模版通讯作者 * 处理模版通讯作者
* @param article_id 文章ID * @param article_id 文章ID
*/ */
private function dealTemplateAuthor($aAuthor = [],$iTemplateId = 0){ private function dealTemplateAuthor($aAuthor = [],$sTemplatePath = ''){
//必填字段判断 //必填字段判断
if(empty($aAuthor) || empty($iTemplateId)){ if(empty($aAuthor) || empty($sTemplatePath)){
return json_encode(['status' => 2,'msg' => 'Corresponding author or template does not exist']); return json_encode(['status' => 2,'msg' => 'Corresponding author or template does not exist']);
} }
//获取通讯作者模版 //获取通讯作者模版
$sAuthorTemplatePath = ROOT_PATH."public/wechatTemplate/template_".$iTemplateId.'/author.html'; $sAuthorTemplatePathAuthor = $sTemplatePath.'/author.html';
if (!file_exists($sAuthorTemplatePath)) { if (!file_exists($sAuthorTemplatePathAuthor)) {
return json_encode(['status' => 3, 'msg' => 'Corresponding author template does not exist','data' => '']); return json_encode(['status' => 3, 'msg' => 'Corresponding author template does not exist','data' => '']);
} }
if (!is_readable($sAuthorTemplatePath)) { if (!is_readable($sAuthorTemplatePathAuthor)) {
return json_encode(['status' => 4, 'msg' => 'The corresponding author template is unreadable','data' => '']); return json_encode(['status' => 4, 'msg' => 'The corresponding author template is unreadable','data' => '']);
} }
$sAuthorTemplate = file_get_contents($sAuthorTemplatePath); $sAuthorTemplate = file_get_contents($sAuthorTemplatePathAuthor);
//处理模版变量 //处理模版变量
$aAuthorSerachInfo = []; $aAuthorSerachInfo = [];
@@ -869,7 +929,7 @@ class Aiarticle extends Base
//请求AI翻译 //请求AI翻译
$sContent = ''; $sContent = '';
$aSearch['{#content#}'] = json_encode($aAuthor); $aSearch['{#content#}'] = json_encode($aAuthor);
$aResult = json_decode($this->_createContentForOpenAI($aSearch,1),true); // $aResult = json_decode($this->_createContentForOpenAI($aSearch,1),true);
$aContent = empty($aResult['data']) ? [] : array_column($aResult['data'], null,'email'); $aContent = empty($aResult['data']) ? [] : array_column($aResult['data'], null,'email');
foreach ($aAuthor as $key => $value) { foreach ($aAuthor as $key => $value) {
@@ -916,7 +976,7 @@ class Aiarticle extends Base
//通讯作者列表 //通讯作者列表
//获取通讯作者汇总模版 //获取通讯作者汇总模版
$sAuthorTemplatePath = ROOT_PATH."public/wechatTemplate/template_".$iTemplateId.'/author_summary.html'; $sAuthorTemplatePath = $sTemplatePath.'/author_summary.html';
if (!file_exists($sAuthorTemplatePath)) { if (!file_exists($sAuthorTemplatePath)) {
return json_encode(['status' => 3, 'msg' => 'Corresponding author template does not exist','data' => '']); return json_encode(['status' => 3, 'msg' => 'Corresponding author template does not exist','data' => '']);
} }
@@ -937,8 +997,8 @@ class Aiarticle extends Base
* 处理期刊主题模版 * 处理期刊主题模版
* @param article_id 文章ID * @param article_id 文章ID
*/ */
private function dealTemplateTopic($sTopic = '',$iTemplateId = 0){ private function dealTemplateTopic($sTopic = '',$sTemplatePath = ''){
if(empty($iTemplateId)){ if(empty($sTemplatePath)){
return json_encode(['status' => 2,'template does not exist']); return json_encode(['status' => 2,'template does not exist']);
} }
if(empty($sTopic)){ if(empty($sTopic)){
@@ -946,14 +1006,14 @@ class Aiarticle extends Base
} }
//获取主题模版 //获取主题模版
$sTemplatePath = ROOT_PATH."public/wechatTemplate/template_".$iTemplateId.'/topic.html'; $sTemplatePathTopic = $sTemplatePath.'/topic.html';
if (!file_exists($sTemplatePath)) { if (!file_exists($sTemplatePathTopic)) {
return json_encode(['status' => 4, 'msg' => 'Corresponding author template does not exist']); return json_encode(['status' => 4, 'msg' => 'Corresponding author template does not exist']);
} }
if (!is_readable($sTemplatePath)) { if (!is_readable($sTemplatePathTopic)) {
return json_encode(['status' => 5, 'msg' => 'The corresponding author template is unreadable']); return json_encode(['status' => 5, 'msg' => 'The corresponding author template is unreadable']);
} }
$sTemplate = file_get_contents($sTemplatePath); $sTemplate = file_get_contents($sTemplatePathTopic);
//数据处理 //数据处理
$sTopic = trim($sTopic); $sTopic = trim($sTopic);
@@ -967,7 +1027,7 @@ class Aiarticle extends Base
} }
//获取期刊主题汇总模版 //获取期刊主题汇总模版
$sTemplatePath = ROOT_PATH."public/wechatTemplate/template_".$iTemplateId.'/topic_summary.html'; $sTemplatePath = $sTemplatePath.'/topic_summary.html';
if (!file_exists($sTemplatePath)) { if (!file_exists($sTemplatePath)) {
return json_encode(['status' => 4, 'msg' => 'Corresponding author template does not exist']); return json_encode(['status' => 4, 'msg' => 'Corresponding author template does not exist']);
} }
@@ -987,20 +1047,20 @@ class Aiarticle extends Base
* 处理往期推荐 * 处理往期推荐
* @param article_id 文章ID * @param article_id 文章ID
*/ */
private function dealTemplatePreviousRecommend($aArticle=[],$iTemplateId = 0){ private function dealTemplatePreviousRecommend($aArticle=[],$sTemplatePath = ''){
if(empty($iTemplateId)){ if(empty($sTemplatePath)){
return json_encode(['status' => 2,'template does not exist']); return json_encode(['status' => 2,'template does not exist']);
} }
//获取往期推荐模版 //获取往期推荐模版
$sTemplatePath = ROOT_PATH."public/wechatTemplate/template_".$iTemplateId.'/previous_recommend.html'; $sTemplatePathRecommend = $sTemplatePath.'/previous_recommend.html';
if (!file_exists($sTemplatePath)) { if (!file_exists($sTemplatePathRecommend)) {
return json_encode(['status' => 3, 'msg' => 'Corresponding author template does not exist']); return json_encode(['status' => 3, 'msg' => 'Corresponding author template does not exist']);
} }
if (!is_readable($sTemplatePath)) { if (!is_readable($sTemplatePathRecommend)) {
return json_encode(['status' => 4, 'msg' => 'The corresponding author template is unreadable']); return json_encode(['status' => 4, 'msg' => 'The corresponding author template is unreadable']);
} }
$sTemplate = file_get_contents($sTemplatePath); $sTemplate = file_get_contents($sTemplatePathRecommend);
//判断是否关联文章 //判断是否关联文章
if(empty($aArticle['related'])){ if(empty($aArticle['related'])){
@@ -1016,7 +1076,7 @@ class Aiarticle extends Base
$aId = array_column($aRecommend, 'article_id'); $aId = array_column($aRecommend, 'article_id');
//查询是否推送到微信公众号并且已发布 //查询是否推送到微信公众号并且已发布
$aWhere['template_id'] = $iTemplateId;//,'is_publish' => 1 $aWhere['is_publish'] = 1;
$aWechatArticle = Db::name('ai_wechat_article')->field('article_id,media_url')->where($aWhere)->select(); $aWechatArticle = Db::name('ai_wechat_article')->field('article_id,media_url')->where($aWhere)->select();
if(empty($aWechatArticle)){ if(empty($aWechatArticle)){
return json_encode(['status' => 6, 'msg' => 'No article published on WeChat official account was found']); return json_encode(['status' => 6, 'msg' => 'No article published on WeChat official account was found']);
@@ -1034,7 +1094,7 @@ class Aiarticle extends Base
} }
//获取往期推荐汇总模版 //获取往期推荐汇总模版
$sTemplatePath = ROOT_PATH."public/wechatTemplate/template_".$iTemplateId.'/previous_recommend_summary.html'; $sTemplatePath = $sTemplatePath.'/previous_recommend_summary.html';
if (!file_exists($sTemplatePath)) { if (!file_exists($sTemplatePath)) {
return json_encode(['status' => 3, 'msg' => 'Corresponding author template does not exist']); return json_encode(['status' => 3, 'msg' => 'Corresponding author template does not exist']);
} }
@@ -1062,16 +1122,12 @@ class Aiarticle extends Base
//文章ID //文章ID
$iArticleId = empty($aParam['article_id'] ) ? '' : $aParam['article_id']; $iArticleId = empty($aParam['article_id'] ) ? '' : $aParam['article_id'];
//模版ID
$iTemplateId = empty($aParam['template_id']) ? '' : $aParam['template_id'];
//必填参数验证 //必填参数验证
if(empty($iArticleId)){ if(empty($iArticleId)){
return json_encode(['status' => 2, 'msg' => 'Please select an article']); return json_encode(['status' => 2, 'msg' => 'Please select an article']);
} }
if(empty($iTemplateId)){
return json_encode(['status' => 2, 'msg' => 'Please select a template']);
}
//判断文章类型 //判断文章类型
$article_type = empty($aParam['article_type']) ? 'news' : $aParam['article_type']; $article_type = empty($aParam['article_type']) ? 'news' : $aParam['article_type'];
if(!in_array($article_type, ['news','newspic'])){ if(!in_array($article_type, ['news','newspic'])){
@@ -1079,7 +1135,7 @@ class Aiarticle extends Base
} }
//查询文章是否存在 //查询文章是否存在
$aWhere = ['article_id' => $iArticleId,'is_delete' => 2]; $aWhere = ['article_id' => $iArticleId,'is_delete' => 2];
$aAiArticle = Db::name('ai_article')->field('title_chinese as title,author,journal_issn,digest')->where($aWhere)->find(); $aAiArticle = Db::name('ai_article')->field('ai_article_id,title_chinese as title,journal_issn,digest,article_type')->where($aWhere)->find();
if(empty($aAiArticle)){ if(empty($aAiArticle)){
return json_encode(['status' => 3, 'msg' => 'The article does not exist']); return json_encode(['status' => 3, 'msg' => 'The article does not exist']);
} }
@@ -1087,9 +1143,17 @@ class Aiarticle extends Base
if(empty($sWechatId)){ if(empty($sWechatId)){
return json_encode(['status' => 2, 'msg' => 'Please select the WeChat official account to push']); return json_encode(['status' => 2, 'msg' => 'Please select the WeChat official account to push']);
} }
//获取模版
$sTemplate = empty($aAiArticle['article_type']) ? 'default' : $aAiArticle['article_type'];//文章类型
if(in_array($sTemplate, ['review','Mini Review'])){
$sTemplate = "Review";
}
//查询该模版是否推送到微信公众号 //查询该模版是否推送到微信公众号
$aWhere['template_id'] = $iTemplateId;
$aWhere['wechat_id'] = $sWechatId; $aWhere['wechat_id'] = $sWechatId;
$aWhere['template_id'] = $sTemplate;
$aWhere['wechat_id'] = $aAiArticle['journal_issn'];
$aWechatArticle = Db::name('ai_wechat_article')->where($aWhere)->find(); $aWechatArticle = Db::name('ai_wechat_article')->where($aWhere)->find();
if(!empty($aWechatArticle)){ if(!empty($aWechatArticle)){
return json_encode(['status' => 4, 'msg' => 'Already uploaded to draft box']); return json_encode(['status' => 4, 'msg' => 'Already uploaded to draft box']);
@@ -1123,7 +1187,7 @@ class Aiarticle extends Base
} }
$aParam += $aAiArticle; $aParam += $aAiArticle;
//获取模版生成内容 //获取模版生成内容
$aTemplateParam = ['article_id' => $iArticleId,'template_id' => $iTemplateId,'is_sync' => 1,'wechat_id' => $sWechatId]; $aTemplateParam = ['article_id' => $iArticleId,'template_id' => $sTemplate,'is_sync' => 1,'wechat_id' => $sWechatId];
$aTemplateParam += $aJournalInfo; $aTemplateParam += $aJournalInfo;
$aResult = json_decode($this->getTemplateContent($aTemplateParam),true); $aResult = json_decode($this->getTemplateContent($aTemplateParam),true);
$sMsg = empty($aResult['msg']) ? 'The content is empty' : $aResult['msg']; $sMsg = empty($aResult['msg']) ? 'The content is empty' : $aResult['msg'];
@@ -1134,7 +1198,6 @@ class Aiarticle extends Base
//请求微信公众号接口推送 //请求微信公众号接口推送
$aParam['content'] = $aData['template']; $aParam['content'] = $aData['template'];
$aParam['template_id'] = $iTemplateId;
$aParam += $aJournalInfo; $aParam += $aJournalInfo;
$oWechat = new Wechat; $oWechat = new Wechat;
$aResult = json_decode($oWechat->addDraft($aParam),true); $aResult = json_decode($oWechat->addDraft($aParam),true);
@@ -1143,7 +1206,7 @@ class Aiarticle extends Base
return json_encode($aResult); return json_encode($aResult);
} }
//插入记录表 //插入记录表
$aParam = ['media_id' => $aData['media_id'],'update_time' => time(),'template_id' => $iTemplateId,'template_content' => $aParam['content'],'article_id' => $iArticleId,'article_type' => $article_type,'need_open_comment' => $aParam['need_open_comment'],'only_fans_can_comment' => $aParam['only_fans_can_comment'],'template_id' => $iTemplateId,'create_time' => time(),'wechat_id' => $sWechatId]; $aParam = ['media_id' => $aData['media_id'],'update_time' => time(),'template_id' => $sTemplate,'template_content' => $aParam['content'],'article_id' => $iArticleId,'article_type' => $article_type,'need_open_comment' => $aParam['need_open_comment'],'only_fans_can_comment' => $aParam['only_fans_can_comment'],'create_time' => time(),'wechat_id' => $sWechatId,'ai_article_id' => $aAiArticle['ai_article_id']];
$result = Db::name('ai_wechat_article')->insert($aParam); $result = Db::name('ai_wechat_article')->insert($aParam);
if($result === false){ if($result === false){
return json_encode(['status' => 5,'msg' => "Article data insertion failed"]); return json_encode(['status' => 5,'msg' => "Article data insertion failed"]);
@@ -1256,22 +1319,20 @@ class Aiarticle extends Base
$aParam = empty($aParam) ? $this->request->post() : $aParam; $aParam = empty($aParam) ? $this->request->post() : $aParam;
//文章ID //文章ID
$iArticleId = empty($aParam['article_id'] ) ? '' : $aParam['article_id']; $iArticleId = empty($aParam['article_id'] ) ? '' : $aParam['article_id'];
//模版ID
$iTemplateId = empty($aParam['template_id']) ? '' : $aParam['template_id'];
// //推送公众号Id
// $sWechatId = empty($aParam['wechat_id']) ? '' : $aParam['wechat_id'];
//必填参数验证 //必填参数验证
if(empty($iArticleId)){ if(empty($iArticleId)){
return json_encode(['status' => 2, 'msg' => 'Please select an article']); return json_encode(['status' => 2, 'msg' => 'Please select an article']);
} }
if(empty($iTemplateId)){
return json_encode(['status' => 2, 'msg' => 'Please select a template']); //查询文章是否存在
$aWhere = ['article_id' => $iArticleId,'is_delete' => 2];
$aAiArticle = Db::name('ai_article')->field('article_type')->where($aWhere)->find();
if(empty($aAiArticle)){
return json_encode(['status' => 3, 'msg' => 'The article does not exist']);
} }
// if(empty($sWechatId)){
// return json_encode(['status' => 2, 'msg' => 'Please select the WeChat official account to push']);
// }
//查询是否上传到草稿箱 //查询是否上传到草稿箱
$aWhere = ['article_id' => $iArticleId,'template_id' => $iTemplateId]; $aWhere = ['article_id' => $iArticleId,'template_id' => $aAiArticle['article_type']];
$aWechatArticle = Db::name('ai_wechat_article')->field('id,is_publish,media_id,publish_status,wechat_id')->where($aWhere)->find(); $aWechatArticle = Db::name('ai_wechat_article')->field('id,is_publish,media_id,publish_status,wechat_id')->where($aWhere)->find();
$sMediaId = empty($aWechatArticle['media_id']) ? '' : $aWechatArticle['media_id']; $sMediaId = empty($aWechatArticle['media_id']) ? '' : $aWechatArticle['media_id'];
if(empty($sMediaId)){ if(empty($sMediaId)){
@@ -1293,7 +1354,6 @@ class Aiarticle extends Base
$aJournalInfo = $this->aWechatConfig; $aJournalInfo = $this->aWechatConfig;
} }
$aJournalInfo['media_id'] = $sMediaId; $aJournalInfo['media_id'] = $sMediaId;
//调用发布接口 //调用发布接口
$oWechat = new Wechat; $oWechat = new Wechat;
$aResult = json_decode($oWechat->publishDraft($aJournalInfo),true); $aResult = json_decode($oWechat->publishDraft($aJournalInfo),true);
@@ -1428,16 +1488,11 @@ class Aiarticle extends Base
//文章ID //文章ID
$iArticleId = empty($aParam['article_id']) ? '' : $aParam['article_id']; $iArticleId = empty($aParam['article_id']) ? '' : $aParam['article_id'];
//模版ID
$iTemplateId = empty($aParam['template_id']) ? '' : $aParam['template_id'];
//必填参数验证 //必填参数验证
if(empty($iArticleId)){ if(empty($iArticleId)){
return json_encode(['status' => 2, 'msg' => 'Please select an article']); return json_encode(['status' => 2, 'msg' => 'Please select an article']);
} }
if(empty($iTemplateId)){
return json_encode(['status' => 2, 'msg' => 'Please select a template']);
}
//获取AI生成文章内容 //获取AI生成文章内容
$aAiContent = json_decode($this->getAiArticle(['article_id' => $iArticleId,'is_select_author' => 1]),true); $aAiContent = json_decode($this->getAiArticle(['article_id' => $iArticleId,'is_select_author' => 1]),true);
@@ -1447,8 +1502,6 @@ class Aiarticle extends Base
if(empty($aAiArticle)){ if(empty($aAiArticle)){
return json_encode(['status' => 3, 'msg' => 'The article content of WeChat official account has not been generated']); return json_encode(['status' => 3, 'msg' => 'The article content of WeChat official account has not been generated']);
} }
//模版ID
$iTemplateId = empty($iTemplateId) ? $aAiArticle['template_id'] : $iTemplateId;
//获取文章内容 //获取文章内容
$aResult = json_decode($this->getArticle($iArticleId),true); $aResult = json_decode($this->getArticle($iArticleId),true);
@@ -1474,8 +1527,14 @@ class Aiarticle extends Base
//文章图片 //文章图片
$sArticleIcon = trim($this->sJournalUsx,'/').$this->sArticleIcon.$aArticle['article_icon']; $sArticleIcon = trim($this->sJournalUsx,'/').$this->sArticleIcon.$aArticle['article_icon'];
//子期刊数据
$aJournalStage = empty($aArticleContent['journal_stage']) ? [] : $aArticleContent['journal_stage'];
//期刊图片 //期刊图片
$sJournalIcon = trim($this->sJournalUsx,'/').$this->sJournalIcon.$aJournal['journal_icon']; $sJournalStageIcon = empty($aJournalStage['stage_icon']) ? '' : '/public/'.$aJournalStage['stage_icon'];
$sJournalIcon = empty($aJournal['journal_icon']) ? '' : $this->sJournalIcon.$aJournal['journal_icon'];
$sJournalIcon = empty($sJournalStageIcon) ? $sJournalIcon : $sJournalStageIcon;
$sJournalIcon = trim($this->sJournalUsx,'/').$sJournalIcon;
//期刊编辑二维码 //期刊编辑二维码
$editor_qrcode = empty($aJournal['editor_qrcode']) ? '' : $aJournal['editor_qrcode']; $editor_qrcode = empty($aJournal['editor_qrcode']) ? '' : $aJournal['editor_qrcode'];
@@ -1483,6 +1542,7 @@ class Aiarticle extends Base
//获取期刊二维码 //获取期刊二维码
$oMaterial = new Material; $oMaterial = new Material;
$aJournal['journal_stage_id'] = empty($aArticle['journal_stage_id']) ? 0 : $aArticle['journal_stage_id'];
$aJournalQrCode = $oMaterial->createJournalQrCode($aJournal); $aJournalQrCode = $oMaterial->createJournalQrCode($aJournal);
$sJournalCode = empty($aJournalQrCode['qrcode_url']) ? '' : $this->sSubmissionUrl.'public/qrcode/'.$aJournalQrCode['qrcode_url']; $sJournalCode = empty($aJournalQrCode['qrcode_url']) ? '' : $this->sSubmissionUrl.'public/qrcode/'.$aJournalQrCode['qrcode_url'];
$aAiArticle['journal_qrcode'] = $sJournalCode; $aAiArticle['journal_qrcode'] = $sJournalCode;
@@ -1500,7 +1560,7 @@ class Aiarticle extends Base
$aUpload['article_info'] = ['icon' => $sArticleIcon]; $aUpload['article_info'] = ['icon' => $sArticleIcon];
//组装期刊数据 //组装期刊数据
$aUpload['journal_info'] = ['icon' => $sJournalIcon,'editor_qrcode' => $editor_qrcode]; $aUpload['journal_info'] = ['icon' => $sJournalIcon,'editor_qrcode' => $editor_qrcode,'journal_stage_id' => $aArticle['journal_stage_id']];
//组装作者数据 //组装作者数据
if(!empty($aAuthor)){ if(!empty($aAuthor)){
@@ -1509,7 +1569,44 @@ class Aiarticle extends Base
//上传微信公众号用到的素材 //上传微信公众号用到的素材
$aUpload += ['article_id' => $iArticleId,'journal_id' => $aArticle['journal_id'],'wechat_app_id' => $sWechatAppId,'wechat_app_secret' => $sWechatAppSecret]; $aUpload += ['article_id' => $iArticleId,'journal_id' => $aArticle['journal_id'],'wechat_app_id' => $sWechatAppId,'wechat_app_secret' => $sWechatAppSecret];
$oMaterial = new Material; $oMaterial = new Material;
return $oMaterial->uploadMaterial($aUpload); $aResult = json_decode($oMaterial->uploadMaterial($aUpload),true);
$iStatus = empty($aResult['status']) ? 0 : $aResult['status'];
if($iStatus != 1){
return json_encode($aResult);
}
Queue::later(60,'app\api\job\WechatDraft@fire', ['article_id' => $iArticleId], 'uploadDraft');
} }
/**
* 对接微信公众号日志
*/
public function addWechatLog($aParam = []){
//获取参数
$aParam = empty($aParam) ? $this->request->post() : $aParam;
//必填参数验证
if(empty($aParam['article_id']) || empty($aParam['type'])){
return json_encode(['status' => 2, 'msg' => '非法操作']);
}
$result = Db::name('wechat_api_log')->insertGetId($aParam);
if($result === false){
return json_encode(['status' => 3, 'msg' => '数据插入失败'.Db::getLastSql()."\n数据内容:",'data' => $aParam]);
}
return json_encode(['status' => 1, 'msg' => '日志插入成功']);
}
/**
* 获取文章对应模版
*/
private function writeFile($sContent,$sPath = ''){
$chunkSize = 1024 * 1024; // 例如每次写入1MB
$file = fopen($sPath, "w");
for ($i = 0; $i < strlen($sContent); $i += $chunkSize) {
$chunk = substr($sContent, $i, $chunkSize);
fwrite($file, $chunk);
}
fclose($file);
}
} }