作者 赵彬吉

rank data

  1 +<?php
  2 +
  3 +namespace App\Console\Commands\RankData;
  4 +
  5 +use Illuminate\Console\Command;
  6 +
  7 +/**
  8 + * Class BaseCommands
  9 + * @package App\Console\Commands\RankData
  10 + * @author zbj
  11 + * @date 2023/5/11
  12 + */
  13 +abstract class BaseCommands extends Command
  14 +{
  15 + /**
  16 + * @author zbj
  17 + * @date 2023/5/11
  18 + */
  19 + public function handle()
  20 + {
  21 + $try = 3;
  22 + do{
  23 + $try--;
  24 + if($try == 0){
  25 + break;
  26 + }
  27 +
  28 + $error = 0;
  29 + try {
  30 + if(!$this->do()){
  31 + $error = 1;
  32 + }
  33 + }catch (\Exception $e){
  34 + errorLog($this->signature . ' error', [], $e);
  35 + $error = 1;
  36 + }
  37 +
  38 + if($error){
  39 + echo 'error';
  40 + }
  41 + $error && sleep(60);
  42 + }while($error);
  43 + }
  44 +
  45 + abstract function do();
  46 +}
  1 +<?php
  2 +
  3 +namespace App\Console\Commands\RankData;
  4 +
  5 +use App\Helper\Arr;
  6 +use App\Helper\SemrushApi;
  7 +use App\Models\RankData\ExternalLinks as ExternalLinksModel;
  8 +use App\Models\Project\DeployOptimize;
  9 +
  10 +/**
  11 + * Class ExternalLinks
  12 + * @package App\Console\Commands
  13 + * @author zbj
  14 + * @date 2023/5/9
  15 + */
  16 +class ExternalLinks extends BaseCommands
  17 +{
  18 + /**
  19 + * The name and signature of the console command.
  20 + *
  21 + * @var string
  22 + */
  23 + protected $signature = 'rank_data_external_links';
  24 +
  25 + /**
  26 + * The console command description.
  27 + *
  28 + * @var string
  29 + */
  30 + protected $description = '排名数据-外链数据';
  31 +
  32 + /**
  33 + * @author zbj
  34 + * @date 2023/5/6
  35 + */
  36 + public function do()
  37 + {
  38 + $error = 0;
  39 + $semrushApi = new SemrushApi();
  40 + //有排名api编号的项目
  41 + $list = DeployOptimize::where('api_no', '>', 0)->pluck('domain', 'project_id')->toArray();
  42 +
  43 + foreach ($list as $project_id => $domain) {
  44 + if(!$domain){
  45 + continue;
  46 + }
  47 + $model = ExternalLinksModel::where('project_id', $project_id)->first();
  48 + if ($model && $model->updated_date == getThisWeekStarDate()) {
  49 + continue;
  50 + }
  51 + if (!$model) {
  52 + $model = new ExternalLinksModel();
  53 + }
  54 +
  55 + //外链数据
  56 + $res = $semrushApi->backlinks_overview($domain);
  57 + if (!$res) {
  58 + $error++;
  59 + continue;
  60 + }
  61 +
  62 + $data = $this->_data($project_id, $res['total']);
  63 +
  64 + $model->project_id = $project_id;
  65 + $model->total = $data['total'];
  66 + $model->data = $data['data'];
  67 + $model->updated_date = date('Y-m-d');
  68 + $model->save();
  69 + }
  70 + return !$error;
  71 + }
  72 +
  73 +
  74 + /**
  75 + * 构造chat数据
  76 + * @param $project_id
  77 + * @param $total
  78 + * @return array|mixed
  79 + * @author zbj
  80 + * @date 2023/5/10
  81 + */
  82 + public function _data($project_id, $total)
  83 + {
  84 + // //外链数
  85 + $data['total'] = intval($total);
  86 + $model = ExternalLinksModel::where('project_id', $project_id)->first();
  87 + if ($model) {
  88 + //特殊处理的外链数
  89 +// $data['total'] = ($total > $model->total) ? intval($total) : $model->total; //特殊处理的
  90 +
  91 + //chat数据
  92 + $chat_data = Arr::s2a($model['data']);
  93 + if (empty($chat_data[date('Y-m-d')])) {
  94 + array_shift($chat_data);
  95 + }
  96 + } else {
  97 + //chat数据
  98 + for ($i = 1; $i < 12; $i++) {
  99 + $date = date("Y-m-d", strtotime(-7 * $i . 'days'));
  100 + $chat_data[$date] = ceil($total - ($total * rand(5, 10) / 100));
  101 + }
  102 + }
  103 + $chat_data[date('Y-m-d')] = $data['total'];
  104 + $data['data'] = $chat_data;
  105 + return $data;
  106 + }
  107 +
  108 +}
  1 +<?php
  2 +
  3 +namespace App\Console\Commands\RankData;
  4 +
  5 +use App\Helper\QuanqiusouApi;
  6 +use App\Models\Project\DeployOptimize;
  7 +use App\Models\RankData\IndexedPages as IndexedPagesModel;
  8 +
  9 +/**
  10 + * Class IndexedPages
  11 + * @package App\Console\Commands
  12 + * @author zbj
  13 + * @date 2023/5/11
  14 + */
  15 +class IndexedPages extends BaseCommands
  16 +{
  17 + /**
  18 + * The name and signature of the console command.
  19 + *
  20 + * @var string
  21 + */
  22 + protected $signature = 'rank_data_indexed_pages';
  23 +
  24 + /**
  25 + * The console command description.
  26 + *
  27 + * @var string
  28 + */
  29 + protected $description = '排名数据-页面收录数';
  30 +
  31 + /**
  32 + * @throws \Exception
  33 + * @author zbj
  34 + * @date 2023/5/11
  35 + */
  36 + public function do(){
  37 + $error = 0;
  38 + $api = new QuanqiusouApi();
  39 + //有排名api编号的项目
  40 + $list = DeployOptimize::where('api_no', '>', 0)->pluck('api_no', 'project_id')->toArray();
  41 +
  42 + foreach ($list as $project_id => $api_no) {
  43 + $model = IndexedPagesModel::where('project_id', $project_id)->first();
  44 + if($model && $model->updated_date == getThisWeekStarDate()){
  45 + continue;
  46 + }
  47 +
  48 + if(!$model){
  49 + $model = new IndexedPagesModel();
  50 + }
  51 +
  52 + $res = $api->getSiteResPer($api_no);
  53 + if(!$res){
  54 + $error++;
  55 + continue;
  56 + }
  57 +
  58 + $model->project_id = $project_id;
  59 + $model->data = $res['data'];
  60 + $model->updated_date = date('Y-m-d');
  61 + $model->save();
  62 + }
  63 +
  64 + return !$error;
  65 + }
  66 +
  67 +}
  1 +<?php
  2 +
  3 +namespace App\Console\Commands\RankData;
  4 +
  5 +use App\Helper\Arr;
  6 +use App\Helper\QuanqiusouApi;
  7 +use App\Models\Project\DeployBuild;
  8 +use App\Models\Project\DeployOptimize;
  9 +use App\Models\RankData\RankData as GoogleRankModel;
  10 +
  11 +/**
  12 + * Class GoogleRank
  13 + * @package App\Console\Commands
  14 + * @author zbj
  15 + * @date 2023/5/6
  16 + */
  17 +class RankData extends BaseCommands
  18 +{
  19 + /**
  20 + * The name and signature of the console command.
  21 + *
  22 + * @var string
  23 + */
  24 + protected $signature = 'rank_data';
  25 +
  26 + /**
  27 + * The console command description.
  28 + *
  29 + * @var string
  30 + */
  31 + protected $description = '谷歌排名数据';
  32 +
  33 + /**
  34 + * @author zbj
  35 + * @date 2023/5/6
  36 + */
  37 + public function do()
  38 + {
  39 + $error = 0;
  40 + $api = new QuanqiusouApi();
  41 + //有排名api编号的项目
  42 + $list = DeployOptimize::where('api_no', '>' , 0)->select('api_no','minor_languages','project_id')->get();
  43 + //当日所有站点谷歌收录数据
  44 + $site_res = $api->getSiteRes();
  45 + if(!$site_res){
  46 + return false;
  47 + }
  48 +
  49 + foreach ($list as $item){
  50 + $model = GoogleRankModel::where('project_id', $item['project_id'])->where('lang', '')->first();
  51 + if (!$model || $model->updated_date == date('Y-m-d')) {
  52 + $res = $api->getGoogleRank($item['api_no']);
  53 + if(!$res){
  54 + $error++;
  55 + continue;
  56 + }
  57 + //收录数
  58 + $indexed_pages_num = $site_res[$item['api_no']];
  59 +
  60 + $this->save_rank($item['project_id'], $res, $indexed_pages_num);
  61 + }
  62 +
  63 + //有小语种的
  64 + if($item['minor_languages']){
  65 + $model = GoogleRankModel::where('project_id', $item['project_id'])->where('lang', '<>', '')->first();
  66 + if (!$model || $model->updated_date == date('Y-m-d')) {
  67 + $res = $api->getGoogleRank($item['api_no'], 1);
  68 + if(!$res){
  69 + $error++;
  70 + continue;
  71 + }
  72 + $data = [];
  73 + //不同的小语种取出来
  74 + foreach ($res as $keyword => $v){
  75 + $data[Arr::last($v)['lang']][$keyword] = $v;
  76 + }
  77 + foreach ($data as $lang => $rank){
  78 + $this->save_rank($item['project_id'], $rank, 0, $lang);
  79 + }
  80 + }
  81 + }
  82 + }
  83 + return !$error;
  84 + }
  85 +
  86 + /**
  87 + * @param $project_id
  88 + * @param int $indexed_pages_num
  89 + * @param $data
  90 + * @param string $lang
  91 + * @author zbj
  92 + * @date 2023/5/8
  93 + */
  94 + public function save_rank($project_id, $data, int $indexed_pages_num = 0, string $lang = ''){
  95 + $without_project_ids = []; //不用处理排名的项目
  96 +
  97 + $first_num = $first_page_num = $first_three_pages_num = $first_five_pages_num = $first_ten_pages_num = 0;
  98 +
  99 + if(!$lang){
  100 + foreach ($data as &$ranks){
  101 + foreach ($ranks as &$rank){
  102 + //处理排名
  103 + if(!in_array($project_id, $without_project_ids)){
  104 + if($rank['position'] >= 10){
  105 + $rank['position'] -= 5;
  106 + }
  107 + //todo 需要特殊处理排名的项目
  108 + }
  109 + }
  110 + $last = Arr::last($ranks);
  111 + //第一名
  112 + if($last['position'] == 1){
  113 + $first_num ++;
  114 + }
  115 + //排名第一页
  116 + if($last['position'] > 0 && $last['position'] <= 10){
  117 + $first_page_num ++;
  118 + }
  119 + //排名前三页
  120 + if($last['position'] > 0 && $last['position'] <= 30){
  121 + $first_three_pages_num ++;
  122 + }
  123 + //排名前五页
  124 + if($last['position'] > 0 && $last['position'] <= 50){
  125 + $first_five_pages_num ++;
  126 + }
  127 + //排名前十页
  128 + if($last['position'] > 0 && $last['position'] <= 100){
  129 + $first_ten_pages_num ++;
  130 + }
  131 + }
  132 + }
  133 +
  134 + $where = [
  135 + 'project_id' => $project_id,
  136 + 'lang' => $lang
  137 + ];
  138 + $model = GoogleRankModel::where($where)->first();
  139 + if(!$model){
  140 + $model = new GoogleRankModel();
  141 + }
  142 +
  143 + //关键词达标天数
  144 + if($model->updated_date != date('Y-m-d')){
  145 + //保证关键词数
  146 + $keyword_num = DeployBuild::where('project_id', $project_id)->value('keyword_num');
  147 + if($first_page_num >= $keyword_num){
  148 + $model->compliance_day = $model->compliance_day + 1;
  149 + }
  150 + }
  151 +
  152 + $model->project_id = $project_id;
  153 + $model->first_num = $first_num;
  154 + $model->first_page_num = $first_page_num;
  155 + $model->first_three_pages_num = $first_three_pages_num;
  156 + $model->first_five_pages_num = $first_five_pages_num;
  157 + $model->first_ten_pages_num = $first_ten_pages_num;
  158 + $model->indexed_pages_num = $indexed_pages_num;
  159 + $model->lang = $lang;
  160 + $model->data = $data;
  161 + $model->updated_date = date('Y-m-d');
  162 + $model->save();
  163 + }
  164 +}
  1 +<?php
  2 +
  3 +namespace App\Console\Commands\RankData;
  4 +
  5 +
  6 +use App\Helper\Arr;
  7 +use App\Helper\QuanqiusouApi;
  8 +use App\Models\Project\DeployOptimize;
  9 +use App\Models\RankData\RankWeek as RankWeekModel;
  10 +
  11 +/**
  12 + * Class WeekRank
  13 + * @package App\Console\Commands
  14 + * @author zbj
  15 + * @date 2023/5/11
  16 + */
  17 +class RankWeek extends BaseCommands
  18 +{
  19 + /**
  20 + * The name and signature of the console command.
  21 + *
  22 + * @var string
  23 + */
  24 + protected $signature = 'rank_data_week';
  25 +
  26 + /**
  27 + * The console command description.
  28 + *
  29 + * @var string
  30 + */
  31 + protected $description = '排名数据-每周排名数据';
  32 +
  33 + /**
  34 + * @author zbj
  35 + * @date 2023/5/6
  36 + */
  37 + public function do()
  38 + {
  39 + $error = 0;
  40 +
  41 + //获取每周排名数据
  42 + $api = new QuanqiusouApi();
  43 +
  44 + $res = $api->getGoogleRankWeek();
  45 + if (!$res) {
  46 + return false;
  47 + }
  48 +
  49 + $res = Arr::s2a($res);
  50 + //有排名api编号的项目
  51 + $list = DeployOptimize::where('api_no', '>', 0)->pluck('api_no', 'project_id')->toArray();
  52 +
  53 + foreach ($list as $project_id => $api_no) {
  54 + $rank_week = RankWeekModel::where('project_id', $project_id)->first();
  55 + if ($rank_week && $rank_week->updated_date == getThisWeekStarDate()) {
  56 + //本周数据已更新
  57 + continue;
  58 + }
  59 +
  60 + if (!$rank_week) {
  61 + $rank_week = new RankWeekModel();
  62 + }
  63 +
  64 + $rank_week->project_id = $project_id;
  65 + $rank_week->data = $res['data'][$api_no];
  66 + $rank_week->date = $res['date'];
  67 + $rank_week->updated_date = date('Y-m-d');
  68 + $rank_week->save();
  69 + }
  70 +
  71 + return !$error;
  72 + }
  73 +}
  1 +<?php
  2 +
  3 +namespace App\Console\Commands\RankData;
  4 +
  5 +use App\Helper\SemrushApi;
  6 +use App\Models\RankData\RecommDomain as RecommDomainModel;
  7 +use App\Models\Project\DeployOptimize;
  8 +
  9 +/**
  10 + * Class RecommDomain
  11 + * @package App\Console\Commands
  12 + * @author zbj
  13 + * @date 2023/5/9
  14 + */
  15 +class RecommDomain extends BaseCommands
  16 +{
  17 + /**
  18 + * The name and signature of the console command.
  19 + *
  20 + * @var string
  21 + */
  22 + protected $signature = 'rank_data_recomm_domain';
  23 +
  24 + /**
  25 + * The console command description.
  26 + *
  27 + * @var string
  28 + */
  29 + protected $description = '排名数据-外链引荐域名数据';
  30 +
  31 + /**
  32 + * @author zbj
  33 + * @date 2023/5/6
  34 + */
  35 + public function do()
  36 + {
  37 + $error = 0;
  38 + $semrushApi = new SemrushApi();
  39 + //有排名api编号的项目
  40 + $list = DeployOptimize::where('api_no', '>', 0)->pluck('domain', 'project_id')->toArray();
  41 +
  42 + foreach ($list as $project_id => $domain) {
  43 + if(!$domain){
  44 + continue;
  45 + }
  46 + $model = RecommDomainModel::where('project_id', $project_id)->first();
  47 + if ($model && $model->updated_date == getThisWeekStarDate()) {
  48 + continue;
  49 + }
  50 + if (!$model) {
  51 + $model = new RecommDomainModel();
  52 + }
  53 +
  54 + //外链引荐域名
  55 + $data = $semrushApi->backlinks_refdomains($domain);
  56 + if (!$data) {
  57 + $error++;
  58 + continue;
  59 + }
  60 +
  61 + $model->project_id = $project_id;
  62 + $model->data = $data;
  63 + $model->updated_date = date('Y-m-d');
  64 + $model->save();
  65 + }
  66 + return !$error;
  67 + }
  68 +}
  1 +<?php
  2 +
  3 +namespace App\Console\Commands\RankData;
  4 +
  5 +use App\Helper\Arr;
  6 +use App\Helper\GoogleSpeedApi;
  7 +use App\Models\Project\DeployOptimize;
  8 +use App\Models\RankData\Speed as GoogleSpeedModel;
  9 +
  10 +/**
  11 + * Class GoogleSpeed
  12 + * @package App\Console\Commands
  13 + * @author zbj
  14 + * @date 2023/5/10
  15 + */
  16 +class Speed extends BaseCommands
  17 +{
  18 + /**
  19 + * The name and signature of the console command.
  20 + *
  21 + * @var string
  22 + */
  23 + protected $signature = 'rank_data_speed';
  24 +
  25 + /**
  26 + * The console command description.
  27 + *
  28 + * @var string
  29 + */
  30 + protected $description = '排名数据-测速数据';
  31 +
  32 +
  33 + /**
  34 + * @author zbj
  35 + * @date 2023/5/10
  36 + */
  37 + public function do()
  38 + {
  39 + $error = 0;
  40 +
  41 + $googleSpeedApi = new GoogleSpeedApi();
  42 +
  43 + //有排名api编号的项目
  44 + $list = DeployOptimize::where('api_no', '>', 0)->pluck('domain', 'project_id')->toArray();
  45 +
  46 + foreach ($list as $project_id => $domain) {
  47 + $model = GoogleSpeedModel::where('project_id', $project_id)->first();
  48 + if ($model && $model->updated_date == getThisWeekStarDate()) {
  49 + //今周已更新 跳过
  50 + continue;
  51 + }
  52 +
  53 + $res = $googleSpeedApi->run($domain);
  54 + if (!$res) {
  55 + $error++;
  56 + }
  57 + if (!$model) {
  58 + $model = new GoogleSpeedModel;
  59 + }
  60 + $model->project_id = $project_id;
  61 + $model->data = $res;
  62 + $model->updated_date = date('Y-m-d');
  63 + $model->save();
  64 + }
  65 +
  66 + return !$error;
  67 + }
  68 +}
