作者 赵彬吉

update

@@ -137,4 +137,70 @@ class QuanqiusouApi @@ -137,4 +137,70 @@ class QuanqiusouApi
137 return $res; 137 return $res;
138 } 138 }
139 139
  140 +
  141 + /**
  142 + * 获取历史排名统计数据
  143 + * @param $api_no
  144 + * @param string $lang
  145 + * @return array|false|int|mixed|null
  146 + * @author zbj
  147 + * @date 2023/5/15
  148 + */
  149 + public function getHistoryCount($api_no, $lang = '')
  150 + {
  151 + $key = "quanqiusou_api_history_count_{$api_no}_{$lang}_" . date('Y-m-d');
  152 + $res = Cache::get($key);
  153 + if (!$res) {
  154 + $api_url = $this->url . '/google-rank/history_count.php';
  155 + $param = [
  156 + 'apino' => $api_no,
  157 + ];
  158 + if ($lang) {
  159 + $param['lang'] = $lang;
  160 + }
  161 + try {
  162 + $res = HttpUtils::get($api_url, $param);
  163 + if($res){
  164 + $res = Arr::s2a($res);
  165 + Cache::put($key, $res, 24 * 3600);
  166 + }
  167 + } catch (\Exception | GuzzleException $e) {
  168 + errorLog('获取历史排名统计数据失败', [], $e);
  169 + return false;
  170 + }
  171 + }
  172 + return $res;
  173 + }
  174 +
  175 +
  176 + /**
  177 + * 获取小语种项目
  178 + * @return array|false|int|mixed|null
  179 + * @author zbj
  180 + * @date 2023/5/15
  181 + */
  182 + public function getLangList()
  183 + {
  184 + $key = "quanqiusou_api_lang_list_" . date('Y-m-d');
  185 + $res = Cache::get($key);
  186 + if (!$res) {
  187 + $api_url = $this->url . '/api/index/langlist';
  188 + $param = [
  189 + 'key' => '289c1fc81c89d79c04ed4fd72822948e',
  190 + ];
  191 + try {
  192 + $res = HttpUtils::get($api_url, $param);
  193 + if($res){
  194 + $res = Arr::s2a($res);
  195 + Cache::put($key, $res, 2 * 3600);
  196 + }
  197 + } catch (\Exception | GuzzleException $e) {
  198 + errorLog('获取小语种项目数据失败', [], $e);
  199 + return false;
  200 + }
  201 + }
  202 + return $res;
  203 + }
  204 +
  205 +
140 } 206 }
@@ -2,8 +2,14 @@ @@ -2,8 +2,14 @@
2 2
3 namespace App\Http\Controllers\Bside; 3 namespace App\Http\Controllers\Bside;
4 4
  5 +
  6 +use App\Helper\GoogleSpeedApi;
  7 +use App\Helper\QuanqiusouApi;
  8 +use App\Http\Logic\Aside\Project\ProjectLogic;
5 use App\Http\Logic\Bside\RankDataLogic; 9 use App\Http\Logic\Bside\RankDataLogic;
6 -use Illuminate\Http\Request; 10 +use App\Models\RankData\Speed as GoogleSpeedModel;
  11 +use App\Services\BatchExportService;
  12 +use Illuminate\Support\Facades\Storage;
7 13
8 /** 14 /**
9 * Class GoogleRankController 15 * Class GoogleRankController
@@ -20,8 +26,126 @@ class RankDataController extends BaseController @@ -20,8 +26,126 @@ class RankDataController extends BaseController
20 return $this->success($data); 26 return $this->success($data);
21 } 27 }
22 28
  29 + /**
  30 + * 关键词排名列表
  31 + * @param RankDataLogic $logic
  32 + * @return \Illuminate\Http\JsonResponse
  33 + * @author zbj
  34 + * @date 2023/5/15
  35 + */
