SplicePrefix.php
7.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
/**
* Created by PhpStorm.
* User: zhl
* Date: 2025/10/27
* Time: 13:42
*/
namespace App\Console\Commands\Product;
use App\Console\Commands\Tdk\UpdateSeoTdk;
use App\Models\Product\Keyword;
use App\Models\Project\Project;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
class SplicePrefix extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'splice_prefix';
/**
* The console command description.
*
* @var string
*/
protected $description = '未达标项目,拼接关键词聚合页前缀';
/**
* @return bool
*/
public function handle()
{
$this->output('project start: ' . 4410); #6 23 38 41 46 47 49
$this->bind(4410);
dd('end');
#TODO 获取当日不达标项目, 检查关键词前缀拼接
$project_ids = $this->getProject();
if (empty($project_ids))
return true;
foreach ($project_ids as $project_id) {
if ($project_id == 1)
continue;
$this->output('project start: ' . $project_id);
$this->bind($project_id);
}
return true;
}
/**
* @param $project_id
* @return bool
*/
public function bind($project_id)
{
$project = ProjectServer::useProject($project_id);
// 客户前缀
$tdk_class = new UpdateSeoTdk();
$info = $tdk_class->getDeployOptimize($project_id);
$fix_keyword = explode(",", $info['keyword_prefix']);
$fix_keyword = array_filter($fix_keyword);
// 所有前缀
$all_prefixes = $tdk_class->getAllPrefix(1, $project_id);
$all_prefixes = array_map('strtolower', $all_prefixes);
$keywords = Keyword::select(['id', 'title', 'seo_title'])->get();
foreach ($keywords as $item) {
$this_fix_keyword = $fix_keyword;
if (empty($item->title))
continue;
$this->output('keyword id:' . $item->id . ' | title: ' . $item->title . ' | old seo title: ' . $item->seo_title);
// 没有 SEO Title 直接生成
if (empty($item->seo_title)) {
$prefix = $tdk_class->getPrefixKeyword($project_id, 'prefix', 2, $item->title);
$suffix = $tdk_class->getPrefixKeyword($project_id, 'suffix', 2, trim($prefix . ' ' . $item->title));
if(Str::startsWith($suffix, ', ')){
$seo_title = $prefix . ' ' . $item->title . $suffix;
}else{
$seo_title = $prefix . ' ' . $item->title . ' ' . $suffix;
}
// $item->seo_title = trim($seo_title);
// $item->save();
$this->output('new seo title: ' . $seo_title);
continue;
}
// 有 SEO Title 需要分析前后缀
$start = strpos($item->seo_title, $item->title);
// Title 和 SEO Title 不存在包含关系
if ($start === FALSE) {
$this->output('Title 和 SEO Title 不存在包含关系');
continue;
}
$prefix = $start == 0 ? '' : trim(substr($item->seo_title, 0, $start));
$prefix_array = explode(' ', $prefix);
$prefix_array = array_filter($prefix_array);
$need_num = 2 - count($prefix_array);
// 已经有两个前缀, 不在处理
if ($need_num <= 0) {
$this->output('已经有两个前缀, 不在处理');
continue;
}
// 关键词最后一个词是前缀的词,前后缀都不拼
$title_words = explode(' ', strtolower($item->title));
// 关键词最后一个词是前缀的词,前后缀都不拼
if(in_array(Arr::last($title_words), $all_prefixes)) {
$this->output('关键词最后一个词是前缀的词, 前后缀都不拼');
continue;
}
// in,for,with,to,near,from 这些介词 只拼前缀,不拼后缀
$ban = [];
// 关键词本身包含了前缀,也可以再拼一个不重复的前缀, 包含两个前缀就不拼前缀了
foreach ($title_words as $title_word) {
if(in_array($title_word, $all_prefixes)){
$ban[] = $title_word;
}
}
$need_num = $need_num - count($ban);
// 关键词本身包含前缀,包含关键词大于等于需要的前缀,当前关键词不需要处理
if ($need_num <= 0) {
$this->output('关键词本身包含前缀,包含关键词大于等于需要的前缀,当前关键词不需要处理');
continue;
}
// services/service 结尾的词,后缀不拼manufacturer,factory
// manufacturer,factory 结尾的词,后缀不拼 services/service
// 有wholesale或cheap的词,后缀不拼 manufacturer,factory,exporter,company
// 关键词以manufacturer,factory,exporter,company结尾, 前缀不拼wholesale或cheap的词
if (Str::endsWith(strtolower($item->title), ['manufacturer', 'manufacturers', 'factory', 'factories', 'exporter', 'exporters', 'company', 'companies', 'supplier', 'suppliers']))
$ban = array_merge($ban, ['wholesale', 'cheap', 'buy']);
foreach ($this_fix_keyword as $k => $keyword) {
// 被禁用的关键词
if (in_array(strtolower(Str::plural($keyword)), $ban)) {
unset($this_fix_keyword[$k]);
}
if (in_array(strtolower(Str::singular($keyword)), $ban)) {
unset($this_fix_keyword[$k]);
}
}
$this_fix_keyword = array_diff($this_fix_keyword, $prefix_array);
shuffle($this_fix_keyword);
$need_keyword = [];
foreach ($this_fix_keyword as $v) {
if ($need_num == 0)
break;
$is_repeat = false;
foreach ($need_keyword as $keyword) {
if (Str::singular($keyword) == Str::singular($v)) {
$is_repeat = true;
break;
}
}
if ($is_repeat)
continue;
$need_keyword[] = $v;
$need_num--;
}
// $item->seo_title = trim(implode(' ', $need_keyword) . ' ' . trim($item->seo_title));
// $item->save();
$this->output('new seo title: ' . implode(' ', $need_keyword) . ' ' . trim($item->seo_title));
}
return true;
}
/**
* 获取当日未达标项目
* @return mixed
*/
public function getProject()
{
$project_ids = Project::where(['type' => Project::TYPE_TWO, 'project_type' => Project::TYPE_ZERO, 'delete_status' => Project::IS_DEL_FALSE, 'is_remain_today' => 0])->pluck('id')->toArray();
return $project_ids;
}
/**
* 输出日志
* @param $message
* @return bool
*/
public function output($message)
{
$message = now() . ' ' . $message . PHP_EOL;
file_put_contents(storage_path('logs/splice_prefix.log'), $message, FILE_APPEND);
echo $message;
return true;
}
}