Files
tougao/application/api/controller/Aiarticle.php
2025-05-17 19:36:15 +08:00

1209 lines
53 KiB
PHP
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace app\api\controller;
use app\api\controller\Base;
use think\Controller;
use think\Db;
use app\common\QrCodeImage;
use app\common\Wechat;
use app\common\Material;
use think\Cache;
/**
* @title 数据库接口
* @description 数据库接口
* @group 数据库接口
*/
class Aiarticle extends Base
{
protected $aLogo = ['media_id' => 'Cn8zlXvVB5DwjcA9h40z9fprHDoc3Jqv97SwrInpmyYiilkeRdKvpD63cWqTYHfz','url' => 'http://mmbiz.qpic.cn/mmbiz_jpg/QHFVW13lONaQJxK9QbHU9CtrvTS2ModZnUyeAvuVN67t8XP85DxVJwDJf2YxCTalrsr17jS080xM6xQv5yGiaEQ/0?wx_fmt=jpeg'];//默认头像
/**OPENAI相关配置----------start**/
//OPENAI接口地址
protected $sUrl = 'https://api.openai.com/v1/chat/completions';
//OPENAI接口key
protected $sApiKey = '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';
protected $proxy = '';
//接口返回对象
protected $sResponesData;
//接口返回错误信息
protected $sError;
//接口超时请求
protected $timeout = 60;
/**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 $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 $sJournalUsx = 'https://www.tmrjournals.com';
//投稿系统
protected $sSubmissionUrl = 'https://submission.tmrjournals.com/';
protected $aOpenAiAsk = [
1 => '"将以下内容翻译为中文,仅返回翻译结果,不要解释:\n {#content#}"',
2 => '
**核心要求**
1 内容涵盖哪些学科及方法请罗列
2 结构化摘要生成【四要素模版】
3 研究背景提炼【三段式结构】
发病率+当前治疗缺口如5年OS<20%
引用2-3篇高被引论文的矛盾结论如Nature vs Cell观点分歧
基于团队前期工作如预实验发现X调控Y
4 针对稿件内容进行结论撰写
与TOP3高被引文献的对比用"与A研究的X结论不同..."句式) 机制解释的3级证据链基因敲除→通路抑制→表型逆转 临床转化的可行性路径基于RECIST标准的ORR提升研究局限性分方法学/样本量/随访维度)
5 针对稿件内容进行展望撰写
现有方法的可扩展性(如空间组学升级计划)
6 按点总结归纳研究结果并做简要阐述【3点以上】
7 总结归纳亮点【3点以上】
8 禁用清单
直接复制讨论部分的原文
未达到统计学显著性的趋势描述
稿件内未提及的内容一定不要总结归纳出来
"可能""或许"等不确定性词汇
单纯古籍原文翻译(需结合现代数据解读)
未经验证的因果断言(如"证明XX药治愈癌症"
9 请将标题翻译成中文
{#title_chinese#}
10 请将稿件内容翻译成中文
{#content#}
**稿件关键信息**
- 稿件简介:{#abstract#}
- 稿件内容:{#content#}
**输出格式**
中文格式[英文简写忽略首字母大写]
格式内容
```json{ "title_chinese": "", "content": "", "covered": "【总字数<=100】", "digest": "【总字数<=500】", "research_background": "", "research_result": "", "highlights": "", "prospect": "", "discussion": ""}'
];
//文章图片icon地址
protected $sArticleIcon = '/public/articleicon/';
//期刊图片icon地址
protected $sJournalIcon = '/public/journalicon/';
//期刊编辑二维码地址
protected $sJournalEditorQrcode = '/public/journaleditorqrcode/';
//作者头像地址
protected $sUserIcon = '/public/usericon/';
//默认头像
protected $sDefaultUserIcon = '/static/img/userImg.f3d9bc3b.jpg';
public function __construct(\think\Request $request = null) {
$this->proxy = '';
parent::__construct($request);
}
/**
* CURL 发送请求到 OpenAI
* @param $messages 内容
* @param $model 模型类型
*/
protected function curlOpenAI($messages, $model = 'gpt-4o'){
$sUrl = $this->sUrl;
$data = [
'model' => $model,
'messages' => $messages,
'temperature' => 0.7
];
$oCurl = curl_init();
// 通用配置
curl_setopt($oCurl, CURLOPT_URL, $sUrl);
// 设置头信息
curl_setopt($oCurl, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . $this->sApiKey
]);
curl_setopt($oCurl, CURLOPT_PROXY,$this->proxy);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER,true);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST,2);
curl_setopt($oCurl, CURLOPT_POST, true); //设置为POST方式
curl_setopt($oCurl, CURLOPT_POSTFIELDS,json_encode($data));
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, TRUE) ; // 获取数据返回
// curl_setopt($oCurl, CURLOPT_TIMEOUT, $this->timeout);
$result = curl_exec($oCurl);
//请求失败
if (curl_errno($oCurl)){
$this->sError = curl_errno($oCurl);
curl_close($oCurl);
return FALSE;
}
$this->sResponesData = json_decode($result);
curl_close($oCurl);
return TRUE;
}
/**
* @title 请求OPENAI
* @param aSearch array 模版内容
* @param iTemplateId 模版ID
*/
private function _createContentForOpenAI($aSearch = [],$iTemplateId = 0){
if(empty($aSearch) || empty($iTemplateId)){
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];
$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 = $aParam['api_model'] ?? 'gpt-4o';
$result = $this->curlOpenAI($messages,$sModel);
if($result == FALSE){
return json_encode(array('status' => 2,'msg' => 'Interface request failed'.$this->sError));
}
//处理返回信息
$data = $this->sResponesData;
if(!is_object($data)){
return json_encode(array('status' => 3,'msg' => 'There is a misunderstanding in the data returned by the interface'));
}
$data = object_to_array($data);
$aChoices = $data['choices'] ?? [];
if(empty($aChoices)){
return json_encode(array('status' => 4,'msg' => 'OPENAI did not return data'));
}
$aChoicesInfo = $aChoices[0] ?? [];
$aMessage = $aChoicesInfo['message'] ?? [];
if(empty($aMessage['content'])){
return json_encode(array('status' => 5,'msg' => 'OPENAI returns empty data'));
}
$sContent = trim(trim($aMessage['content'],'```json'),'```');
$aContent = json_decode($sContent,true);
return json_encode(array('status' => 1,'msg' => 'OPENAI successfully generated content','data' => $aContent));
}
/**
* @title AI生成稿件文章
* @param article_id 文章ID
* @param model 接口模型
* @param stream 是否流式输出 true是false否
*/
public function create(){
//获取参数
$aParam = $this->request->post();
$iArticleId = empty($aParam['article_id']) ? '' : $aParam['article_id'];
if(empty($iArticleId)){
return json_encode(array('status' => 2,'msg' => 'Please select an article' ));
}
//获取文章是否生成AI内容
$aResult = json_decode($this->getArticle($iArticleId),true);
$iStatus = empty($aResult['status']) ? 0 : $aResult['status'];
if($iStatus != 1){
return json_encode($aResult);
}
//获取数据
$aArticleContent = empty($aResult['data']) ? [] : $aResult['data'];
//文章数据
$aArticle = empty($aArticleContent['article']) ? [] : $aArticleContent['article'];
if(empty($aArticle)){
return json_encode(['status' => 3,'msg' => 'The content of the article is empty']);
}
//查询AI内容是否生成
$aAiArticle = json_decode($this->getAiArticle(['article_id' => $iArticleId]),true);
$aAiArticleContent = empty($aAiArticle['data']) ? [] : $aAiArticle['data'];
$aAiArticle = empty($aAiArticleContent['ai_article']) ? [] : $aAiArticleContent['ai_article'];
$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()];
$iId = Db::name('ai_article')->insertGetId($aInsert);
if($iId === false){
return json_encode(['status' => 4,'msg' => 'Data insertion failed']);
}
$aAiArticle = array_merge(['ai_article_id' => $iId,'article_id' => $iArticleId,'is_generate' => 2],$aInsert);
}
//判断是否生成
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']);
}
//请求OPENAI
return $this->createForOpenAi($aArticleContent,$aAiArticle);
}
/**
* @title 请求OPENAI生成内容
* @param article_id 文章ID
* @param model 接口模型
* @param stream 是否流式输出 true是false否
*/
public function createForOpenAi($aArticleContent = [],$aAiArticle = []){
$iId = empty($aAiArticle['ai_article_id']) ? 0 : $aAiArticle['ai_article_id'];
$iArticleId = empty($aAiArticle['article_id']) ? 0 : $aAiArticle['article_id'];
//文章数据
$aArticle = empty($aArticleContent['article']) ? [] : $aArticleContent['article'];
//期刊数据
$aJournal = empty($aArticleContent['journal']) ? [] : $aArticleContent['journal'];
//子期刊数据
$aJournalStage = empty($aArticleContent['journal_stage']) ? [] : $aArticleContent['journal_stage'];
//通讯作者
$aAuthor = empty($aArticleContent['author']) ? [] : $aArticleContent['author'];
//查询文章内容
$aMain = empty($aArticleContent['main']) ? [] : $aArticleContent['main'];;
$sContent = empty($aMain) ? '' : implode(',', $aMain);
//请求OPENAI生成微信公众号文章内容
$aSearch = [];
$aSearch['{#content#}'] = $this->basic_html_filter($sContent);
$aSearch['{#abstract#}'] = $this->basic_html_filter($aArticle['abstract']);
$aSearch['{#title_chinese#}'] = $this->basic_html_filter($aArticle['title']);
$aSearch['{#journal_content#}'] = $this->basic_html_filter($aJournal['journal_content'] ?? '');
$aSearch['{#journal_content#}'] = trim(trim($aSearch['{#journal_content#}'],'.'),'。');
$aResult = $this->_createContentForOpenAI($aSearch,2);
$aResult = json_decode($aResult,true);
if($aResult['status'] != 1){
return json_encode($aResult);
}
$aContent = empty($aResult['data']) ? [] : $aResult['data'];
if(empty($aContent)){
return json_encode(['status' => 4,'msg' => 'Data is empty']);
}
//组装数据
$aContent['article_id'] = $iArticleId;
$aContent['title_english'] = $aArticle['title'];
$aContent['journal_id'] = $aArticle['journal_id'];
//更新数据库
$aContent['ai_article_id'] = $iId;
$aContent['author'] = empty($aJournal['publish_author']) ? 'TMR编辑部' : $aJournal['publish_author'];
$aResult = $this->updateAiArticle($aContent);
return $aResult;
}
/**
* 处理文章引用
* @param string $html
* @return string
*/
private function _cite($aArticle = [],$aJournal = [],$aJournalStage = []){
$no = empty($aJournalStage['stage_no']) ? ':' : '(' . $aJournalStage['stage_no'] . '):';
$jabbr = empty($aJournal['jabbr']) ? '' : $aJournal['jabbr'];
$stage_year = empty($aJournalStage['stage_year']) ? '' : $aJournalStage['stage_year'];
$stage_vol = empty($aJournalStage['stage_vol']) ? '' : $aJournalStage['stage_vol'];
$sCite = '';
if ($aArticle['journal_id'] == 22) {
$sCite = $aArticle['abbr'] . '. ' . $aArticle['title'] . '[J]. ' . choiseJabbr($aArticle['article_id'],$jabbr) . ',' . $stage_year . ',' . $stage_vol . $no . $aArticle['npp'] . '. doi:' . $aArticle['doi'];
} else {
$sCite = $aArticle['abbr'] . '. ' . $aArticle['title'] . '. <i>' . choiseJabbr($aArticle['article_id'], $jabbr) . '</i>. ' . $stage_year . ';' . $stage_vol . $no . $aArticle['npp'] . '. doi:' . $aArticle['doi'];
}
return $sCite;
}
/**
* 接口请求获取Journal数据库文章信息
* @param article_id 文章ID
*/
public function getArticle($iArticleId = ''){
$aParam = $this->request->post();
$iArticleId = empty($iArticleId) ? $aParam['article_id'] : $iArticleId;
//接口请求地址
$sUrl = $this->sJournalUrl.'/wechat/Article/getArticle';
//请求接口
$aParam['article_id'] = $iArticleId;
$aResult = object_to_array(json_decode(myPost($sUrl, $aParam)));
$iStatus = empty($aResult['status']) ? 0 : $aResult['status'];
if($iStatus != 1){
return json_encode(['status' => 0,'msg' => $aResult['msg'] ?? '操作异常']);
}
$aContent = empty($aResult['data']) ? [] : $aResult['data'];
if(!empty($aContent['journal'])){
$aJournal = $aContent['journal'];
//查询期刊主题
$aWhere['issn'] = $aJournal['issn'];
$aJournalInfo = Db::name('journal')->field('journal_topic')->where($aWhere)->find();
$aContent['journal']+=$aJournalInfo;
}
return json_encode(['status' => 1,'msg' => 'success','data' => $aContent]);
}
/**
* 获取Ai生成文章信息
* @param article_id 文章ID
* @param $iSelectAuthor 是否查询作者 1是2否
*/
public function getAiArticle($aParam = []){
$aParam = empty($aParam) ? $this->request->post() : $aParam;
//文章ID
$iArticleId = empty($aParam['article_id']) ? '' : $aParam['article_id'];
if(empty($iArticleId)){
return json_encode(['status' => 2,'msg' => 'Please select the article to be generated']);
}
//是否查询作者 1是2否
$iSelectAuthor = empty($aParam['is_select_author']) ? 2 : $aParam['is_select_author'];
//查询AI生成的文章内容
$aWhere = ['is_delete' => 2];
if(!empty($iArticleId)){
$aWhere['article_id'] = $iArticleId;
}
$aAiArticle = Db::name('ai_article')->where($aWhere)->find();
if(empty($aAiArticle)){
return json_encode(['status' => 1,'msg' => 'data is null','data' => ['ai_article' => []]]);
}
$iArticleId = empty($aAiArticle['article_id']) ? 0 : $aAiArticle['article_id'];
//查询文章通讯作者
$aAiAuthor = [];
if($iSelectAuthor == 2){
$aResult = json_decode($this->getArticle($iArticleId),true);
//获取数据
$aArticleContent = empty($aResult['data']) ? [] : $aResult['data'];
//通讯作者
$aAuthor = empty($aArticleContent['author']) ? [] : $aArticleContent['author'];
if(!empty($aAuthor)){
$aAiAuthor = $this->_dealAuthor($aAuthor);
if(!empty($aAiAuthor)){
foreach ($aAiAuthor as $key => $value) {
$aAiAuthor[$key]['icon_path'] = empty($value['icon']) ? $this->sDefaultUserIcon : $this->sUserIcon.$value['icon'];
}
}
}
}
//查询是否上传到微信草稿箱
if(!empty($iArticleId)){
$aWhere = ['article_id' => $iArticleId,'is_delete' => 2];
if(!empty($aParam['template_id'])){
$aWhere['template_id'] = $aParam['template_id'];
}
if(!empty($aParam['wechat_id'])){
$aWhere['wechat_id'] = $aParam['wechat_id'];
}
$aDraft = Db::name('ai_wechat_article')->field('template_id,wechat_id')->where($aWhere)->select();
$aAiArticle['upload_draft'] = $aDraft;
}
return json_encode(['status' => 1,'msg' => 'success','data' => ['ai_article' => $aAiArticle,'ai_article_author' => $aAiAuthor]]);
}
/**
* @title 处理作者数据
* @param article_id 文章ID
* @param content 内容
*/
protected function _dealAuthor($aAuthor = array()){
//返回数组
if(empty($aAuthor)){
return [];
}
//根据邮箱查询作者信息
$aArticleId = array_unique(array_column($aAuthor, 'article_id'));
$iArticleId = $aArticleId[0];
$aEmail = array_unique(array_column($aAuthor, 'email'));
$aWhere = ['email' => ['in',$aEmail]];
$aUser = Db::name('user')->field('user_id,realname,email,localname,icon')->where($aWhere)->select();
if(empty($aUser)){
return [];
}
//查询作者详情
$aUserId = array_column($aUser, 'user_id');
$aWhere = ['reviewer_id' => ['in',$aUserId]];
$aUserInfo = DB::name('user_reviewer_info')->field('reviewer_id,technical,introduction,company')->where($aWhere)->select();
if(empty($aUserInfo)){
return [];
}
//数据处理
$aUserInfo = array_column($aUserInfo, null,'reviewer_id');
$aAuthor = array_column($aAuthor, null,'email');
$aInfo = [];
foreach ($aUser as $key => $value) {
//姓名
$sName = empty($aAuthor[$value['email']]['author_name']) ? '' : $aAuthor[$value['email']]['author_name'];
$sName = empty($sName) ? $value['localname'] : $sName;
$sName = empty($sName) ? $value['realname'] : $sName;
//单位
$sCompany = empty($aAuthor[$value['email']]['company']) ? '' : $aAuthor[$value['email']]['company'];
$sCompany1 = empty($aUserInfo[$value['user_id']]['company']) ? '' : $aUserInfo[$value['user_id']]['company'];
$sCompany = empty($sCompany) ? $sCompany1 : $sCompany;
//职称
$sTechnical = empty($aUserInfo[$value['user_id']]['technical']) ? '' : $aUserInfo[$value['user_id']]['technical'];
//简介
$sIntroduction = empty($aUserInfo[$value['user_id']]['introduction']) ? '' : $aUserInfo[$value['user_id']]['introduction'];
$aInfo[] = ['user_id' => $value['user_id'],'email' => $value['email'],'author_name' => $sName,'technical' => $sTechnical,'introduction' => $sIntroduction,'company' => $sCompany,'article_id' => $iArticleId,'create_time' => time(),'icon' => $value['icon']];
}
return $aInfo;
}
/**
* 基础HTML过滤
* @param string $html
* @return string
*/
private function basic_html_filter($html) {
// 移除所有HTML标签及PHP标签
$text = empty($html) ? '' : strip_tags($html);
// 转换HTML实体
$text = html_entity_decode($text, ENT_QUOTES | ENT_HTML5, 'UTF-8');
// 合并连续空白字符
$text = preg_replace('/\s+/u', ' ', $text);
return trim($text);
}
/**
* @title AI生成稿件内容入库
* @param article_id 文章ID
* @param content 内容
*/
public function updateAiArticle($aParam = array()){
//返回数组
$aResult = ['status' => 1,'msg' => 'AI article updated successfully','data' => []];
//获取参数
$aParam = empty($aParam) ? $this->request->post() : $aParam;
//主键ID
$iAiArticleId = empty($aParam['ai_article_id']) ? 0 : $aParam['ai_article_id'];
//文章ID
$iArticleId = empty($aParam['article_id']) ? 0 : $aParam['article_id'];
//查询内容是否存在
$aWhere = ['is_delete' => 2];
if(empty($iArticleId) && empty($iArticleId)){
return json_encode(['status' => 2,'msg' => 'Please select the article to be modified']);
}
if(!empty($iArticleId)){
$aWhere['article_id'] = $iArticleId;
}
if(!empty($iAiArticleId)){
$aWhere['ai_article_id'] = $iAiArticleId;
}
$aAiArticle = Db::name('ai_article')->where($aWhere)->find();
if(empty($aAiArticle)){
return json_encode(['status' => 3,'msg' => 'he article content of WeChat official account has not been generated']);
}
//必填参数验证
$aFields = $this->aAiFileds;
$sFiled = '';
$aUpdateParam = [];
foreach($aFields as $val){
if(!isset($aParam[$val])){
continue;
}
$aUpdateParam[$val] = empty($aParam[$val]) ? '' : addslashes(strip_tags($aParam[$val]));
}
if(empty($aUpdateParam)){
return json_encode(['status' => 1,'msg' => 'No data currently being processed']);
}
Db::startTrans();
//执行入库
$aUpdateParam['update_time'] = time();
$aUpdateParam['is_generate'] = 1;
$result = Db::name('ai_article')->where('ai_article_id',$iAiArticleId)->limit(1)->update($aUpdateParam);
if($result === false){
$aResult = json_encode(['status' => 3,'msg' => 'UPDATEING AI article failed']);
}
$aResult['data'] = $aUpdateParam;
//更新作者信息
$aAuthorList = empty($aParam['author_list']) ? []: $aParam['author_list'];
$aAuthorList = is_array( $aAuthorList) ? $aAuthorList: json_decode($aAuthorList,true);
if(!empty($aAuthorList)){
$aAuthorList = array_column($aAuthorList, null,'email');
//根据邮箱查询作者信息
$aEmail = array_keys($aAuthorList);
$aUserInfo = $aUserReviewer = [];
if(!empty($aEmail)){
$aUserParam = ['email' => ['in',$aEmail]];
$aUserInfo = Db::name('user')->field('email,user_id')->where($aUserParam)->select();
if(!empty($aUserInfo)){
$aUserId = array_column($aUserInfo, 'user_id');
//查询用户附表
$aWhere = ['reviewer_id' => ['in',$aUserId]];
$aUserReviewer = Db::name('user_reviewer_info')->where($aWhere)->column('reviewer_id');
}
}
//更新用户信息
if(!empty($aUserInfo)){
foreach ($aUserInfo as $key => $value) {
$aUser = empty($aAuthorList[$value['email']]) ? [] : $aAuthorList[$value['email']];
if(empty($aUser)){
continue;
}
//更新作者名字
if(isset($aUser['author_name'])){
$aUpdate = ['localname' => $aUser['author_name']];
Db::name('user')->where('user_id',$value['user_id'])->update($aUpdate);
}
//更新作者简介和单位
$aUpdateReviewer = [];
if(isset($aUser['technical'])){
$aUpdateReviewer['technical'] = $aUser['technical'];
}
if(isset($aUser['introduction'])){
$aUpdateReviewer['introduction'] = $aUser['introduction'];
}
if(isset($aUser['company'])){
$aUpdateReviewer['company'] = $aUser['company'];
}
if(isset($aUpdateReviewer)){
if(in_array($value['user_id'], $aUserReviewer)){//更新
Db::name('user_reviewer_info')->where('reviewer_id',$value['user_id'])->limit(1)->update($aUpdateReviewer);
}
if(!in_array($value['user_id'], $aUserReviewer)){//插入
$aUpdateReviewer['reviewer_id'] = $value['user_id'];
Db::name('user_reviewer_info')->insert($aUpdateReviewer);
}
}
}
}
}
Db::commit();
$aResult['data'] = $aUpdateParam;
return json_encode($aResult);
}
/**
* 文章内容选择模版生成数据
* @param article_id 文章ID
* @param template_id 模版ID
* @param $iIsSync 是否同步素材到微信 1是2否
*/
public function getTemplateContent($aParam = []){
//获取参数
$aParam = empty($aParam) ? $this->request->post() : $aParam;
//文章ID
$iArticleId = empty($aParam['article_id']) ? '' : $aParam['article_id'];
//模版ID
$iTemplateId = empty($aParam['template_id']) ? '' : $aParam['template_id'];
//模版ID
$iIsSync = empty($aParam['is_sync']) ? 2 : $aParam['is_sync'];
//必填参数验证
if(empty($iArticleId)){
return json_encode(['status' => 2, 'msg' => 'Please select an article']);
}
if(empty($iTemplateId)){
return json_encode(['status' => 2, 'msg' => 'Please select a template']);
}
//获取AI生成文章内容
$aAiContent = json_decode($this->getAiArticle(['article_id' => $iArticleId,'is_select_author' => 1]),true);
$aAiContent = empty($aAiContent['data']) ? [] : $aAiContent['data'];
$aAiArticle = empty($aAiContent['ai_article']) ? [] : $aAiContent['ai_article'];
//判断是否生成AI内容
if(empty($aAiArticle)){
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);
//获取数据
$aArticleContent = empty($aResult['data']) ? [] : $aResult['data'];
if(empty($aArticleContent)){
return json_encode($aResult);
}
//文章数据
$aArticle = empty($aArticleContent['article']) ? [] : $aArticleContent['article'];
if(empty($aArticle)){
return json_encode(['status' => 4,'msg' => 'Article data is empty']);
}
//期刊数据
$aJournal = empty($aArticleContent['journal']) ? [] : $aArticleContent['journal'];
if(empty($aJournal)){
return json_encode(['status' => 4,'msg' => 'Journal data is empty']);
}
//子期刊数据
$aJournalStage = empty($aArticleContent['journal_stage']) ? [] : $aArticleContent['journal_stage'];
//通讯作者
$aAuthor = empty($aArticleContent['author']) ? [] : $aArticleContent['author'];
//文章图片
$sArticleIcon = trim($this->sJournalUsx,'/').$this->sArticleIcon.$aArticle['article_icon'];
//期刊图片
$sJournalIcon = trim($this->sJournalUsx,'/').$this->sJournalIcon.$aJournal['journal_icon'];
//期刊编辑二维码
$sEditorQrcode = empty($aJournal['editor_qrcode']) ? '' : $aJournal['editor_qrcode'];
$sEditorQrcode = trim($this->sSubmissionUrl,'/').$this->sJournalEditorQrcode.$sEditorQrcode;
//获取期刊二维码
$oMaterial = new Material;
$aJournalQrCode = $oMaterial->createJournalQrCode($aJournal);
$sJournalCode = empty($aJournalQrCode['qrcode_url']) ? '' : $this->sSubmissionUrl.'public/qrcode/'.$aJournalQrCode['qrcode_url'];
$aAiArticle['journal_qrcode'] = $sJournalCode;
//文章二维码
$aArticleQrCode = $oMaterial->createArticleQrCode($aArticle);
$sArticleCode = empty($aArticleQrCode['qrcode_url']) ? '' : $this->sSubmissionUrl.'public/qrcode/'.$aArticleQrCode['qrcode_url'];
$aAiArticle['article_qrcode'] = $sArticleCode;
//文章cite
$aAiArticle['cite'] = $this->_cite($aArticle,$aJournal,$aJournalStage);
// //上传素材到微信
if($iIsSync == 1){
//文章图片上传
$sWechatAppId = empty($aParam['wechat_app_id']) ? '' : $aParam['wechat_app_id'];
$sWechatAppSecret = empty($aParam['wechat_app_secret']) ? '' : $aParam['wechat_app_secret'];
$aUpload = ['article_id' => $iArticleId,'icon' => $sArticleIcon,'wechat_app_id' => $sWechatAppId,'wechat_app_secret' => $sWechatAppSecret];
$aArticleResult = json_decode($oMaterial->addArticleMaterial($aUpload),true);
$aArticleData = empty($aArticleResult['data']) ? [] : $aArticleResult['data'];
$aAiArticle['article_icon'] = empty($aArticleData['media_url']) ? $sArticleIcon : $aArticleData['media_url'];
//期刊图片上传
$aUpload = ['journal_id' => $aAiArticle['journal_id'],'icon' => $sJournalIcon,'wechat_app_id' => $sWechatAppId,'wechat_app_secret' => $sWechatAppSecret];
$aJournalResult = json_decode($oMaterial->addJournalMaterial($aUpload),true);
$aJournalData = empty($aJournalResult['data']) ? [] : $aJournalResult['data'];
$aAiArticle['journal_icon'] = empty($aJournalData['media_url']) ? $sJournalIcon : $aJournalData['media_url'];
//期刊二维码图片上传
$aUpload = ['journal_id' => $aAiArticle['journal_id'],'wechat_app_id' => $sWechatAppId,'wechat_app_secret' => $sWechatAppSecret];
$aJournalQrCodeResult = json_decode($oMaterial->updateJournalQrcode($aUpload),true);
$aJournalQrCodeData = empty($aJournalQrCodeResult['data']) ? [] : $aJournalQrCodeResult['data'];
$aAiArticle['journal_qrcode'] = empty($aJournalQrCodeData['media_url']) ? $sJournalIcon : $aJournalQrCodeData['media_url'];
//文章二维码图片上传
$aUpload = ['article_id' => $iArticleId,'wechat_app_id' => $sWechatAppId,'wechat_app_secret' => $sWechatAppSecret];
$aArticleQrCodeResult = json_decode($oMaterial->updateArticleQrcode($aUpload),true);
$aArticleQrCodeData = empty($aArticleQrCodeResult['data']) ? [] : $aArticleQrCodeResult['data'];
$aAiArticle['article_qrcode'] = empty($aArticleQrCodeData['media_url']) ? $sArticleIcon : $aArticleQrCodeData['media_url'];
//作者图片上传
if(!empty($aAuthor)){
$aAuthor = $this->_dealAuthor($aAuthor);
foreach ($aAuthor as $key => $value) {
if(empty($value['icon'])){
continue;
}
//文章二维码图片上传
$aUpload = ['email' => $value['email'],'icon' => $value['icon'],'user_id' => $value['user_id'],'wechat_app_id' => $sWechatAppId,'wechat_app_secret' => $sWechatAppSecret];
$aAuthorResult = json_decode($oMaterial->updateArticleAuthor($aUpload),true);
$aAuthorData = empty($aAuthorResult['data']) ? [] : $aAuthorResult['data'];
$aAuthor[$key]['icon'] = empty($aAuthorData['media_url']) ? $value['icon'] : $aAuthorData['media_url'];
}
}
//上传期刊编辑二维码
$aUpload = ['editor_qrcode' => $aJournal['editor_qrcode']??'','journal_id' => $aAiArticle['journal_id'],'wechat_app_id' => $sWechatAppId,'wechat_app_secret' => $sWechatAppSecret];
$aJournalEditorQrCodeResult = json_decode($oMaterial->updateJournalEditorQrcode($aUpload),true);
$aJournalEditorQrCodeData = empty($aJournalEditorQrCodeResult['data']) ? [] : $aJournalEditorQrCodeResult['data'];
$aAiArticle['editor_qrcode'] = empty($aJournalEditorQrCodeData['media_url']) ? $sEditorQrcode : $aJournalEditorQrCodeData['media_url'];
}else{
//处理文章图片地址数据
$aAiArticle['article_icon'] = $sArticleIcon;
//处理文章期刊图片地址数据
$aAiArticle['journal_icon'] = $sJournalIcon;
//期刊编辑二维码图片地址数据
$aAiArticle['editor_qrcode'] = $sEditorQrcode;
//作者信息处理
if(!empty($aAuthor)){
$aAuthor = $this->_dealAuthor($aAuthor);
}
}
//期刊访问地址
$journal_usx = trim($this->sJournalUsx,'/').'/';
if(!empty($aJournal['journal_usx'])){
//地址
$journal_usx .= $aJournal['journal_usx']??''.'/';
}
$aAiArticle['journal_usx_url'] = trim($journal_usx);
//处理期刊内容
if(empty($aJournal['journal_content'])){
$aJournal['journal_content'] = empty($aJournal['journal_content']) ? $aJournal['journal_content_english'] : $aJournal['journal_content'];
}
$aAiArticle += $aJournal;
//获取模版信息
$sTemplatePath = ROOT_PATH."public/wechatTemplate/template_".$iTemplateId;
//index模版目录
$sTemplateIndexPath = $sTemplatePath.'/index.html';
// 验证文件有效性
if (!file_exists($sTemplateIndexPath)) {
return json_encode(['status' => 4, 'msg' => 'Article template does not exist']);
}
if (!is_readable($sTemplateIndexPath)) {
return json_encode(['status' => 5, 'msg' => 'The article template is unreadable']);
}
//获取模版内容
$sTemplate = file_get_contents($sTemplateIndexPath);
//数据处理
$aSearch = [];
foreach ($aAiArticle as $key => $value) {
if(is_array($value)){
continue;
}
$aSearch['{###'.$key.'###}'] = empty($value) ? '' : htmlspecialchars(strip_tags($value));
}
//处理通讯作者信息数据
$aAuthorInfo = json_decode($this->dealTemplateAuthor($aAuthor,$iTemplateId),true);
$aAuthorInfo = empty($aAuthorInfo['data']) ? [] : $aAuthorInfo['data'];
$aSearch['{###author_summary###}'] = empty($aAuthorInfo['author_info']) ? '' : $aAuthorInfo['author_info'];
//处理往期推荐数据
$aPreviousRecommend = json_decode($this->dealTemplatePreviousRecommend($aArticle,$iTemplateId),true);
$aSearch['{###previous_recommend_summary###}'] = empty($aPreviousRecommend['data']) ? '' : $aPreviousRecommend['data'];
//处理期刊主题数据
$sJournalTopic = empty($aJournal['journal_topic']) ? '' : $aJournal['journal_topic'];
$aTopic = json_decode($this->dealTemplateTopic($sJournalTopic,$iTemplateId),true);
$aSearch['{###topic_name_summary###}'] = empty($aTopic['data']) ? '' : $aTopic['data'];
//公众号名字处理
$aSearch['{###wechat_name###}'] = empty($aSearch['{###wechat_name###}']) ? '“'.$aSearch['{###journal_title###}'].'“' : '“'.$aSearch['{###wechat_name###}'].'“';
//是否显示文章来源
$aSearch['{###article_from###}'] = '';
if(!empty($aParam['wechat_id']) && $aParam['wechat_id'] != $aJournal['issn']){
$sTemplateFromPath = $sTemplatePath.'/article_from.html';
if (file_exists($sTemplateFromPath) && is_readable($sTemplateFromPath)) {
$sTemplateFromPath = file_get_contents($sTemplateFromPath);
$aSearch['{###article_from###}'] = str_replace('{###journal_title###}', $aJournal['journal_title'], $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;
}
//模版替换变量
if(empty($aSearch)){
return json_encode(['status' => 6, 'msg' => '模版内容不能为空']);
}
$aSearch['{###jabbr###}'] = trim(trim($aSearch['{###jabbr###}'],'.'),'。');
$aSearch['{###free_year###}'] = date('Y');
$aSearch['{###covered###}'] = '内容涵盖'.str_replace('研究涵盖', '', $aSearch['{###covered###}']);
$sTemplate = str_replace(array_keys($aSearch), array_values($aSearch), $sTemplate);
file_put_contents(ROOT_PATH.'public/template_info.html', $sTemplate);
return json_encode(['status' => 1, 'msg' => '模版生成成功','data' => ['template' => $sTemplate]]);
}
/**
* 处理模版通讯作者
* @param article_id 文章ID
*/
private function dealTemplateAuthor($aAuthor = [],$iTemplateId = 0){
//必填字段判断
if(empty($aAuthor) || empty($iTemplateId)){
return json_encode(['status' => 2,'msg' => 'Corresponding author or template does not exist']);
}
//处理作者
// $aAuthor = $this->_dealAuthor($aAuthor);
//获取通讯作者模版
$sAuthorTemplatePath = ROOT_PATH."public/wechatTemplate/template_".$iTemplateId.'/author.html';
if (!file_exists($sAuthorTemplatePath)) {
return json_encode(['status' => 3, 'msg' => 'Corresponding author template does not exist','data' => '']);
}
if (!is_readable($sAuthorTemplatePath)) {
return json_encode(['status' => 4, 'msg' => 'The corresponding author template is unreadable','data' => '']);
}
$sAuthorTemplate = file_get_contents($sAuthorTemplatePath);
//处理模版变量
$aAuthorSerachInfo = [];
$sAuthorInfo = '';
$aLogo = $this->aLogo;
//请求AI翻译
$sContent = '';
$aSearch['{#content#}'] = json_encode($aAuthor);
$aResult = json_decode($this->_createContentForOpenAI($aSearch,1),true);
$aContent = empty($aResult['data']) ? [] : array_column($aResult['data'], null,'email');
foreach ($aAuthor as $key => $value) {
//请求AI翻译
$aInfo = empty($aContent[$value['email']]) ? [] : $aContent[$value['email']];
//替换模版
$aAuthorSerach = [];
//所属单位处理
$value['company'] = empty($aInfo['company']) ? $value['company'] : $aInfo['company'];
if(!empty($value['company'])){
$value['company'] =str_replace('', ',', $value['company']);
$aCompany = explode(',', $value['company']);
}
$value['company'] = empty($aCompany[0]) ? $value['company'] : trim($aCompany[0],',');
//简介处理
$value['introduction'] = empty($aInfo['introduction']) ? $value['introduction'] : $aInfo['introduction'];
//职称处理
$value['technical'] = empty($aInfo['technical']) ? $value['technical'] : $aInfo['technical'];
if(empty($value['introduction'])){//简介为空不处理
$sTechnical = empty($value['technical']) ? '老师' : $value['technical'];
$sAuthorInfo .= $value['company'].$value['author_name'].$sTechnical.',';
continue;
}
//缓缓处理进行模版替换
foreach ($value as $k => $val) {
if($k == 'icon'){//头像拼接处理
$sIconInfo = empty($val) ? $aLogo['url'] : $val;
$aParsedUrl = parse_url($sIconInfo);
if(empty($aParsedUrl['scheme'])){
$aAuthorSerach['{###icon_path###}'] = trim($this->sSubmissionUrl,'/').$this->sUserIcon.$val;
}else{
$aAuthorSerach['{###icon_path###}'] = $val;
}
$aAuthorSerach['{###icon_path###}'] = trim($aAuthorSerach['{###icon_path###}'],'/');
}else{
$aAuthorSerach['{###'.$k.'###}'] = trim($val,'/');
}
}
$aAuthorSerachInfo[] = str_replace(array_keys($aAuthorSerach), array_values($aAuthorSerach), $sAuthorTemplate);
}
//通讯作者列表
//获取通讯作者汇总模版
$sAuthorTemplatePath = ROOT_PATH."public/wechatTemplate/template_".$iTemplateId.'/author_summary.html';
if (!file_exists($sAuthorTemplatePath)) {
return json_encode(['status' => 3, 'msg' => 'Corresponding author template does not exist','data' => '']);
}
if (!is_readable($sAuthorTemplatePath)) {
return json_encode(['status' => 4, 'msg' => 'The corresponding author template is unreadable','data' => '']);
}
$sAuthorTemplate = file_get_contents($sAuthorTemplatePath);
//数据处理内容替换
$sInfo = empty($aAuthorSerachInfo) ? '' : implode("\n", $aAuthorSerachInfo);
$sAuthorTemplate = empty($sInfo) ? '' : str_replace('{###author_info###}', $sInfo, $sAuthorTemplate);
//通讯作者字符串处理
$sAuthorInfo = trim($sAuthorInfo,',');
return json_encode(['status' => 1,'msg' => 'success','data' => ['author_info' => $sAuthorTemplate,'author' => trim($sAuthorInfo,';')]]);
}
/**
* 处理期刊主题模版
* @param article_id 文章ID
*/
private function dealTemplateTopic($sTopic = '',$iTemplateId = 0){
if(empty($iTemplateId)){
return json_encode(['status' => 2,'template does not exist']);
}
if(empty($sTopic)){
return json_encode(['status' => 2,'topic does not exist']);
}
//获取主题模版
$sTemplatePath = ROOT_PATH."public/wechatTemplate/template_".$iTemplateId.'/topic.html';
if (!file_exists($sTemplatePath)) {
return json_encode(['status' => 4, 'msg' => 'Corresponding author template does not exist']);
}
if (!is_readable($sTemplatePath)) {
return json_encode(['status' => 5, 'msg' => 'The corresponding author template is unreadable']);
}
$sTemplate = file_get_contents($sTemplatePath);
//数据处理
$sTopic = trim($sTopic);
$aTopic = empty($sTopic) ? [] : explode(',', $sTopic);
$aSearchInfo = [];
if(!empty($aTopic)){
foreach ($aTopic as $key => $value) {
$aSerach['{###topic_name###}'] = empty($value) ? '' : strip_tags($value);
$aSearchInfo[] = str_replace(array_keys($aSerach), array_values($aSerach), $sTemplate);
}
}
//获取期刊主题汇总模版
$sTemplatePath = ROOT_PATH."public/wechatTemplate/template_".$iTemplateId.'/topic_summary.html';
if (!file_exists($sTemplatePath)) {
return json_encode(['status' => 4, 'msg' => 'Corresponding author template does not exist']);
}
if (!is_readable($sTemplatePath)) {
return json_encode(['status' => 5, 'msg' => 'The corresponding author template is unreadable']);
}
$sTemplate = file_get_contents($sTemplatePath);
//模版数据替换
$sTopicName = empty($aSearchInfo) ? '' : implode("\n", $aSearchInfo);
$sTemplate = empty($sTopicName) ? '' : str_replace('{###topic_name###}', $sTopicName, $sTemplate);
return json_encode(['status' => 1,'msg' => 'success','data' => $sTemplate]);
}
/**
* 处理往期推荐
* @param article_id 文章ID
*/
private function dealTemplatePreviousRecommend($aArticle=[],$iTemplateId = 0){
if(empty($iTemplateId)){
return json_encode(['status' => 2,'template does not exist']);
}
//获取往期推荐模版
$sTemplatePath = ROOT_PATH."public/wechatTemplate/template_".$iTemplateId.'/previous_recommend.html';
if (!file_exists($sTemplatePath)) {
return json_encode(['status' => 3, 'msg' => 'Corresponding author template does not exist']);
}
if (!is_readable($sTemplatePath)) {
return json_encode(['status' => 4, 'msg' => 'The corresponding author template is unreadable']);
}
$sTemplate = file_get_contents($sTemplatePath);
//判断是否关联文章
if(empty($aArticle['related'])){
return json_encode(['status' => 6, 'msg' => 'Unrelated articles']);
}
//获取关联文章信息
$aWhere = ['article_id' => ['in',json_decode($aArticle['related'],true)],'is_delete' => 2];
$aRecommend = Db::name('ai_article')->field('article_id')->where($aWhere)->select();
$aSearchInfo = [];
if(!empty($aRecommend)){
$aId = array_column($aRecommend, 'article_id');
//查询是否推送到微信公众号并且已发布
$aWhere['template_id'] = $iTemplateId;//,'is_publish' => 1
$aWechatArticle = Db::name('ai_wechat_article')->field('article_id,media_url')->where($aWhere)->select();
if(empty($aWechatArticle)){
return json_encode(['status' => 6, 'msg' => 'No article published on WeChat official account was found']);
}
//查询文章图片
$aWhere = ['article_id' => ['in',$aId],'is_delete' => 2];
$aArticleMaterial = Db::name('ai_article_material')->where($aWhere)->column('article_id,media_url');
foreach ($aWechatArticle as $key => $value) {
$aSearch['{###wechat_url###}'] = $value['media_url'];
$sIcon = empty($aArticleMaterial[$value['article_id']]) ? '' : $aArticleMaterial[$value['article_id']];
$aSearch['{###wechat_media_url###}'] = $sIcon;
$aSearchInfo[] = str_replace(array_keys($aSearch), array_values($aSearch), $sTemplate);
}
}
//获取往期推荐汇总模版
$sTemplatePath = ROOT_PATH."public/wechatTemplate/template_".$iTemplateId.'/previous_recommend_summary.html';
if (!file_exists($sTemplatePath)) {
return json_encode(['status' => 3, 'msg' => 'Corresponding author template does not exist']);
}
if (!is_readable($sTemplatePath)) {
return json_encode(['status' => 4, 'msg' => 'The corresponding author template is unreadable']);
}
$sTemplate = file_get_contents($sTemplatePath);
//数据处理内容替换
$sInfo = empty($aSearchInfo) ? '' : implode("\n", $aSearchInfo);
$sTemplate = empty($sInfo) ? '' : str_replace('{###previous_recommend###}', $sInfo, $sTemplate);
return json_encode(['status' => 1,'msg' => 'success','data' => $sTemplate]);
}
/**
* 文章同步到微信公众号草稿箱
* @param article_id 文章ID
*/
public function syncWechat(){
//获取参数
$aParam = $this->request->post();
//文章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)){
return json_encode(['status' => 2, 'msg' => 'Please select an article']);
}
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']);
}
//查询文章是否存在
$aWhere = ['article_id' => $iArticleId,'is_delete' => 2];
$aAiArticle = Db::name('ai_article')->field('title_chinese as title,author,journal_id,digest')->where($aWhere)->find();
if(empty($aAiArticle)){
return json_encode(['status' => 3, 'msg' => 'The article does not exist']);
}
//数据处理
$sTitle = empty($aAiArticle['title']) ? '' : $aAiArticle['title'];
$sTitle = mb_convert_encoding($sTitle, 'UTF-8', 'auto');
// 截断处理
$aAiArticle['title'] = $this->truncateByBytes($sTitle, 64);
$sDigest = empty($aAiArticle['digest']) ? '' : $aAiArticle['digest'];
// 编码转换
$sDigest = mb_convert_encoding($sDigest, 'UTF-8', 'auto');
$aAiArticle['digest'] = $this->truncateByBytes($sDigest, 120);
//查询该模版是否推送到微信公众号
$aWhere['template_id'] = $iTemplateId;
$aWhere['wechat_id'] = $sWechatId;
$aWechatArticle = Db::name('ai_wechat_article')->where($aWhere)->find();
if(!empty($aWechatArticle)){
return json_encode(['status' => 4, 'msg' => 'Already uploaded to draft box']);
}
//查询文章封面
$aMaterial = Db::name('ai_article_material')->field('media_id as thumb_media_id')->where('article_id',$iArticleId)->find();
if(!empty($aMaterial)){
$aAiArticle += $aMaterial;
}
unset($aAiArticle['journal_id']);
$aParam += $aAiArticle;
//查询期刊微信公众号配置
$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']);
}
//获取模版生成内容
$aTemplateParam = ['article_id' => $iArticleId,'template_id' => $iTemplateId,'is_sync' => 1,'wechat_id' => $sWechatId];
$aTemplateParam += $aJournalInfo;
$aResult = json_decode($this->getTemplateContent($aTemplateParam),true);
$sMsg = empty($aResult['msg']) ? 'The content is empty' : $aResult['msg'];
$aData = empty($aResult['data']) ? [] : $aResult['data'];
if(empty($aData['template'])){
return json_encode(['status' => 4, 'msg' => $sMsg]);
}
//请求微信公众号接口推送
$aParam['content'] = $aData['template'];
$aParam['template_id'] = $iTemplateId;
$aParam += $aJournalInfo;
$oWechat = new Wechat;
$aResult = $oWechat->addDraft($aParam);
return $aResult;
}
/**
* 字符串处理
* @param article_id 文章ID
*/
private function truncateByBytes($str, $maxBytes) {
$len = 0;
$result = '';
$i = 0;
while ($i < mb_strlen($str, 'UTF-8')) {
$char = mb_substr($str, $i, 1, 'UTF-8');
$charBytes = strlen($char); // UTF-8 下实际字节数
if ($len + $charBytes > $maxBytes) {
break;
}
$result .= $char;
$len += $charBytes;
$i++;
}
return $result;
}
/**
* 获取Ai生成文章状态
* @param article_id 文章ID
*/
public function getAiArticleStatus(){
$aParam = $this->request->post();
//AI生成文章ID
$iArticleId = empty($aParam['article_id']) ? '' : $aParam['article_id'];
if(empty($iArticleId)){
return json_encode(['status' => 2,'msg' => 'Please select the article to be generated']);
}
//查询AI生成的文章内容
$aWhere = ['is_delete' => 2];
if(!empty($iArticleId)){
$aWhere['article_id'] = $iArticleId;
}
$aAiArticle = Db::name('ai_article')->field('is_generate')->where($aWhere)->find();
// 生成状态
$aMsg = [1 => 'The data has been generated, please proceed with the next steps',2 => 'AI content generation in progress',3 => 'AI content not generated'];
$iStatus = empty($aAiArticle['is_generate']) ? 3 : $aAiArticle['is_generate'];
$sMsg = empty($aMsg[$iStatus]) ? 'illegal request' : $aMsg[$iStatus];
return json_encode(['status' => 1,'msg' => $sMsg,'data' => ['is_generate' => $iStatus]]);
}
/**
* 获取公众号信息
*/
public function getWechatLists(){
$aWhere = ['state' => 0,'wechat_app_id' => ['<>',''],'wechat_app_secret' => ['<>','']];
$aJournal = Db::name('journal')->field('issn,wechat_name')->where($aWhere)->select();
return json_encode(['status' => 1,'msg' => 'success','data' => $aJournal]);
}
}