Temp.php
5.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
<?php
namespace App\Console\Commands\Update;
use App\Helper\Arr;
use App\Http\Logic\Bside\Product\CategoryLogic;
use App\Models\Blog\Blog;
use App\Models\Collect\CollectTask;
use App\Models\Com\UpdateLog;
use App\Models\News\News;
use App\Models\Product\Category;
use App\Models\Product\Keyword;
use App\Models\Product\Product;
use App\Models\RouteMap\RouteMap;
use App\Models\Template\BCustomTemplate;
use App\Models\WebSetting\WebSettingReceiving;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
/**
* 4.0,5.0升级到6.0,内容同步
* Class ProjectImport
* @package App\Console\Commands
* @author Akun
* @date 2023/10/9 15:04
*/
class Temp extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'project_update_temp';
/**
* The console command description.
*
* @var string
*/
protected $description = '执行项目升级任务';
public function handle()
{
$this->start_update();
}
protected function start_update()
{
// $data = UpdateLog::where('project_id','<=',530)->where('api_type','category')->get();
$data = UpdateLog::where('project_id', '=', 298)->where('api_type', 'category')->get();
foreach ($data as $task) {
$project_id = $task->project_id;
$api_type = $task->api_type;
$api_url_arr = explode('?', $task->api_url);
$api_url = $api_url_arr[0];
echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', task_type: ' . $api_type . ', update start' . PHP_EOL;
//设置数据库
$project = ProjectServer::useProject($project_id);
if ($project) {
//分类
$url = $api_url . '?' . http_build_query(['w' => 'category']);
$data = curl_c($url);
if (isset($data['code']) && $data['code'] == 200) {
$items = $data['data'] ?? [];
$this->category_insert($project_id, $items, 0);
} else {
continue;
}
}
//关闭数据库
DB::disconnect('custom_mysql');
echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', task_type: ' . $api_type . ', update end ' . PHP_EOL;
}
}
//获取地址路由
protected function get_url_route($url)
{
$arr = parse_url($url);
if (empty($arr['path'])) {
return '';
}
$path = $arr['path'];
if (strpos($path, '.') !== false) {
$path = substr($path, 0, strpos($path, '.'));
}
$path_arr = explode('/', $path);
return end($path_arr) ? end($path_arr) : $path_arr[count($path_arr) - 2];
}
//多级分类入库
protected function category_insert($project_id, $items, $pid = 0)
{
$model = new Category();
foreach ($items as $item) {
if ($item['name'] ?? '') {
$parent = $model->read(['pid' => $pid, 'title' => $item['name']], 'id');
if (!$parent) {
try {
$item['name'] = $this->special2str($item['name']);
$parent_id = $model->addReturnId([
'project_id' => $project_id,
'title' => $item['name'],
'pid' => $pid,
'keywords' => $item['keywords'] ?? '',
'describe' => $item['description'] ?? '',
'route' => $this->get_url_route($item['url'])
]);
$this->set_map($this->get_url_route($item['url']), RouteMap::SOURCE_PRODUCT_CATE, $parent_id, $project_id);
} catch (\Exception $e) {
echo 'date:' . date('Y-m-d H:i:s') . ', category_insert error: ' . $e->getMessage() . PHP_EOL;
continue;
}
} else {
$parent_id = $parent['id'];
}
if (!empty($item['children'])) {
$this->category_insert($project_id, $item['children'], $parent_id);
}
}
}
}
//获取分类名称数组
protected function get_category_name_arr($category, $pid = 0)
{
foreach ($category as $k => $v) {
if ($v['parent'] == $pid) {
return $v;
}
}
return [];
}
//特殊字符转换
protected function special2str($str)
{
if (strpos($str, ';') === false) {
return $str;
}
$list = [
'<' => '<',
'>' => '>',
'&' => '&',
'´' => '´',
'"' => '“',
' ' => ' '
];
foreach ($list as $k => $v) {
$str = str_replace($k, $v, $str);
}
return $str;
}
//路由入库
protected function set_map($route, $source, $source_id, $project_id)
{
if ($route) {
$route_map = RouteMap::where('project_id', $project_id)->where('source', $source)->where('source_id', $source_id)->first();
if (!$route_map) {
$route_map = new RouteMap();
$route_map->project_id = $project_id;
$route_map->source = $source;
$route_map->source_id = $source_id;
$route_map->route = $route;
if ($source == RouteMap::SOURCE_NEWS) {
$route_map->path = RouteMap::SOURCE_NEWS;
} elseif ($source == RouteMap::SOURCE_BLOG) {
$route_map->path = RouteMap::SOURCE_BLOG;
}
$route_map->save();
}
}
}
}