作者 赵彬吉

update

@@ -3,6 +3,7 @@ @@ -3,6 +3,7 @@
3 3
4 namespace App\Helper; 4 namespace App\Helper;
5 5
  6 +use App\Models\RankData\RankDataLog;
6 use App\Utils\HttpUtils; 7 use App\Utils\HttpUtils;
7 use GuzzleHttp\Client; 8 use GuzzleHttp\Client;
8 use GuzzleHttp\Exception\GuzzleException; 9 use GuzzleHttp\Exception\GuzzleException;
@@ -75,6 +76,7 @@ class QuanqiusouApi @@ -75,6 +76,7 @@ class QuanqiusouApi
75 76
76 /** 77 /**
77 * 获取谷歌排名数据 78 * 获取谷歌排名数据
  79 + * @param $project_id
78 * @param $api_no 80 * @param $api_no
79 * @param string $lang 81 * @param string $lang
80 * @param int $day 82 * @param int $day
@@ -82,7 +84,7 @@ class QuanqiusouApi @@ -82,7 +84,7 @@ class QuanqiusouApi
82 * @author zbj 84 * @author zbj
83 * @date 2023/5/11 85 * @date 2023/5/11
84 */ 86 */
85 - public function getGoogleRank($api_no, string $lang = '', int $day = 7, $force = true) 87 + public function getGoogleRank($project_id, $api_no, string $lang = '', int $day = 7, $force = true)
86 { 88 {
87 $key = "quanqiusou_api_rank_{$api_no}_{$lang}_{$day}_" . date('Y-m-d'); 89 $key = "quanqiusou_api_rank_{$api_no}_{$lang}_{$day}_" . date('Y-m-d');
88 $res = Cache::get($key); 90 $res = Cache::get($key);
@@ -105,6 +107,7 @@ class QuanqiusouApi @@ -105,6 +107,7 @@ class QuanqiusouApi
105 if($res){ 107 if($res){
106 $res = Arr::s2a($res); 108 $res = Arr::s2a($res);
107 Cache::put($key, $res, 2 * 3600); 109 Cache::put($key, $res, 2 * 3600);
  110 + RankDataLog::addLog($project_id, $api_no, $lang, $endDay, $res);
108 } 111 }
109 } catch (\Exception | GuzzleException $e) { 112 } catch (\Exception | GuzzleException $e) {
110 errorLog('获取谷歌排名数据失败', [$api_no], $e); 113 errorLog('获取谷歌排名数据失败', [$api_no], $e);
@@ -390,7 +390,7 @@ class RankDataLogic extends BaseLogic @@ -390,7 +390,7 @@ class RankDataLogic extends BaseLogic
390 $api = new QuanqiusouApi(); 390 $api = new QuanqiusouApi();
391 $model = RankData::where('project_id', $project_id)->where('lang', '')->first(); 391 $model = RankData::where('project_id', $project_id)->where('lang', '')->first();
392 if (!$model || $model->updated_date != date('Y-m-d') || $force) { 392 if (!$model || $model->updated_date != date('Y-m-d') || $force) {
393 - $res = $api->getGoogleRank($api_no, '', 7, $force); 393 + $res = $api->getGoogleRank($project_id, $api_no, '', 7, $force);
394 if (!$res) { 394 if (!$res) {
395 throw new \Exception("接口数据获取失败,api_no:{$api_no}"); 395 throw new \Exception("接口数据获取失败,api_no:{$api_no}");
396 } 396 }
@@ -416,7 +416,7 @@ class RankDataLogic extends BaseLogic @@ -416,7 +416,7 @@ class RankDataLogic extends BaseLogic
416 } 416 }
417 $model = RankData::where('project_id', $project_id)->where('lang', $lang)->first(); 417 $model = RankData::where('project_id', $project_id)->where('lang', $lang)->first();
418 if (!$model || $model->updated_date != date('Y-m-d') || $force) { 418 if (!$model || $model->updated_date != date('Y-m-d') || $force) {
419 - $res = $api->getGoogleRank($api_no, $lang, 7, $force); 419 + $res = $api->getGoogleRank($project_id, $api_no, $lang, 7, $force);
420 if (!$res) { 420 if (!$res) {
421 throw new \Exception("接口数据获取失败,api_no:{$api_no},lang"); 421 throw new \Exception("接口数据获取失败,api_no:{$api_no},lang");
422 } 422 }
  1 +<?php
  2 +
  3 +namespace App\Models\RankData;
  4 +
  5 +
  6 +use App\Helper\Arr;
  7 +use App\Models\Base;
  8 +
  9 +/**
  10 + * Class GoogleRank
  11 + * @package App\Models
  12 + * @author zbj
  13 + * @date 2023/5/6
  14 + */
  15 +class RankDataLog extends Base
  16 +{
  17 + //设置关联表名
  18 + protected $table = 'gl_rank_data_log';
  19 +
  20 + public function setDataAttribute($value)
  21 + {
  22 + $this->attributes['data'] = Arr::a2s($value);
  23 + }
  24 +
  25 + public function getDataAttribute($value)
  26 + {
  27 + return Arr::s2a($value);
  28 + }
  29 +
  30 + public static function addLog($project_id, $api_no, $lang, $date, $res){
  31 + $data= [];
  32 + foreach ($res as $key=>$item){
  33 + $data[$key] = $item[$date];
  34 + }
  35 + $model = self::where('project_id', $project_id)->where('date', $date)->where('lang', $lang)->first();
  36 + if(!$model){
  37 + $model = new self();
  38 + }
  39 + $model->project_id = $project_id;
  40 + $model->api_no = $api_no;
  41 + $model->lang = $lang;
  42 + $model->date = $date;
  43 + $model->data = $data;
  44 + $model->save();
  45 +
  46 + return true;
  47 + }
  48 +
  49 +}