diff --git a/application/api/controller/Aiarticle.php b/application/api/controller/Aiarticle.php index 473049f..bbdee2b 100644 --- a/application/api/controller/Aiarticle.php +++ b/application/api/controller/Aiarticle.php @@ -1085,6 +1085,11 @@ class Aiarticle extends Base 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'])){ + return json_encode(['status' => 6,'msg' => "The article type does not match"]); + } //查询文章是否存在 $aWhere = ['article_id' => $iArticleId,'is_delete' => 2]; $aAiArticle = Db::name('ai_article')->field('title_chinese as title,author,journal_id,digest')->where($aWhere)->find(); @@ -1092,17 +1097,6 @@ class Aiarticle extends Base 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; @@ -1110,6 +1104,26 @@ class Aiarticle extends Base if(!empty($aWechatArticle)){ return json_encode(['status' => 4, 'msg' => 'Already uploaded to draft box']); } + + //查询期刊微信公众号配置 + $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']); + } + + //数据处理-标题 + $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); + //是否打开评论,0不打开(默认),1打开 + $aParam['need_open_comment'] = empty($aParam['need_open_comment']) ? 1 : $aParam['need_open_comment']; + //是否粉丝才可评论,0所有人可评论(默认),1粉丝才可评论 + $aParam['only_fans_can_comment'] = empty($aParam['only_fans_can_comment']) ? 1 : $aParam['only_fans_can_comment']; + //查询文章封面 $aMaterial = Db::name('ai_article_material')->field('media_id as thumb_media_id')->where('article_id',$iArticleId)->find(); if(!empty($aMaterial)){ @@ -1117,13 +1131,6 @@ class Aiarticle extends Base } 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; @@ -1139,8 +1146,18 @@ class Aiarticle extends Base $aParam['template_id'] = $iTemplateId; $aParam += $aJournalInfo; $oWechat = new Wechat; - $aResult = $oWechat->addDraft($aParam); - return $aResult; + $aResult = json_decode($oWechat->addDraft($aParam),true); + $aData = empty($aResult['data']) ? [] : $aResult['data']; + if(empty($aData)){ + 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]; + $result = Db::name('ai_wechat_article')->insert($aParam); + if($result === false){ + return json_encode(['status' => 5,'msg' => "Article data insertion failed"]); + } + return json_encode(['status' => 1,'msg' => 'Upload draft box successfully','data' => []]); } /** @@ -1205,4 +1222,169 @@ class Aiarticle extends Base return json_encode(['status' => 1,'msg' => 'success','data' => $aJournal]); } + /** + * 公众号文章发布 + */ + public function publishDraft($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 + $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,'template_id' => $iTemplateId,'wechat_id' => $sWechatId]; + $aWechatArticle = Db::name('ai_wechat_article')->field('id,is_publish,media_id,publish_status')->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']; + + //判断是否发布 + if($aWechatArticle['publish_status'] != '-1'){ + return json_encode(['status' => 5, 'msg' => 'The article has been published, please confirm']); + } + + //查询期刊微信公众号配置 + $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['media_id'] = $sMediaId; + + //调用发布接口 + $oWechat = new Wechat; + $aResult = json_decode($oWechat->publishDraft($aJournalInfo),true); + $aData = empty($aResult['data']) ? [] : $aResult['data']; + if(empty($aData)){ + return json_encode($aResult); + } + + //更新数据表 + $aData['publish_id'] = '100000001'; + //更新文章表publish_id + $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"]); + } + return json_encode(['status' => 1,'msg' => 'Draft box article successfully published','data' => []]); + } + /** + * CURL 查询文章的发布状态 + * @param $sToken Token + * @param article_id array 文章ID + */ + public function queryStatus(){ + + //获取参数 + $aParam = $this->request->post(); + + //查询条件 + $aWhere = ['is_publish' => 1,'publish_id' => ['<>',''],'publish_status' => 1,'is_delete' => 2]; + //文章ID + if(!empty($aParam['article_id'])){ + $aWhere['article_id'] = $aParam['article_id']; + } + //模版ID + if(!empty($aParam['template_id'])){ + $aWhere['template_id'] = $aParam['template_id']; + } + //推送公众号Id + if(!empty($aParam['wechat_id'])){ + $aWhere['wechat_id'] = $aParam['wechat_id']; + } + //查询待发布文章信息 + $aWechatArticle = Db::name('ai_wechat_article')->where($aWhere)->field('id,article_id,template_id,wechat_id,publish_id')->select(); + if(empty($aWechatArticle)){ + return json_encode(['status' => 2,'msg' => 'No articles requiring synchronization status were found'],true); + } + + //查询期刊微信公众号配置 + $aIssn = array_unique(array_column($aWechatArticle, 'wechat_id')); + if(empty($aIssn)){ + return json_encode(['status' => 3,'msg' => 'Official account information is empty']); + } + $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']); + } + $aJournalInfo = array_column($aJournalInfo, null,'issn'); + //循环处理 + $oWechat = new Wechat; + $aInsert = $aUpdate = []; + foreach ($aWechatArticle as $key => $value) { + if(empty($value['wechat_id']) || empty($value['publish_id'])){ + continue; + } + //账号信息 + $aAccount = empty($aJournalInfo[$value['wechat_id']]) ? [] : $aJournalInfo[$value['wechat_id']]; + if(empty($aAccount['wechat_app_id']) || empty($aAccount['wechat_app_secret'])){ + continue; + } + $aAccount['publish_id'] = $value['publish_id']; + $aResult = json_decode($oWechat->queryStatus($aAccount),true); + $aData = empty($aResult['data']) ? [] : $aResult['data']; + + if(empty($aData)){ + continue; + } + //日志记录 + $aData = $this->dealStatusData($aData); + $aInsert[] = $aData; + //更新状态 + $aUpdate[$aData['publish_status']][] = $value['id']; + } + + Db::startTrans(); + //插入日志表 + if(!empty($aInsert)){ + $result = Db::name('wechat_article_publish_log')->insertAll($aInsert); + } + //更新文章表的发布状态 + if(!empty($aUpdate)){ + foreach ($aUpdate as $key => $value) { + if(empty($value)){ + continue; + } + $result = Db::name('ai_wechat_article')->whereIn('id',$value)->limit(count($value))->update(['publish_status' => $key,'update_time' => time()]); + } + } + Db::commit(); + return json_encode(['status' => 1,'msg' => 'success']); + } + + private function dealStatusData($aParam = []){ + + //log日志需要参数 + $aField = ['publish_id','publish_status' ,'article_id' ,'article_detail' ,'fail_idx' ,'create_time']; + $aResult = []; + foreach ($aField as $value) { + if($value == 'create_time'){ + $aResult[$value] = time(); + continue; + } + $aResult[$value] = empty($aParam[$value]) ? ' ' : $aParam[$value]; + if(is_array($aResult[$value])){ + $aResult[$value] = json_encode($aResult[$value]); + } + } + return $aResult; + } + }