作者 刘锟

update

@@ -282,6 +282,7 @@ class HtmlCollect extends Command @@ -282,6 +282,7 @@ class HtmlCollect extends Command
282 protected function url_check($url, $project_id, $domain, $web_url_domain, $home_url) 282 protected function url_check($url, $project_id, $domain, $web_url_domain, $home_url)
283 { 283 {
284 if ($url) { 284 if ($url) {
  285 + $url = str_replace('"', '', $url);
285 $arr = parse_url($url); 286 $arr = parse_url($url);
286 $scheme = $arr['scheme'] ?? ''; 287 $scheme = $arr['scheme'] ?? '';
287 $host = $arr['host'] ?? ''; 288 $host = $arr['host'] ?? '';
@@ -343,6 +344,7 @@ class HtmlCollect extends Command @@ -343,6 +344,7 @@ class HtmlCollect extends Command
343 $url_arr = explode('/', $vs['url_complete']); 344 $url_arr = explode('/', $vs['url_complete']);
344 $target_arr = explode('/', $new_source); 345 $target_arr = explode('/', $new_source);
345 foreach ($css_source as $vcs) { 346 foreach ($css_source as $vcs) {
  347 + $vcs = str_replace('"', '', $vcs);
346 $vcs_arr = parse_url($vcs); 348 $vcs_arr = parse_url($vcs);
347 if (isset($vcs_arr['domain'])) { 349 if (isset($vcs_arr['domain'])) {
348 //不是相对路径,不下载 350 //不是相对路径,不下载
@@ -82,10 +82,10 @@ class HtmlLanguageCollect extends Command @@ -82,10 +82,10 @@ class HtmlLanguageCollect extends Command
82 //采集html页面,下载资源到本地并替换 82 //采集html页面,下载资源到本地并替换
83 try { 83 try {
84 $html = curl_c('https://' . $collect_info->domain . $collect_info->route, false); 84 $html = curl_c('https://' . $collect_info->domain . $collect_info->route, false);
85 - if($html == '0'){ 85 + if ($html == '0') {
86 $collect_info->status = CollectTask::STATUS_FAIL; 86 $collect_info->status = CollectTask::STATUS_FAIL;
87 $collect_info->save(); 87 $collect_info->save();
88 - echo 'date:' . date('Y-m-d H:i:s') . ', project_id: ' . $project_id . ', collect_id: ' . $collect_id . ', error: no html' . PHP_EOL; 88 + echo 'date:' . date('Y-m-d H:i:s') . ', project_id: ' . $project_id . ', collect_id: ' . $collect_id . ', error: no html' . PHP_EOL;
89 sleep(2); 89 sleep(2);
90 return true; 90 return true;
91 } 91 }
@@ -282,6 +282,7 @@ class HtmlLanguageCollect extends Command @@ -282,6 +282,7 @@ class HtmlLanguageCollect extends Command
282 protected function url_check($url, $project_id, $domain, $web_url_domain, $home_url) 282 protected function url_check($url, $project_id, $domain, $web_url_domain, $home_url)
283 { 283 {
284 if ($url) { 284 if ($url) {
  285 + $url = str_replace('"', '', $url);
285 $arr = parse_url($url); 286 $arr = parse_url($url);
286 $scheme = $arr['scheme'] ?? ''; 287 $scheme = $arr['scheme'] ?? '';
287 $host = $arr['host'] ?? ''; 288 $host = $arr['host'] ?? '';
@@ -343,6 +344,7 @@ class HtmlLanguageCollect extends Command @@ -343,6 +344,7 @@ class HtmlLanguageCollect extends Command
343 $url_arr = explode('/', $vs['url_complete']); 344 $url_arr = explode('/', $vs['url_complete']);
344 $target_arr = explode('/', $new_source); 345 $target_arr = explode('/', $new_source);
345 foreach ($css_source as $vcs) { 346 foreach ($css_source as $vcs) {
  347 + $vcs = str_replace('"', '', $vcs);
346 $vcs_arr = parse_url($vcs); 348 $vcs_arr = parse_url($vcs);
347 if (isset($vcs_arr['domain'])) { 349 if (isset($vcs_arr['domain'])) {
348 //不是相对路径,不下载 350 //不是相对路径,不下载
@@ -357,7 +359,7 @@ class HtmlLanguageCollect extends Command @@ -357,7 +359,7 @@ class HtmlLanguageCollect extends Command
357 continue; 359 continue;
358 } 360 }
359 $path_arr = explode('.', $vcs); 361 $path_arr = explode('.', $vcs);
360 - if(in_array(end($path_arr), ['html', 'php', 'com', 'xml'])){ 362 + if (in_array(end($path_arr), ['html', 'php', 'com', 'xml'])) {
361 continue; 363 continue;
362 } 364 }
363 365
  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 + while (true) {
  45 + $this->start_update();
  46 + }
  47 + }
  48 +
  49 + protected function start_update()
  50 + {
  51 + $list = UpdateLog::where('api_type', 'category')->get();
  52 +
  53 + foreach ($list as $task) {
  54 +
  55 +
  56 + $project_id = $task->project_id;
  57 + $api_type = $task->api_type;
  58 + $api_url_arr = explode('?', $task->api_url);
  59 + $api_url = $api_url_arr[0];
  60 +
  61 + $page_size = 20;
  62 +
  63 + echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', task_type: ' . $api_type . ', update start' . PHP_EOL;
  64 +
  65 + $task->status = UpdateLog::STATUS_ING;//同步中
  66 + $task->save();
  67 +
  68 + $domain_arr = parse_url($api_url);
  69 + //获取网站配置
  70 + $link_type = 0;
  71 + $web_url_domain = $domain_arr['host'];
  72 + $home_url = $domain_arr['host'];
  73 + $url_web_config = 'https://' . $domain_arr['host'] . '/wp-content/cache/user_config.text';
  74 + $data_config = curl_c($url_web_config);
  75 + if ($data_config) {
  76 + $link_type = $data_config['link_type'] ?? 0;
  77 +
  78 + $web_url_arr = parse_url($data_config['web_url_domain'] ?? '');
  79 + if (isset($web_url_arr['host'])) {
  80 + $web_url_domain = $web_url_arr['host'];
  81 + }
  82 +
  83 + $home_url_arr = parse_url($data_config['home_url'] ?? '');
  84 + if (isset($home_url_arr['host'])) {
  85 + $home_url = $home_url_arr['host'];
  86 + }
  87 + }
  88 + //获取所有语种
  89 + $language_list = [];
  90 + $url_language = 'https://' . $domain_arr['host'] . '/wp-content/plugins/proofreading/json/user_language.json';
  91 + $data_language = curl_c($url_language);
  92 + if ($data_language) {
  93 + $language_list = array_column($data_language, 'short');
  94 + }
  95 + //获取所有页面
  96 + $page_list = [];
  97 + $url_page = 'https://' . $domain_arr['host'] . '/wp-content/cache/pages_list.json';
  98 + $data_page = curl_c($url_page);
  99 + if ($data_page) {
  100 + $page_list = array_column($data_page, 'path');
  101 + }
  102 +
  103 + //设置数据库
  104 + $project = ProjectServer::useProject($project_id);
  105 + if ($project) {
  106 + if ($api_type == 'category') {
  107 + //产品分类
  108 + $url = $api_url . '?' . http_build_query(['w' => 'category']);
  109 + $data = curl_c($url);
  110 + if (isset($data['code']) && $data['code'] == 200) {
  111 + $items = $data['data'] ?? [];
  112 + $this->category_insert($project_id, $items, 0);
  113 + } else {
  114 + return true;
  115 + }
  116 + } elseif ($api_type == 'post') {
  117 + //产品
  118 + $url = $api_url . '?' . http_build_query(['w' => 'post', 'page' => 1, 'pagesize' => 0]);
  119 + $data = curl_c($url);
  120 + if (isset($data['code']) && $data['code'] == 200) {
  121 + $count = $data['data']['count'] ?? 0;
  122 +
  123 + $total_page = ceil($count / $page_size);
  124 + for ($page = 1; $page <= $total_page; $page++) {
  125 + $url_page = $api_url . '?' . http_build_query(['w' => 'post', 'page' => $page, 'pagesize' => $page_size]);
  126 + $data_page = curl_c($url_page);
  127 + if (isset($data_page['code']) && $data_page['code'] == 200) {
  128 + $items = $data_page['data']['data'] ?? [];
  129 +
  130 + $model = new Product();
  131 + $category_model = new Category();
  132 + $logic = new CategoryLogic();
  133 +
  134 + foreach ($items as $item) {
  135 + $route = $this->get_url_route($item['url'] ?? '');
  136 + if ($route) {
  137 + $product = $model->read(['route' => $route], 'id');
  138 + if (!$product) {
  139 + //图片
  140 + $gallery = [];
  141 + if ($item['images'] ?? []) {
  142 + foreach ($item['images'] as $k_img => $img) {
  143 + $gallery[] = ['alt' => '这是一张产品图', 'url' => $this->source_download($img, $project_id, $domain_arr['host'], $web_url_domain, $home_url)];
  144 + }
  145 + }
  146 + //分类
  147 + $category_id = '';
  148 + if ($item['category'] ?? []) {
  149 + $category_arr = $category_model->list(['original_id' => ['in', array_column($item['category'], 'id')]]);
  150 + $category_id = $logic->getLastCategory(array_column($category_arr, 'id'));
  151 + }
  152 + try {
  153 + $item['ttile'] = $this->special2str($item['ttile'] ?? '');
  154 + $id = $model->insertGetId([
  155 + 'project_id' => $project_id,
  156 + 'title' => $item['ttile'],
  157 + 'intro' => $item['short_description'] ?? '',
  158 + 'content' => $item['content'] ?? '',
  159 + 'category_id' => $category_id,
  160 + 'thumb' => isset($gallery[0]) ? Arr::a2s($gallery[0]) : '',
  161 + 'gallery' => Arr::a2s($gallery),
  162 + 'seo_mate' => Arr::a2s([
  163 + 'title' => $item['ttile'],
  164 + 'keyword' => $item['keywords'] ?? '',
  165 + 'description' => $item['description'] ?? ''
  166 + ]),
  167 + 'status' => Product::STATUS_ON,
  168 + 'created_at' => $item['post_date'] ?? date('Y-m-d H:i:s'),
  169 + 'updated_at' => $item['post_date'] ?? date('Y-m-d H:i:s'),
  170 + 'sort' => $item['sort'] ?? 0,
  171 + 'is_upgrade' => 1,
  172 + 'six_read' => 1,
  173 + 'route' => $route
  174 + ]);
  175 + $this->set_map($route, RouteMap::SOURCE_PRODUCT, $id, $project_id);
  176 +
  177 + CollectTask::_insert($item['url'], $project_id, RouteMap::SOURCE_PRODUCT, $id, $link_type, $language_list, $page_list);
  178 + } catch (\Exception $e) {
  179 + echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', error: ' . $e->getMessage() . PHP_EOL;
  180 + continue;
  181 + }
  182 + } else {
  183 + $category_id = '';
  184 + if ($item['category'] ?? []) {
  185 + $category_arr = $category_model->list(['original_id' => ['in', array_column($item['category'], 'id')]]);
  186 + $category_id = $logic->getLastCategory(array_column($category_arr, 'id'));
  187 + }
  188 +
  189 + $model->edit(['category_id' => $category_id, 'product_type' => ''], ['id' => $product['id']]);
  190 + }
  191 + }
  192 + }
  193 + }
  194 + }
  195 + } else {
  196 + return true;
  197 + }
  198 + }
  199 + }
  200 + //关闭数据库
  201 + DB::disconnect('custom_mysql');
  202 +
  203 + $task->status = UpdateLog::STATUS_COM;//同步完成
  204 + if ($api_type == 'post' || $api_type == 'page' || $api_type == 'news' || $api_type == 'blog') {
  205 + $task->collect_status = UpdateLog::COLLECT_STATUS_UN;
  206 + }
  207 + $task->save();
  208 +
  209 + echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', task_type: ' . $api_type . ', update end ' . PHP_EOL;
  210 +
  211 + sleep(2);
  212 + }
  213 + }
  214 +
  215 +
  216 + //获取地址路由
  217 + protected function get_url_route($url)
  218 + {
  219 + $arr = parse_url(urldecode($url));
  220 + if (empty($arr['path'])) {
  221 + return '';
  222 + }
  223 + $path = $arr['path'];
  224 +
  225 + if (strpos($path, '.') !== false) {
  226 + $path = substr($path, 0, strpos($path, '.'));
  227 + }
  228 +
  229 + $path_arr = explode('/', $path);
  230 +
  231 + return end($path_arr) ? end($path_arr) : $path_arr[count($path_arr) - 2];
  232 + }
  233 +
  234 + //产品多级分类入库
  235 + protected function category_insert($project_id, $items, $pid = 0)
  236 + {
  237 + $model = new Category();
  238 + foreach ($items as $item) {
  239 + $route = $this->get_url_route($item['url'] ?? '');
  240 + if ($route) {
  241 + $parent = $model->read(['pid' => $pid, 'route' => $route], 'id');
  242 + if (!$parent) {
  243 + try {
  244 + $item['name'] = $this->special2str($item['name'] ?? '');
  245 + $parent_id = $model->addReturnId([
  246 + 'project_id' => $project_id,
  247 + 'title' => $item['name'],
  248 + 'pid' => $pid,
  249 + 'keywords' => $item['keywords'] ?? '',
  250 + 'describe' => $item['description'] ?? '',
  251 + 'original_id' => $item['id'],
  252 + 'route' => $route
  253 + ]);
  254 + $this->set_map($route, RouteMap::SOURCE_PRODUCT_CATE, $parent_id, $project_id);
  255 + } catch (\Exception $e) {
  256 + echo 'date:' . date('Y-m-d H:i:s') . ', category_insert error: ' . $e->getMessage() . PHP_EOL;
  257 + continue;
  258 + }
  259 + } else {
  260 + $parent_id = $parent['id'];
  261 + }
  262 +
  263 + if (!empty($item['children'])) {
  264 + $this->category_insert($project_id, $item['children'], $parent_id);
  265 + }
  266 + }
  267 + }
  268 + }
  269 +
  270 + //特殊字符转换
  271 + protected function special2str($str)
  272 + {
  273 + if (strpos($str, ';') === false) {
  274 + return $str;
  275 + }
  276 +
  277 + $list = [
  278 + '&lt;' => '<',
  279 + '&gt;' => '>',
  280 + '&amp;' => '&',
  281 + '&acute;' => '´',
  282 + '&quot;' => '“',
  283 + '&nbsp;' => ' '
  284 + ];
  285 +
  286 + foreach ($list as $k => $v) {
  287 + $str = str_replace($k, $v, $str);
  288 + }
  289 +
  290 + return $str;
  291 + }
  292 +
  293 + //路由入库
  294 + protected function set_map($route, $source, $source_id, $project_id)
  295 + {
  296 + if ($route) {
  297 + $route_map = RouteMap::where('project_id', $project_id)->where('source', $source)->where('source_id', $source_id)->first();
  298 + if (!$route_map) {
  299 + $route_map = new RouteMap();
  300 + $route_map->project_id = $project_id;
  301 + $route_map->source = $source;
  302 + $route_map->source_id = $source_id;
  303 + $route_map->route = $route;
  304 +
  305 + if ($source == RouteMap::SOURCE_NEWS) {
  306 + $route_map->path = RouteMap::SOURCE_NEWS;
  307 + } elseif ($source == RouteMap::SOURCE_BLOG) {
  308 + $route_map->path = RouteMap::SOURCE_BLOG;
  309 + }
  310 +
  311 + $route_map->save();
  312 + }
  313 + }
  314 + }
  315 +
  316 + //资源下载
  317 + protected function source_download($url, $project_id, $domain, $web_url_domain, $home_url)
  318 + {
  319 + if (!$url) {
  320 + return '';
  321 + }
  322 +
  323 + $arr = parse_url($url);
  324 + $scheme = $arr['scheme'] ?? '';
  325 + $host = $arr['host'] ?? '';
  326 + $path = $arr['path'] ?? '';
  327 +
  328 + $url_complete = ($scheme ?: 'https') . '://' . ($host ?: $domain) . $path;
  329 +
  330 + if ((empty($host) || $host == $web_url_domain || $host == $home_url) && $path) {
  331 +
  332 + $source = CollectSource::where('project_id', $project_id)->where('origin', $url)->first();
  333 + if (!$source) {
  334 + $new_url = CosService::uploadRemote($project_id, 'image_product', $url_complete);
  335 +
  336 + if ($new_url) {
  337 + CollectSource::insert([
  338 + 'project_id' => $project_id,
  339 + 'origin' => $url,
  340 + 'target' => $new_url,
  341 + 'created_at' => date('Y-m-d H:i:s'),
  342 + 'updated_at' => date('Y-m-d H:i:s'),
  343 + ]);
  344 +
  345 + return getImageUrl($new_url);
  346 + } else {
  347 + return $url_complete;
  348 + }
  349 + } else {
  350 + return getImageUrl($source['target']);
  351 + }
  352 + } else {
  353 + return $url_complete;
  354 + }
  355 + }
  356 +}