Temp.php
4.0 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
<?php
namespace App\Console\Commands\Update;
use App\Models\Blog\Blog;
use App\Models\Com\UpdateLog;
use App\Models\News\News;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
/**
* 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()
{
$list = UpdateLog::where('project_id', '!=', 437)->where('api_type', 'news')->get();
foreach ($list 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];
$page_size = 20;
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' => $api_type, 'page' => 1, 'pagesize' => 0]);
$data = curl_c($url);
if (isset($data['code']) && $data['code'] == 200) {
$count = $data['data']['count'] ?? 0;
$total_page = ceil($count / $page_size);
for ($page = 1; $page <= $total_page; $page++) {
$url_page = $api_url . '?' . http_build_query(['w' => $api_type, 'page' => $page, 'pagesize' => $page_size]);
$data_page = curl_c($url_page);
if (isset($data_page['code']) && $data_page['code'] == 200) {
$items = $data_page['data']['data'] ?? [];
if ($api_type == 'news') {
$model = new News();
} else {
$model = new Blog();
}
foreach ($items as $item) {
if ($item['ttile'] ?? '') {
$news = $model->read(['name' => $item['ttile']], 'id');
if ($news) {
try {
if (is_array($item['images'])) {
$image = $item['images'][0] ?? '';
} else {
$image = $item['images'] ?? '';
}
if (strpos($image, '//') === 0) {
$image = 'https:' . $image;
}
$model->edit(['image' => $image], ['id' => $news['id']]);
} catch (\Exception $e) {
echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', error: ' . $e->getMessage() . PHP_EOL;
continue;
}
}
}
}
}
}
} 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;
}
}
}