作者 刘锟

升级项目新增分类描述采集

@@ -780,6 +780,26 @@ class ProjectUpdate extends Command @@ -780,6 +780,26 @@ class ProjectUpdate extends Command
780 $image = $item['images'] ?? ''; 780 $image = $item['images'] ?? '';
781 } 781 }
782 $new_img = $this->source_download($image, $project_id, $domain, $web_url_domain, $home_url); 782 $new_img = $this->source_download($image, $project_id, $domain, $web_url_domain, $home_url);
  783 + //描述
  784 + if (isset($item['description'])) {
  785 + //匹配描述资源
  786 + $source_list = $this->html_preg($item['description'], $project_id, $domain, $web_url_domain, $home_url);
  787 + if ($source_list) {
  788 + foreach ($source_list as $vs) {
  789 + if ($vs['download']) {
  790 + //需要下载资源
  791 + $down_url = CosService::uploadRemote($project_id, 'image_product_category', $vs['url_complete']);
  792 + if ($down_url) {
  793 + $item['description'] = str_replace($vs['url'], $down_url, $item['description']);
  794 + }
  795 + } else {
  796 + //已经下载过资源
  797 + $item['description'] = str_replace($vs['url'], $vs['url_complete'], $item['description']);
  798 + }
  799 + break;
  800 + }
  801 + }
  802 + }
783 $parent_id = $model->addReturnId([ 803 $parent_id = $model->addReturnId([
784 'project_id' => $project_id, 804 'project_id' => $project_id,
785 'title' => $item['name'], 805 'title' => $item['name'],
@@ -978,4 +998,101 @@ class ProjectUpdate extends Command @@ -978,4 +998,101 @@ class ProjectUpdate extends Command
978 return $key . $i; 998 return $key . $i;
979 } 999 }
980 } 1000 }
  1001 +
  1002 + //正则匹配html资源
  1003 + protected function html_preg($html, $project_id, $domain, $web_url_domain, $home_url)
  1004 + {
  1005 + $source = [];
  1006 +
  1007 + if (!$html) {
  1008 + return $source;
  1009 + }
  1010 +
  1011 + //image
  1012 + preg_match_all('/<img\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i', $html, $result_img);
  1013 + $img = $result_img[2] ?? [];
  1014 + foreach ($img as $vi) {
  1015 + $check_vi = $this->url_check($vi, $project_id, $domain, $web_url_domain, $home_url);
  1016 + if ($check_vi && (!in_array($check_vi, $source))) {
  1017 + $source[] = $check_vi;
  1018 + }
  1019 + }
  1020 +
  1021 + //video
  1022 + preg_match_all('/<source\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i', $html, $result_video);
  1023 + $video = $result_video[2] ?? [];
  1024 + foreach ($video as $vv) {
  1025 + $check_vv = $this->url_check($vv, $project_id, $domain, $web_url_domain, $home_url);
  1026 + if ($check_vv && (!in_array($check_vv, $source))) {
  1027 + $source[] = $check_vv;
  1028 + }
  1029 + }
  1030 + preg_match_all('/<video\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i', $html, $result_video_2);
  1031 + $video_2 = $result_video_2[2] ?? [];
  1032 + foreach ($video_2 as $vv2) {
  1033 + $check_vv2 = $this->url_check($vv2, $project_id, $domain, $web_url_domain, $home_url);
  1034 + if ($check_vv2 && (!in_array($check_vv2, $source))) {
  1035 + $source[] = $check_vv2;
  1036 + }
  1037 + }
  1038 +
  1039 + //a标签下载资源
  1040 + preg_match_all('/<a\s+[^>]*?href\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i', $html, $result_a);
  1041 + $down = $result_a[2] ?? [];
  1042 + foreach ($down as $vd) {
  1043 + $check_vd = $this->url_check($vd, $project_id, $domain, $web_url_domain, $home_url);
  1044 + if ($check_vd && (!in_array($check_vd, $source))) {
  1045 + $source[] = $check_vd;
  1046 + }
  1047 + }
  1048 +
  1049 + return $source;
  1050 + }
  1051 +
  1052 + //判断资源是否需要下载
  1053 + protected function url_check($url, $project_id, $domain, $web_url_domain, $home_url)
  1054 + {
  1055 + $url = trim($url);
  1056 + if ($url) {
  1057 + $url = str_replace('&quot;', '', $url);
  1058 + $arr = parse_url($url);
  1059 + $scheme = $arr['scheme'] ?? '';
  1060 + $host = $arr['host'] ?? '';
  1061 + $path = $arr['path'] ?? '';
  1062 + $query = $arr['query'] ?? '';
  1063 +
  1064 + $path_arr = explode('.', $path);
  1065 + $path_end = end($path_arr);
  1066 + if (
  1067 + (empty($scheme) || $scheme == 'https' || $scheme == 'http')
  1068 + && (empty($host) || (strpos($web_url_domain, $host) !== false) || (strpos($home_url, $host) !== false))
  1069 + && $path
  1070 + && (substr($path, 0, 1) == '/')
  1071 + && (strpos($path, '.') !== false)
  1072 + && (strpos($path_end, 'html') === false)
  1073 + && (strpos($path_end, 'php') === false)
  1074 + && (strpos($path_end, 'com') === false)
  1075 + && (strpos($path_end, 'xml') === false)
  1076 + ) {
  1077 + $source = CollectSource::where('project_id', $project_id)->where('origin', $url)->first();
  1078 + if (!$source) {
  1079 + return [
  1080 + 'download' => true,
  1081 + 'url' => $url,
  1082 + 'url_complete' => ($scheme ?: 'https') . '://' . $domain . $path . ($query ? '?' . $query : '')
  1083 + ];
  1084 + } else {
  1085 + return [
  1086 + 'download' => false,
  1087 + 'url' => $url,
  1088 + 'url_complete' => $source['target']
  1089 + ];
  1090 + }
  1091 + } else {
  1092 + return false;
  1093 + }
  1094 + } else {
  1095 + return false;
  1096 + }
  1097 + }
981 } 1098 }
@@ -35,14 +35,14 @@ class ProjectServer @@ -35,14 +35,14 @@ class ProjectServer
35 return false; 35 return false;
36 } 36 }
37 // 设置 database.connections.custom_mysql 配置 37 // 设置 database.connections.custom_mysql 配置
38 - config(['database.connections.custom_mysql.host' => $project->mysqlConfig->host]);  
39 - config(['database.connections.custom_mysql.port' => $project->mysqlConfig->port]);  
40 - config(['database.connections.custom_mysql.database' => $project->databaseName()]);  
41 - config(['database.connections.custom_mysql.username' => $project->mysqlConfig->user]);  
42 - config(['database.connections.custom_mysql.password' => $project->mysqlConfig->password]); 38 + config(['database.connections.custom_mysql.host' => '127.0.0.1']);
  39 + config(['database.connections.custom_mysql.port' => '3306']);
  40 + config(['database.connections.custom_mysql.database' => 'gl_data_24']);
  41 + config(['database.connections.custom_mysql.username' => 'root']);
  42 + config(['database.connections.custom_mysql.password' => 'root']);
43 43
44 //清除现有的数据库连接配置 44 //清除现有的数据库连接配置
45 - DB::purge('custom_mysql'); 45 + DB::purge('custom');
46 //重连 46 //重连
47 DB::connection('custom_mysql')->reconnect(); 47 DB::connection('custom_mysql')->reconnect();
48 48