作者 lyh

gx

<?php
/**
* @remark :
* @name :GoogleSearchKeyword.php
* @author :lyh
* @method :post
* @time :2025/3/31 11:29
*/
namespace App\Console\Commands\GoogleSearch;
use App\Models\Com\NoticeLog;
use App\Models\GoogleSearch\GoogleSearch;
use App\Models\GoogleSearch\GoogleSearchDetail;
use App\Models\Project\Project;
use App\Services\GoogleSearchService;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
/**
* @remark :客户搜索词
* @name :GoogleSearchKeyword
* @author :lyh
* @method :post
* @time :2025/3/31 11:29
*/
class GoogleSearchKeyword extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'search_keyword';
/**
* The console command description.
*
* @var string
*/
protected $description = 'google搜索词';
/**
* @remark :拉取google搜索数据
* @name :handle
* @author :lyh
* @method :post
* @time :2025/3/31 11:37
*/
public function handle(){
while (true){
$list = NoticeLog::where('type', NoticeLog::GOOGLE_SEARCH)->where('status', NoticeLog::STATUS_PENDING)->get();
if(empty($list)){
sleep(50);
return true;
}
foreach ($list as $val){
$data = json_decode($val['data'],true);
echo '执行的任务id:'.$val['id'].',项目id:'.$data['project_id']??''.PHP_EOL;
$this->_action($data['domain'],$data['type'],$data['project_id']);
echo '任务结束'.PHP_EOL;
}
}
return true;
}
/**
* @remark :执行的方法
* @name :_action
* @author :lyh
* @method :post
* @time :2025/3/31 11:30
*/
public function _action($domain,$type,$project_id){
$googleService = new GoogleSearchService();
$data = $googleService->googleSearch($domain,$type);
if(empty($data)){
echo '域名:'.$domain.'拉取数据为空.'.PHP_EOL;
return true;
}
//保存一条主记录诗句
$searchModel = new GoogleSearch();
$searchModel->addReturnId(['date'=>date('Y-m-d'),'project_id'=>$project_id,'type'=>$type,'text'=>json_encode($data,true)]);
$saveData = [];
$clicksNum = array_sum(array_column($data, 'clicks'));
$impressionsNum = array_sum(array_column($data, 'impressions'));
foreach ($data as $val){
$saveData[] = [
'date'=>date('Y-m-d'),
'project_id'=>$project_id,
'type'=>$type,
'keys'=>$val['keys'][0],
'clicks'=>$val['clicks'],
'impressions'=>$val['impressions'],
'ctr'=>$val['ctr'],
'position'=>$val['position'],
'rate'=>number_format($val['clicks'] / $clicksNum, 2),
];
}
if(!empty($saveData)){
DB::beginTransaction();
try {
//清空以前的数据
$detailModel = new GoogleSearchDetail();
$detailModel->del(['project_id'=>$project_id,'type'=>$type]);
$detailModel->insertAll($saveData);
DB::commit();
}catch (\Exception $e){
DB::rollBack();
echo '重新添加数据失败:project_id'.$project_id.PHP_EOL;
}
}
return true;
}
}
... ...
... ... @@ -11,6 +11,9 @@ namespace App\Http\Controllers\Bside\GoogleKeyword;
use App\Enums\Common\Code;
use App\Http\Controllers\Bside\BaseController;
use App\Models\Com\NoticeLog;
use App\Models\GoogleSearch\GoogleSearch;
use App\Models\GoogleSearch\GoogleSearchDetail;
use App\Services\GoogleSearchService;
class GoogleSearchController extends BaseController
... ... @@ -28,10 +31,19 @@ class GoogleSearchController extends BaseController
],[
'search.required' => '搜索类型不能为空',
]);
//查询数据库查看是否有数据
$googleSearchModel = new GoogleSearch();
$searchInfo = $googleSearchModel->read(['type'=>$this->map['search'],'project_id'=>$this->user['project_id'],'date'=>['<=',date('Y-m-d', strtotime('-7 days'))]],['id']);
if($searchInfo === false){
//获取当前项目域名
$domain = parse_url($this->user['domain'], PHP_URL_HOST);
$googleService = new GoogleSearchService();
$data = $googleService->googleSearch($domain,$this->param['search']);
$data = NoticeLog::createLog(NoticeLog::DELETE_CUSTOM_CATEGORY, ['project_id' => $this->user['project_id'],'domain'=>$domain,'type'=>$this->param['search']]);
$this->response('数据生成中...',Code::SUCCESS,$data);
}
//查询详情数据
$searchDetailModel = new GoogleSearchDetail();
$this->map['project_id']= $this->user['project_id'];
$data = $searchDetailModel->lists($this->map,$this->page,$this->row);
$this->response('success',Code::SUCCESS,$data);
}
}
... ...
... ... @@ -20,6 +20,7 @@ class NoticeLog extends Base
const DELETE_NEWS_CATEGORY = 'delete_news_category';
const DELETE_CUSTOM_CATEGORY = 'delete_custom_category';
const GENERATE_PAGE = 'generate_page';//生成页面单独改为守护进程
const GOOGLE_SEARCH = 'google_search';//google搜索任务date
const STATUS_PENDING = 0;
const STATUS_SUCCESS = 1;
const STATUS_FAIL = 2;
... ...
<?php
/**
* @remark :
* @name :GoogleSearch.php
* @author :lyh
* @method :post
* @time :2025/3/31 13:36
*/
namespace App\Models\GoogleSearch;
use App\Models\Base;
class GoogleSearch extends Base
{
protected $table = 'gl_google_search';
}
... ...
<?php
/**
* @remark :
* @name :GoogleSearchDetail.php
* @author :lyh
* @method :post
* @time :2025/3/31 13:40
*/
namespace App\Models\GoogleSearch;
use App\Models\Base;
class GoogleSearchDetail extends Base
{
protected $table = 'gl_google_search_detail';
}
... ...
... ... @@ -74,7 +74,6 @@ class GoogleSearchService
$err = curl_error($curl);
curl_close($curl);
if ($err) {
errorLog("热门关键词cURL Error #:", $url, $err);
return false;
} else {
return json_decode($response,true);
... ... @@ -90,16 +89,13 @@ class GoogleSearchService
* @param :date,query,page,device,country/时间、聚合、前页、设备、国家
*/
public function googleSearch($domain,$search){
$data = [
1=>'date',
2=>'query',
3=>'page',
4=>'device',
5=>'country'
];
$data = [1=>'date', 2=>'query', 3=>'page', 4=>'device', 5=>'country'];
$this->url = 'https://www.cmer.site/api/google/search';
$url = $this->url.'?domain='.$domain.'&q='.$data[$search];
$data = http_get($url);
if(!isset($data['status']) || $data['status'] != 200){
return [];
}
$data = $data['data'] ?? [];
if(!empty($data) && ($search == 5)){
foreach ($data as $key => $val){
... ... @@ -109,5 +105,4 @@ class GoogleSearchService
}
return $data;
}
}
... ...