作者 李宇航

合并分支 'lyh-server' 到 'master'

Lyh server



查看合并请求 !2327
@@ -34,147 +34,198 @@ class GeoQuestionRes extends Command @@ -34,147 +34,198 @@ class GeoQuestionRes extends Command
34 */ 34 */
35 protected $description = 'geo设置请求获取结果'; 35 protected $description = 'geo设置请求获取结果';
36 36
37 - public function handle(){  
38 - while (true){ 37 +
  38 + /**
  39 + * @return bool
  40 + */
  41 + public function handle()
  42 + {
  43 + while (true) {
39 $task_id = $this->getTaskId(); 44 $task_id = $this->getTaskId();
40 - if(empty($task_id)){  
41 - sleep(30); 45 + if (empty($task_id)) {
  46 + sleep(300);
  47 + continue;
  48 + }
  49 + $task = GeoQuestion::where(['id' => $task_id, 'status' => GeoQuestion::STATUS_OPEN])->where('next_time', '<=', date('Y-m-d'))->first();
  50 + if (empty($task)) {
42 continue; 51 continue;
43 } 52 }
44 - echo date('Y-m-d H:i:s').'->task_id:'.$task_id.PHP_EOL;  
45 - $questionModel = new GeoQuestion();//问题  
46 - $info = $questionModel->read(['id'=>$task_id]);  
47 - //获取当前项目的执行频率  
48 - $projectModel = new Project();  
49 - $projectInfo = $projectModel->read(['id'=>$info['project_id']],['geo_status','geo_frequency']);  
50 - if($projectInfo['geo_status'] == 0){  
51 - $questionModel->edit(['status'=>0],['id'=>$task_id]); 53 + $project = Project::select(['geo_status', 'geo_frequency'])->where(['id' => $task->project_id])->first();
  54 + if (empty($project->get_status)) {
  55 + $task->status = GeoQuestion::STATUS_CLOSE;
  56 + $task->save();
52 continue; 57 continue;
53 } 58 }
54 - $questionArr = $info['question'];  
55 - if(empty($questionArr)){  
56 - echo date('Y-m-d H:i:s').'当前任务不存在问题。'.PHP_EOL;  
57 - $questionModel->edit(['status'=>0],['id'=>$task_id]); 59 + if ((empty($task->question) || FALSE == is_array($task->question)) || (empty($task->keywords) && empty($task->url))) {
  60 + $this->output('task id: ' . $task_id . ', error: 任务数据缺失, continue!');
  61 + $task->status = GeoQuestion::STATUS_CLOSE;
  62 + $task->save();
  63 + continue;
58 } 64 }
59 - //获取平台信息  
60 - $platformModel = new GeoPlatform();//平台  
61 - $platformArr = $platformModel->selectField(['status'=>$platformModel::STATUS_ON],'en_name');  
62 - if(empty($platformArr)){  
63 - echo date('Y-m-d H:i:s').'请求平台为空。'.PHP_EOL; 65 + $platforms = GeoPlatform::where(['status' => GeoPlatform::STATUS_ON])->get();
  66 + if ($platforms->isEmpty) {
  67 + $this->output('未设置AI模型!');
64 continue; 68 continue;
65 } 69 }
66 - $geoService = new GeoService();  
67 - $keywordArr = $info['keywords'] ?? [];  
68 - $urlArr = $info['url'] ?? []; 70 + $geo_service = new GeoService();
69 $geoResultModel = new GeoQuestionResult(); 71 $geoResultModel = new GeoQuestionResult();
70 - foreach ($questionArr as $q_item){  
71 - foreach ($platformArr as $p_item){  
72 - $keywords = [];//命中的关键词  
73 - $urls = [];//命中的网址 72 + $geoLogModel = new GeoQuestionLog();
  73 + foreach ($task->question as $question) {
  74 + $error_num = 0;
  75 + foreach ($platforms as $platform) {
  76 + // 设置重试, 有的平台不一定能正常获取到数据
  77 + GET_RESULT:
  78 + $error_num++;
74 try { 79 try {
75 - $result_data = $geoService->setWebSearchChatAction($q_item,$p_item);  
76 - echo 'success:'.$result_data['code'].PHP_EOL;  
77 - if(isset($result_data) && $result_data['code'] == 200){  
78 - $result_data_annotations[] = $result_data['text'];  
79 - if(!empty($result_data['annotations'])){  
80 - $url = array_column(array_column($result_data['annotations'], 'url_citation'), 'url');  
81 - $title = array_column(array_column($result_data['annotations'], 'url_citation'), 'title');;  
82 - $result_data_annotations = array_merge($url,$title,$result_data_annotations);  
83 - }  
84 - $keywords = $this->getKeywords($keywordArr,$result_data_annotations ?? []);  
85 - $urls = $this->getUrl($urlArr,$result_data_annotations ?? []);  
86 - }  
87 - }catch (\Exception $e){  
88 - echo $e->getMessage().PHP_EOL; 80 + if ($platform->en_name == 'Google AI Overview') {
  81 + // overview 数据结构不确定, 需要单独处理数据
  82 + $data = $geo_service->getGooglePlatformResult($question);
  83 + $result = $this->dealGoogleData($data);
  84 + } else {
  85 + $result = $geo_service->getAiPlatformResult($question, $platform->en_name);
  86 + }
  87 + if (empty($result['text'])){
  88 + goto GET_RESULT;
  89 + }
  90 + } catch (\Exception $e) {
  91 + $this->output('task id:' . $task_id . ', question: ' . $question . ', platform: ' . $question . ', error: ' . $e->getMessage());
  92 + if ($error_num < 5) {
  93 + goto GET_RESULT;
  94 + }
89 continue; 95 continue;
90 } 96 }
91 - //查询当前是否已有执行保存记录  
92 - $resultInfo = $geoResultModel->read(['project_id'=>$info['project_id'],'question_id'=>$info['id'],'platform'=>$p_item,'question'=>$q_item],['id']);  
93 - //保存一条结果记录  
94 - $data = [  
95 - 'project_id'=>$info['project_id'],  
96 - 'question_id'=>$info['id'],  
97 - 'platform'=>$p_item,  
98 - 'question'=>$q_item,  
99 - 'keywords'=>json_encode($keywords ?? [],true),//命中的关键词  
100 - 'text'=>json_encode($result_data ?? [],true),  
101 - 'url'=>json_encode($urls ?? [],true),//命中的网址  
102 - 'type'=>$info['type'] ?? 1 97 + // 命中文案
  98 + $hit_data[] = $result['text'];
  99 + if(FALSE == empty($result['annotations'])){
  100 + $url = array_column(array_column($result['annotations'], 'url_citation'), 'url');
  101 + $title = array_column(array_column($result['annotations'], 'url_citation'), 'title');;
  102 + $hit_data = array_merge($url, $title, $hit_data);
  103 + }
  104 + $hit_string = implode(',', $hit_data);
  105 + // 命中关键词和路由
  106 + $hit_keyword = $hit_url = [];
  107 + $hit = 0;
  108 + if ($task->keywords) {
  109 + $pattern = '/(' . implode('|', array_map('preg_quote', $task->keywords)) . ')/i';
  110 + if (preg_match($pattern, $hit_string, $matches)) {
  111 + $hit_keyword = $matches[0];
  112 + $hit++;
  113 + }
  114 + }
  115 + if ($task->url) {
  116 + $pattern = '/(' . implode('|', array_map('preg_quote', $task->url)) . ')/i';
  117 + if (preg_match($pattern, $hit_string, $matches)) {
  118 + $hit_url = $matches[0];
  119 + $hit++;
  120 + }
  121 + }
  122 + // 保存数据结果
  123 + $geo_result = GeoQuestionResult::where(['project_id' => $task['project_id'], 'question_id' => $task['id'], 'platform' => $platform, 'question' => $question])->first();
  124 + $save_data = [
  125 + 'project_id' => $task->project_id,
  126 + 'question_id' => $task->id,
  127 + 'type' => $task->type ?? GeoQuestion::TYPE_BRAND,
  128 + 'platform' => $platform,
  129 + 'question' => $question,
  130 + 'keywords' => json_encode($hit_keyword,true),//命中的关键词
  131 + 'url' => json_encode($hit_url,true),//命中的网址
  132 + 'text' => json_encode($result,true),
  133 + 'hit' => $hit
103 ]; 134 ];
104 - if($resultInfo === false){  
105 - $geoResultModel->addReturnId($data); 135 + if(empty($geo_result)){
  136 + $geoResultModel->addReturnId($save_data);
106 }else{ 137 }else{
107 - $geoResultModel->edit($data,['id'=>$resultInfo['id']]); 138 + $geoResultModel->edit($save_data, ['id' => $geo_result->id]);
108 } 139 }
109 - //日志记录  
110 - $geoLogModel = new GeoQuestionLog(); 140 + $save_data['text'] = json_encode(FALSE == empty($data) ? $data : $result,true);
111 $geoLogModel->addReturnId($data); 141 $geoLogModel->addReturnId($data);
112 } 142 }
113 } 143 }
114 - //更新下次执行时间  
115 - $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']]); 144 + $task->current_time = date('Y-m-d');
  145 + $task->next_time = date('Y-m-d', strtotime('+' . $project->geo_frequency . ' days'));
  146 + $task->save();
116 } 147 }
  148 + return true;
117 } 149 }
118 150
119 /** 151 /**
120 - * @remark :获取命中的url  
121 - * @name :getUrl  
122 - * @author :lyh  
123 - * @method :post  
124 - * @time :2025/7/3 16:38 152 + * 整合Google平台数据
  153 + * @param $data
  154 + * @return array
125 */ 155 */
126 - public function getUrl($urlArr = [],$result_annotations = []){  
127 - $url = [];  
128 - if(!empty($urlArr)){  
129 - $str = implode(',',$result_annotations);  
130 - foreach ($urlArr as $u_item){  
131 - if (str_contains($str, $u_item)) {  
132 - $url[] = $u_item; 156 + public function dealGoogleData($data)
  157 + {
  158 + $result = [
  159 + 'code' => 200,
  160 + 'model' => 'Google AI Overview',
  161 + 'text' => '',
  162 + ];
  163 + if (FALSE == empty($data['ai_overview']['texts']) && is_array($data['ai_overview']['texts'])) {
  164 + $texts = [];
  165 + foreach ($data['ai_overview']['texts'] as $item) {
  166 + // 提取链接
  167 + if (FALSE == empty($item['links'])) {
  168 + foreach ($item['links'] as $link) {
  169 + if (FALSE == empty($link['text']) && FALSE == empty($link['link'])) {
  170 + $result['annotations'][] = [
  171 + 'type' => 'url_citation',
  172 + 'url_citation' => [
  173 + 'url' => $link['link'],
  174 + 'title' => $link['text']
  175 + ],
  176 + ];
133 } 177 }
134 } 178 }
135 } 179 }
136 - return array_values(array_unique($url)); 180 + // 第一层就有内容
  181 + if (FALSE == empty($item['snippet'])) {
  182 + // title 放到数组最前面
  183 + if (FALSE == empty($item['type']) && $item['type'] == 'title')
  184 + array_unshift($texts, $item['snippet']);
  185 + else
  186 + array_push($texts, $item['snippet']);
  187 + }
  188 + // list类型
  189 + if (FALSE == empty($item['type']) && $item['type'] == 'list' && FALSE == empty($item['list']) && is_array($item['list'])) {
  190 + foreach ($item['list'] as $list) {
  191 + if (FALSE == empty($list['snippet']))
  192 + array_push($texts, $list['snippet']);
137 } 193 }
138 -  
139 - /**  
140 - * @remark :获取命中的关键词  
141 - * @name :getKeywords  
142 - * @author :lyh  
143 - * @method :post  
144 - * @time :2025/7/3 16:26  
145 - */  
146 - public function getKeywords($keywordArr = [],$result_text = []){  
147 - $keywords = [];  
148 - if(!empty($keywordArr) && !empty($result_text)){  
149 - $str = implode(',',$result_text);  
150 - foreach ($keywordArr as $k_item){  
151 - if (str_contains($str, $k_item)) {  
152 - $keywords[] = $k_item;  
153 } 194 }
154 } 195 }
  196 + $text = implode(PHP_EOL, $texts);
  197 + $result['text'] = $text;
155 } 198 }
156 - return $keywords; 199 + return $result;
157 } 200 }
158 201
159 /** 202 /**
160 - * @remark :拉取任务id  
161 - * @name :getTaskId  
162 - * @author :lyh  
163 - * @method :post  
164 - * @time :2025/7/3 15:15 203 + * 获取待执行任务ID
  204 + * @return mixed
165 */ 205 */
166 public function getTaskId(){ 206 public function getTaskId(){
167 - $task_id = Redis::rpop('geo_question_result');  
168 - if(empty($task_id)){  
169 - $questionModel = new GeoQuestion();  
170 - $ids = $questionModel->selectField(['status'=>1,'next_time'=>['<=',date('Y-m-d')]],'id');  
171 - if(!empty($ids)){  
172 - foreach ($ids as $id) {  
173 - Redis::lpush('geo_question_result', $id);  
174 - } 207 + $key = 'geo_task_list';
  208 + $task_id = Redis::rpop($key);
  209 + if (empty($task_id)) {
  210 + $ids = GeoQuestion::where(['status' => GeoQuestion::STATUS_OPEN])->where('next_time', '<=', date('Y-m-d'))->get('id');
  211 + if ($ids->isEmpty())
  212 + return $task_id;
  213 + foreach ($ids as $item) {
  214 + Redis::lpush($key, $item->id);
175 } 215 }
176 - $task_id = Redis::rpop('geo_question_result'); 216 + $task_id = Redis::rpop($key);
177 } 217 }
178 return $task_id; 218 return $task_id;
179 } 219 }
  220 +
  221 + /**
  222 + * 输出日志
  223 + * @param $message
  224 + * @return bool
  225 + */
  226 + public function output($message)
  227 + {
  228 + echo date('Y-m-d H:i:s') . ' ' . $message . PHP_EOL;
  229 + return true;
  230 + }
