作者 刘锟

合并分支 'akun' 到 'master'

Akun



查看合并请求 !158
1 -<?php  
2 -  
3 -namespace App\Console\Commands\Update;  
4 -  
5 -use App\Helper\Arr;  
6 -use App\Http\Logic\Bside\Product\CategoryLogic;  
7 -use App\Models\Collect\CollectSource;  
8 -use App\Models\Collect\CollectTask;  
9 -use App\Models\Com\UpdateLog;  
10 -use App\Models\Product\Category;  
11 -use App\Models\Product\Product;  
12 -use App\Models\RouteMap\RouteMap;  
13 -use App\Services\CosService;  
14 -use App\Services\ProjectServer;  
15 -use Illuminate\Console\Command;  
16 -use Illuminate\Support\Facades\DB;  
17 -  
18 -/**  
19 - * 4.0,5.0升级到6.0,内容同步  
20 - * Class ProjectImport  
21 - * @package App\Console\Commands  
22 - * @author Akun  
23 - * @date 2023/10/9 15:04  
24 - */  
25 -class ProjectUpdateTemp extends Command  
26 -{  
27 - /**  
28 - * The name and signature of the console command.  
29 - *  
30 - * @var string  
31 - */  
32 - protected $signature = 'project_update_temp';  
33 -  
34 - /**  
35 - * The console command description.  
36 - *  
37 - * @var string  
38 - */  
39 - protected $description = '执行项目升级任务';  
40 -  
41 -  
42 - public function handle()  
43 - {  
44 - $this->start_update();  
45 - }  
46 -  
47 - protected function start_update()  
48 - {  
49 - $list = UpdateLog::where('api_type', 'post')->get();  
50 -  
51 - foreach ($list as $task) {  
52 -  
53 -  
54 - $project_id = $task->project_id;  
55 - $api_type = $task->api_type;  
56 - $api_url_arr = explode('?', $task->api_url);  
57 - $api_url = $api_url_arr[0];  
58 -  
59 - $page_size = 20;  
60 -  
61 - echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', task_type: ' . $api_type . ', update start' . PHP_EOL;  
62 -  
63 - //设置数据库  
64 - $project = ProjectServer::useProject($project_id);  
65 - if ($project) {  
66 - if ($api_type == 'category') {  
67 - //产品分类  
68 - $url = $api_url . '?' . http_build_query(['w' => 'category']);  
69 - $data = curl_c($url);  
70 - if (isset($data['code']) && $data['code'] == 200) {  
71 - $items = $data['data'] ?? [];  
72 - $this->category_insert($project_id, $items, 0);  
73 - } else {  
74 - return true;  
75 - }  
76 - } elseif ($api_type == 'post') {  
77 - //产品  
78 - $url = $api_url . '?' . http_build_query(['w' => 'post', 'page' => 1, 'pagesize' => 0]);  
79 - $data = curl_c($url);  
80 - if (isset($data['code']) && $data['code'] == 200) {  
81 - $count = $data['data']['count'] ?? 0;  
82 -  
83 - $total_page = ceil($count / $page_size);  
84 - for ($page = 1; $page <= $total_page; $page++) {  
85 - $url_page = $api_url . '?' . http_build_query(['w' => 'post', 'page' => $page, 'pagesize' => $page_size]);  
86 - $data_page = curl_c($url_page);  
87 - if (isset($data_page['code']) && $data_page['code'] == 200) {  
88 - $items = $data_page['data']['data'] ?? [];  
89 -  
90 - $model = new Product();  
91 - $category_model = new Category();  
92 - $logic = new CategoryLogic();  
93 -  
94 - foreach ($items as $item) {  
95 - $route = $this->get_url_route($item['url'] ?? '');  
96 - if ($route) {  
97 - $product = $model->read(['route' => $route], 'id');  
98 - if ($product) {  
99 - $category_id = '';  
100 - if ($item['category'] ?? []) {  
101 - $category_arr = $category_model->list(['original_id' => ['in', array_column($item['category'], 'id')]]);  
102 - $category_id = $logic->getLastCategory(array_column($category_arr, 'id'));  
103 - }  
104 -  
105 - $model->edit(['category_id' => $category_id, 'product_type' => ''], ['id' => $product['id']]);  
106 - }  
107 - }  
108 - }  
109 - }  
110 - }  
111 - } else {  
112 - return true;  
113 - }  
114 - }  
115 - }  
116 - //关闭数据库  
117 - DB::disconnect('custom_mysql');  
118 -  
119 - echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', task_type: ' . $api_type . ', update end ' . PHP_EOL;  
120 -  
121 - sleep(2);  
122 - }  
123 - }  
124 -  
125 -  
126 - //获取地址路由  
127 - protected function get_url_route($url)  
128 - {  
129 - $arr = parse_url(urldecode($url));  
130 - if (empty($arr['path'])) {  
131 - return '';  
132 - }  
133 - $path = $arr['path'];  
134 -  
135 - if (strpos($path, '.') !== false) {  
136 - $path = substr($path, 0, strpos($path, '.'));  
137 - }  
138 -  
139 - $path_arr = explode('/', $path);  
140 -  
141 - return end($path_arr) ? end($path_arr) : $path_arr[count($path_arr) - 2];  
142 - }  
143 -  
144 - //产品多级分类入库  
145 - protected function category_insert($project_id, $items, $pid = 0)  
146 - {  
147 - $model = new Category();  
148 - foreach ($items as $item) {  
149 - $route = $this->get_url_route($item['url'] ?? '');  
150 - if ($route) {  
151 - $parent = $model->read(['pid' => $pid, 'route' => $route], 'id');  
152 - if (!$parent) {  
153 - try {  
154 - $item['name'] = $this->special2str($item['name'] ?? '');  
155 - $parent_id = $model->addReturnId([  
156 - 'project_id' => $project_id,  
157 - 'title' => $item['name'],  
158 - 'pid' => $pid,  
159 - 'keywords' => $item['keywords'] ?? '',  
160 - 'describe' => $item['description'] ?? '',  
161 - 'original_id' => $item['id'],  
162 - 'route' => $route  
163 - ]);  
164 - $this->set_map($route, RouteMap::SOURCE_PRODUCT_CATE, $parent_id, $project_id);  
165 - } catch (\Exception $e) {  
166 - echo 'date:' . date('Y-m-d H:i:s') . ', category_insert error: ' . $e->getMessage() . PHP_EOL;  
167 - continue;  
168 - }  
169 - } else {  
170 - $parent_id = $parent['id'];  
171 - }  
172 -  
173 - if (!empty($item['children'])) {  
174 - $this->category_insert($project_id, $item['children'], $parent_id);  
175 - }  
176 - }  
177 - }  
178 - }  
179 -  
180 - //特殊字符转换  
181 - protected function special2str($str)  
182 - {  
183 - if (strpos($str, ';') === false) {  
184 - return $str;  
185 - }  
186 -  
187 - $list = [  
188 - '&lt;' => '<',  
189 - '&gt;' => '>',  
190 - '&amp;' => '&',  
191 - '&acute;' => '´',  
192 - '&quot;' => '“',  
193 - '&nbsp;' => ' '  
194 - ];  
195 -  
196 - foreach ($list as $k => $v) {  
197 - $str = str_replace($k, $v, $str);  
198 - }  
199 -  
200 - return $str;  
201 - }  
202 -  
203 - //路由入库  
204 - protected function set_map($route, $source, $source_id, $project_id)  
205 - {  
206 - if ($route) {  
207 - $route_map = RouteMap::where('project_id', $project_id)->where('source', $source)->where('source_id', $source_id)->first();  
208 - if (!$route_map) {  
209 - $route_map = new RouteMap();  
210 - $route_map->project_id = $project_id;  
211 - $route_map->source = $source;  
212 - $route_map->source_id = $source_id;  
213 - $route_map->route = $route;  
214 -  
215 - if ($source == RouteMap::SOURCE_NEWS) {  
216 - $route_map->path = RouteMap::SOURCE_NEWS;  
217 - } elseif ($source == RouteMap::SOURCE_BLOG) {  
218 - $route_map->path = RouteMap::SOURCE_BLOG;  
219 - }  
220 -  
221 - $route_map->save();  
222 - }  
223 - }  
224 - }  
225 -}