GoogleKeywordInsightController.php
4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
/**
* @remark :
* @name :GoogleKeywordInsightController.php
* @author :lyh
* @method :post
* @time :2025/3/25 14:23
*/
namespace App\Http\Controllers\Bside\GoogleKeyword;
use App\Enums\Common\Code;
use App\Http\Controllers\Bside\BaseController;
use App\Http\Logic\Bside\GoogleKeywordInsight\GoogleKeywordInsightLogic;
use App\Models\GoogleKeywordInsight\GoogleKeywordInsightDetail;
use App\Models\Project\ProjectKeyword;
/**
* @remark :谷歌洞察数据
* @name :GoogleKeywordInsightController
* @author :lyh
* @method :post
* @time :2025/3/25 14:24
*/
class GoogleKeywordInsightController extends BaseController
{
/**
* @remark :保存数据
* @name :saveKeywordInsight
* @author :lyh
* @method :post
* @time :2025/3/25 14:30
*/
public function getKeywordInsight(GoogleKeywordInsightLogic $logic){
$this->request->validate([
'keyword' => 'required'
],[
'keyword.required' => '关键词不能为空',
]);
$logic->getGoogleInsight();
$detailModel = new GoogleKeywordInsightDetail();
$data = $detailModel->lists(['search'=>$this->param['keyword']],$this->page,$this->row);
$this->response('success',Code::SUCCESS,$data);
}
/**
* @remark :获取优化关键词列表
* @name :getOptimizeList
* @author :lyh
* @method :post
* @time :2025/4/1 9:12
*/
public function getOptimizeList(){
$this->request->validate([
'field' => 'required'
],[
'field.required' => 'field不能为空',
]);
$projectKeywordModel = new ProjectKeyword();
$info = $projectKeywordModel->read(['project_id'=>$this->user['project_id']],[$this->param['field']]);
if($info === false || empty($info[$this->param['field']])){
$this->response('success');
}
$array = explode("\r\n", $info[$this->param['field']]);
$detailModel = new GoogleKeywordInsightDetail();
$resultData = [];
if(!empty($array)){
$resultData = $this->paginateArray($array,$this->page,$this->row);
$detailList = $detailModel->list(['search'=>['in',$resultData['list']]]);
foreach ($resultData['list'] as $key => $item){
$result['keyword'] = $item;
$searchKeyword = $this->getSearchDetail($item,$detailList);
if($searchKeyword === false){
$result['data'] = [];
}else{
$result['data'] = $searchKeyword;
}
$resultData['list'][$key] = $result;
}
}
$this->response('success',Code::SUCCESS,$resultData);
}
/**
* @remark :保存一条关键字数据
* @name :getKeywordInsightDetail
* @author :lyh
* @method :post
* @time :2025/4/1 11:23
*/
public function getKeywordInsightDetail(GoogleKeywordInsightLogic $logic){
$this->request->validate([
'keyword' => 'required'
],[
'keyword.required' => '关键词不能为空',
]);
$result = $logic->getGoogleInsightDetail();
$detailModel = new GoogleKeywordInsightDetail();
$data = $detailModel->read(['id'=>$result['id']]);
$this->response('success',Code::SUCCESS,$data);
}
/**
* @remark :查看当前数据是否存在数组中
* @name :getSearchDetail
* @author :lyh
* @method :post
* @time :2025/4/1 9:56
*/
public function getSearchDetail($keyword,$detailList){
if(!empty($detailList)){
foreach ($detailList as $value){
if($keyword == $value['search']){
return $value;
}
}
}
return [];
}
/**
* @remark :返回分页数据
* @name :paginateArray
* @author :lyh
* @method :post
* @time :2025/4/1 9:41
*/
public function paginateArray($array, $page = 1, $pageSize = 20) {
$totalItems = count($array);
$totalPages = ceil($totalItems / $pageSize);
// 确保页码有效
$page = max(1, min($page, $totalPages));
$offset = ($page - 1) * $pageSize;
$data = array_slice($array, $offset, $pageSize);
return [
'list' => $data,
'page' => $page,
'size' => $pageSize,
'total_page' => $totalPages,
'total' => $totalItems,
];
}
}