作者 lyh

gx

  1 +<?php
  2 +/**
  3 + * @remark :
  4 + * @name :GoogleSearchKeyword.php
  5 + * @author :lyh
  6 + * @method :post
  7 + * @time :2025/3/31 11:29
  8 + */
  9 +
  10 +namespace App\Console\Commands\GoogleSearch;
  11 +
  12 +use App\Models\Com\NoticeLog;
  13 +use App\Models\GoogleSearch\GoogleSearch;
  14 +use App\Models\GoogleSearch\GoogleSearchDetail;
  15 +use App\Models\Project\Project;
  16 +use App\Services\GoogleSearchService;
  17 +use App\Services\ProjectServer;
  18 +use Illuminate\Console\Command;
  19 +use Illuminate\Support\Facades\DB;
  20 +
  21 +/**
  22 + * @remark :客户搜索词
  23 + * @name :GoogleSearchKeyword
  24 + * @author :lyh
  25 + * @method :post
  26 + * @time :2025/3/31 11:29
  27 + */
  28 +class GoogleSearchKeyword extends Command
  29 +{
  30 + /**
  31 + * The name and signature of the console command.
  32 + *
  33 + * @var string
  34 + */
  35 + protected $signature = 'search_keyword';
  36 +
  37 + /**
  38 + * The console command description.
  39 + *
  40 + * @var string
  41 + */
  42 + protected $description = 'google搜索词';
  43 +
  44 + /**
  45 + * @remark :拉取google搜索数据
  46 + * @name :handle
  47 + * @author :lyh
  48 + * @method :post
  49 + * @time :2025/3/31 11:37
  50 + */
  51 + public function handle(){
  52 + while (true){
  53 + $list = NoticeLog::where('type', NoticeLog::GOOGLE_SEARCH)->where('status', NoticeLog::STATUS_PENDING)->get();
  54 + if(empty($list)){
  55 + sleep(50);
  56 + return true;
  57 + }
  58 + foreach ($list as $val){
  59 + $data = json_decode($val['data'],true);
  60 + echo '执行的任务id:'.$val['id'].',项目id:'.$data['project_id']??''.PHP_EOL;
  61 + $this->_action($data['domain'],$data['type'],$data['project_id']);
  62 + echo '任务结束'.PHP_EOL;
  63 + }
  64 + }
  65 + return true;
  66 + }
  67 +
  68 + /**
  69 + * @remark :执行的方法
  70 + * @name :_action
  71 + * @author :lyh
  72 + * @method :post
  73 + * @time :2025/3/31 11:30
  74 + */
  75 + public function _action($domain,$type,$project_id){
  76 + $googleService = new GoogleSearchService();
  77 + $data = $googleService->googleSearch($domain,$type);
  78 + if(empty($data)){
  79 + echo '域名:'.$domain.'拉取数据为空.'.PHP_EOL;
  80 + return true;
  81 + }
  82 + //保存一条主记录诗句
  83 + $searchModel = new GoogleSearch();
  84 + $searchModel->addReturnId(['date'=>date('Y-m-d'),'project_id'=>$project_id,'type'=>$type,'text'=>json_encode($data,true)]);
  85 + $saveData = [];
  86 + $clicksNum = array_sum(array_column($data, 'clicks'));
  87 + $impressionsNum = array_sum(array_column($data, 'impressions'));
  88 + foreach ($data as $val){
  89 + $saveData[] = [
  90 + 'date'=>date('Y-m-d'),
  91 + 'project_id'=>$project_id,
  92 + 'type'=>$type,
  93 + 'keys'=>$val['keys'][0],
  94 + 'clicks'=>$val['clicks'],
  95 + 'impressions'=>$val['impressions'],
  96 + 'ctr'=>$val['ctr'],
  97 + 'position'=>$val['position'],
  98 + 'rate'=>number_format($val['clicks'] / $clicksNum, 2),
  99 + ];
  100 + }
  101 +
  102 + if(!empty($saveData)){
  103 + DB::beginTransaction();
  104 + try {
  105 + //清空以前的数据
  106 + $detailModel = new GoogleSearchDetail();
  107 + $detailModel->del(['project_id'=>$project_id,'type'=>$type]);
  108 + $detailModel->insertAll($saveData);
  109 + DB::commit();
  110 + }catch (\Exception $e){
  111 + DB::rollBack();
  112 + echo '重新添加数据失败:project_id'.$project_id.PHP_EOL;
  113 + }
  114 + }
  115 + return true;
  116 + }
  117 +}
@@ -11,6 +11,9 @@ namespace App\Http\Controllers\Bside\GoogleKeyword; @@ -11,6 +11,9 @@ namespace App\Http\Controllers\Bside\GoogleKeyword;
11 11
12 use App\Enums\Common\Code; 12 use App\Enums\Common\Code;
13 use App\Http\Controllers\Bside\BaseController; 13 use App\Http\Controllers\Bside\BaseController;
  14 +use App\Models\Com\NoticeLog;
  15 +use App\Models\GoogleSearch\GoogleSearch;
  16 +use App\Models\GoogleSearch\GoogleSearchDetail;
