diff --git a/application/api/controller/Aiarticle.php b/application/api/controller/Aiarticle.php index fb16e27..4c07754 100644 --- a/application/api/controller/Aiarticle.php +++ b/application/api/controller/Aiarticle.php @@ -77,7 +77,7 @@ class Aiarticle extends Base **输出格式** 中文格式[英文简写忽略首字母大写] 格式内容 - ```json{"covered": "【总字数<=100】", "overview": "【总字数>=1000】", "summary": "【总字数>=3500】", "title_chinese": "", "content": ""}' + ```json{"covered": "【总字数<=100】", "overview": "【总字数>=1000】", "summary": "【总字数>=350】", "title_chinese": "", "content": ""}' ]; //文章图片icon地址 @@ -220,8 +220,8 @@ class Aiarticle extends Base * @param model 接口模型 * @param stream 是否流式输出 true是false否 */ - public function create(){ + public function create(){ //获取参数 $aParam = $this->request->post(); $iArticleId = empty($aParam['article_id']) ? '' : $aParam['article_id']; @@ -250,8 +250,7 @@ class Aiarticle extends Base 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内容是否生成 $aAiArticle = json_decode($this->getAiArticle(['article_id' => $iArticleId]),true); $aAiArticleContent = empty($aAiArticle['data']) ? [] : $aAiArticle['data']; @@ -274,7 +273,24 @@ class Aiarticle extends Base } //请求OPENAI - return $this->createForOpenAi($aArticleContent,$aAiArticle); + $aResult = json_decode($this->createForOpenAi($aArticleContent,$aAiArticle),true); + $iStatus = empty($aResult['status']) ? 0 : $aResult['status']; + $sMsg = empty($aResult['msg']) ? '生成失败' : $aResult['msg']; + if($iStatus == 1){ + //四小时后推送上传素材并推送草稿箱 + $iDelaySeconds = 3;//4 * 3600; // 4小时的秒数 + Queue::later($iDelaySeconds,'app\api\job\WechatMaterial@fire', ['article_id' => $iArticleId], 'WechatMaterial'); + $sMsg = '文章AI内容生成成功'; + }else{ + $iStatus = 2; + } + + //插入日志记录 + $oMaterial = new Material; + $aLogInfo = ['article_id' => $iArticleId,'type' => 5,'msg' =>$sMsg,'status' => $iStatus,'create_time' => time()]; + $result = json_decode($oMaterial->addWechatLog($aLogInfo),true); + return json_encode($aResult); + } /** @@ -929,7 +945,7 @@ class Aiarticle extends Base //请求AI翻译 $sContent = ''; $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'); foreach ($aAuthor as $key => $value) { @@ -1202,6 +1218,17 @@ class Aiarticle extends Base $oWechat = new Wechat; $aResult = json_decode($oWechat->addDraft($aParam),true); $aData = empty($aResult['data']) ? [] : $aResult['data']; + //插入操作日志 + $iStatus = empty($aResult['status']) ? 0 : $aResult['status']; + $sMsg = empty($aResult['msg']) ? '' : $aResult['msg']; + if ($iStatus != 1) { + $iStatus = 2; + $sMsg = empty($sMsg) ? '草稿箱推送失败' : $sMsg; + } + //插入日志记录 + $oMaterial = new Material; + $aLogInfo = ['article_id' => $iArticleId,'type' => 2,'msg' =>$sMsg,'status' => $iStatus,'create_time' => time()]; + $result = json_decode($oMaterial->addWechatLog($aLogInfo),true); if(empty($aData)){ return json_encode($aResult); } @@ -1211,6 +1238,9 @@ class Aiarticle extends Base if($result === false){ return json_encode(['status' => 5,'msg' => "Article data insertion failed"]); } + + //写入发布队列 + // Queue::later(60,'app\api\job\WechatPublishDraft@fire', ['article_id' => $iArticleId], 'WechatPublishDraft'); return json_encode(['status' => 1,'msg' => 'Upload draft box successfully','data' => []]); } @@ -1357,18 +1387,33 @@ class Aiarticle extends Base //调用发布接口 $oWechat = new Wechat; $aResult = json_decode($oWechat->publishDraft($aJournalInfo),true); - $aData = empty($aResult['data']) ? [] : $aResult['data']; - if(empty($aData)){ - return json_encode($aResult); + $iStatus = empty($aResult['status']) ? 0 : $aResult['status']; + $sMsg = empty($aResult['msg']) ? '' : $aResult['msg']; + if ($iStatus != 1) { + $iStatus = 2; + $sMsg = empty($sMsg) ? '发布失败' : $sMsg; + } + + //插入日志记录 + $oMaterial = new Material; + $aLogInfo = ['article_id' => $iArticleId,'type' => 3,'msg' =>$sMsg,'status' => $iStatus,'create_time' => time()]; + $result = json_decode($oMaterial->addWechatLog($aLogInfo),true); + if($iStatus != 1){ + return json_encode($aResult); } //更新数据表 //更新文章表publish_id + $aData = empty($aResult['data']) ? [] : $aResult['data']; $aUpdate = ['update_time' => time(),'is_publish' => 1,'publish_id' => $aData['publish_id'],'publish_status' => 1]; $result = Db::name('ai_wechat_article')->where('id',$iId)->limit(1)->update($aUpdate); if($result === false){ return json_encode(['status' => 5,'msg' => "Failed to update the status of the published article"]); } + + //发布成功查询状态 + $iDelaySeconds = 30;//4 * 3600; // 30秒数 + Queue::later($iDelaySeconds,'app\api\job\WechatQueryStatus@fire', ['article_id' => $iArticleId], 'WechatQueryStatus'); return json_encode(['status' => 1,'msg' => 'Draft box article successfully published','data' => []]); } /** @@ -1494,6 +1539,13 @@ class Aiarticle extends Base return json_encode(['status' => 2, 'msg' => 'Please select an article']); } + //判断素材是否上传 + $oMaterial = new Material; + $aLog = json_decode($oMaterial->getWechatLog(['article_id' => $iArticleId,'type' => 1,'status' =>1]),true); + if(!empty($aLog['data'])){ + return json_encode(['status' => -1,'msg' => 'The material has been uploaded','data' => []]); + } + //获取AI生成文章内容 $aAiContent = json_decode($this->getAiArticle(['article_id' => $iArticleId,'is_select_author' => 1]),true); $aAiContent = empty($aAiContent['data']) ? [] : $aAiContent['data']; @@ -1568,30 +1620,24 @@ class Aiarticle extends Base } //上传微信公众号用到的素材 $aUpload += ['article_id' => $iArticleId,'journal_id' => $aArticle['journal_id'],'wechat_app_id' => $sWechatAppId,'wechat_app_secret' => $sWechatAppSecret]; - $oMaterial = new Material; $aResult = json_decode($oMaterial->uploadMaterial($aUpload),true); $iStatus = empty($aResult['status']) ? 0 : $aResult['status']; - if($iStatus != 1){ - return json_encode($aResult); + $sMsg = empty($aResult['msg']) ? '' : $aResult['msg']; + if ($iStatus != 1) { + $iStatus = 2; + $sMsg = empty($sMsg) ? '素材上传失败' : $sMsg; } - Queue::later(60,'app\api\job\WechatDraft@fire', ['article_id' => $iArticleId], 'uploadDraft'); - } + //插入日志记录 + $aLogInfo = ['article_id' => $iArticleId,'type' => 1,'msg' =>$sMsg,'status' => $iStatus,'create_time' => time()]; + $result = json_decode($oMaterial->addWechatLog($aLogInfo),true); - /** - * 对接微信公众号日志 - */ - 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' => '非法操作']); + //素材上传成功调用上传草稿箱的任务 + if($iStatus == 1){ + //1分钟后推送草稿箱 + $iDelaySeconds = 60;//4 * 3600; // 1分钟的秒数 + Queue::later($iDelaySeconds,'app\api\job\WechatDraft@fire', ['article_id' => $iArticleId], 'WechatDraft'); } - $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' => '日志插入成功']); + return json_encode(['status' => $iStatus,'msg' => $sMsg,'data' => $aLogInfo]); } /** diff --git a/application/api/controller/Reviewer.php b/application/api/controller/Reviewer.php index 2ca55a6..260f45c 100644 --- a/application/api/controller/Reviewer.php +++ b/application/api/controller/Reviewer.php @@ -2274,6 +2274,7 @@ class Reviewer extends Base //数据处理 foreach ($aReviewerMajor as $key => $value) { $aReviewerMajor[$key]['major_title'] = getMajorStr($value['major_id']); + $aReviewerMajor[$key]['shu'] = getMajorShu($value['major_id']); } return json_encode(['status' => 1,'msg' => "Reviewer's field information is empty",'data' => $aReviewerMajor]); } @@ -2313,12 +2314,10 @@ class Reviewer extends Base $aAdd[] = ['major_id' => $value,'user_id' => $iUserId,'ctime' => time(),'state' => 0]; } $result = Db::name('major_to_user')->insertAll($aAdd); - echo Db::getLastSql(); } if(!empty($aDelete)){//修改状态为1 $aWhere = ['user_id' => $iUserId,'state' => 0,'major_id' => ['in',$aDelete]]; $result = Db::name('major_to_user')->where($aWhere)->limit(count($aDelete))->update(['state' => 1,'ctime' => time()]); - echo Db::getLastSql(); } Db::commit(); return json_encode(['status' => 1,'msg' => "Reviewer's field has been successfully modified"]); diff --git a/application/api/job/ArticleAiCreateContent.php b/application/api/job/ArticleAiCreateContent.php new file mode 100644 index 0000000..23b2a01 --- /dev/null +++ b/application/api/job/ArticleAiCreateContent.php @@ -0,0 +1,61 @@ +getRawBody())){ + $aJob = json_decode($job->getRawBody(), true); + $aParam = [ + 'job_id' => empty($aJob['id']) ? '' : $aJob['id'], + 'job_class' => get_class($this), + 'status' => 0, + 'create_time' => time(), + 'params' => json_encode($aJob, JSON_UNESCAPED_UNICODE) + ]; + $iLogId = $oQueueJob->addLog($aParam); + // 步骤1:上传素材(图片) + $iArticleId = empty($data['article_id']) ? 0 : $data['article_id']; + if (!empty($iArticleId)) { + //上传素材 + $oAiarticle = new Aiarticle; + $aResult = json_decode($oAiarticle->create($data),true); + $iStatus = empty($aResult['status']) ? 0 : $aResult['status']; + $sMsg = empty($aResult['msg']) ? '文章AI内容声场失败' : $aResult['msg']; + } + } + $job->delete(); + + //更新任务状态 + $aParam = ['log_id' => $iLogId,'status' => 1,'update_time' => time(),'error' => $sMsg]; + $oQueueJob->updateLog($aParam); + // // 记录日志 + // \think\Log::info("ArticleAiCreateContent延迟任务执行成功: ".json_encode($data)); + } catch (\Exception $e) { + + //实例化 + $oQueueJob = new QueueJob; + //更新任务状态 + $sMsg = empty($e->getMessage()) ? '任务出错' : $e->getMessage(); + $aParam = ['log_id' => $iLogId,'status' => 2,'update_time' => time(),'error' => $sMsg]; + $oQueueJob->updateLog($aParam); + $job->delete(); + // // 记录错误日志 + // \think\Log::error("ArticleAiCreateContent延迟任务失败: ".$e->getMessage()); + }finally { + gc_collect_cycles(); // 强制垃圾回收 + } + } + +} \ No newline at end of file diff --git a/application/api/job/WechatDraft.php b/application/api/job/WechatDraft.php new file mode 100644 index 0000000..d23b518 --- /dev/null +++ b/application/api/job/WechatDraft.php @@ -0,0 +1,62 @@ +getRawBody())){ + $aJob = json_decode($job->getRawBody(), true); + $aParam = [ + 'job_id' => empty($aJob['id']) ? '' : $aJob['id'], + 'job_class' => get_class($this), + 'status' => 0, + 'create_time' => time(), + 'params' => json_encode($aJob, JSON_UNESCAPED_UNICODE) + ]; + $iLogId = $oQueueJob->addLog($aParam); + // 步骤1:上传素材(图片) + $iArticleId = empty($data['article_id']) ? 0 : $data['article_id']; + if (!empty($iArticleId)) { + //上传素材 + $oAiarticle = new Aiarticle; + $aResult = json_decode($oAiarticle->syncWechat($data),true); + $iStatus = empty($aResult['status']) ? 0 : $aResult['status']; + $sMsg = empty($aResult['msg']) ? '上传草稿箱失败' : $aResult['msg']; + } + } + $job->delete(); + + //更新任务状态 + $aParam = ['log_id' => $iLogId,'status' => 1,'update_time' => time(),'error' => $sMsg]; + $oQueueJob->updateLog($aParam); + // // 记录日志 + // \think\Log::info("WechatDraft-延迟任务执行成功: ".json_encode($data)); + } catch (\Exception $e) { + + //实例化 + $oQueueJob = new QueueJob; + //更新任务状态 + $sMsg = empty($e->getMessage()) ? '任务出错' : $e->getMessage(); + $aParam = ['log_id' => $iLogId,'status' => 2,'update_time' => time(),'error' => $sMsg]; + $oQueueJob->updateLog($aParam); + $job->delete(); + // // 记录错误日志 + // \think\Log::error("WechatDraft-延迟任务失败: ".$e->getMessage()); + }finally { + gc_collect_cycles(); // 强制垃圾回收 + } + } + +} \ No newline at end of file diff --git a/application/api/job/WechatDraftPublish.php b/application/api/job/WechatDraftPublish.php new file mode 100644 index 0000000..54d9924 --- /dev/null +++ b/application/api/job/WechatDraftPublish.php @@ -0,0 +1,62 @@ +getRawBody())){ + $aJob = json_decode($job->getRawBody(), true); + $aParam = [ + 'job_id' => empty($aJob['id']) ? '' : $aJob['id'], + 'job_class' => get_class($this), + 'status' => 0, + 'create_time' => time(), + 'params' => json_encode($aJob, JSON_UNESCAPED_UNICODE) + ]; + $iLogId = $oQueueJob->addLog($aParam); + // 步骤1:上传素材(图片) + $iArticleId = empty($data['article_id']) ? 0 : $data['article_id']; + if (!empty($iArticleId)) { + //上传素材 + $oAiarticle = new Aiarticle; + $aResult = json_decode($oAiarticle->publishDraft($data),true); + $iStatus = empty($aResult['status']) ? 0 : $aResult['status']; + $sMsg = empty($aResult['msg']) ? '草稿箱发布失败' : $aResult['msg']; + } + } + $job->delete(); + + //更新任务状态 + $aParam = ['log_id' => $iLogId,'status' => 1,'update_time' => time(),'error' => $sMsg]; + $oQueueJob->updateLog($aParam); + // // 记录日志 + // \think\Log::info("WechatDraft-延迟任务执行成功: ".json_encode($data)); + } catch (\Exception $e) { + + //实例化 + $oQueueJob = new QueueJob; + //更新任务状态 + $sMsg = empty($e->getMessage()) ? '任务出错' : $e->getMessage(); + $aParam = ['log_id' => $iLogId,'status' => 2,'update_time' => time(),'error' => $sMsg]; + $oQueueJob->updateLog($aParam); + $job->delete(); + // // 记录错误日志 + // \think\Log::error("WechatDraft-延迟任务失败: ".$e->getMessage()); + }finally { + gc_collect_cycles(); // 强制垃圾回收 + } + } + +} \ No newline at end of file diff --git a/application/api/job/WechatMaterial.php b/application/api/job/WechatMaterial.php index dceae42..cd681b3 100644 --- a/application/api/job/WechatMaterial.php +++ b/application/api/job/WechatMaterial.php @@ -1,40 +1,63 @@ uploadMaterial($data),true); - $iStatus = empty($aResult['status']) ? 0 : $aResult['status']; - $sMsg = empty($aResult['msg']) ? '' : $aResult['msg']; - if ($iStatus != 1) { - $iStatus = 2; - $sMsg = empty($sMsg) ? '素材上传失败' : $sMsg; - } - //插入日志记录 - $aLogInfo = ['article_id' => $data['article_id'],'type' => 1,'msg' =>$sMsg,'status' => $iStatus,'create_time' => time()]; - $result = json_decode($oAiarticle->addWechatLog($aLogInfo),true); - $iStatus = empty($result['status']) ? 0 : $result['status']; - $sMsg = empty($result['msg']) ? '插入日志记录失败' : $result['msg']; - if($iStatus != 1){ - Log::error($sMsg."\n".json_encode($aLogInfo) . $e->getMessage()); + + //实例化 + $oQueueJob = new QueueJob; + $sMsg = '上传素材成功'; + if(!empty($job->getRawBody())){ + $aJob = json_decode($job->getRawBody(), true); + $aParam = [ + 'job_id' => empty($aJob['id']) ? '' : $aJob['id'], + 'job_class' => get_class($this), + 'status' => 0, + 'create_time' => time(), + 'params' => json_encode($aJob, JSON_UNESCAPED_UNICODE) + ]; + $iLogId = $oQueueJob->addLog($aParam); + // 步骤1:上传素材(图片) + $iArticleId = empty($data['article_id']) ? 0 : $data['article_id']; + if (!empty($iArticleId)) { + //上传素材 + $oAiarticle = new Aiarticle; + $aResult = json_decode($oAiarticle->uploadMaterial($data),true); + $iStatus = empty($aResult['status']) ? 0 : $aResult['status']; + $sMsg = empty($aResult['msg']) ? '上传素材失败' : $aResult['msg']; } } + $job->delete(); + //更新任务状态 + $aParam = ['log_id' => $iLogId,'status' => 1,'update_time' => time(),'error' => $sMsg]; + $oQueueJob->updateLog($aParam); + + // // 记录日志 + // \think\Log::info("WechatMaterial-延迟任务执行成功: ".json_encode($data)); } catch (\Exception $e) { - Log::error("上传素材任务异常:" . $e->getMessage()); + + //实例化 + $oQueueJob = new QueueJob; + //更新任务状态 + $sMsg = empty($e->getMessage()) ? '任务出错' : $e->getMessage(); + $aParam = ['log_id' => $iLogId,'status' => 2,'update_time' => time(),'error' => $sMsg]; + $oQueueJob->updateLog($aParam); + $job->delete(); + + // // 记录错误日志 + // \think\Log::error("WechatMaterial-延迟任务失败: ".$e->getMessage()); }finally { gc_collect_cycles(); // 强制垃圾回收 } } - } \ No newline at end of file diff --git a/application/api/job/WechatQueryStatus.php b/application/api/job/WechatQueryStatus.php new file mode 100644 index 0000000..3a1d821 --- /dev/null +++ b/application/api/job/WechatQueryStatus.php @@ -0,0 +1,61 @@ +getRawBody())){ + $aJob = json_decode($job->getRawBody(), true); + $aParam = [ + 'job_id' => empty($aJob['id']) ? '' : $aJob['id'], + 'job_class' => get_class($this), + 'status' => 0, + 'create_time' => time(), + 'params' => json_encode($aJob, JSON_UNESCAPED_UNICODE) + ]; + $iLogId = $oQueueJob->addLog($aParam); + // 步骤1:上传素材(图片) + $iArticleId = empty($data['article_id']) ? 0 : $data['article_id']; + if (!empty($iArticleId)) { + //上传素材 + $oAiarticle = new Aiarticle; + $aResult = json_decode($oAiarticle->queryStatus($data),true); + $iStatus = empty($aResult['status']) ? 0 : $aResult['status']; + $sMsg = empty($aResult['msg']) ? '上传草稿箱失败' : $aResult['msg']; + } + } + $job->delete(); + + //更新任务状态 + $aParam = ['log_id' => $iLogId,'status' => 1,'update_time' => time(),'error' => $sMsg]; + $oQueueJob->updateLog($aParam); + // // 记录日志 + // \think\Log::info("WechatQueryStatus延迟任务执行成功: ".json_encode($data)); + } catch (\Exception $e) { + + //实例化 + $oQueueJob = new QueueJob; + //更新任务状态 + $sMsg = empty($e->getMessage()) ? '任务出错' : $e->getMessage(); + $aParam = ['log_id' => $iLogId,'status' => 2,'update_time' => time(),'error' => $sMsg]; + $oQueueJob->updateLog($aParam); + $job->delete(); + // // 记录错误日志 + // \think\Log::error("WechatQueryStatus延迟任务失败: ".$e->getMessage()); + }finally { + gc_collect_cycles(); // 强制垃圾回收 + } + } + +} \ No newline at end of file diff --git a/application/common/Material.php b/application/common/Material.php index 7a8b82f..d053b3e 100644 --- a/application/common/Material.php +++ b/application/common/Material.php @@ -527,6 +527,7 @@ class Material if(empty($iArticleId)){ return json_encode(['status' => 2,'msg' => 'article_id is empty']); } + //期刊ID $iJournalId = empty($aParam['journal_id']) ? '' : $aParam['journal_id']; if(empty($iJournalId)){ @@ -705,4 +706,36 @@ class Material } return json_encode(['status' => 1,'msg' => 'Successfully obtained data','data' => $aData]); } + + + /** + * 对接微信公众号日志 + */ + 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' => '日志插入成功']); + } + + /** + * 获取公众号 + */ + public function getWechatLog($aParam = []){ + //获取参数 + $aParam = empty($aParam) ? $this->request->post() : $aParam; + //必填参数验证 + if(empty($aParam['article_id']) || empty($aParam['type'])){ + return json_encode(['status' => 2, 'msg' => '非法操作']); + } + $aLog = Db::name('wechat_api_log')->where($aParam)->find(); + return json_encode(['status' => 1, 'msg' => '获取数据成功','data' => $aLog]); + } } diff --git a/application/common/QueueJob.php b/application/common/QueueJob.php new file mode 100644 index 0000000..eaf1c3d --- /dev/null +++ b/application/common/QueueJob.php @@ -0,0 +1,48 @@ +aField; + $aInsert = []; + foreach ($aField as $key => $value) { + if(isset($aParam[$value])){ + $aInsert[$value] = $aParam[$value]; + } + } + $result = 0; + if(!empty($aInsert)){ + $result = DB::name('wechat_queue_logs')->insertGetId($aParam); + } + return $result; + } + + // 记录任务成功 + public function updateLog($aParam = []) { + + $iLogId = empty($aParam['log_id']) ? 0 : $aParam['log_id']; + if(empty($iLogId)){ + return false; + } + //数据处理 + $aField = $this->aField; + $aUpdate = []; + foreach ($aField as $key => $value) { + if(isset($aParam[$value])){ + $aUpdate[$value] = $aParam[$value]; + } + } + unset($aParam['log_id']); + $result = DB::name('wechat_queue_logs')->where('log_id',$iLogId)->limit(1)->update($aUpdate); + return $result; + } +} +?> \ No newline at end of file