@@ -16,6 +16,12 @@ class Kernel extends ConsoleKernel @@ -16,6 +16,12 @@ class Kernel extends ConsoleKernel
16 protected function schedule(Schedule $schedule) 16 protected function schedule(Schedule $schedule)
17 { 17 {
18 // $schedule->command('inspire')->hourly(); 18 // $schedule->command('inspire')->hourly();
  19 + $schedule->command('rank_data')->dailyAt('01:00')->withoutOverlapping(1); // 排名数据,每天凌晨执行一次
  20 + $schedule->command('rank_data_speed')->weeklyOn(1, '01:00')->withoutOverlapping(1); // 排名数据-测速数据,每周一凌晨执行一次
  21 + $schedule->command('rank_data_external_links')->weeklyOn(1, '01:00')->withoutOverlapping(1); // 排名数据-外链,每周一凌晨执行一次
  22 + $schedule->command('rank_data_indexed_pages')->weeklyOn(1, '01:00')->withoutOverlapping(1); // 排名数据-页面收录,每周一凌晨执行一次
  23 + $schedule->command('rank_data_recomm_domain')->weeklyOn(1, '01:00')->withoutOverlapping(1); // 排名数据-引荐域名,每周一凌晨执行一次
  24 + $schedule->command('rank_data_week')->weeklyOn(1, '01:00')->withoutOverlapping(1); // 排名数据,每周一凌晨执行一次
19 } 25 }
20 26
21 /** 27 /**
  1 +<?php
  2 +
  3 +
  4 +namespace App\Helper;
  5 +
  6 +use App\Utils\HttpUtils;
  7 +use GuzzleHttp\Exception\GuzzleException;
  8 +
  9 +
  10 +/**
  11 + * Class PageSpeed
  12 + * @package App\Helper
  13 + * @author zbj
  14 + * @date 2023/5/10
  15 + */
  16 +class GoogleSpeedApi
  17 +{
  18 +
  19 + protected $areas = [
  20 + [
  21 + "area" => "洛杉矶",
  22 + "numericValue" => 0,
  23 + ],
  24 + [
  25 + "area" => "圣地亚哥",
  26 + "numericValue" => 0,
  27 + ],
  28 + [
  29 + "area" => "伦敦",
  30 + "numericValue" => 0,
  31 + ],
  32 + [
  33 + "area" => "西雅图",
  34 + "numericValue" => 0,
  35 + ],
  36 + [
  37 + "area" => "吉隆坡",
  38 + "numericValue" => 0,
  39 + ],
  40 + [
  41 + "area" => "雅加达",
  42 + "numericValue" => 0,
  43 + ],
  44 + [
  45 + "area" => "孟买",
  46 + "numericValue" => 0,
  47 + ],
  48 + [
  49 + "area" => "迪拜",
  50 + "numericValue" => 0,
  51 + ],
  52 + [
  53 + "area" => "法兰克福",
  54 + "numericValue" => 0,
  55 + ],
  56 + [
  57 + "area" => "新加坡",
  58 + "numericValue" => 0,
  59 + ],
  60 + [
  61 + "area" => "悉尼",
  62 + "numericValue" => 0,
  63 + ],
  64 + [
  65 + "area" => "东京",
  66 + "numericValue" => 0,
  67 + ],
  68 + [
  69 + "area" => "硅谷",
  70 + "numericValue" => 0,
  71 + ],
  72 + [
  73 + "area" => "弗吉尼亚",
  74 + "numericValue" => 0,
  75 + ],
  76 + [
  77 + "area" => "香港",
  78 + "numericValue" => 0,
  79 + ],
  80 + [
  81 + "area" => "圣保罗",
  82 + "numericValue" => 0,
  83 + ],
  84 + [
  85 + "area" => "雅典",
  86 + "numericValue" => 0,
  87 + ],
  88 + [
  89 + "area" => "巴黎",
  90 + "numericValue" => 0,
  91 + ],
  92 + [
  93 + "area" => "罗马",
  94 + "numericValue" => 0,
  95 + ],
  96 + [
  97 + "area" => "马德里",
  98 + "numericValue" => 0,
  99 + ],
  100 + ];
  101 +
  102 + /**
  103 + * @param $url
  104 + * @return array|false
  105 + * @author zbj
  106 + * @date 2023/5/10
  107 + */
  108 + function run($url)
  109 + {
  110 + try {
  111 + $params = [
  112 + 'url' => $url
  113 + ];
  114 + $res = HttpUtils::get('http://45.136.131.72/api.php', $params);
  115 + if ($res) {
  116 + $res = Arr::s2a($res);
  117 + $area_data = Arr::s2a($res['area_data']);
  118 + }
  119 + $numericValue = $area_data[0]['numericValue'] ?? rand(500, 1000);
  120 + foreach ($this->areas as &$area) {
  121 + $start = -$numericValue * 0.5;
  122 + $end = $numericValue * 0.5;
  123 + $numer = rand($start, $end);
  124 + $area["numericValue"] = ceil($numericValue - $numer);
  125 + }
  126 +
  127 + return [
  128 + "url" => $url,
  129 + "area_data" => $this->areas,
  130 + "created_at" => date("Y-m-d H:i:s")
  131 + ];
  132 +
  133 + } catch (\Exception | GuzzleException $e) {
  134 + errorLog('测速失败', $params, $e);
  135 + return false;
  136 + }
  137 + }
  138 +}
  1 +<?php
  2 +
  3 +
  4 +namespace App\Helper;
  5 +
  6 +use App\Utils\HttpUtils;
  7 +use GuzzleHttp\Exception\GuzzleException;
  8 +use Illuminate\Support\Facades\Cache;
  9 +
  10 +
  11 +/**
  12 + * Class QuanqiusouApi
  13 + * @package App\Helper
  14 + * @author zbj
  15 + * @date 2023/5/11
  16 + */
  17 +class QuanqiusouApi
  18 +{
  19 + //接口地址
  20 + protected $url = 'http://api.quanqiusou.cn';
  21 +
  22 + /**
  23 + * 所有站点收录页面数
  24 + * @author zbj
  25 + * @date 2023/5/11
  26 + */
  27 + public function getSiteRes()
  28 + {
  29 + $key = 'quanqiusou_api_site_res_' . date('Y-m-d');
  30 + $res = Cache::get($key);
  31 + if (!$res) {
  32 + $api_url = $this->url . '/google-rank/echo_site_res.php';
  33 + try {
  34 + $res = HttpUtils::get($api_url, []);
  35 + if($res){
  36 + $res = Arr::s2a($res);
  37 + Cache::put($key, $res, 24 * 3600);
  38 + }
  39 + } catch (\Exception | GuzzleException $e) {
  40 + errorLog('获取站点收录页面数', [], $e);
  41 + return false;
  42 + }
  43 + }
  44 + return $res;
  45 + }
  46 +
  47 + /**
  48 + * 指定站点收录页面数
  49 + * @param $api_no
  50 + * @return array|false|mixed
  51 + * @author zbj
  52 + * @date 2023/5/11
  53 + */
  54 + public function getSiteResPer($api_no){
  55 + $key = 'quanqiusou_api_site_res_per_' . $api_no . '_' . date('Y-m-d');
  56 + $res = Cache::get($key);
  57 + if (!$res) {
  58 + $api_url = $this->url . '/google-rank/echo_site_res_per.php';
  59 + try {
  60 + $res = HttpUtils::get($api_url, ['apino' => $api_no]);
  61 + if($res){
  62 + $res = Arr::s2a($res);
  63 + Cache::put($key, $res, 24 * 3600);
  64 + }
  65 + } catch (\Exception | GuzzleException $e) {
  66 + errorLog('获取站点收录页面数', [], $e);
  67 + return false;
  68 + }
  69 + }
  70 + return $res;
  71 + }
  72 +
  73 +
  74 + /**
  75 + * 获取谷歌排名数据
  76 + * @param $api_no
  77 + * @param int $lang
  78 + * @param int $day
  79 + * @return array|false|mixed
  80 + * @author zbj
  81 + * @date 2023/5/11
  82 + */
  83 + public function getGoogleRank($api_no, int $lang = 0, int $day = 7)
  84 + {
  85 + $key = "quanqiusou_api_rank_{$api_no}_{$lang}_{$day}_" . date('Y-m-d');
  86 + $res = Cache::get($key);
  87 + if (!$res) {
  88 + $param = [
  89 + 'key' => '289c1fc81c89d79c04ed4fd72822948e',
  90 + 'w' => $api_no,
  91 + 'type' => $day
  92 + ];
  93 + if ($lang) {
  94 + $param['lang'] = $lang;
  95 + }
  96 +
  97 + $api_url = $this->url . '/api';
  98 +
  99 + try {
  100 + $res = HttpUtils::get($api_url, $param);
  101 + if($res){
  102 + $res = Arr::s2a($res);
  103 + Cache::put($key, $res, 24 * 3600);
  104 + }
  105 + } catch (\Exception | GuzzleException $e) {
  106 + errorLog('获取谷歌排名数据失败', $api_no, $e);
  107 + return false;
  108 + }
  109 + }
  110 +
  111 + return $res;
  112 + }
  113 +
  114 + /**
  115 + * 获取每周谷歌排名数据
  116 + * @return array|false|mixed
  117 + * @author zbj
  118 + * @date 2023/5/11
  119 + */
  120 + public function getGoogleRankWeek()
  121 + {
  122 + $key = "quanqiusou_api_week_data_" . date('Y-m-d');
  123 + $res = Cache::get($key);
  124 + if (!$res) {
  125 + $api_url = $this->url . '/google-rank/echo_week_data.php';
  126 + try {
  127 + $res = HttpUtils::get($api_url, []);
  128 + if($res){
  129 + $res = Arr::s2a($res);
  130 + Cache::put($key, $res, 24 * 3600);
  131 + }
  132 + } catch (\Exception | GuzzleException $e) {
  133 + errorLog('获取每周谷歌排名数据失败', [], $e);
  134 + return false;
  135 + }
  136 + }
  137 + return $res;
  138 + }
  139 +
  140 +}
  1 +<?php
  2 +
  3 +
  4 +namespace App\Helper;
  5 +
  6 +use App\Utils\HttpUtils;
  7 +use GuzzleHttp\Exception\GuzzleException;
  8 +
  9 +
  10 +/**
  11 + * Class SemrushApi
  12 + * @package App\Helper
  13 + * @author zbj
  14 + * @date 2023/5/9
  15 + */
  16 +class SemrushApi
  17 +{
  18 + //接口地址
  19 + protected $url = 'https://api.semrush.com';
  20 +
  21 + protected $key = '2927058317c47207e4a8c4cacf10acfd';
  22 +
  23 + /**
  24 + * 反链概述
  25 + */
  26 + function backlinks_overview($target,$target_type = "root_domain"){
  27 + if($target){
  28 + $url = $this->url."/analytics/v1/?";
  29 + $params = [
  30 + "type"=>"backlinks_overview",
  31 + "key"=>$this->key,
  32 + "target"=>$target,
  33 + "target_type"=>$target_type,
  34 + "export_columns"=>"ascore,total,domains_num,urls_num,ips_num,ipclassc_num,follows_num,nofollows_num,sponsored_num,ugc_num,texts_num,images_num,forms_num,frames_num"
  35 + ];
  36 + try {
  37 + $res = HttpUtils::get($url, $params);
  38 + return $this->data($res)[0];
  39 + }catch (\Exception|GuzzleException $e){
  40 + errorLog('获取站点外链数据', $params, $e);
  41 + return false;
  42 + }
  43 + }
  44 + return [];
  45 + }
  46 +
  47 + /**
  48 + * 引荐域名
  49 + */
  50 + public function backlinks_refdomains($target,$target_type = "root_domain",$offset = 0){
  51 + if($target){
  52 + $url = $this->url."/analytics/v1/?";
  53 + $params = [
  54 + "type"=>"backlinks_refdomains",
  55 + "key"=>$this->key,
  56 + "target"=>$target,
  57 + "target_type"=>$target_type,
  58 + "export_columns"=>"domain_ascore,domain,backlinks_num,ip,country,first_seen,last_seen",
  59 + "display_limit"=>10,
  60 + "display_offset"=>$offset
  61 + ];
  62 + try {
  63 + $res = HttpUtils::get($url, $params);
  64 + return $this->data($res);
  65 + }catch (\Exception|GuzzleException $e){
  66 + errorLog('获取站点外链数据', $params, $e);
  67 + return false;
  68 + }
  69 + }
  70 + return [];
  71 + }
  72 +
  73 +
  74 + /**
  75 + * 处理结果
  76 + * @param $res
  77 + * @return array
  78 + * @author zbj
  79 + * @date 2023/5/9
  80 + */
  81 + protected function data($res){
  82 + $temp = explode("\n",$res);
  83 +
  84 + $arr =array_map(function ($v){
  85 + return explode(";",$v);
  86 + }, $temp);
  87 + $data = [];
  88 +
  89 + for ($i = 1; $i < count($arr) - 1; $i++) {
  90 + $tmp = [];
  91 + foreach($arr[0] as $k =>$v){
  92 + $tmp[trim($v)] = trim($arr[$i][$k]);
  93 + }
  94 + $data[] = $tmp;
  95 + }
  96 + return $data;
  97 + }
  98 +
  99 +}
