定时推送微信公众号
This commit is contained in:
167
application/api/controller/Cronwechat.php
Normal file
167
application/api/controller/Cronwechat.php
Normal file
@@ -0,0 +1,167 @@
|
||||
<?php
|
||||
namespace app\api\controller;
|
||||
use think\Controller;
|
||||
use think\Db;
|
||||
use app\api\controller\Aiarticle;
|
||||
class Cronwechat extends Controller
|
||||
{
|
||||
|
||||
|
||||
//期刊接口地址
|
||||
protected $iTemplateId = 1;//模版ID
|
||||
|
||||
/**
|
||||
* 查询生成的AI文章推送到微信公众号草稿箱
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function articlePushDraft(){
|
||||
|
||||
$aParam = $this->request->post();
|
||||
//获取前一天日期
|
||||
$sDate = empty($aParam['date']) ? date('Y-m-d', strtotime('-1 day')) : $aParam['date'];
|
||||
//模版ID
|
||||
$iTemplateId = empty($aParam['template_id']) ? $this->iTemplateId : $aParam['template_id'] ;
|
||||
//获取AI生成的文章
|
||||
$sStartDate = strtotime($sDate.' 00:00:00');
|
||||
//结束时间
|
||||
$sEndDate = strtotime($sDate.'23:59:59');
|
||||
$aWhere = ['is_delete' => 2,'update_time' => ['between',[$sStartDate,$sEndDate]]];
|
||||
$aAiArticleId = Db::name('ai_article')->where($aWhere)->column('article_id');
|
||||
if(empty($aAiArticleId)){
|
||||
$this->showMessage('未查询到AI生成的文章:'.$sDate."\n\n",2);
|
||||
exit;
|
||||
}
|
||||
|
||||
//查询草稿箱是否生成
|
||||
$aWhere = ['article_id' => ['in',$aAiArticleId],'is_delete' => 2];
|
||||
$aWechatArticle = Db::name('ai_wechat_article')->where($aWhere)->column('article_id');
|
||||
|
||||
//数据处理
|
||||
$oAiarticle = new Aiarticle;
|
||||
|
||||
//日志记录
|
||||
$aLog = [];
|
||||
foreach ($aAiArticleId as $value) {
|
||||
if(in_array($value, $aWechatArticle)){
|
||||
continue;
|
||||
}
|
||||
//获取上传素材接口
|
||||
$aUploadParm = ['article_id' => $value,'template_id' => $iTemplateId];
|
||||
$aResult = json_decode($oAiarticle->uploadMaterial([$aUploadParm]),true);
|
||||
$iStatus = empty($aResult['status']) ? 0 : $aResult['status'];
|
||||
$sMsg = empty($aResult['msg']) ? '操作异常'."\n" : '上传素材失败:'.$aResult['msg'];
|
||||
$aLogInfo = ['article_id' => $value,'type' => 1,'msg' => $sMsg,'status' => 1,'create_time' => time()];
|
||||
if($iStatus != 1){
|
||||
$aLogInfo['status'] = 2;
|
||||
$aLog[] = $aLogInfo;
|
||||
$this->showMessage('文章ID:'.$value.'====='.$sMsg."\n",2);
|
||||
continue;
|
||||
}
|
||||
$aLog[] = $aLogInfo;
|
||||
|
||||
//推送草稿箱
|
||||
$aResult = json_decode($oAiarticle->syncWechat([$aUploadParm]),true);
|
||||
$iStatus = empty($aResult['status']) ? 0 : $aResult['status'];
|
||||
$sMsg = empty($aResult['msg']) ? '操作异常'."\n" : '推送草稿箱失败:'.$aResult['msg'];
|
||||
$aLogInfo = ['article_id' => $value,'type' => 2,'msg' => $sMsg,'status' => 1,'create_time' => time()];
|
||||
if($iStatus != 1){
|
||||
$aLogInfo['status'] = 2;
|
||||
$aLog[] = $aLogInfo;
|
||||
$this->showMessage('文章ID:'.$value.'====='.$sMsg."\n",2);
|
||||
continue;
|
||||
}
|
||||
$aLog[] = $aLogInfo;
|
||||
}
|
||||
//插入操作日志
|
||||
if(!empty($aLog)){
|
||||
$result = Db::name('wechat_crontab_log')->insertAll($aLog);
|
||||
if($result === false){
|
||||
$this->showMessage('插入日志记录失败:'.json_encode($aLog)."\n",2);
|
||||
}
|
||||
}
|
||||
$this->showMessage('AI生成的文章推送草稿箱成功:'.$sDate."\n",1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询推送到微信公众号草稿箱的AI文章并发布
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function articlePublishDraft(){
|
||||
|
||||
$aParam = $this->request->post();
|
||||
//获取前一天日期
|
||||
$sDate = empty($aParam['date']) ? date('Y-m-d', strtotime('-1 day')) : $aParam['date'];
|
||||
//模版ID
|
||||
$iTemplateId = empty($aParam['template_id']) ? $this->iTemplateId : $aParam['template_id'] ;
|
||||
//获取AI生成的文章
|
||||
$sStartDate = strtotime($sDate.' 00:00:00');
|
||||
//结束时间
|
||||
$sEndDate = strtotime($sDate.'23:59:59');
|
||||
$aWhere = ['is_delete' => 2,'update_time' => ['between',[$sStartDate,$sEndDate]]];
|
||||
//查询推送到草稿箱未发布的文章
|
||||
$aWhere = ['publish_status' => '-1','is_delete' => 2];
|
||||
$aWechatArticle = Db::name('ai_wechat_article')->field('article_id,template_id,media_id')->where($aWhere)->select();
|
||||
if(empty($aWechatArticle)){
|
||||
$this->showMessage('未查询到需要发布的文章:'.$sDate."\n\n",2);
|
||||
exit;
|
||||
}
|
||||
|
||||
//数据处理
|
||||
$oAiarticle = new Aiarticle;
|
||||
//日志记录
|
||||
$aLog = [];
|
||||
foreach ($aWechatArticle as $value) {
|
||||
$aLogInfo = ['article_id' => $value,'type' => 3,'msg' =>'','status' => 1,'create_time' => time()];
|
||||
if(empty($value['media_id'])){
|
||||
$sMsg = '需要发布的文章:'.$value['article_id']."未查询到media_id\n\n";
|
||||
$aLogInfo['status'] = 2;
|
||||
$aLogInfo['msg'] = $sMsg;
|
||||
$aLog[] = $aLogInfo;
|
||||
$this->showMessage($sMsg,2);
|
||||
continue;
|
||||
}
|
||||
//发布草稿箱
|
||||
$aUploadParm = ['article_id' => $value['article_id'],'template_id' => $value['template_id']];
|
||||
$aResult = json_decode($oAiarticle->publishDraft([$aUploadParm]),true);
|
||||
$iStatus = empty($aResult['status']) ? 0 : $aResult['status'];
|
||||
$sMsg = empty($aResult['msg']) ? '操作异常'."\n" : '发布草稿箱文章失败:'.$aResult['msg'];
|
||||
if($iStatus != 1){
|
||||
$aLogInfo['msg'] = $sMsg;
|
||||
$aLogInfo['status'] = 2;
|
||||
$aLog[] = $aLogInfo;
|
||||
$this->showMessage('文章ID:'.$value.'====='.$sMsg."\n",2);
|
||||
continue;
|
||||
}
|
||||
$aLog[] = $aLogInfo;
|
||||
}
|
||||
//插入操作日志
|
||||
if(!empty($aLog)){
|
||||
$result = Db::name('wechat_crontab_log')->insertAll($aLog);
|
||||
if($result === false){
|
||||
$this->showMessage('插入日志记录失败:'.json_encode($aLog)."\n",2);
|
||||
}
|
||||
}
|
||||
$this->showMessage('AI生成的文章发布草稿箱成功:'.$sDate."\n",1);
|
||||
}
|
||||
/**
|
||||
*
|
||||
* 格式化信息输出
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
* @author huangpu
|
||||
* @date 2018.09.28
|
||||
* @param $[message] [<显示信息>]
|
||||
* @param $[status] [<输出信息1成功,2失败>]
|
||||
*/
|
||||
private function showMessage($message, $status = 1) {
|
||||
if ($status == 1) {
|
||||
echo "[SUCCESS]";
|
||||
} else {
|
||||
echo "[ERROR]";
|
||||
}
|
||||
echo date("Y-m-d H:i:s") . " " . $message . "\n";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user