作者 刘锟

自定义模块次啊及

@@ -10,6 +10,9 @@ use App\Models\Collect\CollectSource; @@ -10,6 +10,9 @@ use App\Models\Collect\CollectSource;
10 use App\Models\Collect\CollectTask; 10 use App\Models\Collect\CollectTask;
11 use App\Models\Com\UpdateLog; 11 use App\Models\Com\UpdateLog;
12 use App\Models\Com\UpdateOldInfo; 12 use App\Models\Com\UpdateOldInfo;
  13 +use App\Models\CustomModule\CustomModule;
  14 +use App\Models\CustomModule\CustomModuleCategory;
  15 +use App\Models\CustomModule\CustomModuleContent;
13 use App\Models\News\News; 16 use App\Models\News\News;
14 use App\Models\News\NewsCategory; 17 use App\Models\News\NewsCategory;
15 use App\Models\Product\Category; 18 use App\Models\Product\Category;
@@ -54,9 +57,9 @@ class ProjectUpdate extends Command @@ -54,9 +57,9 @@ class ProjectUpdate extends Command
54 public function handle() 57 public function handle()
55 { 58 {
56 ini_set('memory_limit', '512M'); 59 ini_set('memory_limit', '512M');
57 - while (true) {  
58 - $this->start_update();  
59 - } 60 +// while (true) {
  61 + $this->start_update();
  62 +// }
60 } 63 }
61 64
62 protected function start_update() 65 protected function start_update()
@@ -187,6 +190,7 @@ class ProjectUpdate extends Command @@ -187,6 +190,7 @@ class ProjectUpdate extends Command
187 if (isset($data['code']) && $data['code'] == 200) { 190 if (isset($data['code']) && $data['code'] == 200) {
188 $phones = $data['data']['phones'] ?? ''; 191 $phones = $data['data']['phones'] ?? '';
189 $emails = $data['data']['emails'] ?? ''; 192 $emails = $data['data']['emails'] ?? '';
  193 + $custom_types = $data['data']['customposttypes'] ?? '';
190 194
191 $model = new WebSettingReceiving(); 195 $model = new WebSettingReceiving();
192 if ($phones) { 196 if ($phones) {
@@ -229,6 +233,25 @@ class ProjectUpdate extends Command @@ -229,6 +233,25 @@ class ProjectUpdate extends Command
229 } 233 }
230 } 234 }
231 } 235 }
  236 + if ($custom_types) {
  237 + $custom_model = new CustomModule();
  238 + foreach ($custom_types as $v_custom) {
  239 + try {
  240 + $custom_info = $custom_model->read(['route' => $v_custom]);
  241 + if (!$custom_info) {
  242 + $custom_model->add([
  243 + 'name' => $v_custom,
  244 + 'project_id' => $project_id,
  245 + 'route' => $v_custom
  246 + ]);
  247 + }
  248 + UpdateLog::createLog($project_id, $v_custom, $task->api_url);
  249 + } catch (\Exception $e) {
  250 + echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', error: ' . $e->getMessage() . PHP_EOL;
  251 + continue;
  252 + }
  253 + }
  254 + }