1 <?php 1 <?php
2 2
3 use App\Utils\LogUtils; 3 use App\Utils\LogUtils;
  4 +use Illuminate\Support\Carbon;
4 5
5 define('HTTP_OPENAI_URL','http://openai.waimaoq.com/'); 6 define('HTTP_OPENAI_URL','http://openai.waimaoq.com/');
6 /** 7 /**
@@ -144,3 +145,16 @@ if (!function_exists('checkDomain')) { @@ -144,3 +145,16 @@ if (!function_exists('checkDomain')) {
144 } 145 }
145 } 146 }
146 } 147 }
  148 +
  149 +if (!function_exists('getThisWeekStarDate')) {
  150 + /**
  151 + * 获取本周一的日期
  152 + * @return mixed
  153 + * @author zbj
  154 + * @date 2023/5/11
  155 + */
  156 + function getThisWeekStarDate()
  157 + {
  158 + return Carbon::now()->startOfWeek()->toDateString();
  159 + }
  160 +}
  1 +<?php
  2 +
  3 +namespace App\Http\Controllers\Bside;
  4 +
  5 +use App\Http\Logic\Bside\RankDataLogic;
  6 +use Illuminate\Http\Request;
  7 +
  8 +/**
  9 + * Class GoogleRankController
  10 + * @package App\Http\Controllers\Bside
  11 + * @author zbj
  12 + * @date 2023/5/9
  13 + */
  14 +class RankDataController extends BaseController
  15 +{
  16 +
  17 + public function index(RankDataLogic $logic)
  18 + {
  19 + $data = $logic->index();
  20 + return $this->success($data);
  21 + }
  22 +
  23 + public function keywords_rank_list(RankDataLogic $logic){
  24 + $data = $logic->keywords_rank_list();
  25 + return $this->success($data);
  26 + }
  27 +}
  1 +<?php
  2 +
  3 +namespace App\Http\Logic\Bside;
  4 +
  5 +
  6 +use App\Helper\Arr;
  7 +use App\Helper\Translate;
  8 +use App\Http\Logic\Aside\Project\ProjectLogic;
  9 +use App\Models\RankData\ExternalLinks;
  10 +use App\Models\RankData\IndexedPages;
  11 +use App\Models\RankData\RankData;
  12 +use App\Models\RankData\RankWeek;
  13 +use App\Models\RankData\RecommDomain;
  14 +use App\Models\RankData\Speed;
  15 +use Illuminate\Support\Str;
  16 +
  17 +class RankDataLogic extends BaseLogic
  18 +{
  19 +
  20 + /**
  21 + * 统计数据
  22 + * @author zbj
  23 + * @date 2023/5/11
  24 + */
  25 + public function index()
  26 + {
  27 + $project_id = $this->user['project_id'];
  28 +
  29 + //查数据
  30 + $project = app(ProjectLogic::class)->getInfo($project_id);
  31 + $rank = RankData::where('project_id', $project_id)->first();
  32 + $rank_week = RankWeek::where('project_id', $project_id)->first();
  33 + $recomm_domain = RecommDomain::where('project_id', $project_id)->first();
  34 + $external_links = ExternalLinks::where('project_id', $project_id)->first();
  35 + $indexed_pages = IndexedPages::where('project_id', $project_id)->first();
  36 + $speed = Speed::where('project_id', $project_id)->first();
  37 +
  38 + //排名数据
  39 + $data = [
  40 + 'first_num' => $rank['first_num'] ?? 0,
  41 + 'first_page_num' => $rank['first_page_num'] ?? 0,
  42 + 'first_three_pages_num' => $rank['first_three_pages_num'] ?? 0,
  43 + 'first_five_pages_num' => $rank['first_five_pages_num'] ?? 0,
  44 + 'first_ten_pages_num' => $rank['first_ten_pages_num'] ?? 0,
  45 + 'indexed_pages_num' => $rank['indexed_pages_num'] ?? 0,
  46 + 'external_links_num' => $external_links['total'] ?? 0,
  47 + ];
  48 +
  49 + //小语种列表
  50 + $langs = Arr::pluck($project['deploy_optimize']['minor_languages'], 'tl');
  51 + foreach ($langs as $lang){
  52 + $data['langs'][$lang] = Translate::getTls($lang);
  53 + }
  54 +
  55 + //项目信息
  56 + $data['project'] = [
  57 + 'company' => $project['company'],
  58 + 'domain' => $project['deploy_optimize']['domain'],
  59 + 'domain_info' => '',
  60 + 'cert_info' => '',
  61 + 'plan' => str_replace('营销大师-', '全球搜-', $project['deploy_build']['plan'][0]),
  62 + 'keyword_num' => $project['deploy_build']['keyword_num'],
  63 + 'compliance_day' => $rank['compliance_day'] ?? 0,
  64 + 'remain_day' => $project['deploy_build']['service_duration'] - ($rank['compliance_day'] ?? 0),
  65 + ];
  66 +
  67 + //测速
  68 + $data['speed'] = $speed['data'] ?? [];
  69 +
  70 + //seo基础设置
  71 + $data['seo'] = [
  72 + 'H1,H2,H3标签',
  73 + 'TDK设置(Title, Description, Keywords)',
  74 + 'Google 收录提交',
  75 + 'Sitemap.xml Google站长地图',
  76 + 'Robots.txt文件优化',
  77 + 'Google站长工具优化设置'
  78 + ];
  79 +
  80 + //外链引荐域名
  81 + $data['external_links_domain_chat'] = [
  82 + 'labels' => array_map(function ($item) {
  83 + return Str::substrReplace($item, '***', 2, 3);
  84 + }, Arr::pluck($recomm_domain['data'] ?? [], 'domain')),
  85 + 'data' => Arr::pluck($recomm_domain['data'] ?? [], 'backlinks_num'),
  86 + 'list_date' => $recomm_domain['updated_at'] ?? '',
  87 + ];
  88 + //SEO数据周期分析图 外链数
  89 + $data['external_links_chat'] = [
  90 + 'labels' => array_keys($external_links['data'] ?? []),
  91 + 'data' => array_values($external_links['data'] ?? []),
  92 + ];
  93 + //SEO数据周期分析图 收录数
  94 + $data['indexed_pages_chat'] = [
  95 + 'labels' => array_keys($indexed_pages['data'] ?? []),
  96 + 'data' => array_values($indexed_pages['data'] ?? []),
  97 + ];
  98 + //收录数加上当日数据
  99 + if((Arr::last($data['indexed_pages_chat']['labels'])) != date('Y-m-d')){
  100 + $data['indexed_pages_chat']['labels'][] = date('Y-m-d');
  101 + $data['indexed_pages_chat']['data'][] = $data['indexed_pages_num'];
  102 + }
  103 + //关键词排名分析图
  104 + $data['rank_chat'] = [
  105 + 'data' => $rank_week['data'],
  106 + 'labels' => $rank_week['date'],
  107 + ];
  108 +
  109 + return $data;
  110 + }
  111 +
  112 + /**
  113 + * 关键词排名
  114 + * @author zbj
  115 + * @date 2023/5/12
  116 + */
  117 + public function keywords_rank_list(){
  118 + $lang = $this->request['lang'];
  119 + }
  120 +}