14 use App\Services\GoogleSearchService; 17 use App\Services\GoogleSearchService;
15 18
16 class GoogleSearchController extends BaseController 19 class GoogleSearchController extends BaseController
@@ -28,10 +31,19 @@ class GoogleSearchController extends BaseController @@ -28,10 +31,19 @@ class GoogleSearchController extends BaseController
28 ],[ 31 ],[
29 'search.required' => '搜索类型不能为空', 32 'search.required' => '搜索类型不能为空',
30 ]); 33 ]);
31 - //获取当前项目域名  
32 - $domain = parse_url($this->user['domain'], PHP_URL_HOST);  
33 - $googleService = new GoogleSearchService();  
34 - $data = $googleService->googleSearch($domain,$this->param['search']); 34 + //查询数据库查看是否有数据
  35 + $googleSearchModel = new GoogleSearch();
  36 + $searchInfo = $googleSearchModel->read(['type'=>$this->map['search'],'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 + $data = NoticeLog::createLog(NoticeLog::DELETE_CUSTOM_CATEGORY, ['project_id' => $this->user['project_id'],'domain'=>$domain,'type'=>$this->param['search']]);
  41 + $this->response('数据生成中...',Code::SUCCESS,$data);
  42 + }
  43 + //查询详情数据
  44 + $searchDetailModel = new GoogleSearchDetail();
  45 + $this->map['project_id']= $this->user['project_id'];
  46 + $data = $searchDetailModel->lists($this->map,$this->page,$this->row);
35 $this->response('success',Code::SUCCESS,$data); 47 $this->response('success',Code::SUCCESS,$data);
36 } 48 }
37 } 49 }
@@ -20,6 +20,7 @@ class NoticeLog extends Base @@ -20,6 +20,7 @@ class NoticeLog extends Base
20 const DELETE_NEWS_CATEGORY = 'delete_news_category'; 20 const DELETE_NEWS_CATEGORY = 'delete_news_category';
21 const DELETE_CUSTOM_CATEGORY = 'delete_custom_category'; 21 const DELETE_CUSTOM_CATEGORY = 'delete_custom_category';
22 const GENERATE_PAGE = 'generate_page';//生成页面单独改为守护进程 22 const GENERATE_PAGE = 'generate_page';//生成页面单独改为守护进程
  23 + const GOOGLE_SEARCH = 'google_search';//google搜索任务date
23 const STATUS_PENDING = 0; 24 const STATUS_PENDING = 0;
24 const STATUS_SUCCESS = 1; 25 const STATUS_SUCCESS = 1;
25 const STATUS_FAIL = 2; 26 const STATUS_FAIL = 2;
  1 +<?php
  2 +/**
  3 + * @remark :
  4 + * @name :GoogleSearch.php
  5 + * @author :lyh
  6 + * @method :post
  7 + * @time :2025/3/31 13:36
  8 + */
  9 +
  10 +namespace App\Models\GoogleSearch;
  11 +
  12 +use App\Models\Base;
  13 +
  14 +class GoogleSearch extends Base
  15 +{
  16 + protected $table = 'gl_google_search';
  17 +}
  1 +<?php
  2 +/**
  3 + * @remark :
  4 + * @name :GoogleSearchDetail.php
  5 + * @author :lyh
  6 + * @method :post
  7 + * @time :2025/3/31 13:40
  8 + */
  9 +
  10 +namespace App\Models\GoogleSearch;
  11 +
  12 +use App\Models\Base;
  13 +
  14 +class GoogleSearchDetail extends Base
  15 +{
  16 + protected $table = 'gl_google_search_detail';
  17 +}
@@ -74,7 +74,6 @@ class GoogleSearchService @@ -74,7 +74,6 @@ class GoogleSearchService
74 $err = curl_error($curl); 74 $err = curl_error($curl);
75 curl_close($curl); 75 curl_close($curl);
76 if ($err) { 76 if ($err) {
77 - errorLog("热门关键词cURL Error #:", $url, $err);  
78 return false; 77 return false;
79 } else { 78 } else {
80 return json_decode($response,true); 79 return json_decode($response,true);
@@ -90,16 +89,13 @@ class GoogleSearchService @@ -90,16 +89,13 @@ class GoogleSearchService
90 * @param :date,query,page,device,country/时间、聚合、前页、设备、国家 89 * @param :date,query,page,device,country/时间、聚合、前页、设备、国家
91 */ 90 */
92 public function googleSearch($domain,$search){ 91 public function googleSearch($domain,$search){
93 - $data = [  
94 - 1=>'date',  
95 - 2=>'query',  
96 - 3=>'page',  
97 - 4=>'device',  
98 - 5=>'country'  
99 - ]; 92 + $data = [1=>'date', 2=>'query', 3=>'page', 4=>'device', 5=>'country'];
100 $this->url = 'https://www.cmer.site/api/google/search'; 93 $this->url = 'https://www.cmer.site/api/google/search';
101 $url = $this->url.'?domain='.$domain.'&q='.$data[$search]; 94 $url = $this->url.'?domain='.$domain.'&q='.$data[$search];
102 $data = http_get($url); 95 $data = http_get($url);
  96 + if(!isset($data['status']) || $data['status'] != 200){
  97 + return [];
  98 + }
103 $data = $data['data'] ?? []; 99 $data = $data['data'] ?? [];
104 if(!empty($data) && ($search == 5)){ 100 if(!empty($data) && ($search == 5)){
105 foreach ($data as $key => $val){ 101 foreach ($data as $key => $val){
@@ -109,5 +105,4 @@ class GoogleSearchService @@ -109,5 +105,4 @@ class GoogleSearchService
109 } 105 }
110 return $data; 106 return $data;
111 } 107 }
112 -  
113 } 108 }