GeoQuestionRes.php
5.5 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
/**
* @remark :
* @name :GeoQuestionRes.php
* @author :lyh
* @method :post
* @time :2025/7/3 15:13
*/
namespace App\Console\Commands\Geo;
use App\Models\Geo\GeoPlatform;
use App\Models\Geo\GeoQuestion;
use App\Models\Geo\GeoQuestionResult;
use App\Models\Project\Project;
use App\Services\Geo\GeoService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Redis;
class GeoQuestionRes extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'geo_question_result';
/**
* The console command description.
*
* @var string
*/
protected $description = 'geo设置请求获取结果';
public function handle(){
while (true){
$task_id = $this->getTaskId();
$questionModel = new GeoQuestion();//问题
$info = $questionModel->read(['id'=>$task_id]);
//获取当前项目的执行频率
$projectModel = new Project();
$projectInfo = $projectModel->read(['id'=>$info['project_id']],['geo_status','geo_frequency']);
if($projectInfo['geo_status'] == 0){
$questionModel->edit(['status'=>0],['id'=>$task_id]);
continue;
}
$questionArr = $info['question'];
if(empty($questionArr)){
echo date('Y-m-d H:i:s').'当前任务不存在问题。'.PHP_EOL;
$questionModel->edit(['status'=>0],['id'=>$task_id]);
}
//获取平台信息
$platformModel = new GeoPlatform();//平台
$platformArr = $platformModel->selectField(['status'=>$platformModel::STATUS_ON],'en_name');
if(empty($platformArr)){
echo date('Y-m-d H:i:s').'请求平台为空。'.PHP_EOL;
continue;
}
$geoService = new GeoService();
$keywordArr = $info['keywords'] ?? [];
$urlArr = $info['url'] ?? [];
$geoResultModel = new GeoQuestionResult();
foreach ($questionArr as $q_item){
foreach ($platformArr as $p_item){
echo '执行数据详情:'.$q_item.'/'.$p_item.PHP_EOL;
$keywords = [];//命中的关键词
$urls = [];//命中的网址
try {
$result_data = $geoService->setWebSearchChatAction($q_item,$p_item);
echo 'success:'.$result_data['code'].PHP_EOL;
if(isset($result_data) && $result_data['code'] == 200){
$keywords = $this->getKeywords($keywordArr,$result_data['text'] ?? []);
$urls = $this->getUrl($urlArr,$result_data['annotations'] ?? []);
}
}catch (\Exception $e){
echo $e->getMessage().PHP_EOL;
continue;
}
//保存一条结果记录
$data = [
'project_id'=>$info['project_id'],
'question_id'=>$info['id'],
'platform'=>$p_item,
'question'=>$q_item,
'keywords'=>json_encode($keywords ?? [],true),//命中的关键词
'text'=>json_encode($result_data ?? [],true),
'url'=>json_encode($urls ?? [],true),//命中的网址
];
$geoResultModel->addReturnId($data);
}
}
//更新下次执行时间
$questionModel->edit(['current_time'=>date('Y-m-d'),'next_time'=>date('Y-m-d', strtotime(date('Y-m-d') . ' +5 days'))],['id'=>$info['id']]);
}
}
/**
* @remark :获取命中的url
* @name :getUrl
* @author :lyh
* @method :post
* @time :2025/7/3 16:38
*/
public function getUrl($urlArr = [],$result_annotations = []){
$url = [];
if(!empty($urlArr) && !empty($result_annotations)){
foreach ($urlArr as $u_item){
foreach ($result_annotations as $a_item){
if (str_contains($a_item['url_citation']['url'], $u_item)) {
$url[] = $u_item;
}
}
}
}
return $url;
}
/**
* @remark :获取命中的关键词
* @name :getKeywords
* @author :lyh
* @method :post
* @time :2025/7/3 16:26
*/
public function getKeywords($keywordArr = [],$result_text = []){
$keywords = [];
if(!empty($keywordArr) && !empty($result_text)){
foreach ($keywordArr as $k_item){
if (str_contains($result_text, $k_item)) {
$keywords[] = $k_item;
}
}
}
return $keywords;
}
/**
* @remark :拉取任务id
* @name :getTaskId
* @author :lyh
* @method :post
* @time :2025/7/3 15:15
*/
public function getTaskId(){
$task_id = Redis::rpop('geo_question_result');
if(empty($task_id)){
$questionModel = new GeoQuestion();
$ids = $questionModel->selectField(['status'=>1,'next_time'=>['<=',date('Y-m-d')]],'id');
if(!empty($ids)){
foreach ($ids as $id) {
Redis::lpush('geo_question_result', $id);
}
}
$task_id = Redis::rpop('geo_question_result');
}
return $task_id;
}
}