@@ -13,11 +13,11 @@ class DeployBuild extends Base @@ -13,11 +13,11 @@ class DeployBuild extends Base
13 13
14 14
15 public function setPlanAttribute($value){ 15 public function setPlanAttribute($value){
16 - $this->attributes['plan'] = Arr::arrToSet($value); 16 + $this->attributes['plan'] = Arr::arrToSet($value, 'trim');
17 } 17 }
18 18
19 public function getPlanAttribute($value){ 19 public function getPlanAttribute($value){
20 - return Arr::setToArr($value); 20 + return Arr::setToArr($value, 'trim');
21 } 21 }
22 22
23 public static function clearCache($row){ 23 public static function clearCache($row){
@@ -50,6 +50,26 @@ class Project extends Base @@ -50,6 +50,26 @@ class Project extends Base
50 ]; 50 ];
51 } 51 }
52 52
  53 + public static function planMap(){
  54 + return [
  55 + '营销大师-体验版',
  56 + '营销大师-标准版',
  57 + '营销大师-商务版',
  58 + '营销大师-旗舰版',
  59 + '数据大师-体验版',
  60 + '数据大师-智能版',
  61 + '数据大师-智慧版',
  62 + 'PLUS-尊享版',
  63 + 'PLUS-尊贵版',
  64 + 'PLUS-至尊版',
  65 + '防疫物质推广方案',
  66 + '疫情期间免费版',
  67 + '建站大师定制优化方案',
  68 + '星链网站(1年版)',
  69 + '星链网站(2年版)',
  70 + ];
  71 + }
  72 +
