作者 lyh

GXgeo设置

@@ -41,8 +41,7 @@ class GeoQuestionRes extends Command @@ -41,8 +41,7 @@ class GeoQuestionRes extends Command
41 public function handle() 41 public function handle()
42 { 42 {
43 while (true) { 43 while (true) {
44 -// $task_id = $this->getTaskId();  
45 - $task_id = 5; 44 + $task_id = $this->getTaskId();
46 if (empty($task_id)) { 45 if (empty($task_id)) {
47 sleep(300); 46 sleep(300);
48 continue; 47 continue;
@@ -51,27 +50,23 @@ class GeoQuestionRes extends Command @@ -51,27 +50,23 @@ class GeoQuestionRes extends Command
51 if (empty($task)) { 50 if (empty($task)) {
52 continue; 51 continue;
53 } 52 }
54 -  
55 $project = Project::select(['geo_status', 'geo_frequency'])->where(['id' => $task->project_id])->first(); 53 $project = Project::select(['geo_status', 'geo_frequency'])->where(['id' => $task->project_id])->first();
56 if (empty($project->get_status)) { 54 if (empty($project->get_status)) {
57 $task->status = GeoQuestion::STATUS_CLOSE; 55 $task->status = GeoQuestion::STATUS_CLOSE;
58 $task->save(); 56 $task->save();
59 continue; 57 continue;
60 } 58 }
61 -  
62 if ((empty($task->question) || FALSE == is_array($task->question)) || (empty($task->keywords) && empty($task->url))) { 59 if ((empty($task->question) || FALSE == is_array($task->question)) || (empty($task->keywords) && empty($task->url))) {
63 $this->output('task id: ' . $task_id . ', error: 任务数据缺失, continue!'); 60 $this->output('task id: ' . $task_id . ', error: 任务数据缺失, continue!');
64 $task->status = GeoQuestion::STATUS_CLOSE; 61 $task->status = GeoQuestion::STATUS_CLOSE;
65 $task->save(); 62 $task->save();
66 continue; 63 continue;
67 } 64 }
68 -  
69 $platforms = GeoPlatform::where(['status' => GeoPlatform::STATUS_ON])->get(); 65 $platforms = GeoPlatform::where(['status' => GeoPlatform::STATUS_ON])->get();
70 if ($platforms->isEmpty) { 66 if ($platforms->isEmpty) {
71 $this->output('未设置AI模型!'); 67 $this->output('未设置AI模型!');
72 continue; 68 continue;
73 } 69 }
74 -  
75 $geo_service = new GeoService(); 70 $geo_service = new GeoService();
76 $geoResultModel = new GeoQuestionResult(); 71 $geoResultModel = new GeoQuestionResult();
77 $geoLogModel = new GeoQuestionLog(); 72 $geoLogModel = new GeoQuestionLog();
@@ -89,8 +84,9 @@ class GeoQuestionRes extends Command @@ -89,8 +84,9 @@ class GeoQuestionRes extends Command
89 } else { 84 } else {
90 $result = $geo_service->getAiPlatformResult($question, $platform->en_name); 85 $result = $geo_service->getAiPlatformResult($question, $platform->en_name);
91 } 86 }
92 - if (empty($result['text'])) 87 + if (empty($result['text'])){
93 goto GET_RESULT; 88 goto GET_RESULT;
  89 + }
94 } catch (\Exception $e) { 90 } catch (\Exception $e) {
95 $this->output('task id:' . $task_id . ', question: ' . $question . ', platform: ' . $question . ', error: ' . $e->getMessage()); 91 $this->output('task id:' . $task_id . ', question: ' . $question . ', platform: ' . $question . ', error: ' . $e->getMessage());
96 if ($error_num < 5) { 92 if ($error_num < 5) {
@@ -98,7 +94,6 @@ class GeoQuestionRes extends Command @@ -98,7 +94,6 @@ class GeoQuestionRes extends Command
98 } 94 }
99 continue; 95 continue;
100 } 96 }
101 -  
102 // 命中文案 97 // 命中文案
103 $hit_data[] = $result['text']; 98 $hit_data[] = $result['text'];
104 if(FALSE == empty($result['annotations'])){ 99 if(FALSE == empty($result['annotations'])){
@@ -107,7 +102,6 @@ class GeoQuestionRes extends Command @@ -107,7 +102,6 @@ class GeoQuestionRes extends Command
107 $hit_data = array_merge($url, $title, $hit_data); 102 $hit_data = array_merge($url, $title, $hit_data);
108 } 103 }
109 $hit_string = implode(',', $hit_data); 104 $hit_string = implode(',', $hit_data);
110 -  
111 // 命中关键词和路由 105 // 命中关键词和路由
112 $hit_keyword = $hit_url = []; 106 $hit_keyword = $hit_url = [];
113 $hit = 0; 107 $hit = 0;
@@ -125,7 +119,6 @@ class GeoQuestionRes extends Command @@ -125,7 +119,6 @@ class GeoQuestionRes extends Command
125 $hit++; 119 $hit++;
126 } 120 }
127 } 121 }
128 -  
129 // 保存数据结果 122 // 保存数据结果
130 $geo_result = GeoQuestionResult::where(['project_id' => $task['project_id'], 'question_id' => $task['id'], 'platform' => $platform, 'question' => $question])->first(); 123 $geo_result = GeoQuestionResult::where(['project_id' => $task['project_id'], 'question_id' => $task['id'], 'platform' => $platform, 'question' => $question])->first();
131 $save_data = [ 124 $save_data = [
@@ -167,7 +160,6 @@ class GeoQuestionRes extends Command @@ -167,7 +160,6 @@ class GeoQuestionRes extends Command
167 'model' => 'Google AI Overview', 160 'model' => 'Google AI Overview',
168 'text' => '', 161 'text' => '',
169 ]; 162 ];
170 -  
171 if (FALSE == empty($data['ai_overview']['texts']) && is_array($data['ai_overview']['texts'])) { 163 if (FALSE == empty($data['ai_overview']['texts']) && is_array($data['ai_overview']['texts'])) {
172 $texts = []; 164 $texts = [];
173 foreach ($data['ai_overview']['texts'] as $item) { 165 foreach ($data['ai_overview']['texts'] as $item) {
@@ -185,7 +177,6 @@ class GeoQuestionRes extends Command @@ -185,7 +177,6 @@ class GeoQuestionRes extends Command
185 } 177 }
186 } 178 }
187 } 179 }
188 -  
189 // 第一层就有内容 180 // 第一层就有内容
190 if (FALSE == empty($item['snippet'])) { 181 if (FALSE == empty($item['snippet'])) {
191 // title 放到数组最前面 182 // title 放到数组最前面
@@ -194,7 +185,6 @@ class GeoQuestionRes extends Command @@ -194,7 +185,6 @@ class GeoQuestionRes extends Command
194 else 185 else
195 array_push($texts, $item['snippet']); 186 array_push($texts, $item['snippet']);
196 } 187 }
197 -  
198 // list类型 188 // list类型
199 if (FALSE == empty($item['type']) && $item['type'] == 'list' && FALSE == empty($item['list']) && is_array($item['list'])) { 189 if (FALSE == empty($item['type']) && $item['type'] == 'list' && FALSE == empty($item['list']) && is_array($item['list'])) {
200 foreach ($item['list'] as $list) { 190 foreach ($item['list'] as $list) {
@@ -209,128 +199,6 @@ class GeoQuestionRes extends Command @@ -209,128 +199,6 @@ class GeoQuestionRes extends Command
209 return $result; 199 return $result;
210 } 200 }
211 201
212 - public function handle1(){  
213 - while (true){  
214 - $task_id = $this->getTaskId();  
215 - if(empty($task_id)){  
216 - sleep(30);  
217 - continue;  
218 - }  
219 - echo date('Y-m-d H:i:s').'->task_id:'.$task_id.PHP_EOL;  
220 - $questionModel = new GeoQuestion();//问题  
221 - $info = $questionModel->read(['id'=>$task_id]);  
222 - //获取当前项目的执行频率  
223 - $projectModel = new Project();  
224 - $projectInfo = $projectModel->read(['id'=>$info['project_id']],['geo_status','geo_frequency']);  
225 - if($projectInfo['geo_status'] == 0){  
226 - $questionModel->edit(['status'=>0],['id'=>$task_id]);  
227 - continue;  
228 - }  
229 - $questionArr = $info['question'];  
230 - if(empty($questionArr)){  
231 - echo date('Y-m-d H:i:s').'当前任务不存在问题。'.PHP_EOL;  
232 - $questionModel->edit(['status'=>0],['id'=>$task_id]);  
233 - }  
234 - //获取平台信息  
235 - $platformModel = new GeoPlatform();//平台  
236 - $platformArr = $platformModel->selectField(['status'=>$platformModel::STATUS_ON],'en_name');  
237 - if(empty($platformArr)){  
238 - echo date('Y-m-d H:i:s').'请求平台为空。'.PHP_EOL;  
239 - continue;  
240 - }  
241 - $geoService = new GeoService();  
242 - $keywordArr = $info['keywords'] ?? [];  
243 - $urlArr = $info['url'] ?? [];  
244 - $geoResultModel = new GeoQuestionResult();  
245 - foreach ($questionArr as $q_item){  
246 - foreach ($platformArr as $p_item){  
247 - $keywords = [];//命中的关键词  
248 - $urls = [];//命中的网址  
249 - try {  
250 - $result_data = $geoService->setWebSearchChatAction($q_item,$p_item);  
251 - echo 'success:'.$result_data['code'].PHP_EOL;  
252 - if(isset($result_data) && $result_data['code'] == 200){  
253 - $result_data_annotations[] = $result_data['text'];  
254 - if(!empty($result_data['annotations'])){  
255 - $url = array_column(array_column($result_data['annotations'], 'url_citation'), 'url');  
256 - $title = array_column(array_column($result_data['annotations'], 'url_citation'), 'title');;  
257 - $result_data_annotations = array_merge($url,$title,$result_data_annotations);  
258 - }  
259 - $keywords = $this->getKeywords($keywordArr,$result_data_annotations ?? []);  
260 - $urls = $this->getUrl($urlArr,$result_data_annotations ?? []);  
261 - }  
262 - }catch (\Exception $e){  
263 - echo $e->getMessage().PHP_EOL;  
264 - continue;  
265 - }  
266 - //查询当前是否已有执行保存记录  
267 - $resultInfo = $geoResultModel->read(['project_id'=>$info['project_id'],'question_id'=>$info['id'],'platform'=>$p_item,'question'=>$q_item],['id']);  
268 - //保存一条结果记录  
269 - $data = [  
270 - 'project_id'=>$info['project_id'],  
271 - 'question_id'=>$info['id'],  
272 - 'platform'=>$p_item,  
273 - 'question'=>$q_item,  
274 - 'keywords'=>json_encode($keywords ?? [],true),//命中的关键词  
275 - 'text'=>json_encode($result_data ?? [],true),  
276 - 'url'=>json_encode($urls ?? [],true),//命中的网址  
277 - 'type'=>$info['type'] ?? GeoQuestion::TYPE_BRAND  
278 - ];  
279 - if($resultInfo === false){  
280 - $geoResultModel->addReturnId($data);  
281 - }else{  
282 - $geoResultModel->edit($data,['id'=>$resultInfo['id']]);  
283 - }  
284 - //日志记录  
285 - $geoLogModel = new GeoQuestionLog();  
286 - $geoLogModel->addReturnId($data);  
287 - }  
288 - }  
289 - //更新下次执行时间  
290 - $questionModel->edit(['current_time'=>date('Y-m-d'),'next_time'=>date('Y-m-d', strtotime(date('Y-m-d') . ' +'.(int)$projectInfo['geo_frequency'].' days'))],['id'=>$info['id']]);  
291 - }  
292 - }  
293 -  
294 - /**  
295 - * @remark :获取命中的url  
296 - * @name :getUrl  
297 - * @author :lyh  
298 - * @method :post  
299 - * @time :2025/7/3 16:38  
300 - */  
301 - public function getUrl($urlArr = [],$result_annotations = []){  
302 - $url = [];  
303 - if(!empty($urlArr)){  
304 - $str = implode(',',$result_annotations);  
305 - foreach ($urlArr as $u_item){  
306 - if (str_contains($str, $u_item)) {  
307 - $url[] = $u_item;  
308 - }  
309 - }  
310 - }  
311 - return array_values(array_unique($url));  
312 - }  
313 -  
314 - /**  
315 - * @remark :获取命中的关键词  
316 - * @name :getKeywords  
317 - * @author :lyh  
318 - * @method :post  
319 - * @time :2025/7/3 16:26  
320 - */  
321 - public function getKeywords($keywordArr = [],$result_text = []){  
322 - $keywords = [];  
323 - if(!empty($keywordArr) && !empty($result_text)){  
324 - $str = implode(',',$result_text);  
325 - foreach ($keywordArr as $k_item){  
326 - if (str_contains($str, $k_item)) {  
327 - $keywords[] = $k_item;  
328 - }  
329 - }  
330 - }  
331 - return $keywords;  
332 - }  
333 -  
334 /** 202 /**
335 * 获取待执行任务ID 203 * 获取待执行任务ID
336 * @return mixed 204 * @return mixed