作者 lyh

gx

... ... @@ -10,6 +10,7 @@
namespace App\Console\Commands\GoogleSearch;
use App\Models\Com\NoticeLog;
use App\Models\Domain\DomainInfo;
use App\Models\GoogleSearch\GoogleSearch;
use App\Models\GoogleSearch\GoogleSearchDetail;
use App\Models\Project\Project;
... ... @@ -49,23 +50,19 @@ class GoogleSearchKeyword extends Command
* @time :2025/3/31 11:37
*/
public function handle(){
$noticeModel = new NoticeLog();
while (true){
$list = $noticeModel->list(['type'=>['like',NoticeLog::GOOGLE_SEARCH.'_%'],'status'=>NoticeLog::STATUS_PENDING]);;
if(empty($list)){
sleep(50);
return true;
$projectModel = new Project();
$lists = $projectModel->list(['delete_status' => 0,'type'=>['!=',$projectModel::TYPE_ONE]], 'id', ['id']);
$domainModel = new DomainInfo();
foreach ($lists as $val) {
echo date('Y-m-d H:i:s') . '开始--项目的id:'. $val['id'] . PHP_EOL;
$domainInfo = $domainModel->read(['project_id'=>$val['id']]);
if($domainInfo === false){
echo '域名不存在。'.PHP_EOL;
continue;
}
foreach ($list as $val){
$data = $val['data'];
echo '执行的任务id:'.$val['id'].',项目id:'.$data['project_id']??''.PHP_EOL;
$result = $this->_action($data['domain'],$data['type'],$data['project_id']);
$status = NoticeLog::STATUS_SUCCESS;
if($result === false){
$status = NoticeLog::STATUS_FAIL;
}
$noticeModel->edit(['status'=>$status],['id'=>$val['id']]);
echo '任务结束'.PHP_EOL;
$typeData = [1=>'date', 2=>'query', 3=>'page', 4=>'device', 5=>'country'];
foreach ($typeData as $valT){
$this->_action($domainInfo['domain'],$valT,$val['id']);
}
}
return true;
... ... @@ -87,7 +84,7 @@ class GoogleSearchKeyword extends Command
}
//保存一条主记录诗句
$searchModel = new GoogleSearch();
$searchModel->addReturnId(['date'=>date('Y-m-d'),'project_id'=>$project_id,'type'=>$type,'text'=>json_encode($data,true)]);
$searchModel->addReturnId(['date'=>date('Y-m-d'),'project_id'=>$project_id,'type'=>$type]);
$saveData = [];
$clicksNum = array_sum(array_column($data, 'clicks'));
$impressionsNum = array_sum(array_column($data, 'impressions'));
... ... @@ -98,7 +95,9 @@ class GoogleSearchKeyword extends Command
'type'=>$type,
'keys'=>$val['keys'][0],
'clicks'=>$val['clicks'],
'click_rate'=>number_format($val['clicks'] / $clicksNum, 2),
'impressions'=>$val['impressions'],
'impressions_rate'=>number_format($val['impressions'] / $impressionsNum, 2),
'ctr'=>$val['ctr'],
'position'=>$val['position'],
];
... ...
... ... @@ -31,20 +31,6 @@ class GoogleSearchController extends BaseController
],[
'type.required' => '搜索类型不能为空',
]);
//查询数据库查看是否有数据
$googleSearchModel = new GoogleSearch();
$searchInfo = $googleSearchModel->read(['type'=>$this->map['type'],'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);
$noticeModel = new NoticeLog();
$info = $noticeModel->read(['status'=>NoticeLog::STATUS_PENDING,'type'=>$noticeModel::GOOGLE_SEARCH.'_'.$this->map['type']]);
if($info === false){
$noticeModel->addReturnId(['data'=>json_encode(['project_id' => $this->user['project_id'],'domain'=>$domain,'type'=>$this->param['type']],true
),'type'=>$noticeModel::GOOGLE_SEARCH.'_'.$this->map['type']]);
}
$this->response('数据更新时间为7天,新数据生成中...');
}
//查询详情数据
$searchDetailModel = new GoogleSearchDetail();
$this->map['project_id']= $this->user['project_id'];
... ...
... ... @@ -89,9 +89,8 @@ class GoogleSearchService
* @param :date,query,page,device,country/时间、聚合、前页、设备、国家
*/
public function googleSearch($domain,$search){
$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];
$url = $this->url.'?domain='.$domain.'&q='.$search;
$data = http_get($url);
if(!isset($data['status']) || $data['status'] != 200){
return [];
... ...