180 } 231 }
@@ -25,6 +25,12 @@ class GeoQuestion extends Base @@ -25,6 +25,12 @@ class GeoQuestion extends Base
25 25
26 public $frequency = [1,2,3,4,5,6,7,8,9,10];//类型 26 public $frequency = [1,2,3,4,5,6,7,8,9,10];//类型
27 27
  28 + const TYPE_BRAND = 1;
  29 + const TYPE_MARKET = 2;
  30 +
  31 + const STATUS_CLOSE = 0;
  32 + const STATUS_OPEN = 1;
  33 +
28 /** 34 /**
29 * @remark :geo提交网址获取器 35 * @remark :geo提交网址获取器
30 * @name :getUrlAttribute 36 * @name :getUrlAttribute
@@ -71,16 +77,13 @@ class GeoQuestion extends Base @@ -71,16 +77,13 @@ class GeoQuestion extends Base
71 } 77 }
72 78
73 /** 79 /**
74 - * @remark :品牌类型  
75 - * @name :brandType  
76 - * @author :lyh  
77 - * @method :post  
78 - * @time :2025/7/3 9:43 80 + * 品牌类型
  81 + * @return array
79 */ 82 */
80 public function brandType(){ 83 public function brandType(){
81 return [ 84 return [
82 - 1=>'品牌数据',  
83 - 2=>'营销数据' 85 + self::TYPE_BRAND => '品牌数据',
  86 + self::TYPE_MARKET => '营销数据'
84 ]; 87 ];
85 } 88 }
86 } 89 }
@@ -14,16 +14,16 @@ class GeoService @@ -14,16 +14,16 @@ class GeoService
14 public $api_key = '7yn!We6$&NnVA38bpGy*A@4TQ5iYLJcW'; 14 public $api_key = '7yn!We6$&NnVA38bpGy*A@4TQ5iYLJcW';
15 15
16 public $api_url = 'https://api.cmer.com/'; 16 public $api_url = 'https://api.cmer.com/';
  17 +
