作者 赵彬吉

update

... ... @@ -3,6 +3,7 @@
namespace App\Helper;
use App\Models\RankData\RankDataLog;
use App\Utils\HttpUtils;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
... ... @@ -75,6 +76,7 @@ class QuanqiusouApi
/**
* 获取谷歌排名数据
* @param $project_id
* @param $api_no
* @param string $lang
* @param int $day
... ... @@ -82,7 +84,7 @@ class QuanqiusouApi
* @author zbj
* @date 2023/5/11
*/
public function getGoogleRank($api_no, string $lang = '', int $day = 7, $force = true)
public function getGoogleRank($project_id, $api_no, string $lang = '', int $day = 7, $force = true)
{
$key = "quanqiusou_api_rank_{$api_no}_{$lang}_{$day}_" . date('Y-m-d');
$res = Cache::get($key);
... ... @@ -105,6 +107,7 @@ class QuanqiusouApi
if($res){
$res = Arr::s2a($res);
Cache::put($key, $res, 2 * 3600);
RankDataLog::addLog($project_id, $api_no, $lang, $endDay, $res);
}
} catch (\Exception | GuzzleException $e) {
errorLog('获取谷歌排名数据失败', [$api_no], $e);
... ...
... ... @@ -390,7 +390,7 @@ class RankDataLogic extends BaseLogic
$api = new QuanqiusouApi();
$model = RankData::where('project_id', $project_id)->where('lang', '')->first();
if (!$model || $model->updated_date != date('Y-m-d') || $force) {
$res = $api->getGoogleRank($api_no, '', 7, $force);
$res = $api->getGoogleRank($project_id, $api_no, '', 7, $force);
if (!$res) {
throw new \Exception("接口数据获取失败,api_no:{$api_no}");
}
... ... @@ -416,7 +416,7 @@ class RankDataLogic extends BaseLogic
}
$model = RankData::where('project_id', $project_id)->where('lang', $lang)->first();
if (!$model || $model->updated_date != date('Y-m-d') || $force) {
$res = $api->getGoogleRank($api_no, $lang, 7, $force);
$res = $api->getGoogleRank($project_id, $api_no, $lang, 7, $force);
if (!$res) {
throw new \Exception("接口数据获取失败,api_no:{$api_no},lang");
}
... ...
<?php
namespace App\Models\RankData;
use App\Helper\Arr;
use App\Models\Base;
/**
* Class GoogleRank
* @package App\Models
* @author zbj
* @date 2023/5/6
*/
class RankDataLog extends Base
{
//设置关联表名
protected $table = 'gl_rank_data_log';
public function setDataAttribute($value)
{
$this->attributes['data'] = Arr::a2s($value);
}
public function getDataAttribute($value)
{
return Arr::s2a($value);
}
public static function addLog($project_id, $api_no, $lang, $date, $res){
$data= [];
foreach ($res as $key=>$item){
$data[$key] = $item[$date];
}
$model = self::where('project_id', $project_id)->where('date', $date)->where('lang', $lang)->first();
if(!$model){
$model = new self();
}
$model->project_id = $project_id;
$model->api_no = $api_no;
$model->lang = $lang;
$model->date = $date;
$model->data = $data;
$model->save();
return true;
}
}
... ...