232 } else { 255 } else {
233 return true; 256 return true;
234 } 257 }
@@ -392,7 +415,7 @@ class ProjectUpdate extends Command @@ -392,7 +415,7 @@ class ProjectUpdate extends Command
392 $category_id = $logic->getCategory(array_column($category_arr, 'id')); 415 $category_id = $logic->getCategory(array_column($category_arr, 'id'));
393 } 416 }
394 //图片 417 //图片
395 - if (is_array($item['images'])) { 418 + if (is_array($item['images'] ?? '')) {
396 $image = $item['images'][0] ?? ''; 419 $image = $item['images'][0] ?? '';
397 } else { 420 } else {
398 $image = $item['images'] ?? ''; 421 $image = $item['images'] ?? '';
@@ -448,7 +471,7 @@ class ProjectUpdate extends Command @@ -448,7 +471,7 @@ class ProjectUpdate extends Command
448 } else { 471 } else {
449 return true; 472 return true;
450 } 473 }
451 - } else { 474 + } elseif ($api_type == 'page') {
452 //单页 475 //单页
453 $url = $api_url . '?' . http_build_query(['w' => 'page', 'page' => 1, 'pagesize' => 0]); 476 $url = $api_url . '?' . http_build_query(['w' => 'page', 'page' => 1, 'pagesize' => 0]);
454 $data = curl_c($url); 477 $data = curl_c($url);
@@ -510,6 +533,79 @@ class ProjectUpdate extends Command @@ -510,6 +533,79 @@ class ProjectUpdate extends Command
510 } else { 533 } else {
511 return true; 534 return true;
512 } 535 }
  536 + } else {
  537 + //自定义模块
  538 + $custom_model = new CustomModule();
  539 + $custom_info = $custom_model->read(['route' => $api_type]);
  540 + if ($custom_info) {
  541 + $url = $api_url . '?' . http_build_query(['w' => $api_type, 'page' => 1, 'pagesize' => 0]);
  542 + $data = curl_c($url);
  543 + if (isset($data['code']) && $data['code'] == 200) {
  544 + $category = $data['data']['category'] ?? [];
  545 + $this->category_custom_insert($project_id, $custom_info['id'], $category, 0);
  546 +
  547 + $count = $data['data']['count'] ?? 0;
  548 +
  549 + $total_page = ceil($count / $page_size);
  550 + for ($page = 1; $page <= $total_page; $page++) {
  551 + $url_page = $api_url . '?' . http_build_query(['w' => $api_type, 'page' => $page, 'pagesize' => $page_size]);
  552 + $data_page = curl_c($url_page);
  553 + if (isset($data_page['code']) && $data_page['code'] == 200) {
  554 + $items = $data_page['data']['data'] ?? [];
  555 +
  556 + $model = new CustomModuleContent();
  557 + $category_model = new CustomModuleCategory();
  558 +
  559 + foreach ($items as $item) {
  560 + $route = $this->get_url_route($item['url'] ?? '');
  561 + if ($route) {
  562 + //分类
  563 + $category_id = '';
  564 + if ($item['category'] ?? []) {
  565 + $category_arr = $category_model->list(['original_id' => ['in', array_column($item['category'], 'id')]]);
  566 + $category_id = implode(',', array_column($category_arr, 'id'));
  567 + }
  568 + //名称去掉特殊符号
  569 + $item['title'] = $this->special2str($item['title'] ?? '');
  570 +
  571 + try {
  572 + $custom_content = $model->read(['route' => $route], 'id');
  573 + if (!$custom_content) {
  574 + $id = $model->insertGetId([
  575 + 'project_id' => $project_id,
  576 + 'name' => $item['title'],
  577 + 'module_id' => $custom_info['id'],
  578 + 'category_id' => $category_id,
  579 + 'content' => $item['content'] ?? '',
  580 + 'created_at' => $item['post_time'] ?? date('Y-m-d H:i:s'),
  581 + 'updated_at' => $item['post_time'] ?? date('Y-m-d H:i:s'),
  582 + 'is_upgrade' => 1,
  583 + 'six_read' => 1,
  584 + 'route' => $route
  585 + ]);
  586 + $this->set_map($route, RouteMap::SOURCE_MODULE, $id, $project_id);
  587 +
  588 + CollectTask::_insert($item['url'], $project_id, RouteMap::SOURCE_MODULE, $id, $domain_arr['host'], $link_type, $language_list, $page_list);
  589 + } else {
  590 + $id = $custom_content['id'];
  591 + $model->edit([
  592 + 'name' => $item['title'],
  593 + 'category_id' => $category_id,
  594 + 'content' => $item['content'] ?? '',
  595 + ], ['id' => $id]);
  596 + }
  597 + } catch (\Exception $e) {
  598 + echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', error: ' . $e->getMessage() . PHP_EOL;
  599 + continue;
  600 + }
  601 + }
  602 + }
  603 + }
  604 + }
  605 + } else {
  606 + return true;
  607 + }
  608 + }
513 } 609 }
514 } 610 }
515 //关闭数据库 611 //关闭数据库
@@ -595,7 +691,7 @@ class ProjectUpdate extends Command @@ -595,7 +691,7 @@ class ProjectUpdate extends Command
595 $parent_id = $parent['id']; 691 $parent_id = $parent['id'];
596 } 692 }
597 693
598 - if (!empty($item['children'])) { 694 + if (!empty($item['children'] ?? [])) {
599 $this->category_insert($project_id, $item['children'], $parent_id); 695 $this->category_insert($project_id, $item['children'], $parent_id);
600 } 696 }
601 } 697 }
@@ -629,13 +725,48 @@ class ProjectUpdate extends Command @@ -629,13 +725,48 @@ class ProjectUpdate extends Command
629 $parent_id = $parent['id']; 725 $parent_id = $parent['id'];
630 } 726 }
631 727
632 - if (!empty($item['children'])) { 728 + if (!empty($item['children'] ?? [])) {
633 $this->category_news_insert($project_id, $item['children'], $parent_id); 729 $this->category_news_insert($project_id, $item['children'], $parent_id);
634 } 730 }
635 } 731 }
636 } 732 }
637 } 733 }
638 734
  735 + //扩展模块多级分类入库
  736 + protected function category_custom_insert($project_id, $module_id, $items, $pid = 0)
  737 + {
  738 + $model = new CustomModuleCategory();
  739 + foreach ($items as $item) {
  740 + $route = $this->get_url_route($item['url'] ?? '');
  741 + if ($route) {
  742 + $parent = $model->read(['pid' => $pid, 'route' => $route], 'id');
  743 + if (!$parent) {
  744 + try {
  745 + $item['name'] = $this->special2str($item['name'] ?? '');
  746 + $parent_id = $model->addReturnId([
  747 + 'project_id' => $project_id,
  748 + 'module_id' => $module_id,
  749 + 'name' => $item['name'],
  750 + 'pid' => $pid,
  751 + 'original_id' => $item['id'],
  752 + 'route' => $route
  753 + ]);
  754 + $this->set_map($route, RouteMap::SOURCE_MODULE_CATE, $parent_id, $project_id);
  755 + } catch (\Exception $e) {
  756 + echo 'date:' . date('Y-m-d H:i:s') . ', category_custom_insert error: ' . $e->getMessage() . PHP_EOL;
  757 + continue;
  758 + }
  759 + } else {
  760 + $parent_id = $parent['id'];
  761 + }
  762 +
  763 + if (!empty($item['children'] ?? [])) {
  764 + $this->category_custom_insert($project_id, $module_id, $item['children'], $parent_id);
  765 + }
  766 + }
  767 + }
  768 + }
  769 +
639 //特殊字符转换 770 //特殊字符转换
640 protected function special2str($str) 771 protected function special2str($str)
641 { 772 {