23 public function keywords_rank_list(RankDataLogic $logic){ 36 public function keywords_rank_list(RankDataLogic $logic){
24 $data = $logic->keywords_rank_list(); 37 $data = $logic->keywords_rank_list();
25 return $this->success($data); 38 return $this->success($data);
26 } 39 }
  40 +
  41 +
  42 + /**
  43 + * 刷新测速
  44 + * @param ProjectLogic $projectLogic
  45 + * @param GoogleSpeedApi $googleSpeedApi
  46 + * @return \Illuminate\Http\JsonResponse
  47 + * @throws \App\Exceptions\AsideGlobalException
  48 + * @throws \App\Exceptions\BsideGlobalException
  49 + * @author zbj
  50 + * @date 2023/5/15
  51 + */
  52 + public function speed(ProjectLogic $projectLogic, GoogleSpeedApi $googleSpeedApi){
  53 + $project_id = $this->user['project_id'];
  54 + $project = $projectLogic->getInfo($project_id);
  55 + $domain = $project['deploy_optimize']['domain'] ?? '';
  56 + $data = $googleSpeedApi->run($domain);
  57 + if($data){
  58 + $model = GoogleSpeedModel::where('project_id', $project_id)->first();
  59 + $model->project_id = $project_id;
  60 + $model->data = $data;
  61 + $model->updated_date = date('Y-m-d');
  62 + $model->save();
  63 + }
  64 + return $this->success($data);
  65 + }
  66 +
  67 + /**
  68 + * 数据导出
  69 + * @author zbj
  70 + * @date 2023/5/15
  71 + */
  72 + public function export(RankDataLogic $logic){
  73 + $data = $logic->keywords_rank_list(true);
  74 + foreach ($data as &$item){
  75 + $item['domain'] = explode(':', $item['domain'])[1];
  76 + $item['lang'] = $this->request['lang'] ?: 'en';
  77 + $item['g_text'] = $item['g'] == 1 ? '主关键词' : '拓展关键词';
  78 + foreach ($item['position'] as $date => $position){
  79 + $item[$date] = $position;
  80 + }
  81 + }
  82 +
  83 + $map = [
  84 + 'keyword' => '关键词',
  85 + 'domain' => '排名网址',
  86 + 'lang' => '语言',
  87 + 'g_text' => '关键词类型',
  88 + ];
  89 + foreach ($data[0]['position'] as $date => $position){
  90 + $map[$date] = $date;
  91 + }
  92 +
  93 + //生成文件,发送到客户端
  94 + $table = new BatchExportService("关键词数据导出");
  95 + $file = $table->head($map)->data($data)->save();
  96 + if (!$file) {
  97 + throw new \Exception('文件生成失败,请重试');
  98 + }
  99 + $fileurl = Storage::disk('runtime')->url($file);
  100 +// return Storage::disk('runtime')->download($file); //直接下载
  101 + return $this->success(['url' => $fileurl]);
  102 + }
  103 +
  104 + /**
  105 + * 数据导出
  106 + * @author zbj
  107 + * @date 2023/5/15
  108 + */
  109 + public function export_history(RankDataLogic $logic, ProjectLogic $projectLogic, QuanqiusouApi $quanqiusouApi){
  110 + $project_id = $this->user['project_id'];
  111 + $project = $projectLogic->getInfo($project_id);
  112 + $lang = $this->request['lang'] ??'';
  113 + $data = $quanqiusouApi->getHistoryCount($project['deploy_optimize']['api_no'], $lang);
  114 + if($lang){
  115 + foreach ($data['data'] as &$item){
  116 + $item['c_date'] = date("Y-m-d",strtotime($item['c_date'] . ' +1day'));
  117 + $item['reach'] = $item['home_cnt'] >= $data['bz_count'] ? '是' : '否';
  118 + }
  119 + $map = [
  120 + 'c_date' => '日期',
  121 + 'home_cnt' => '第一页',
  122 + 'reach' => '是否达标',
  123 + ];
  124 + }else{
  125 + foreach ($data['data'] as &$item){
  126 + $item['c_date'] = date("Y-m-d",strtotime($item['c_date'] . ' +1day'));
  127 + $item['reach'] = $item['cnt_home'] >= $project['deploy_build']['keyword_num'] ? '是' : '否';
  128 + }
  129 + $map = [
  130 + 'c_date' => '日期',
  131 + 'cnt_first' => '排名第一',
  132 + 'cnt_home' => '第一页',
  133 + 'cnt_thirty' => '前三页',
  134 + 'cnt_fifty' => '前五页',
  135 + 'cnt_hundred' => '前十页',
  136 + 'reach' => '是否达标',
  137 + ];
  138 + }
  139 +
  140 +
  141 + //生成文件,发送到客户端
  142 + $table = new BatchExportService($lang . "关键词历史数据导出");
  143 + $file = $table->head($map)->data($data['data'])->save();
  144 + if (!$file) {
  145 + throw new \Exception('文件生成失败,请重试');
  146 + }
  147 + $fileurl = Storage::disk('runtime')->url($file);
  148 +// return Storage::disk('runtime')->download($file); //直接下载
  149 + return $this->success(['url' => $fileurl]);
  150 + }
