作者 lyh

gx

<?php
/**
* @remark :
* @name :AiBlogTask.php
* @author :lyh
* @method :post
* @time :2025/2/14 11:14
*/
namespace App\Console\Commands\AiBlog;
use App\Models\Ai\AiBlog;
use App\Models\Project\ProjectAiSetting;
use App\Services\AiBlogService;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use App\Models\Project\AiBlogTask as AiBlogTaskModel;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use function Symfony\Component\String\s;
class AiBlogTask extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'save_ai_blog';
/**
* The console command description.
*
* @var string
*/
protected $description = '查询ai_blog是否已经生成';
public function handle(){
$aiBlogTaskModel = new AiBlogTaskModel();
while (true){
$info = $aiBlogTaskModel->where('status',1)->orderBy('id','asc')->first();
if($info === false){
sleep(10);
}
$info = $info->toArray();
echo '开始->任务id:' . $info['task_id'] . PHP_EOL . date('Y-m-d H:i:s');
//获取配置
$aiSettingInfo = $this->getSetting($info['project_id']);
$aiBlogService = new AiBlogService();
$aiBlogService->mch_id = $aiSettingInfo['mch_id'];
$aiBlogService->key = $aiSettingInfo['key'];
$aiBlogService->task_id = $info['task_id'];
$result = $aiBlogService->getDetail();
if($result['status'] != 200){
sleep(5);
continue;
}
//修改任务状态
$aiBlogTaskModel->edit(['status'=>2],['id'=>$info['id']]);
//保存当前项目ai_blog数据
ProjectServer::useProject($info['project_id']);
$aiBlogModel = new AiBlog();
$aiBlogModel->edit(['new_title'=>$result['data']['title'] ?? '','text'=>$result['data']['text'] ?? '','status'=>2],['task_id'=>$info['task_id']]);
DB::disconnect('custom_mysql');
echo '结束->任务id:' . $info['task_id'] . PHP_EOL . date('Y-m-d H:i:s');
}
return true;
}
/**
* @remark :获取项目配置
* @name :getSetting
* @author :lyh
* @method :post
* @time :2025/2/14 11:27
*/
public function getSetting($project_id){
$ai_cache = Cache::get('ai_blog_'.$project_id);
if($ai_cache){
return $ai_cache;
}
$projectAiSettingModel = new ProjectAiSetting();
$aiSettingInfo = $projectAiSettingModel->read(['project_id'=>$project_id]);
Cache::put('ai_blog_'.$project_id,$aiSettingInfo,3600);
return $aiSettingInfo;
}
}
... ...
... ... @@ -56,7 +56,7 @@ class AiBlogLogic extends BaseLogic
$aiBlogTaskModel = new AiBlogTask();
$aiBlogTaskModel->addReturnId(['project_id'=>$this->user['project_id'],'task_id'=>$result['data']['task_id'],'status'=>$result['data']['status']]);
$aiBlogModel = new AiBlog();
$aiBlogModel->addReturnId(['keywords'=>$this->param['keyword'], 'status'=>$result['data']['status'], 'task_id'=>$result['data']['task_id'],'project_id'=>$this->user['project_id'],
$aiBlogModel->addReturnId(['keyword'=>$this->param['keyword'], 'status'=>$result['data']['status'], 'task_id'=>$result['data']['task_id'],'project_id'=>$this->user['project_id'],
]);
}catch (\Exception $e){
$this->fail('请求ai_blog失败,请联系管理员');
... ...
... ... @@ -19,7 +19,7 @@ class AiBlogService
public $webhook = 'https://develop.globalso.com/api/ai_webhook';//回调地址
public $task_id = '';//任务id
/**
* @remark :创建项目
* @name :createProject
... ... @@ -98,7 +98,7 @@ class AiBlogService
'mch_id'=>$this->mch_id,
'sign'=>$this->sign,
];
$result = http_post($request_url,$param);
$result = http_post($request_url,json_encode($param,true));
return $result;
}
... ... @@ -123,4 +123,23 @@ class AiBlogService
$sign = strtoupper($sign);
return $sign;
}
/**
* @remark :获取文章详情
* @name :getDetail
* @author :lyh
* @method :post
* @time :2025/2/14 11:23
*/
public function getDetail(){
$request_url = $this->url.'api/result/detail';
$param = [
'mch_id'=>$this->mch_id,
'task_id'=>$this->task_id,
];
$this->sign = $this->generateSign($param,$this->key);
$param['sign'] = $this->sign;
$result = http_post($request_url,json_encode($param,true));
return $result;
}
}
... ...