53 /** 73 /**
54 * 项目部署服务器信息 74 * 项目部署服务器信息
55 * @return \Illuminate\Database\Eloquent\Relations\HasOne 75 * @return \Illuminate\Database\Eloquent\Relations\HasOne
  1 +<?php
  2 +
  3 +namespace App\Models\RankData;
  4 +
  5 +
  6 +use App\Helper\Arr;
  7 +use App\Models\Base;
  8 +
  9 +
  10 +/**
  11 + * Class ExternalLinks
  12 + * @package App\Models
  13 + * @author zbj
  14 + * @date 2023/5/10
  15 + */
  16 +class ExternalLinks extends Base
  17 +{
  18 + //设置关联表名
  19 + protected $table = 'gl_rank_data_external_links';
  20 +
  21 + public function setDataAttribute($value)
  22 + {
  23 + $this->attributes['data'] = Arr::a2s($value);
  24 + }
  25 +
  26 + public function getDataAttribute($value)
  27 + {
  28 + return Arr::s2a($value);
  29 + }
  30 +}
  1 +<?php
  2 +
  3 +namespace App\Models\RankData;
  4 +
  5 +
  6 +use App\Helper\Arr;
  7 +use App\Models\Base;
  8 +
  9 +
  10 +/**
  11 + * Class IndexedPages
  12 + * @package App\Models
  13 + * @author zbj
  14 + * @date 2023/5/10
  15 + */
  16 +class IndexedPages extends Base
  17 +{
  18 + //设置关联表名
  19 + protected $table = 'gl_rank_data_indexed_pages';
  20 +
  21 + public function setDataAttribute($value)
  22 + {
  23 + $this->attributes['data'] = Arr::a2s($value);
  24 + }
  25 +
  26 + public function getDataAttribute($value)
  27 + {
  28 + return Arr::s2a($value);
  29 + }
  30 +}
  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 RankData extends Base
  16 +{
  17 + //设置关联表名
  18 + protected $table = 'gl_rank_data';
  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 +}
  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 RankWeek extends Base
  16 +{
  17 + //设置关联表名
  18 + protected $table = 'gl_rank_data_week';
  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 function setDateAttribute($value)
  31 + {
  32 + $this->attributes['date'] = Arr::a2s($value);
  33 + }
  34 +
  35 + public function getDateAttribute($value)
  36 + {
  37 + return Arr::s2a($value);
  38 + }
  39 +}
  1 +<?php
  2 +
  3 +namespace App\Models\RankData;
  4 +
  5 +
  6 +use App\Helper\Arr;
  7 +use App\Models\Base;
  8 +
  9 +
  10 +/**
  11 + * Class RecommDomain
  12 + * @package App\Models
  13 + * @author zbj
  14 + * @date 2023/5/10
  15 + */
  16 +class RecommDomain extends Base
  17 +{
  18 + //设置关联表名
  19 + protected $table = 'gl_rank_data_recomm_domain';
  20 +
  21 + public function setDataAttribute($value)
  22 + {
  23 + $this->attributes['data'] = Arr::a2s($value);
  24 + }
  25 +
  26 + public function getDataAttribute($value)
  27 + {
  28 + return Arr::s2a($value);
  29 + }
  30 +}
  1 +<?php
  2 +
  3 +namespace App\Models\RankData;
  4 +
  5 +
  6 +use App\Helper\Arr;
  7 +use App\Models\Base;
  8 +
  9 +
  10 +/**
  11 + * Class GoogleSpeed
  12 + * @package App\Models
  13 + * @author zbj
  14 + * @date 2023/5/10
  15 + */
  16 +class Speed extends Base
  17 +{
  18 + //设置关联表名
  19 + protected $table = 'gl_rank_data_speed';
  20 +
  21 + public function setDataAttribute($value)
  22 + {
  23 + $this->attributes['data'] = Arr::a2s($value);
  24 + }
  25 +
  26 + public function getDataAttribute($value)
  27 + {
  28 + return Arr::s2a($value);
  29 + }
  30 +
  31 +}
@@ -242,7 +242,11 @@ Route::middleware(['bloginauth'])->group(function () { @@ -242,7 +242,11 @@ Route::middleware(['bloginauth'])->group(function () {
242 Route::delete('/delete', [\App\Http\Controllers\Bside\NavController::class, 'delete'])->name('bside_nav_delete'); 242 Route::delete('/delete', [\App\Http\Controllers\Bside\NavController::class, 'delete'])->name('bside_nav_delete');
243 }); 243 });
244 244
245 - 245 + //排名数据
  246 + Route::prefix('rank_data')->group(function () {
  247 + Route::any('/index', [\App\Http\Controllers\Bside\RankDataController::class, 'index'])->name('rank_data');
  248 + Route::any('/keywords_rank_list', [\App\Http\Controllers\Bside\RankDataController::class, 'keywords_rank_list'])->name('rank_data_keywords_rank_list');
  249 + });
246 250
247 251
248 }); 252 });