作者 赵彬吉

update

... ... @@ -137,4 +137,70 @@ class QuanqiusouApi
return $res;
}
/**
* 获取历史排名统计数据
* @param $api_no
* @param string $lang
* @return array|false|int|mixed|null
* @author zbj
* @date 2023/5/15
*/
public function getHistoryCount($api_no, $lang = '')
{
$key = "quanqiusou_api_history_count_{$api_no}_{$lang}_" . date('Y-m-d');
$res = Cache::get($key);
if (!$res) {
$api_url = $this->url . '/google-rank/history_count.php';
$param = [
'apino' => $api_no,
];
if ($lang) {
$param['lang'] = $lang;
}
try {
$res = HttpUtils::get($api_url, $param);
if($res){
$res = Arr::s2a($res);
Cache::put($key, $res, 24 * 3600);
}
} catch (\Exception | GuzzleException $e) {
errorLog('获取历史排名统计数据失败', [], $e);
return false;
}
}
return $res;
}
/**
* 获取小语种项目
* @return array|false|int|mixed|null
* @author zbj
* @date 2023/5/15
*/
public function getLangList()
{
$key = "quanqiusou_api_lang_list_" . date('Y-m-d');
$res = Cache::get($key);
if (!$res) {
$api_url = $this->url . '/api/index/langlist';
$param = [
'key' => '289c1fc81c89d79c04ed4fd72822948e',
];
try {
$res = HttpUtils::get($api_url, $param);
if($res){
$res = Arr::s2a($res);
Cache::put($key, $res, 2 * 3600);
}
} catch (\Exception | GuzzleException $e) {
errorLog('获取小语种项目数据失败', [], $e);
return false;
}
}
return $res;
}
}
... ...
... ... @@ -2,8 +2,14 @@
namespace App\Http\Controllers\Bside;
use App\Helper\GoogleSpeedApi;
use App\Helper\QuanqiusouApi;
use App\Http\Logic\Aside\Project\ProjectLogic;
use App\Http\Logic\Bside\RankDataLogic;
use Illuminate\Http\Request;
use App\Models\RankData\Speed as GoogleSpeedModel;
use App\Services\BatchExportService;
use Illuminate\Support\Facades\Storage;
/**
* Class GoogleRankController
... ... @@ -20,8 +26,126 @@ class RankDataController extends BaseController
return $this->success($data);
}
/**
* 关键词排名列表
* @param RankDataLogic $logic
* @return \Illuminate\Http\JsonResponse
* @author zbj
* @date 2023/5/15
*/
public function keywords_rank_list(RankDataLogic $logic){
$data = $logic->keywords_rank_list();
return $this->success($data);
}
/**
* 刷新测速
* @param ProjectLogic $projectLogic
* @param GoogleSpeedApi $googleSpeedApi
* @return \Illuminate\Http\JsonResponse
* @throws \App\Exceptions\AsideGlobalException
* @throws \App\Exceptions\BsideGlobalException
* @author zbj
* @date 2023/5/15
*/
public function speed(ProjectLogic $projectLogic, GoogleSpeedApi $googleSpeedApi){
$project_id = $this->user['project_id'];
$project = $projectLogic->getInfo($project_id);
$domain = $project['deploy_optimize']['domain'] ?? '';
$data = $googleSpeedApi->run($domain);
if($data){
$model = GoogleSpeedModel::where('project_id', $project_id)->first();
$model->project_id = $project_id;
$model->data = $data;
$model->updated_date = date('Y-m-d');
$model->save();
}
return $this->success($data);
}
/**
* 数据导出
* @author zbj
* @date 2023/5/15
*/
public function export(RankDataLogic $logic){
$data = $logic->keywords_rank_list(true);
foreach ($data as &$item){
$item['domain'] = explode(':', $item['domain'])[1];
$item['lang'] = $this->request['lang'] ?: 'en';
$item['g_text'] = $item['g'] == 1 ? '主关键词' : '拓展关键词';
foreach ($item['position'] as $date => $position){
$item[$date] = $position;
}
}
$map = [
'keyword' => '关键词',
'domain' => '排名网址',
'lang' => '语言',
'g_text' => '关键词类型',
];
foreach ($data[0]['position'] as $date => $position){
$map[$date] = $date;
}
//生成文件,发送到客户端
$table = new BatchExportService("关键词数据导出");
$file = $table->head($map)->data($data)->save();
if (!$file) {
throw new \Exception('文件生成失败,请重试');
}
$fileurl = Storage::disk('runtime')->url($file);
// return Storage::disk('runtime')->download($file); //直接下载
return $this->success(['url' => $fileurl]);
}
/**
* 数据导出
* @author zbj
* @date 2023/5/15
*/
public function export_history(RankDataLogic $logic, ProjectLogic $projectLogic, QuanqiusouApi $quanqiusouApi){
$project_id = $this->user['project_id'];
$project = $projectLogic->getInfo($project_id);
$lang = $this->request['lang'] ??'';
$data = $quanqiusouApi->getHistoryCount($project['deploy_optimize']['api_no'], $lang);
if($lang){
foreach ($data['data'] as &$item){
$item['c_date'] = date("Y-m-d",strtotime($item['c_date'] . ' +1day'));
$item['reach'] = $item['home_cnt'] >= $data['bz_count'] ? '是' : '否';
}
$map = [
'c_date' => '日期',
'home_cnt' => '第一页',
'reach' => '是否达标',
];
}else{
foreach ($data['data'] as &$item){
$item['c_date'] = date("Y-m-d",strtotime($item['c_date'] . ' +1day'));
$item['reach'] = $item['cnt_home'] >= $project['deploy_build']['keyword_num'] ? '是' : '否';
}
$map = [
'c_date' => '日期',
'cnt_first' => '排名第一',
'cnt_home' => '第一页',
'cnt_thirty' => '前三页',
'cnt_fifty' => '前五页',
'cnt_hundred' => '前十页',
'reach' => '是否达标',
];
}
//生成文件,发送到客户端
$table = new BatchExportService($lang . "关键词历史数据导出");
$file = $table->head($map)->data($data['data'])->save();
if (!$file) {
throw new \Exception('文件生成失败,请重试');
}
$fileurl = Storage::disk('runtime')->url($file);
// return Storage::disk('runtime')->download($file); //直接下载
return $this->success(['url' => $fileurl]);
}
}
... ...
... ... @@ -119,7 +119,7 @@ class RankDataLogic extends BaseLogic
* @author zbj
* @date 2023/5/12
*/
public function keywords_rank_list()
public function keywords_rank_list($export = false)
{
$page = intval($this->request['page'] ?: 1);
$lang = $this->request['lang'] ?: '';
... ... @@ -160,11 +160,11 @@ class RankDataLogic extends BaseLogic
$domain_text = 'AI域名:' . $last['r'];
if (in_array($flg_ext, [1, 2]) || $flg_ai == 1) {
if ($last['r'] == $ai_domain) {
$domain_text = '星链域名: ' . $ai_domain;
$domain_text = '星链域名' . $ai_domain;
} else if ($last['r'] == $ext_domain) {
$domain_text = '主域名2: ' . $ext_domain;
$domain_text = '主域名2' . $ext_domain;
} else {
$domain_text = 'AI域名: ' . $last['r'];
$domain_text = 'AI域名' . $last['r'];
}
}
}
... ... @@ -213,6 +213,10 @@ class RankDataLogic extends BaseLogic
}
return true;
})->values();
if($export){
return $list->toArray();
}
$data = [
"list" => $list->forPage($page, 100)->toArray(),
"total" => $list->count(),
... ...
... ... @@ -254,6 +254,9 @@ Route::middleware(['bloginauth'])->group(function () {
Route::prefix('rank_data')->group(function () {
Route::any('/index', [\App\Http\Controllers\Bside\RankDataController::class, 'index'])->name('rank_data');
Route::any('/keywords_rank_list', [\App\Http\Controllers\Bside\RankDataController::class, 'keywords_rank_list'])->name('rank_data_keywords_rank_list');
Route::any('/speed', [\App\Http\Controllers\Bside\RankDataController::class, 'speed'])->name('rank_data_speed');
Route::any('/export', [\App\Http\Controllers\Bside\RankDataController::class, 'export'])->name('rank_data_export');
Route::any('/export_history', [\App\Http\Controllers\Bside\RankDataController::class, 'export_history'])->name('rank_data_export_history');
});
... ...