From 267a645e273819e2725f683bfab47c61c549f2db Mon Sep 17 00:00:00 2001 From: chengxl Date: Wed, 14 May 2025 16:57:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=AC=E4=BC=97=E5=8F=B7=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/api/controller/Aiarticle.php | 335 +++++++++++++++++++---- 1 file changed, 276 insertions(+), 59 deletions(-) diff --git a/application/api/controller/Aiarticle.php b/application/api/controller/Aiarticle.php index 5a9c856..6927d27 100644 --- a/application/api/controller/Aiarticle.php +++ b/application/api/controller/Aiarticle.php @@ -32,31 +32,15 @@ class Aiarticle extends Base protected $timeout = 60; 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'; // + protected $sJournalUrl = '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 => ' - **核心要求** - 1️ 请将姓名翻译成中文 - {#author_name#} - 2 请将单位翻译成中文 - {#company#} - 3 请将简介翻译成中文 - {#introduction#} - 4 请将职称翻译成中文 - {#technical#} - - **输出格式** - 格式内容 - ```json - { - "author_name": "", - "company": "", - "introduction": "", - "technical": "" - } - ', + **核心要求** + "将以下内容翻译为中文,仅返回翻译结果,不要解释:\n {#content#}" + ', + 2 => ' **核心要求** 1️ ​内容涵盖哪些学科及方法请罗列 @@ -236,11 +220,79 @@ class Aiarticle extends Base //获取文章是否生成AI内容 $aResult = json_decode($this->getArticle($iArticleId),true); - if($aResult['status'] != 1){ + $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($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']); + } + } + + //判断是否生成 + if($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); + // //返回数据 + // return json_encode(['status' => 1,'msg' => 'Data generation takes some time, please do not cancel this operation','data' => ['ai_article_id' => $iId]]); + } + + public function createForOpenAi($aArticleContent = [],$aAiArticle = []){ + + if(empty($aArticleContent)){ + + $aParam = $this->request->post(); + + $iId = empty($aParam['ai_article_id']) ? 0 : $aParam['ai_article_id']; + + //查询AI内容是否生成 + $aAiArticle = json_decode($this->getAiArticle(0,1,$iId),true); + $aAiArticleContent = empty($aAiArticle['data']) ? [] : $aAiArticle['data']; + $aAiArticle = empty($aAiArticleContent['ai_article']) ? [] : $aAiArticleContent['ai_article']; + if(empty($aAiArticle)){ + return json_encode(['status' => 4,'msg' => 'The article has not been generated, please confirm']); + } + //判断是否生成 + if($aAiArticle['is_generate'] == 1){ + return json_encode(['status' => 5,'msg' => 'The data has been generated, please proceed with the next steps']); + } + $iArticleId = empty($aAiArticle['article_id']) ? '' : $aAiArticle['article_id']; + + //获取文章是否生成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']; + } + + $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']; @@ -294,27 +346,76 @@ class Aiarticle extends Base $aContent['journal_id'] = $aArticle['journal_id']; - //获取AI生成表里的数据判断是新增或更新 - $aAiContent = json_decode($this->getAiArticle($iArticleId,1),true); - $aAiContent = empty($aAiContent['data']) ? [] : $aAiContent['data']; - $aAiArticle = empty($aAiContent['ai_article']) ? [] : $aAiContent['ai_article']; - $aAiAuthor = empty($aAiContent['ai_article_autho']) ? [] : $aAiContent['ai_article_autho']; - if(empty($aAiArticle)){//新增 - //执行数据入库 - $aResult = $this->addAiArticle($aContent); - } - if(!empty($aAiArticle)){//更新 - if(empty($aAiArticle['ai_article_id'])){ - return json_encode(array('status' => 8,'msg' => 'No AI article found to update')); - } - //执行数据入库 - $aContent['ai_article_id'] = $aAiArticle['ai_article_id']; - $aContent['author_list'] = $aAuthor; - $aResult = $this->updateAiArticle($aContent); - } - + //更新数据库 + $aContent['ai_article_id'] = $iId; + $aResult = $this->updateAiArticle($aContent); return $aResult; } + // public function createForOpenAi($aArticleContent = [],$iId = 0){ + + + // if(empty($aArticleContent) || empty($iId)){ + // return json_encode(['status' => 2,'msg' => 'data is empty']); + // } + + + // //文章数据 + // $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']; + + + // //生成期刊图片二维码 + // $aJournalQrCode = $this->createJournalQrCode($aJournal); + // if(empty($aJournalQrCode['qrcode_url'])){ + // return json_encode(['status' => 3,'msg' => 'Journal QR code not generated']); + // } + + // //生成文章图片二维码 + // $aArticleQrCode = $this->createArticleQrCode($aArticle); + // if(empty($aArticleQrCode['qrcode_url'])){ + // return json_encode(['status' => 4,'msg' => 'Article QR code not generated']); + // } + + // //查询文章内容 + // $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; + // $aResult = $this->updateAiArticle($aContent); + // return $aResult; + // } /** * 处理文章引用 @@ -427,17 +528,36 @@ class Aiarticle extends Base * 获取Ai生成文章信息 * @param article_id 文章ID */ - public function getAiArticle($iArticleId = 0,$iSelectAuthor = 2){ + public function getAiArticle($iArticleId = 0,$iSelectAuthor = 2,$iAiArticleId = 0){ $aParam = $this->request->post(); - $iArticleId = empty($iArticleId) ? $aParam['article_id'] : $iArticleId; + + //文章ID + $iArticleId = empty($iArticleId) ? '' : $iArticleId; + $iArticleIdParam = empty($aParam['article_id']) ? '' : $aParam['article_id']; + $iArticleId = empty($iArticleId) ? $iArticleIdParam : $iArticleId; + //AI生成文章ID + $iAiArticleId = empty($iAiArticleId) ? '' : $iAiArticleId; + $iAiArticleIdParam = empty($aParam['ai_article_id']) ? '' : $aParam['ai_article_id']; + $iAiArticleId = empty($iAiArticleId) ? $iAiArticleIdParam : $iAiArticleId; + + if(empty($iArticleId) && empty($iAiArticleId)){ + return json_encode(['status' => 2,'msg' => 'Please select the article to be generated']); + } + //查询AI生成的文章内容 - $aWhere['article_id'] = $iArticleId; - $aWhere['is_delete'] = 2; + $aWhere = ['is_delete' => 2]; + 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' => 1,'msg' => 'data is null','data' => ['ai_article' => []]]); } + $iArticleId = empty($aAiArticle['article_id']) ? 0 : $aAiArticle['article_id']; //查询文章通讯作者 $aAiAuthor = []; @@ -532,7 +652,7 @@ class Aiarticle extends Base $aResult = ['status' => 1,'msg' => 'AI article updated successfully','data' => []]; //获取参数 - $aParam = $this->request->post(); + $aParam = empty($aParam) ? $this->request->post() : $aParam; //主键ID $iAiArticleId = empty($aParam['ai_article_id']) ? 0 : $aParam['ai_article_id']; @@ -571,13 +691,78 @@ class Aiarticle extends Base 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(!empty($aUser['author_name'])){ + $aUpdate = ['localname' => $aUser['author_name']]; + Db::name('user')->where('user_id',$value['user_id'])->limit(1)->update($aUpdate); + } + //更新作者简介和单位 + $aUpdateReviewer = []; + if(!empty($aUser['technical'])){ + $aUpdateReviewer['technical'] = $aUser['technical']; + } + if(!empty($aUser['introduction'])){ + $aUpdateReviewer['introduction'] = $aUser['introduction']; + } + if(!empty($aUser['company'])){ + $aUpdateReviewer['company'] = $aUser['company']; + } + if(!empty($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); } @@ -809,26 +994,32 @@ class Aiarticle extends Base $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翻译 - $aSearch['{#company#}'] = empty($value['company']) ? '' : $value['company']; - $aSearch['{#technical#}'] = empty($value['technical']) ? '' : $value['technical']; - $aSearch['{#introduction#}'] = empty($value['introduction']) ? '' : $value['introduction']; - $aResult = $this->_createContentForOpenAI($aSearch,1); - $aResult = json_decode($aResult,true); - if($aResult['status'] == 1){ - $aContent = empty($aResult['data']) ? [] : $aResult['data']; - $value['company'] = empty($aContent['company']) ? $value['company'] : $aContent['company']; - $value['technical'] = empty($aContent['technical']) ? $value['technical'] : $aContent['technical']; - $value['introduction'] = empty($aContent['introduction']) ? $value['introduction'] : $aContent['introduction']; - } + $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['technical'] = empty($aInfo['technical']) ? $value['technical'] : $aInfo['technical']; + //简介处理 + $value['introduction'] = empty($aInfo['introduction']) ? $value['introduction'] : $aInfo['introduction']; + foreach ($value as $k => $val) { if($k == 'icon'){ $aAuthorSerach['{###icon_path###}'] = empty($val) ? $aLogo['url'] : trim($this->sSubmissionUrl,'/').'/public/usericon/'.$val; @@ -1137,7 +1328,6 @@ class Aiarticle extends Base $aParam['template_content'] = $aData['template']; $aParam['template_id'] = $iTemplateId; $aResult = $this->addDraft($aParam); - echo '
';var_dump($aResult);exit;
         return $aResult;
     }
 
@@ -1430,7 +1620,6 @@ class Aiarticle extends Base
                 curl_close($curl);
                 return FALSE;
             }
-var_dump($this->sError,$result);
             $result = json_decode($result, true);
 
             if (!empty($result['access_token'])) {
@@ -1880,4 +2069,32 @@ $response['publish_id'] = '100000001';
         $aResult['data'] = $aInsert;
         return json_encode($aResult);
     }
+
+    /**
+     * 获取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]]);
+    }
 }