17 /** 18 /**
18 - * @remark :请求的方法  
19 - * @name :requestAction  
20 - * @author :lyh  
21 - * @method :post  
22 - * @time :2025/7/3 14:26 19 + * 获取AI平台数据
  20 + * @param $content
  21 + * @param $platform
  22 + * @return mixed|string
23 */ 23 */
24 - public function setWebSearchChatAction($content,$platform){  
25 - $route = 'v1/websearch_chat';  
26 - $url = $this->api_url.$route; 24 + public function getAiPlatformResult($content,$platform)
  25 + {
  26 + $url = $this->api_url . 'v1/websearch_chat';
27 $header = [ 27 $header = [
28 'accept: application/json', 28 'accept: application/json',
29 'X-CmerApi-Host: llm-chat.p.cmer.com', 29 'X-CmerApi-Host: llm-chat.p.cmer.com',
@@ -37,10 +37,33 @@ class GeoService @@ -37,10 +37,33 @@ class GeoService
37 'role'=>'user' 37 'role'=>'user'
38 ], 38 ],
39 ], 39 ],
40 - 'platform'=>$platform,  
41 - 'security_check'=>true 40 + 'platform' => $platform,
  41 + 'security_check' => true
42 ]; 42 ];
43 $data = http_post($url,json_encode($message,true),$header); 43 $data = http_post($url,json_encode($message,true),$header);
44 return $data; 44 return $data;
45 } 45 }
  46 +
  47 + /**
  48 + * 获取Google数据
  49 + * @param $search
  50 + * @param int $lum_json 默认1 不只是什么参数
  51 + * @return mixed|string
  52 + */
  53 + public function getGooglePlatformResult($search, $lum_json = 1)
  54 + {
  55 + $url = 'http://43.153.56.18:58000/google_ai_summary';
  56 + $header = [
  57 + 'accept: application/json',
  58 + 'x-api-key: 6EipgPsy3Q7Q9M2jCx',
  59 + 'Content-Type: application/json'
  60 + ];
  61 + $param = [
  62 + 'q' => $search,
  63 + 'lum_json' => $lum_json
  64 + ];
  65 +
  66 + $url = $url . '?' . http_build_query($param);
  67 + return http_get($url, $header);
  68 + }
46 } 69 }