AiBlogAuthorId.php
3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
/**
* @remark :
* @name :AiBlogAuthorId.php
* @author :lyh
* @method :post
* @time :2025/5/26 15:57
*/
namespace App\Console\Commands\Ai;
use App\Models\Domain\DomainInfo;
use App\Models\Project\AiBlogTask as AiBlogTaskModel;
use App\Models\Ai\AiBlogAuthor as AiBlogAuthorModel;
use App\Services\AiBlogService;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
class AiBlogAuthorId extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'save_ai_blog_author_id';
/**
* The console command description.
*
* @var string
*/
protected $description = '拉取对应作者的页面';
public $route = [];
public function handle(){
while (true){
//获取任务id
$task_id = $this->getTaskId();
if(empty($task_id)){
sleep(300);
continue;
}
$this->_action($task_id);
}
}
public function getTaskId()
{
$task_id = Redis::rpop('ai_blog_author_id');
if (empty($task_id)) {
$aiBlogTaskModel = new AiBlogTaskModel();
$ids = $aiBlogTaskModel->formatQuery(['status'=>$aiBlogTaskModel::STATUS_RUNNING, 'type'=>$aiBlogTaskModel::TYPE_AUTHOR_ID])->pluck('id');
if(!empty($ids)){
foreach ($ids as $id) {
Redis::lpush('ai_blog_author_id', $id);
}
}
$task_id = Redis::rpop('ai_blog_author_id');
}
return $task_id;
}
/**
* @remark :执行方法
* @name :_action
* @author :lyh
* @method :post
* @time :2025/5/26 16:06
*/
public function _action($task_id){
$aiBlogTaskModel = new AiBlogTaskModel();
$item = $aiBlogTaskModel->read(['id'=>$task_id]);
if($item === false){
echo '当前数据不存在.'.$item['id'].PHP_EOL;
return true;
}
$aiBlogService = new AiBlogService($item['project_id']);
ProjectServer::useProject($item['project_id']);
$aiBlogService->author_id = $item['task_id'];
$result = $aiBlogService->getAuthorDetail();
if(isset($result['status']) && $result['status'] == 200){
//当前作者的页面
$aiBlogAuthorModel = new AiBlogAuthorModel();
$authorInfo = $aiBlogAuthorModel->read(['author_id'=>$item['task_id']],['id','route']);
if($authorInfo !== false && !empty($result['data']['section'])){
$this->route[] = $authorInfo['route'];
$aiBlogAuthorModel->edit(['text'=>$result['data']['section']],['author_id'=>$item['task_id']]);
}
}
DB::disconnect('custom_mysql');
$aiBlogTaskModel->edit(['status'=>2],['id'=>$task_id]);
$this->sendCPost($item['project_id']);
return true;
}
/**
* @remark :通知C端
* @name :sendCPost
* @author :lyh
* @method :post
* @time :2025/5/26 16:21
*/
public function sendCPost($project_id){
$domainModel = new DomainInfo();
$domain = $domainModel->getProjectIdDomain($project_id);
$c_url = $domain.'api/update_page/';
$param = [
'project_id' => $project_id,
'type' => 1,
'route' => 3,
'url' => $this->route,
'language'=> [],
'is_sitemap' => 0
];
$res = http_post($c_url, json_encode($param,true));
echo 'notify: project id: ' . $project_id . ', result: ' . json_encode($res,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
}
}