27 } 151 }
@@ -119,7 +119,7 @@ class RankDataLogic extends BaseLogic @@ -119,7 +119,7 @@ class RankDataLogic extends BaseLogic
119 * @author zbj 119 * @author zbj
120 * @date 2023/5/12 120 * @date 2023/5/12
121 */ 121 */
122 - public function keywords_rank_list() 122 + public function keywords_rank_list($export = false)
123 { 123 {
124 $page = intval($this->request['page'] ?: 1); 124 $page = intval($this->request['page'] ?: 1);
125 $lang = $this->request['lang'] ?: ''; 125 $lang = $this->request['lang'] ?: '';
@@ -160,11 +160,11 @@ class RankDataLogic extends BaseLogic @@ -160,11 +160,11 @@ class RankDataLogic extends BaseLogic
160 $domain_text = 'AI域名:' . $last['r']; 160 $domain_text = 'AI域名:' . $last['r'];
161 if (in_array($flg_ext, [1, 2]) || $flg_ai == 1) { 161 if (in_array($flg_ext, [1, 2]) || $flg_ai == 1) {
162 if ($last['r'] == $ai_domain) { 162 if ($last['r'] == $ai_domain) {
163 - $domain_text = '星链域名: ' . $ai_domain; 163 + $domain_text = '星链域名' . $ai_domain;
164 } else if ($last['r'] == $ext_domain) { 164 } else if ($last['r'] == $ext_domain) {
165 - $domain_text = '主域名2: ' . $ext_domain; 165 + $domain_text = '主域名2' . $ext_domain;
166 } else { 166 } else {
167 - $domain_text = 'AI域名: ' . $last['r']; 167 + $domain_text = 'AI域名' . $last['r'];
168 } 168 }
169 } 169 }
170 } 170 }
@@ -213,6 +213,10 @@ class RankDataLogic extends BaseLogic @@ -213,6 +213,10 @@ class RankDataLogic extends BaseLogic
213 } 213 }
214 return true; 214 return true;
215 })->values(); 215 })->values();
  216 +
  217 + if($export){
  218 + return $list->toArray();
  219 + }
216 $data = [ 220 $data = [
217 "list" => $list->forPage($page, 100)->toArray(), 221 "list" => $list->forPage($page, 100)->toArray(),
218 "total" => $list->count(), 222 "total" => $list->count(),
@@ -254,6 +254,9 @@ Route::middleware(['bloginauth'])->group(function () { @@ -254,6 +254,9 @@ Route::middleware(['bloginauth'])->group(function () {
254 Route::prefix('rank_data')->group(function () { 254 Route::prefix('rank_data')->group(function () {
255 Route::any('/index', [\App\Http\Controllers\Bside\RankDataController::class, 'index'])->name('rank_data'); 255 Route::any('/index', [\App\Http\Controllers\Bside\RankDataController::class, 'index'])->name('rank_data');
256 Route::any('/keywords_rank_list', [\App\Http\Controllers\Bside\RankDataController::class, 'keywords_rank_list'])->name('rank_data_keywords_rank_list'); 256 Route::any('/keywords_rank_list', [\App\Http\Controllers\Bside\RankDataController::class, 'keywords_rank_list'])->name('rank_data_keywords_rank_list');
  257 + Route::any('/speed', [\App\Http\Controllers\Bside\RankDataController::class, 'speed'])->name('rank_data_speed');
  258 + Route::any('/export', [\App\Http\Controllers\Bside\RankDataController::class, 'export'])->name('rank_data_export');
  259 + Route::any('/export_history', [\App\Http\Controllers\Bside\RankDataController::class, 'export_history'])->name('rank_data_export_history');
257 }); 260 });
258 261
259 262