|
|
|
1
|
+<?php
|
|
|
|
2
|
+/**
|
|
|
|
3
|
+ * Created by PhpStorm.
|
|
|
|
4
|
+ * User: zhl
|
|
|
|
5
|
+ * Date: 2025/10/27
|
|
|
|
6
|
+ * Time: 13:42
|
|
|
|
7
|
+ */
|
|
|
|
8
|
+namespace App\Console\Commands\Product;
|
|
|
|
9
|
+
|
|
|
|
10
|
+use App\Console\Commands\Tdk\UpdateSeoTdk;
|
|
|
|
11
|
+use App\Models\Product\Keyword;
|
|
|
|
12
|
+use App\Models\Project\Project;
|
|
|
|
13
|
+use App\Services\ProjectServer;
|
|
|
|
14
|
+use Illuminate\Console\Command;
|
|
|
|
15
|
+use Illuminate\Support\Arr;
|
|
|
|
16
|
+use Illuminate\Support\Str;
|
|
|
|
17
|
+
|
|
|
|
18
|
+class SplicePrefix extends Command
|
|
|
|
19
|
+{
|
|
|
|
20
|
+ /**
|
|
|
|
21
|
+ * The name and signature of the console command.
|
|
|
|
22
|
+ *
|
|
|
|
23
|
+ * @var string
|
|
|
|
24
|
+ */
|
|
|
|
25
|
+ protected $signature = 'splice_prefix';
|
|
|
|
26
|
+
|
|
|
|
27
|
+ /**
|
|
|
|
28
|
+ * The console command description.
|
|
|
|
29
|
+ *
|
|
|
|
30
|
+ * @var string
|
|
|
|
31
|
+ */
|
|
|
|
32
|
+ protected $description = '未达标项目,拼接关键词聚合页前缀';
|
|
|
|
33
|
+
|
|
|
|
34
|
+ /**
|
|
|
|
35
|
+ * @return bool
|
|
|
|
36
|
+ */
|
|
|
|
37
|
+ public function handle()
|
|
|
|
38
|
+ {
|
|
|
|
39
|
+ $this->output('project start: ' . 4410); #6 23 38 41 46 47 49
|
|
|
|
40
|
+ $this->bind(4410);
|
|
|
|
41
|
+ dd('end');
|
|
|
|
42
|
+ #TODO 获取当日不达标项目, 检查关键词前缀拼接
|
|
|
|
43
|
+ $project_ids = $this->getProject();
|
|
|
|
44
|
+ if (empty($project_ids))
|
|
|
|
45
|
+ return true;
|
|
|
|
46
|
+
|
|
|
|
47
|
+ foreach ($project_ids as $project_id) {
|
|
|
|
48
|
+ if ($project_id == 1)
|
|
|
|
49
|
+ continue;
|
|
|
|
50
|
+ $this->output('project start: ' . $project_id);
|
|
|
|
51
|
+ $this->bind($project_id);
|
|
|
|
52
|
+ }
|
|
|
|
53
|
+ return true;
|
|
|
|
54
|
+ }
|
|
|
|
55
|
+
|
|
|
|
56
|
+ /**
|
|
|
|
57
|
+ * @param $project_id
|
|
|
|
58
|
+ * @return bool
|
|
|
|
59
|
+ */
|
|
|
|
60
|
+ public function bind($project_id)
|
|
|
|
61
|
+ {
|
|
|
|
62
|
+ $project = ProjectServer::useProject($project_id);
|
|
|
|
63
|
+
|
|
|
|
64
|
+ // 客户前缀
|
|
|
|
65
|
+ $tdk_class = new UpdateSeoTdk();
|
|
|
|
66
|
+ $info = $tdk_class->getDeployOptimize($project_id);
|
|
|
|
67
|
+ $fix_keyword = explode(",", $info['keyword_prefix']);
|
|
|
|
68
|
+ $fix_keyword = array_filter($fix_keyword);
|
|
|
|
69
|
+
|
|
|
|
70
|
+ // 所有前缀
|
|
|
|
71
|
+ $all_prefixes = $tdk_class->getAllPrefix(1, $project_id);
|
|
|
|
72
|
+ $all_prefixes = array_map('strtolower', $all_prefixes);
|
|
|
|
73
|
+
|
|
|
|
74
|
+ $keywords = Keyword::select(['id', 'title', 'seo_title'])->get();
|
|
|
|
75
|
+ foreach ($keywords as $item) {
|
|
|
|
76
|
+ $this_fix_keyword = $fix_keyword;
|
|
|
|
77
|
+ if (empty($item->title))
|
|
|
|
78
|
+ continue;
|
|
|
|
79
|
+
|
|
|
|
80
|
+ $this->output('keyword id:' . $item->id . ' | title: ' . $item->title . ' | old seo title: ' . $item->seo_title);
|
|
|
|
81
|
+
|
|
|
|
82
|
+ // 没有 SEO Title 直接生成
|
|
|
|
83
|
+ if (empty($item->seo_title)) {
|
|
|
|
84
|
+ $prefix = $tdk_class->getPrefixKeyword($project_id, 'prefix', 2, $item->title);
|
|
|
|
85
|
+ $suffix = $tdk_class->getPrefixKeyword($project_id, 'suffix', 2, trim($prefix . ' ' . $item->title));
|
|
|
|
86
|
+ if(Str::startsWith($suffix, ', ')){
|
|
|
|
87
|
+ $seo_title = $prefix . ' ' . $item->title . $suffix;
|
|
|
|
88
|
+ }else{
|
|
|
|
89
|
+ $seo_title = $prefix . ' ' . $item->title . ' ' . $suffix;
|
|
|
|
90
|
+ }
|
|
|
|
91
|
+// $item->seo_title = trim($seo_title);
|
|
|
|
92
|
+// $item->save();
|
|
|
|
93
|
+ $this->output('new seo title: ' . $seo_title);
|
|
|
|
94
|
+ continue;
|
|
|
|
95
|
+ }
|
|
|
|
96
|
+
|
|
|
|
97
|
+ // 有 SEO Title 需要分析前后缀
|
|
|
|
98
|
+ $start = strpos($item->seo_title, $item->title);
|
|
|
|
99
|
+
|
|
|
|
100
|
+ // Title 和 SEO Title 不存在包含关系
|
|
|
|
101
|
+ if ($start === FALSE) {
|
|
|
|
102
|
+ $this->output('Title 和 SEO Title 不存在包含关系');
|
|
|
|
103
|
+ continue;
|
|
|
|
104
|
+ }
|
|
|
|
105
|
+
|
|
|
|
106
|
+ $prefix = $start == 0 ? '' : trim(substr($item->seo_title, 0, $start));
|
|
|
|
107
|
+ $prefix_array = explode(' ', $prefix);
|
|
|
|
108
|
+ $prefix_array = array_filter($prefix_array);
|
|
|
|
109
|
+ $need_num = 2 - count($prefix_array);
|
|
|
|
110
|
+ // 已经有两个前缀, 不在处理
|
|
|
|
111
|
+ if ($need_num <= 0) {
|
|
|
|
112
|
+ $this->output('已经有两个前缀, 不在处理');
|
|
|
|
113
|
+ continue;
|
|
|
|
114
|
+ }
|
|
|
|
115
|
+
|
|
|
|
116
|
+ // 关键词最后一个词是前缀的词,前后缀都不拼
|
|
|
|
117
|
+ $title_words = explode(' ', strtolower($item->title));
|
|
|
|
118
|
+
|
|
|
|
119
|
+ // 关键词最后一个词是前缀的词,前后缀都不拼
|
|
|
|
120
|
+ if(in_array(Arr::last($title_words), $all_prefixes)) {
|
|
|
|
121
|
+ $this->output('关键词最后一个词是前缀的词, 前后缀都不拼');
|
|
|
|
122
|
+ continue;
|
|
|
|
123
|
+ }
|
|
|
|
124
|
+
|
|
|
|
125
|
+ // in,for,with,to,near,from 这些介词 只拼前缀,不拼后缀
|
|
|
|
126
|
+ $ban = [];
|
|
|
|
127
|
+
|
|
|
|
128
|
+ // 关键词本身包含了前缀,也可以再拼一个不重复的前缀, 包含两个前缀就不拼前缀了
|
|
|
|
129
|
+ foreach ($title_words as $title_word) {
|
|
|
|
130
|
+ if(in_array($title_word, $all_prefixes)){
|
|
|
|
131
|
+ $ban[] = $title_word;
|
|
|
|
132
|
+ }
|
|
|
|
133
|
+ }
|
|
|
|
134
|
+ $need_num = $need_num - count($ban);
|
|
|
|
135
|
+
|
|
|
|
136
|
+ // 关键词本身包含前缀,包含关键词大于等于需要的前缀,当前关键词不需要处理
|
|
|
|
137
|
+ if ($need_num <= 0) {
|
|
|
|
138
|
+ $this->output('关键词本身包含前缀,包含关键词大于等于需要的前缀,当前关键词不需要处理');
|
|
|
|
139
|
+ continue;
|
|
|
|
140
|
+ }
|
|
|
|
141
|
+
|
|
|
|
142
|
+
|
|
|
|
143
|
+ // services/service 结尾的词,后缀不拼manufacturer,factory
|
|
|
|
144
|
+ // manufacturer,factory 结尾的词,后缀不拼 services/service
|
|
|
|
145
|
+ // 有wholesale或cheap的词,后缀不拼 manufacturer,factory,exporter,company
|
|
|
|
146
|
+
|
|
|
|
147
|
+ // 关键词以manufacturer,factory,exporter,company结尾, 前缀不拼wholesale或cheap的词
|
|
|
|
148
|
+ if (Str::endsWith(strtolower($item->title), ['manufacturer', 'manufacturers', 'factory', 'factories', 'exporter', 'exporters', 'company', 'companies', 'supplier', 'suppliers']))
|
|
|
|
149
|
+ $ban = array_merge($ban, ['wholesale', 'cheap', 'buy']);
|
|
|
|
150
|
+
|
|
|
|
151
|
+
|
|
|
|
152
|
+ foreach ($this_fix_keyword as $k => $keyword) {
|
|
|
|
153
|
+ // 被禁用的关键词
|
|
|
|
154
|
+ if (in_array(strtolower(Str::plural($keyword)), $ban)) {
|
|
|
|
155
|
+ unset($this_fix_keyword[$k]);
|
|
|
|
156
|
+ }
|
|
|
|
157
|
+ if (in_array(strtolower(Str::singular($keyword)), $ban)) {
|
|
|
|
158
|
+ unset($this_fix_keyword[$k]);
|
|
|
|
159
|
+ }
|
|
|
|
160
|
+ }
|
|
|
|
161
|
+ $this_fix_keyword = array_diff($this_fix_keyword, $prefix_array);
|
|
|
|
162
|
+ shuffle($this_fix_keyword);
|
|
|
|
163
|
+ $need_keyword = [];
|
|
|
|
164
|
+ foreach ($this_fix_keyword as $v) {
|
|
|
|
165
|
+ if ($need_num == 0)
|
|
|
|
166
|
+ break;
|
|
|
|
167
|
+
|
|
|
|
168
|
+ $is_repeat = false;
|
|
|
|
169
|
+ foreach ($need_keyword as $keyword) {
|
|
|
|
170
|
+ if (Str::singular($keyword) == Str::singular($v)) {
|
|
|
|
171
|
+ $is_repeat = true;
|
|
|
|
172
|
+ break;
|
|
|
|
173
|
+ }
|
|
|
|
174
|
+ }
|
|
|
|
175
|
+ if ($is_repeat)
|
|
|
|
176
|
+ continue;
|
|
|
|
177
|
+
|
|
|
|
178
|
+ $need_keyword[] = $v;
|
|
|
|
179
|
+ $need_num--;
|
|
|
|
180
|
+ }
|
|
|
|
181
|
+// $item->seo_title = trim(implode(' ', $need_keyword) . ' ' . trim($item->seo_title));
|
|
|
|
182
|
+// $item->save();
|
|
|
|
183
|
+ $this->output('new seo title: ' . implode(' ', $need_keyword) . ' ' . trim($item->seo_title));
|
|
|
|
184
|
+ }
|
|
|
|
185
|
+ return true;
|
|
|
|
186
|
+ }
|
|
|
|
187
|
+
|
|
|
|
188
|
+ /**
|
|
|
|
189
|
+ * 获取当日未达标项目
|
|
|
|
190
|
+ * @return mixed
|
|
|
|
191
|
+ */
|
|
|
|
192
|
+ public function getProject()
|
|
|
|
193
|
+ {
|
|
|
|
194
|
+ $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();
|
|
|
|
195
|
+ return $project_ids;
|
|
|
|
196
|
+ }
|
|
|
|
197
|
+
|
|
|
|
198
|
+ /**
|
|
|
|
199
|
+ * 输出日志
|
|
|
|
200
|
+ * @param $message
|
|
|
|
201
|
+ * @return bool
|
|
|
|
202
|
+ */
|
|
|
|
203
|
+ public function output($message)
|
|
|
|
204
|
+ {
|
|
|
|
205
|
+ $message = now() . ' ' . $message . PHP_EOL;
|
|
|
|
206
|
+ file_put_contents(storage_path('logs/splice_prefix.log'), $message, FILE_APPEND);
|
|
|
|
207
|
+ echo $message;
|
|
|
|
208
|
+ return true;
|
|
|
|
209
|
+ }
|
|
|
|
210
|
+} |