正在显示
3 个修改的文件
包含
109 行增加
和
3 行删除
app/Console/Commands/AiBlog/AiBlogTask.php
0 → 100644
| 1 | +<?php | ||
| 2 | +/** | ||
| 3 | + * @remark : | ||
| 4 | + * @name :AiBlogTask.php | ||
| 5 | + * @author :lyh | ||
| 6 | + * @method :post | ||
| 7 | + * @time :2025/2/14 11:14 | ||
| 8 | + */ | ||
| 9 | + | ||
| 10 | +namespace App\Console\Commands\AiBlog; | ||
| 11 | + | ||
| 12 | +use App\Models\Ai\AiBlog; | ||
| 13 | +use App\Models\Project\ProjectAiSetting; | ||
| 14 | +use App\Services\AiBlogService; | ||
| 15 | +use App\Services\ProjectServer; | ||
| 16 | +use Illuminate\Console\Command; | ||
| 17 | +use App\Models\Project\AiBlogTask as AiBlogTaskModel; | ||
| 18 | +use Illuminate\Support\Facades\Cache; | ||
| 19 | +use Illuminate\Support\Facades\DB; | ||
| 20 | +use function Symfony\Component\String\s; | ||
| 21 | + | ||
| 22 | +class AiBlogTask extends Command | ||
| 23 | +{ | ||
| 24 | + /** | ||
| 25 | + * The name and signature of the console command. | ||
| 26 | + * | ||
| 27 | + * @var string | ||
| 28 | + */ | ||
| 29 | + protected $signature = 'save_ai_blog'; | ||
| 30 | + | ||
| 31 | + /** | ||
| 32 | + * The console command description. | ||
| 33 | + * | ||
| 34 | + * @var string | ||
| 35 | + */ | ||
| 36 | + protected $description = '查询ai_blog是否已经生成'; | ||
| 37 | + | ||
| 38 | + public function handle(){ | ||
| 39 | + $aiBlogTaskModel = new AiBlogTaskModel(); | ||
| 40 | + while (true){ | ||
| 41 | + $info = $aiBlogTaskModel->where('status',1)->orderBy('id','asc')->first(); | ||
| 42 | + if($info === false){ | ||
| 43 | + sleep(10); | ||
| 44 | + } | ||
| 45 | + $info = $info->toArray(); | ||
| 46 | + echo '开始->任务id:' . $info['task_id'] . PHP_EOL . date('Y-m-d H:i:s'); | ||
| 47 | + //获取配置 | ||
| 48 | + $aiSettingInfo = $this->getSetting($info['project_id']); | ||
| 49 | + $aiBlogService = new AiBlogService(); | ||
| 50 | + $aiBlogService->mch_id = $aiSettingInfo['mch_id']; | ||
| 51 | + $aiBlogService->key = $aiSettingInfo['key']; | ||
| 52 | + $aiBlogService->task_id = $info['task_id']; | ||
| 53 | + $result = $aiBlogService->getDetail(); | ||
| 54 | + if($result['status'] != 200){ | ||
| 55 | + sleep(5); | ||
| 56 | + continue; | ||
| 57 | + } | ||
| 58 | + //修改任务状态 | ||
| 59 | + $aiBlogTaskModel->edit(['status'=>2],['id'=>$info['id']]); | ||
| 60 | + //保存当前项目ai_blog数据 | ||
| 61 | + ProjectServer::useProject($info['project_id']); | ||
| 62 | + $aiBlogModel = new AiBlog(); | ||
| 63 | + $aiBlogModel->edit(['new_title'=>$result['data']['title'] ?? '','text'=>$result['data']['text'] ?? '','status'=>2],['task_id'=>$info['task_id']]); | ||
| 64 | + DB::disconnect('custom_mysql'); | ||
| 65 | + echo '结束->任务id:' . $info['task_id'] . PHP_EOL . date('Y-m-d H:i:s'); | ||
| 66 | + } | ||
| 67 | + return true; | ||
| 68 | + } | ||
| 69 | + | ||
| 70 | + /** | ||
| 71 | + * @remark :获取项目配置 | ||
| 72 | + * @name :getSetting | ||
| 73 | + * @author :lyh | ||
| 74 | + * @method :post | ||
| 75 | + * @time :2025/2/14 11:27 | ||
| 76 | + */ | ||
| 77 | + public function getSetting($project_id){ | ||
| 78 | + $ai_cache = Cache::get('ai_blog_'.$project_id); | ||
| 79 | + if($ai_cache){ | ||
| 80 | + return $ai_cache; | ||
| 81 | + } | ||
| 82 | + $projectAiSettingModel = new ProjectAiSetting(); | ||
| 83 | + $aiSettingInfo = $projectAiSettingModel->read(['project_id'=>$project_id]); | ||
| 84 | + Cache::put('ai_blog_'.$project_id,$aiSettingInfo,3600); | ||
| 85 | + return $aiSettingInfo; | ||
| 86 | + } | ||
| 87 | +} |
| @@ -56,7 +56,7 @@ class AiBlogLogic extends BaseLogic | @@ -56,7 +56,7 @@ class AiBlogLogic extends BaseLogic | ||
| 56 | $aiBlogTaskModel = new AiBlogTask(); | 56 | $aiBlogTaskModel = new AiBlogTask(); |
| 57 | $aiBlogTaskModel->addReturnId(['project_id'=>$this->user['project_id'],'task_id'=>$result['data']['task_id'],'status'=>$result['data']['status']]); | 57 | $aiBlogTaskModel->addReturnId(['project_id'=>$this->user['project_id'],'task_id'=>$result['data']['task_id'],'status'=>$result['data']['status']]); |
| 58 | $aiBlogModel = new AiBlog(); | 58 | $aiBlogModel = new AiBlog(); |
| 59 | - $aiBlogModel->addReturnId(['keywords'=>$this->param['keyword'], 'status'=>$result['data']['status'], 'task_id'=>$result['data']['task_id'],'project_id'=>$this->user['project_id'], | 59 | + $aiBlogModel->addReturnId(['keyword'=>$this->param['keyword'], 'status'=>$result['data']['status'], 'task_id'=>$result['data']['task_id'],'project_id'=>$this->user['project_id'], |
| 60 | ]); | 60 | ]); |
| 61 | }catch (\Exception $e){ | 61 | }catch (\Exception $e){ |
| 62 | $this->fail('请求ai_blog失败,请联系管理员'); | 62 | $this->fail('请求ai_blog失败,请联系管理员'); |
| @@ -19,7 +19,7 @@ class AiBlogService | @@ -19,7 +19,7 @@ class AiBlogService | ||
| 19 | 19 | ||
| 20 | public $webhook = 'https://develop.globalso.com/api/ai_webhook';//回调地址 | 20 | public $webhook = 'https://develop.globalso.com/api/ai_webhook';//回调地址 |
| 21 | 21 | ||
| 22 | - | 22 | + public $task_id = '';//任务id |
| 23 | /** | 23 | /** |
| 24 | * @remark :创建项目 | 24 | * @remark :创建项目 |
| 25 | * @name :createProject | 25 | * @name :createProject |
| @@ -98,7 +98,7 @@ class AiBlogService | @@ -98,7 +98,7 @@ class AiBlogService | ||
| 98 | 'mch_id'=>$this->mch_id, | 98 | 'mch_id'=>$this->mch_id, |
| 99 | 'sign'=>$this->sign, | 99 | 'sign'=>$this->sign, |
| 100 | ]; | 100 | ]; |
| 101 | - $result = http_post($request_url,$param); | 101 | + $result = http_post($request_url,json_encode($param,true)); |
| 102 | return $result; | 102 | return $result; |
| 103 | } | 103 | } |
| 104 | 104 | ||
| @@ -123,4 +123,23 @@ class AiBlogService | @@ -123,4 +123,23 @@ class AiBlogService | ||
| 123 | $sign = strtoupper($sign); | 123 | $sign = strtoupper($sign); |
| 124 | return $sign; | 124 | return $sign; |
| 125 | } | 125 | } |
| 126 | + | ||
| 127 | + /** | ||
| 128 | + * @remark :获取文章详情 | ||
| 129 | + * @name :getDetail | ||
| 130 | + * @author :lyh | ||
| 131 | + * @method :post | ||
| 132 | + * @time :2025/2/14 11:23 | ||
| 133 | + */ | ||
| 134 | + public function getDetail(){ | ||
| 135 | + $request_url = $this->url.'api/result/detail'; | ||
| 136 | + $param = [ | ||
| 137 | + 'mch_id'=>$this->mch_id, | ||
| 138 | + 'task_id'=>$this->task_id, | ||
| 139 | + ]; | ||
| 140 | + $this->sign = $this->generateSign($param,$this->key); | ||
| 141 | + $param['sign'] = $this->sign; | ||
| 142 | + $result = http_post($request_url,json_encode($param,true)); | ||
| 143 | + return $result; | ||
| 144 | + } | ||
| 126 | } | 145 | } |
-
请 注册 或 登录 后发表评论