作者 刘锟

update

@@ -122,19 +122,19 @@ class ProjectImport extends Command @@ -122,19 +122,19 @@ class ProjectImport extends Command
122 $total_count += 1; 122 $total_count += 1;
123 try { 123 try {
124 if ($task->type == ImportTask::TYPE_NEWS) { 124 if ($task->type == ImportTask::TYPE_NEWS) {
125 - if ((new NewsLogic())->importNews($task->project_id, $task->user_id, $v)) { 125 + if ((new NewsLogic())->importNews($task->project_id, $task->user_id, $task->domain, $v)) {
126 $success_count += 1; 126 $success_count += 1;
127 } else { 127 } else {
128 $repeat_count += 1; 128 $repeat_count += 1;
129 } 129 }
130 } elseif ($task->type == ImportTask::TYPE_BLOG) { 130 } elseif ($task->type == ImportTask::TYPE_BLOG) {
131 - if ((new BlogLogic())->importBlog($task->project_id, $task->user_id, $v)) { 131 + if ((new BlogLogic())->importBlog($task->project_id, $task->user_id, $task->domain, $v)) {
132 $success_count += 1; 132 $success_count += 1;
133 } else { 133 } else {
134 $repeat_count += 1; 134 $repeat_count += 1;
135 } 135 }
136 } else { 136 } else {
137 - if ((new ProductLogic())->importProduct($task->project_id, $task->user_id, $v)) { 137 + if ((new ProductLogic())->importProduct($task->project_id, $task->user_id, $task->domain, $v)) {
138 $success_count += 1; 138 $success_count += 1;
139 } else { 139 } else {
140 $repeat_count += 1; 140 $repeat_count += 1;
@@ -654,40 +654,41 @@ function redis_add($key,$val,$ttl=3600){ @@ -654,40 +654,41 @@ function redis_add($key,$val,$ttl=3600){
654 /** 654 /**
655 * 判断远程地址是否需要下载 655 * 判断远程地址是否需要下载
656 * @param $url 656 * @param $url
  657 + * @param $project_id
  658 + * @param $domain
  659 + * @param $is_complete
657 * @author Akun 660 * @author Akun
658 * @return bool 661 * @return bool
659 * @date 2023/12/08 14:17 662 * @date 2023/12/08 14:17
660 */ 663 */
661 -function check_remote_url_down($url){  
662 - if(!$url){  
663 - return false;  
664 - } 664 +function check_remote_url_down($url,$project_id,$domain,$is_complete=0){
665 665
666 - if(substr($url,0,2) == '//'){  
667 - return false; 666 + if (!$url) {
  667 + return '';
668 } 668 }
669 669
670 $arr = parse_url($url); 670 $arr = parse_url($url);
671 $scheme = $arr['scheme'] ?? ''; 671 $scheme = $arr['scheme'] ?? '';
672 $host = $arr['host'] ?? ''; 672 $host = $arr['host'] ?? '';
673 - $path = $arr['path'] ?? '';  
674 -  
675 - if($scheme && !in_array($scheme,['http','https'])){  
676 - return false;  
677 - }  
678 -  
679 - if(!$host){  
680 - return false;  
681 - }  
682 -  
683 $host_arr = explode('.',$host); 673 $host_arr = explode('.',$host);
684 - if(strpos($host_arr[0],'cdn') !== false){  
685 - return false;  
686 - } 674 + $path = $arr['path'] ?? '';
687 675
688 - if(strpos($path, '.') === false){ 676 + $url_complete = ($scheme ?: 'https') . '://' . ($host ?: $domain) . $path;
  677 +
  678 + if (
  679 + (empty($scheme) || $scheme == 'https' || $scheme == 'http')
  680 + && (empty($host) || (strpos($host_arr[0], 'cdn') === false))
  681 + && $path
  682 + && (substr($path, 0, 1) == '/')
  683 + && (strpos($path, '.') !== false)
  684 + ) {
  685 + $new_url = CosService::uploadRemote($project_id,'image_product',$url_complete);
  686 + if($new_url){
  687 + return $is_complete ? getImageUrl($new_url) : $new_url;
  688 + }else{
  689 + return false;
  690 + }
  691 + }else{
689 return false; 692 return false;
690 } 693 }
691 -  
692 - return true;  
693 } 694 }
@@ -224,13 +224,14 @@ class BlogLogic extends BaseLogic @@ -224,13 +224,14 @@ class BlogLogic extends BaseLogic
224 * 博客导入 224 * 博客导入
225 * @param $project_id 225 * @param $project_id
226 * @param $user_id 226 * @param $user_id
  227 + * @param $domain
227 * @param $data 228 * @param $data
228 * @return bool 229 * @return bool
229 * @throws \Exception 230 * @throws \Exception
230 * @author Akun 231 * @author Akun
231 * @date 2023/09/20 17:51 232 * @date 2023/09/20 17:51
232 */ 233 */
233 - public function importBlog($project_id, $user_id, $data) 234 + public function importBlog($project_id, $user_id, $domain, $data)
234 { 235 {
235 $route = $data[1]??'';//自定义路由 236 $route = $data[1]??'';//自定义路由
236 if($route){ 237 if($route){
@@ -253,7 +254,8 @@ class BlogLogic extends BaseLogic @@ -253,7 +254,8 @@ class BlogLogic extends BaseLogic
253 preg_match_all('/<img\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i', $data[4], $result); 254 preg_match_all('/<img\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i', $data[4], $result);
254 if($result[2]??[]){ 255 if($result[2]??[]){
255 foreach ($result[2] as $img){ 256 foreach ($result[2] as $img){
256 - check_remote_url_down($img) && $data[4] = str_replace($img,getImageUrl(CosService::uploadRemote($project_id,'image_news',$img)),$data[4]); 257 + $new_img = check_remote_url_down($img,$project_id,$domain,1);
  258 + $new_img && $data[4] = str_replace($img,$new_img,$data[4]);
257 } 259 }
258 } 260 }
259 261
@@ -261,7 +263,8 @@ class BlogLogic extends BaseLogic @@ -261,7 +263,8 @@ class BlogLogic extends BaseLogic
261 preg_match_all('/<source\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i', $data[4], $result_video); 263 preg_match_all('/<source\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i', $data[4], $result_video);
262 if($result_video[2]??[]){ 264 if($result_video[2]??[]){
263 foreach ($result_video[2] as $video){ 265 foreach ($result_video[2] as $video){
264 - check_remote_url_down($video) && $data[4] = str_replace($video,getImageUrl(CosService::uploadRemote($project_id,'image_news',$video)),$data[4]); 266 + $new_video = check_remote_url_down($video,$project_id,$domain,1);
  267 + $new_video && $data[4] = str_replace($video,$new_video,$data[4]);
265 } 268 }
266 } 269 }
267 270
@@ -270,7 +273,10 @@ class BlogLogic extends BaseLogic @@ -270,7 +273,10 @@ class BlogLogic extends BaseLogic
270 273
271 $img = ''; 274 $img = '';
272 if($data[5]??''){ 275 if($data[5]??''){
273 - $img = check_remote_url_down($data[5]) ? CosService::uploadRemote($project_id, 'image_news', $data[5]) : $data[5]; 276 + $one_img = check_remote_url_down($data[5],$project_id,$domain);
  277 + if($one_img){
  278 + $img = $one_img;
  279 + }
274 } 280 }
275 281
276 $seo_title = ''; 282 $seo_title = '';
@@ -257,13 +257,14 @@ class NewsLogic extends BaseLogic @@ -257,13 +257,14 @@ class NewsLogic extends BaseLogic
257 * 新闻导入 257 * 新闻导入
258 * @param $project_id 258 * @param $project_id
259 * @param $user_id 259 * @param $user_id
  260 + * @param $domain
260 * @param $data 261 * @param $data
261 * @return bool 262 * @return bool
262 * @throws \Exception 263 * @throws \Exception
263 * @author Akun 264 * @author Akun
264 * @date 2023/09/20 17:51 265 * @date 2023/09/20 17:51
265 */ 266 */
266 - public function importNews($project_id, $user_id, $data) 267 + public function importNews($project_id, $user_id, $domain, $data)
267 { 268 {
268 $route = $data[1]??'';//自定义路由 269 $route = $data[1]??'';//自定义路由
269 if($route){ 270 if($route){
@@ -286,7 +287,8 @@ class NewsLogic extends BaseLogic @@ -286,7 +287,8 @@ class NewsLogic extends BaseLogic
286 preg_match_all('/<img\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i', $data[4], $result); 287 preg_match_all('/<img\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i', $data[4], $result);
287 if($result[2]??[]){ 288 if($result[2]??[]){
288 foreach ($result[2] as $img){ 289 foreach ($result[2] as $img){
289 - check_remote_url_down($img) && $data[4] = str_replace($img,getImageUrl(CosService::uploadRemote($project_id,'image_news',$img)),$data[4]); 290 + $new_img = check_remote_url_down($img,$project_id,$domain,1);
  291 + $new_img && $data[4] = str_replace($img,$new_img,$data[4]);
290 } 292 }
291 } 293 }
292 294
@@ -294,7 +296,8 @@ class NewsLogic extends BaseLogic @@ -294,7 +296,8 @@ class NewsLogic extends BaseLogic
294 preg_match_all('/<source\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i', $data[4], $result_video); 296 preg_match_all('/<source\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i', $data[4], $result_video);
295 if($result_video[2]??[]){ 297 if($result_video[2]??[]){
296 foreach ($result_video[2] as $video){ 298 foreach ($result_video[2] as $video){
297 - check_remote_url_down($video) && $data[4] = str_replace($video,getImageUrl(CosService::uploadRemote($project_id,'image_news',$video)),$data[4]); 299 + $new_video = check_remote_url_down($video,$project_id,$domain,1);
  300 + $new_video && $data[4] = str_replace($video,$new_video,$data[4]);
298 } 301 }
299 } 302 }
300 303
@@ -303,7 +306,10 @@ class NewsLogic extends BaseLogic @@ -303,7 +306,10 @@ class NewsLogic extends BaseLogic
303 306
304 $img = ''; 307 $img = '';
305 if($data[5]??''){ 308 if($data[5]??''){
306 - $img = check_remote_url_down($data[5]) ? CosService::uploadRemote($project_id, 'image_news', $data[5]) : $data[5]; 309 + $one_img = check_remote_url_down($data[5],$project_id,$domain);
  310 + if($one_img){
  311 + $img = $one_img;
  312 + }
307 } 313 }
308 314
309 $seo_title = ''; 315 $seo_title = '';
@@ -584,13 +584,14 @@ class ProductLogic extends BaseLogic @@ -584,13 +584,14 @@ class ProductLogic extends BaseLogic
584 * 产品导入 584 * 产品导入
585 * @param $project_id 585 * @param $project_id
586 * @param $user_id 586 * @param $user_id
  587 + * @param $domain
587 * @param $data 588 * @param $data
588 * @return bool 589 * @return bool
589 * @throws \Exception 590 * @throws \Exception
590 * @author Akun 591 * @author Akun
591 * @date 2023/09/21 14:55 592 * @date 2023/09/21 14:55
592 */ 593 */
593 - public function importProduct($project_id, $user_id, $data) 594 + public function importProduct($project_id, $user_id, $domain, $data)
594 { 595 {
595 $route = $data[1]??'';//自定义路由 596 $route = $data[1]??'';//自定义路由
596 if($route){ 597 if($route){
@@ -622,7 +623,7 @@ class ProductLogic extends BaseLogic @@ -622,7 +623,7 @@ class ProductLogic extends BaseLogic
622 $img_arr = explode('^v6sp$',$data[7]); 623 $img_arr = explode('^v6sp$',$data[7]);
623 foreach ($img_arr as $v_img){ 624 foreach ($img_arr as $v_img){
624 if($v_img){ 625 if($v_img){
625 - $one_img = check_remote_url_down($v_img) ? CosService::uploadRemote($project_id,'image_product',$v_img) : $v_img; 626 + $one_img = check_remote_url_down($v_img,$project_id,$domain);
626 if($one_img){ 627 if($one_img){
627 $one_gallery = [ 628 $one_gallery = [
628 'alt' => '这是一张产品图', 629 'alt' => '这是一张产品图',
@@ -645,7 +646,8 @@ class ProductLogic extends BaseLogic @@ -645,7 +646,8 @@ class ProductLogic extends BaseLogic
645 preg_match_all('/<img\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i', $data[5], $result_intro); 646 preg_match_all('/<img\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i', $data[5], $result_intro);
646 if($result_intro[2]??[]){ 647 if($result_intro[2]??[]){
647 foreach ($result_intro[2] as $vi_img){ 648 foreach ($result_intro[2] as $vi_img){
648 - check_remote_url_down($vi_img) && $data[5] = str_replace($vi_img,getImageUrl(CosService::uploadRemote($project_id,'image_product',$vi_img)),$data[5]); 649 + $new_vi_img = check_remote_url_down($vi_img,$project_id,$domain,1);
  650 + $new_vi_img && $data[5] = str_replace($vi_img,$new_vi_img,$data[5]);
649 } 651 }
650 } 652 }
651 653
@@ -653,7 +655,8 @@ class ProductLogic extends BaseLogic @@ -653,7 +655,8 @@ class ProductLogic extends BaseLogic
653 preg_match_all('/<source\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i', $data[5], $result_intro_video); 655 preg_match_all('/<source\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i', $data[5], $result_intro_video);
654 if($result_intro_video[2]??[]){ 656 if($result_intro_video[2]??[]){
655 foreach ($result_intro_video[2] as $vi_video){ 657 foreach ($result_intro_video[2] as $vi_video){
656 - check_remote_url_down($vi_video) && $data[5] = str_replace($vi_video,getImageUrl(CosService::uploadRemote($project_id,'image_product',$vi_video)),$data[5]); 658 + $new_vi_video = check_remote_url_down($vi_video,$project_id,$domain,1);
  659 + $new_vi_video && $data[5] = str_replace($vi_video,$new_vi_video,$data[5]);
657 } 660 }
658 } 661 }
659 662
@@ -666,7 +669,8 @@ class ProductLogic extends BaseLogic @@ -666,7 +669,8 @@ class ProductLogic extends BaseLogic
666 preg_match_all('/<img\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i', $data[6], $result_content); 669 preg_match_all('/<img\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i', $data[6], $result_content);
667 if($result_content[2]??[]){ 670 if($result_content[2]??[]){
668 foreach ($result_content[2] as $vc_img){ 671 foreach ($result_content[2] as $vc_img){
669 - check_remote_url_down($vc_img) && $data[6] = str_replace($vc_img,getImageUrl(CosService::uploadRemote($project_id,'image_product',$vc_img)),$data[6]); 672 + $new_vc_img = check_remote_url_down($vc_img,$project_id,$domain,1);
  673 + $new_vc_img && $data[6] = str_replace($vc_img,$new_vc_img,$data[6]);
670 } 674 }
671 } 675 }
672 676
@@ -674,7 +678,8 @@ class ProductLogic extends BaseLogic @@ -674,7 +678,8 @@ class ProductLogic extends BaseLogic
674 preg_match_all('/<source\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i', $data[6], $result_content_video); 678 preg_match_all('/<source\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i', $data[6], $result_content_video);
675 if($result_content_video[2]??[]){ 679 if($result_content_video[2]??[]){
676 foreach ($result_content_video[2] as $vc_video){ 680 foreach ($result_content_video[2] as $vc_video){
677 - check_remote_url_down($vc_video) && $data[6] = str_replace($vc_video,getImageUrl(CosService::uploadRemote($project_id,'image_product',$vc_video)),$data[6]); 681 + $new_vc_video = check_remote_url_down($vc_video,$project_id,$domain,1);
  682 + $new_vc_video && $data[6] = str_replace($vc_video,$new_vc_video,$data[6]);
678 } 683 }
679 } 684 }
680 685
@@ -715,7 +720,8 @@ class ProductLogic extends BaseLogic @@ -715,7 +720,8 @@ class ProductLogic extends BaseLogic
715 720
716 if($result_desc[2]??[]){ 721 if($result_desc[2]??[]){
717 foreach ($result_desc[2] as $vdesc_img){ 722 foreach ($result_desc[2] as $vdesc_img){
718 - check_remote_url_down($vdesc_img) && $v_desc['text'] = str_replace($vdesc_img,getImageUrl(CosService::uploadRemote($project_id,'image_product',$vdesc_img)),$v_desc['text']); 723 + $new_vdesc_img = check_remote_url_down($vdesc_img,$project_id,$domain,1);
  724 + $new_vdesc_img && $v_desc['text'] = str_replace($vdesc_img,$new_vdesc_img,$v_desc['text']);
719 } 725 }
720 } 726 }
721 727
@@ -723,19 +729,32 @@ class ProductLogic extends BaseLogic @@ -723,19 +729,32 @@ class ProductLogic extends BaseLogic
723 preg_match_all('/<source\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i', $v_desc['text'], $result_desc_video); 729 preg_match_all('/<source\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i', $v_desc['text'], $result_desc_video);
724 if($result_desc_video[2]??[]){ 730 if($result_desc_video[2]??[]){
725 foreach ($result_desc_video[2] as $vdesc_video){ 731 foreach ($result_desc_video[2] as $vdesc_video){
726 - check_remote_url_down($vdesc_video) && $v_desc['text'] = str_replace($vdesc_video,getImageUrl(CosService::uploadRemote($project_id,'image_product',$vdesc_video)),$v_desc['text']); 732 + $new_vdesc_video = check_remote_url_down($vdesc_video,$project_id,$domain,1);
  733 + $new_vdesc_video && $v_desc['text'] = str_replace($vdesc_video,$new_vdesc_video,$v_desc['text']);
727 } 734 }
728 } 735 }
729 } 736 }
730 } 737 }
731 738
  739 + //处理参数
  740 + $attrs = [];
  741 + if($data[4]??''){
  742 + $attr_arr = json_decode($data[4],true);
  743 + foreach ($attr_arr as $ka=>$va){
  744 + $attrs[] = [
  745 + 'key' => $ka,
  746 + 'value' => $va
  747 + ];
  748 + }
  749 + }
  750 +
732 $id = $this->model->addReturnId( 751 $id = $this->model->addReturnId(
733 [ 752 [
734 'project_id' => $project_id, 753 'project_id' => $project_id,
735 'title' => $data[0], 754 'title' => $data[0],
736 'thumb' => $thumb, 755 'thumb' => $thumb,
737 'gallery' => Arr::a2s($gallery), 756 'gallery' => Arr::a2s($gallery),
738 - 'attrs' => trim($data[4]) ? $data[4] : Arr::a2s([]), 757 + 'attrs' => Arr::a2s($attrs),
739 'category_id' => $category_id, 758 'category_id' => $category_id,
740 'keyword_id' => $keyword_id, 759 'keyword_id' => $keyword_id,
741 'intro' => $intro, 760 'intro' => $intro,