微信公众号推送文章相关接口调整

This commit is contained in:
chengxl
2025-05-19 11:55:20 +08:00
parent ed84ad7477
commit 6168790573
2 changed files with 61 additions and 36 deletions

View File

@@ -32,7 +32,7 @@ class Aiarticle extends Base
/**OPENAI相关配置----------end**/
//数据表必填字段[ai_article]
protected $aAiFileds = ['article_id','title_english','title_chinese','journal_id','covered','digest','research_result','content','highlights','discussion','prospect','research_background',];
protected $aAiFileds = ['article_id','title_english','title_chinese','journal_issn','covered','digest','research_result','content','highlights','discussion','prospect','research_background',];
//期刊接口地址
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'; //
@@ -86,6 +86,13 @@ class Aiarticle extends Base
protected $sUserIcon = '/public/usericon/';
//默认头像
protected $sDefaultUserIcon = '/static/img/userImg.f3d9bc3b.jpg';
//默认公众号配置
protected $aWechatConfig = [
'issn' => 'default',
'wechat_name' => 'TMR Publishing Group',
'wechat_app_id' => 'wxda4cc30fe32e6313',
'wechat_app_secret' => 'd5e6002b8b48de46f64dc9a02312f944'
];
public function __construct(\think\Request $request = null) {
@@ -209,7 +216,6 @@ class Aiarticle extends Base
if($iStatus != 1){
return json_encode($aResult);
}
//获取数据
$aArticleContent = empty($aResult['data']) ? [] : $aResult['data'];
//文章数据
@@ -217,6 +223,9 @@ class Aiarticle extends Base
if(empty($aArticle)){
return json_encode(['status' => 3,'msg' => 'The content of the article is empty']);
}
//期刊数据
$aJournal = empty($aArticleContent['journal']) ? [] : $aArticleContent['journal'];
//查询AI内容是否生成
$aAiArticle = json_decode($this->getAiArticle(['article_id' => $iArticleId]),true);
@@ -226,7 +235,8 @@ class Aiarticle extends Base
$iId = empty($aAiArticle['ai_article_id']) ? 0 : $aAiArticle['ai_article_id'];
if(empty($aAiArticle)){
//插入t_ai_article数据
$aInsert = ['title_english' => $aArticle['title'],'article_id' => $iArticleId,'create_time' => time()];
$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']];
$iId = Db::name('ai_article')->insertGetId($aInsert);
if($iId === false){
return json_encode(['status' => 4,'msg' => 'Data insertion failed']);
@@ -292,7 +302,7 @@ class Aiarticle extends Base
//组装数据
$aContent['article_id'] = $iArticleId;
$aContent['title_english'] = $aArticle['title'];
$aContent['journal_id'] = $aArticle['journal_id'];
$aContent['journal_issn'] = empty($aJournal['issn']) ? '' : $aJournal['issn'];
//更新数据库
@@ -817,19 +827,26 @@ class Aiarticle extends Base
$aSearch['{###wechat_name###}'] = empty($aSearch['{###wechat_name###}']) ? '“'.$aSearch['{###journal_title###}'].'“' : '“'.$aSearch['{###wechat_name###}'].'“';
//是否显示文章来源
//查询app_id和app_secret
$sIssn = empty($aJournal['issn']) ? '' : $aJournal['issn'];
$aTougaoJournal = Db::name('journal')->field('wechat_app_id,wechat_app_secret')->where('issn',$sIssn)->find();
if(!empty($aJournal)){
$wechat_app_id = empty($aTougaoJournal['wechat_app_id']) ? '' : $aTougaoJournal['wechat_app_id'];
$aJournal['wechat_app_id'] = empty($aJournal['wechat_app_id']) ? $wechat_app_id : $aJournal['wechat_app_id'];
$wechat_app_secret = empty($aTougaoJournal['wechat_app_secret']) ? '' : $aTougaoJournal['wechat_app_secret'];
$aJournal['wechat_app_secret'] = empty($aJournal['wechat_app_secret']) ? $wechat_app_secret : $aJournal['wechat_app_secret'];
}
$aSearch['{###article_from###}'] = '';
if(!empty($aParam['wechat_id']) && $aParam['wechat_id'] != $aJournal['issn']){
if(empty($aJournal['wechat_app_id']) || empty($aJournal['wechat_app_secret'])){//获取默认推送公众号
$sTemplateFromPath = $sTemplatePath.'/article_from.html';
//获取默认推送公众号配置
$aWechatConfig = $this->aWechatConfig;
if (file_exists($sTemplateFromPath) && is_readable($sTemplateFromPath)) {
$sTemplateFromPath = file_get_contents($sTemplateFromPath);
$aSearch['{###article_from###}'] = str_replace('{###journal_title###}', $aJournal['journal_title'], $sTemplateFromPath);
$aSearch['{###article_from###}'] = str_replace('{###journal_title###}', $aJournal['wechat_name']??$aWechatConfig['wechat_name'], $sTemplateFromPath);
}
//获取推送公众号名称
$aJournalInfo = Db::name('journal')->field('title,wechat_name')->where('issn',$aParam['wechat_id'])->find();
$sWechatName = empty($aJournalInfo['wechat_name']) ? '' : '“'.$aJournalInfo['wechat_name'].'“';
$sTitle = empty($aJournalInfo['title']) ? '' : '“'.$aJournalInfo['title'].'“';
$aSearch['{###wechat_name###}'] = empty($sWechatName) ? $sTitle : $sWechatName;
$aSearch['{###wechat_name###}'] = '“'.$aWechatConfig['wechat_name'].'“';
}
//模版替换变量
@@ -1072,8 +1089,8 @@ class Aiarticle extends Base
$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'];
// //推送公众号Id
// $sWechatId = empty($aParam['wechat_id']) ? '' : $aParam['wechat_id'];
//必填参数验证
if(empty($iArticleId)){
@@ -1082,9 +1099,9 @@ class Aiarticle extends Base
if(empty($iTemplateId)){
return json_encode(['status' => 2, 'msg' => 'Please select a template']);
}
if(empty($sWechatId)){
return json_encode(['status' => 2, 'msg' => 'Please select the WeChat official account to push']);
}
// if(empty($sWechatId)){
// return json_encode(['status' => 2, 'msg' => 'Please select the WeChat official account to push']);
// }
//判断文章类型
$article_type = empty($aParam['article_type']) ? 'news' : $aParam['article_type'];
if(!in_array($article_type, ['news','newspic'])){
@@ -1092,11 +1109,14 @@ class Aiarticle extends Base
}
//查询文章是否存在
$aWhere = ['article_id' => $iArticleId,'is_delete' => 2];
$aAiArticle = Db::name('ai_article')->field('title_chinese as title,author,journal_id,digest')->where($aWhere)->find();
$aAiArticle = Db::name('ai_article')->field('title_chinese as title,author,journal_issn,digest')->where($aWhere)->find();
if(empty($aAiArticle)){
return json_encode(['status' => 3, 'msg' => 'The article does not exist']);
}
$sWechatId = empty($aAiArticle['journal_issn']) ? '' : $aAiArticle['journal_issn'];
if(empty($sWechatId)){
return json_encode(['status' => 2, 'msg' => 'Please select the WeChat official account to push']);
}
//查询该模版是否推送到微信公众号
$aWhere['template_id'] = $iTemplateId;
$aWhere['wechat_id'] = $sWechatId;
@@ -1108,7 +1128,9 @@ class Aiarticle extends Base
//查询期刊微信公众号配置
$aJournalInfo = Db::name('journal')->field('wechat_app_id,wechat_app_secret')->where('issn',$sWechatId)->find();
if(empty($aJournalInfo['wechat_app_id']) || empty($aJournalInfo['wechat_app_secret'])){
return json_encode(['status' => 3, 'msg' => 'WeChat official account interface docking account is not configured, please confirm']);
//获取默认配置
$aJournalInfo = $this->aWechatConfig;
$sWechatId = $aJournalInfo['issn'] ?? 'default';
}
//数据处理-标题
@@ -1129,7 +1151,6 @@ class Aiarticle extends Base
if(!empty($aMaterial)){
$aAiArticle += $aMaterial;
}
unset($aAiArticle['journal_id']);
$aParam += $aAiArticle;
//获取模版生成内容
$aTemplateParam = ['article_id' => $iArticleId,'template_id' => $iTemplateId,'is_sync' => 1,'wechat_id' => $sWechatId];
@@ -1233,8 +1254,8 @@ class Aiarticle extends Base
$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'];
// //推送公众号Id
// $sWechatId = empty($aParam['wechat_id']) ? '' : $aParam['wechat_id'];
//必填参数验证
if(empty($iArticleId)){
return json_encode(['status' => 2, 'msg' => 'Please select an article']);
@@ -1242,18 +1263,20 @@ class Aiarticle extends Base
if(empty($iTemplateId)){
return json_encode(['status' => 2, 'msg' => 'Please select a template']);
}
if(empty($sWechatId)){
return json_encode(['status' => 2, 'msg' => 'Please select the WeChat official account to push']);
}
// if(empty($sWechatId)){
// return json_encode(['status' => 2, 'msg' => 'Please select the WeChat official account to push']);
// }
//查询是否上传到草稿箱
$aWhere = ['article_id' => $iArticleId,'template_id' => $iTemplateId,'wechat_id' => $sWechatId];
$aWechatArticle = Db::name('ai_wechat_article')->field('id,is_publish,media_id,publish_status')->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'];
if(empty($sMediaId)){
return json_encode(['status' => 3, 'msg' => 'The article was not found in the draft box of WeChat official account']);
}
$iId = empty($aWechatArticle['id']) ? 0 : $aWechatArticle['id'];
//推送公众号Id
$sWechatId = empty($aWechatArticle['wechat_id']) ? '' : $aWechatArticle['wechat_id'];
//判断是否发布
if($aWechatArticle['publish_status'] != '-1'){
return json_encode(['status' => 5, 'msg' => 'The article has been published, please confirm']);
@@ -1262,7 +1285,8 @@ class Aiarticle extends Base
//查询期刊微信公众号配置
$aJournalInfo = Db::name('journal')->field('wechat_app_id,wechat_app_secret')->where('issn',$sWechatId)->find();
if(empty($aJournalInfo['wechat_app_id']) || empty($aJournalInfo['wechat_app_secret'])){
return json_encode(['status' => 4, 'msg' => 'WeChat official account interface docking account is not configured, please confirm']);
//获取默认配置
$aJournalInfo = $this->aWechatConfig;
}
$aJournalInfo['media_id'] = $sMediaId;
@@ -1321,10 +1345,11 @@ class Aiarticle extends Base
}
$aWhere = ['issn' => ['in',$aIssn]];
$aJournalInfo = Db::name('journal')->field('issn,wechat_app_id,wechat_app_secret')->where($aWhere)->select();
if(empty($aJournalInfo)){
return json_encode(['status' => 4, 'msg' => 'WeChat official account interface docking account is not configured, please confirm']);
if(!empty($aJournalInfo)){
// return json_encode(['status' => 4, 'msg' => 'WeChat official account interface docking account is not configured, please confirm']);
$aJournalInfo = array_column($aJournalInfo, null,'issn');
}
$aJournalInfo = array_column($aJournalInfo, null,'issn');
//循环处理
$oWechat = new Wechat;
$aInsert = $aUpdate = [];
@@ -1335,7 +1360,7 @@ class Aiarticle extends Base
//账号信息
$aAccount = empty($aJournalInfo[$value['wechat_id']]) ? [] : $aJournalInfo[$value['wechat_id']];
if(empty($aAccount['wechat_app_id']) || empty($aAccount['wechat_app_secret'])){
continue;
$aAccount = $this->aWechatConfig;
}
$aAccount['publish_id'] = $value['publish_id'];
$aResult = json_decode($oWechat->queryStatus($aAccount),true);

View File

@@ -190,11 +190,11 @@ class Wechat
if(empty($iTemplateId)){
return json_encode(['status' => 2,'msg' => 'Template ID is empty']);
}
//发布公众号ID
$sWechatId = empty($aParam['wechat_id']) ? 0 : $aParam['wechat_id'];
if(empty($sWechatId)){
return json_encode(['status' => 2,'msg' => 'Please select the official account to be pushed']);
}
// //发布公众号ID
// $sWechatId = empty($aParam['wechat_id']) ? 0 : $aParam['wechat_id'];
// if(empty($sWechatId)){
// return json_encode(['status' => 2,'msg' => 'Please select the official account to be pushed']);
// }
//生成模版内容
$sContent = empty($aParam['content']) ? '' : $aParam['content'];
if(empty($sContent)){