UpdateSeoTdk.php
10.9 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?php
namespace App\Console\Commands;
use App\Helper\Arr;
use App\Helper\Common;
use App\Helper\Gpt;
use App\Helper\Translate;
use App\Models\Ai\AiCommand;
use App\Models\Project\DeployOptimize;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
/**
* 初始化项目
* Class InitProject
* @package App\Console\Commands
* @author zbj
* @date 2023/10/8
*/
class UpdateSeoTdk extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'update_seo_tdk';
/**
* The console command description.
*
* @var string
*/
protected $description = '一键生成tdk';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* '表' => [
* '指令key' => '表字段'
* ]
* @return array
* @author zbj
* @date 2023/11/3
*/
protected $maps = [
'gl_web_custom_template' => [
'page_title' => 'title',
'page_meta_keywords' => 'keywords',
'page_meta_description' => 'description',
],
'gl_product' => [
'product_title' => 'seo_mate.title',
'product_meta_keywords' => 'seo_mate.keyword',
'product_meta_description' => 'seo_mate.description',
],
'gl_product_category' => [
'product_cat_title' => 'seo_title',
'product_cat_meta_keywords' => 'seo_keywords',
'product_cat_meta_description' => 'seo_des',
],
'gl_blog' => [
'blog_title' => 'seo_title',
'blog_meta_keywords' => 'seo_keywords',
'blog_meta_description' => 'seo_description',
],
'gl_blog_category' => [
'blog_cat_title' => 'seo_title',
'blog_cat_meta_keywords' => 'seo_keywords',
'blog_cat_meta_description' => 'seo_des',
],
'gl_news' => [
'news_title' => 'seo_title',
'news_meta_keywords' => 'seo_keywords',
'news_meta_description' => 'seo_description',
],
'gl_news_category' => [
'news_cat_title' => 'seo_title',
'news_cat_meta_keywords' => 'seo_keywords',
'news_cat_meta_description' => 'seo_des',
],
'gl_product_keyword' => [
'tags_title' => 'seo_title',
'tags_meta_keywords' => 'seo_keywords',
'tags_meta_description' => 'seo_description',
'tags_content_title' => 'keyword_title',
'tags_content_description' => 'keyword_content',
]
];
/**
* topic-对相应字段
* @var array
*/
protected $topic_fields = [
'gl_web_custom_template' => 'name',
'gl_product' => 'title',
'gl_product_category' => 'title',
'gl_blog' => 'name',
'gl_blog_category' => 'name',
'gl_news' => 'name',
'gl_news_category' => 'name',
'gl_product_keyword' => 'title'
];
/**
* 使用核心关键词的key 数量
* @var array
*/
protected $core_keyword_keys = [
'page_meta_keywords' => 1,
'blog_cat_meta_keywords' => 8,
'news_cat_meta_keywords' => 8,
];
/**
* @return bool
*/
public function handle()
{
while (true) {
$project_id = Redis::rpop('updateSeoTdk');
if (!$project_id) {
sleep(2);
continue;
}
echo date('Y-m-d H:i:s') . ' start project_id: ' . $project_id . PHP_EOL;
try {
ProjectServer::useProject($project_id);
$this->seo_tdk($project_id);
} catch (\Exception $e) {
echo date('Y-m-d H:i:s') . ' line '. $e->getLine() .' error: ' . $project_id . '->' . $e->getMessage() . PHP_EOL;
}
echo date('Y-m-d H:i:s') . ' end: ' . $project_id . PHP_EOL;
}
}
public function seo_tdk($project_id)
{
$ai_commands = AiCommand::select('key', 'scene', 'ai')->get()->toArray();
$ai_commands = Arr::setValueToKey($ai_commands, 'key');
foreach ($this->maps as $table => $map) {
echo date('Y-m-d H:i:s') . '更新--' . $table . ': 项目id' . $project_id . PHP_EOL;
$list = DB::connection('custom_mysql')->table($table)->get()->toArray();
if (!empty($list)) {
foreach ($list as $v) {
$v = (array)$v;
echo date('Y-m-d H:i:s') . '更新--' . $table . ':id' . $v['id'] . PHP_EOL;
$data = [];
$json_field = '';
foreach ($map as $ai_key => $field) {
$field_arr = explode('.', $field);
if (count($field_arr) > 1) {
$json_field = $field_arr[0];
$value = json_decode($v[$field_arr[0]], true)[$field_arr[1]] ?? '';
} else {
$value = $v[$field] ?? '';
}
//已有值的 跳过
if ($value) {
echo $field.'已有值 跳过' . PHP_EOL;
continue;
}
//AI生成
if (!empty($ai_commands[$ai_key]['ai'])) {
$prompt = $this->getPrompt($project_id, $ai_commands[$ai_key]['ai'], $table, $v);
if(!$prompt){
continue;
}
if (count($field_arr) > 1) {
$data[$field_arr[0]][$field_arr[1]] = $this->ai_send($prompt);
}else{
$data[$field] = $this->ai_send($prompt);
}
} else {
//直接使用topic
if (count($field_arr) > 1) {
$data[$field_arr[0]][$field_arr[1]] = $v[$this->topic_fields[$table]] ?? '';
//使用核心关键词
if(in_array($ai_key, array_keys($this->core_keyword_keys))){
$data[$field_arr[0]][$field_arr[1]] = $this->mainKeywords($project_id, $this->core_keyword_keys[$ai_key]);
}
}else{
$data[$field] = $v[$this->topic_fields[$table]] ?? '';
//使用核心关键词
if(in_array($ai_key, array_keys($this->core_keyword_keys))){
$data[$field] = $this->mainKeywords($project_id, $this->core_keyword_keys[$ai_key]);
}
}
}
}
if(!$data){
continue;
}
if($json_field){
$old_data = json_decode($v[$field_arr[0]], true);
foreach ($old_data as $kk=>$vv){
empty($data[$json_field][$kk]) && $data[$json_field][$kk] = $vv;
}
$data[$json_field] = json_encode($data[$json_field]);
}
DB::connection('custom_mysql')->table($table)->where(['id' => $v['id']])->update($data);
}
}
}
}
public function getPrompt($project_id, $prompt, $table, $data){
$lang = '';
if(strpos($prompt, '{topic}') !== false){
$topic = $data[$this->topic_fields[$table]] ?? '';
if(!$topic){
echo 'topic为空 跳过' . PHP_EOL;
return false;
}
$prompt = str_replace('{topic}', $topic, $prompt);
$lang = $this->getLang($topic);
}
if(strpos($prompt, '{keyword}') !== false) {
$keyword = $this->mainKeywords($project_id, 8);
if(!$keyword){
echo '核心关键词为空 跳过' . PHP_EOL;
return false;
}
$prompt = str_replace('{keyword}', $keyword, $prompt);
!$lang && $lang = $this->getLang($keyword);
}
if(strpos($prompt, '{company name}') !== false) {
$company_name = $this->companyName($project_id);
if(!$company_name){
echo '公司英文全称为空 跳过' . PHP_EOL;
return false;
}
$prompt = str_replace('{company name}', $company_name, $prompt);
}
$prompt .= '.Please answer in ' . ($lang ?: 'English');
echo $prompt . PHP_EOL;
return $prompt;
}
/**
* @remark :获取公司英文名称
* @name :companyName
* @author :lyh
* @method :post
* @time :2023/10/30 11:22
*/
public function companyName($project_id, $key = '')
{
$data = [
'product_long_description',
];
$projectOptimizeModel = new DeployOptimize();
$info = $projectOptimizeModel->read(['project_id' => $project_id], ['id', 'company_en_name', 'company_en_description']);
if (in_array($key, $data)) {
return $info['company_en_description'];
} else {
return $info['company_en_name'];
}
}
/**
* @remark :获取公司核心关键词
* @name :mainKeywords
* @author :lyh
* @method :post
* @time :2023/10/30 11:22
*/
public function mainKeywords($project_id, $num)
{
$str = '';
$projectOptimizeModel = new DeployOptimize();
$info = $projectOptimizeModel->read(['project_id' => $project_id], ['id', 'main_keywords']);
if (!empty($info['main_keywords'])) {
$main_keywords = explode(PHP_EOL, $info['main_keywords']);
//随机取
shuffle($main_keywords);
$main_keywords = array_slice($main_keywords, 0, $num);
$str = implode(",", $main_keywords);
}
return $str;
}
public function getLang($content){
$result = Translate::translateSl($content);
if (isset($result['texts']['sl']) && isset(Translate::$tls_list[$result['texts']['sl']])) {
$lang = Translate::$tls_list[$result['texts']['sl']]['lang_en'];
} else {
$lang = 'English';
}
return $lang;
}
/**
* @remark :AI发送
* @name :ai_send
* @author :lyh
* @method :post
* @time :2023/8/19 10:40
*/
public function ai_send($prompt)
{
$text = Gpt::instance()->openai_chat_qqs($prompt);
$text = Common::deal_keywords($text);
return Common::deal_str($text);
}
}