作者 lyh

gx

@@ -10,6 +10,7 @@ @@ -10,6 +10,7 @@
10 namespace App\Console\Commands\GoogleSearch; 10 namespace App\Console\Commands\GoogleSearch;
11 11
12 use App\Models\Com\NoticeLog; 12 use App\Models\Com\NoticeLog;
  13 +use App\Models\Domain\DomainInfo;
13 use App\Models\GoogleSearch\GoogleSearch; 14 use App\Models\GoogleSearch\GoogleSearch;
14 use App\Models\GoogleSearch\GoogleSearchDetail; 15 use App\Models\GoogleSearch\GoogleSearchDetail;
15 use App\Models\Project\Project; 16 use App\Models\Project\Project;
@@ -49,23 +50,19 @@ class GoogleSearchKeyword extends Command @@ -49,23 +50,19 @@ class GoogleSearchKeyword extends Command
49 * @time :2025/3/31 11:37 50 * @time :2025/3/31 11:37
50 */ 51 */
51 public function handle(){ 52 public function handle(){
52 - $noticeModel = new NoticeLog();  
53 - while (true){  
54 - $list = $noticeModel->list(['type'=>['like',NoticeLog::GOOGLE_SEARCH.'_%'],'status'=>NoticeLog::STATUS_PENDING]);;  
55 - if(empty($list)){  
56 - sleep(50);  
57 - return true; 53 + $projectModel = new Project();
  54 + $lists = $projectModel->list(['delete_status' => 0,'type'=>['!=',$projectModel::TYPE_ONE]], 'id', ['id']);
  55 + $domainModel = new DomainInfo();
  56 + foreach ($lists as $val) {
  57 + echo date('Y-m-d H:i:s') . '开始--项目的id:'. $val['id'] . PHP_EOL;
  58 + $domainInfo = $domainModel->read(['project_id'=>$val['id']]);
  59 + if($domainInfo === false){
  60 + echo '域名不存在。'.PHP_EOL;
  61 + continue;
58 } 62 }
59 - foreach ($list as $val){  
60 - $data = $val['data'];  
61 - echo '执行的任务id:'.$val['id'].',项目id:'.$data['project_id']??''.PHP_EOL;  
62 - $result = $this->_action($data['domain'],$data['type'],$data['project_id']);  
63 - $status = NoticeLog::STATUS_SUCCESS;  
64 - if($result === false){  
65 - $status = NoticeLog::STATUS_FAIL;  
66 - }  
67 - $noticeModel->edit(['status'=>$status],['id'=>$val['id']]);  
68 - echo '任务结束'.PHP_EOL; 63 + $typeData = [1=>'date', 2=>'query', 3=>'page', 4=>'device', 5=>'country'];
  64 + foreach ($typeData as $valT){
  65 + $this->_action($domainInfo['domain'],$valT,$val['id']);
69 } 66 }
70 } 67 }
71 return true; 68 return true;
@@ -87,7 +84,7 @@ class GoogleSearchKeyword extends Command @@ -87,7 +84,7 @@ class GoogleSearchKeyword extends Command
87 } 84 }
88 //保存一条主记录诗句 85 //保存一条主记录诗句
89 $searchModel = new GoogleSearch(); 86 $searchModel = new GoogleSearch();
90 - $searchModel->addReturnId(['date'=>date('Y-m-d'),'project_id'=>$project_id,'type'=>$type,'text'=>json_encode($data,true)]); 87 + $searchModel->addReturnId(['date'=>date('Y-m-d'),'project_id'=>$project_id,'type'=>$type]);
91 $saveData = []; 88 $saveData = [];
92 $clicksNum = array_sum(array_column($data, 'clicks')); 89 $clicksNum = array_sum(array_column($data, 'clicks'));
93 $impressionsNum = array_sum(array_column($data, 'impressions')); 90 $impressionsNum = array_sum(array_column($data, 'impressions'));
@@ -98,7 +95,9 @@ class GoogleSearchKeyword extends Command @@ -98,7 +95,9 @@ class GoogleSearchKeyword extends Command
98 'type'=>$type, 95 'type'=>$type,
99 'keys'=>$val['keys'][0], 96 'keys'=>$val['keys'][0],
100 'clicks'=>$val['clicks'], 97 'clicks'=>$val['clicks'],
  98 + 'click_rate'=>number_format($val['clicks'] / $clicksNum, 2),
101 'impressions'=>$val['impressions'], 99 'impressions'=>$val['impressions'],
  100 + 'impressions_rate'=>number_format($val['impressions'] / $impressionsNum, 2),
102 'ctr'=>$val['ctr'], 101 'ctr'=>$val['ctr'],
103 'position'=>$val['position'], 102 'position'=>$val['position'],
104 ]; 103 ];
@@ -31,20 +31,6 @@ class GoogleSearchController extends BaseController @@ -31,20 +31,6 @@ class GoogleSearchController extends BaseController
31 ],[ 31 ],[
32 'type.required' => '搜索类型不能为空', 32 'type.required' => '搜索类型不能为空',
33 ]); 33 ]);
34 - //查询数据库查看是否有数据  
35 - $googleSearchModel = new GoogleSearch();  
36 - $searchInfo = $googleSearchModel->read(['type'=>$this->map['type'],'project_id'=>$this->user['project_id'],'date'=>['>=',date('Y-m-d', strtotime('-7 days'))]],['id']);  
37 - if($searchInfo === false){  
38 - //获取当前项目域名  
39 - $domain = parse_url($this->user['domain'], PHP_URL_HOST);  
40 - $noticeModel = new NoticeLog();  
41 - $info = $noticeModel->read(['status'=>NoticeLog::STATUS_PENDING,'type'=>$noticeModel::GOOGLE_SEARCH.'_'.$this->map['type']]);  
42 - if($info === false){  
43 - $noticeModel->addReturnId(['data'=>json_encode(['project_id' => $this->user['project_id'],'domain'=>$domain,'type'=>$this->param['type']],true  
44 - ),'type'=>$noticeModel::GOOGLE_SEARCH.'_'.$this->map['type']]);  
45 - }  
46 - $this->response('数据更新时间为7天,新数据生成中...');  
47 - }  
48 //查询详情数据 34 //查询详情数据
49 $searchDetailModel = new GoogleSearchDetail(); 35 $searchDetailModel = new GoogleSearchDetail();
50 $this->map['project_id']= $this->user['project_id']; 36 $this->map['project_id']= $this->user['project_id'];
@@ -89,9 +89,8 @@ class GoogleSearchService @@ -89,9 +89,8 @@ class GoogleSearchService
89 * @param :date,query,page,device,country/时间、聚合、前页、设备、国家 89 * @param :date,query,page,device,country/时间、聚合、前页、设备、国家
90 */ 90 */
91 public function googleSearch($domain,$search){ 91 public function googleSearch($domain,$search){
92 - $data = [1=>'date', 2=>'query', 3=>'page', 4=>'device', 5=>'country'];  
93 $this->url = 'https://www.cmer.site/api/google/search'; 92 $this->url = 'https://www.cmer.site/api/google/search';
94 - $url = $this->url.'?domain='.$domain.'&q='.$data[$search]; 93 + $url = $this->url.'?domain='.$domain.'&q='.$search;
95 $data = http_get($url); 94 $data = http_get($url);
96 if(!isset($data['status']) || $data['status'] != 200){ 95 if(!isset($data['status']) || $data['status'] != 200){
97 return []; 96 return [];