From 947cdf0a943f470f15a8b483722edb5cdb250543 Mon Sep 17 00:00:00 2001 From: chengxl Date: Fri, 30 May 2025 16:07:30 +0800 Subject: [PATCH] =?UTF-8?q?=E9=98=9F=E5=88=97=E7=9B=B8=E5=85=B3=E7=9A=84?= =?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/job/WechatMaterial.php | 63 ++++++++++++++++++-------- 1 file changed, 43 insertions(+), 20 deletions(-) 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