UpdateKeyword.php
5.2 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
<?php
/**
* @remark :
* @name :UpdateKeyword.php
* @author :lyh
* @method :post
* @time :2024/7/3 9:23
*/
namespace App\Console\Commands\Update;
use App\Models\Domain\DomainInfo;
use App\Models\Product\Keyword;
use App\Models\Product\KeywordPage;
use App\Models\Product\Product;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class UpdateKeyword extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'update_keyword_content';
/**
* The console command description.
*
* @var string
*/
protected $description = '批量更新关键词内容';
public function handle(){
while (true){
$keywordPageModel = new KeywordPage();
$lists = $keywordPageModel->list(['status'=>0]);
if(empty($lists)){
sleep(100);
continue;
}
$domainModel = new DomainInfo();
foreach ($lists as $v){
ProjectServer::useProject($v['project_id']);
$this->saveKeywordContent($v);
DB::disconnect('custom_mysql');
//获取当前项目的域名
$domainInfo = $domainModel->read(['project_id'=>$v['project_id']]);
if($domainInfo !== false){
$this->curlDelRoute($domainInfo['domain'],$v['project_id']);
}
$keywordPageModel->edit(['status'=>1],['id'=>$v['id']]);
}
sleep(10);
}
}
/**
* @remark :更新关键词内容
* @name :saveKeywordContent
* @author :lyh
* @method :post
* @time :2024/7/3 10:25
*/
public function saveKeywordContent($info){
$keywordModel = new Keyword();
$updateObject = json_decode($info['update_object'],true);
$text = json_decode($info['text'],true);
if(empty($text)){
return false;
}
$number = count($text);
if($updateObject['type'] == 0){//更新所有关键字
//获取所有关键字的id
$idArr = $keywordModel->selectField(['id'=>['>',0]],'id');
if(empty($idArr)){
return true;
}
if($info['update_method'] != 1){
$idArr = shuffle($idArr);
}
if(!is_array($idArr)){
return false;
}
$result = $this->splitArrayIntoParts($idArr,$number);
foreach ($result as $key => $val){
$keywordModel->edit(['keyword_content'=>$text[$key]],['id'=>['in',$val]]);
}
}else{
//按传递的关键字更新
if(!empty($updateObject['keyword'])){
$updateObject['keyword'] = (array)$updateObject['keyword'];
foreach ($updateObject['keyword'] as $key => $item){
if($info['update_method'] != 1){
$randomNumber = rand(0, $number - 1);
}else{
$randomNumber = $text[$key] ?? rand(0, $number - 1);
}
if(isset($text[$randomNumber])){
$keywordModel->edit(['keyword_content'=>$text[$randomNumber]],['title'=>$item]);
}
}
}
//按给定的数量更新
if(!empty($updateObject['number']) && ($updateObject['number'] != 0)){
$keywordIdArr = $keywordModel->where("status",1)->inRandomOrder()->take($updateObject['number'])->pluck('id')->toArray();
foreach ($keywordIdArr as $key => $item){
if($info['update_method'] != 1){
$randomNumber = rand(0, $number - 1);
}else{
$randomNumber = $text[$key] ?? rand(0, $number - 1);
}
if(isset($text[$randomNumber])){
$keywordModel->edit(['keyword_content'=>$text[$randomNumber]],['title'=>$item]);
}
}
}
}
return true;
}
/**
* @remark :删除路由通知C端
* @name :curlDelRoute
* @author :lyh
* @method :post
* @time :2023/11/30 14:43
*/
public function curlDelRoute($domain,$project_id){
if (strpos($domain, 'https://') === false) {
$domain = 'https://' . $domain . '/';
}
$url = $domain.'api/update_page/?project_id='.$project_id.'&route=4';
shell_exec('curl -k "'.$url.'"');
return true;
}
public function splitArrayIntoParts(array $data, int $parts): array
{
$count = count($data);
// 每份的最小长度(向下取整)
$minSize = intdiv($count, $parts);
// 余数(说明有一些数组项要平均分配给前面的几份)
$remainder = $count % $parts;
$result = [];
$start = 0;
for ($i = 0; $i < $parts; $i++) {
$size = $minSize + ($i < $remainder ? 1 : 0);
$result[] = array_slice($data, $start, $size);
$start += $size;
}
return $result;
}
}