作者 lyh

Merge branch 'develop' of http://47.244.231.31:8099/zhl/globalso-v6 into develop

@@ -50,6 +50,18 @@ class ProjectImport extends Command @@ -50,6 +50,18 @@ class ProjectImport extends Command
50 50
51 echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', import start' . PHP_EOL; 51 echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', import start' . PHP_EOL;
52 52
  53 + $is_gbk = 0;
  54 + $file_code_type = $this->get_code_type($task->file_url);
  55 + if ($file_code_type === false) {
  56 + echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', import fail, error: 文件编码格式错误' . PHP_EOL;
  57 + $task->status = ImportTask::STATUS_COM;//导入完成
  58 + $task->save();
  59 + return true;
  60 + } elseif ($file_code_type === 'GBK') {
  61 + $is_gbk = 1;
  62 + setlocale(LC_ALL, 'zh_CN');
  63 + }
  64 +
53 //读取csv文件 65 //读取csv文件
54 $line_of_text = []; 66 $line_of_text = [];
55 try { 67 try {
@@ -67,7 +79,6 @@ class ProjectImport extends Command @@ -67,7 +79,6 @@ class ProjectImport extends Command
67 79
68 $total_count = 0; //总条数 80 $total_count = 0; //总条数
69 $success_count = 0; //成功导入条数 81 $success_count = 0; //成功导入条数
70 - setlocale(LC_ALL,'zh_CN');  
71 if (count($line_of_text) > 1) { 82 if (count($line_of_text) > 1) {
72 $task->status = ImportTask::STATUS_ING;//导入中 83 $task->status = ImportTask::STATUS_ING;//导入中
73 $task->save(); 84 $task->save();
@@ -78,8 +89,10 @@ class ProjectImport extends Command @@ -78,8 +89,10 @@ class ProjectImport extends Command
78 foreach ($line_of_text as $k => $v) { 89 foreach ($line_of_text as $k => $v) {
79 if ($k > 0 && isset($v[0]) && $v[0]) { 90 if ($k > 0 && isset($v[0]) && $v[0]) {
80 91
81 - foreach ($v as $kk=>$vv){  
82 - $v[$kk] = mb_convert_encoding($vv, 'utf-8', 'gbk'); 92 + if ($is_gbk) {
  93 + foreach ($v as $kk => $vv) {
  94 + $v[$kk] = mb_convert_encoding($vv, 'utf-8', 'gbk');
  95 + }
83 } 96 }
84 97
85 $total_count += 1; 98 $total_count += 1;
@@ -109,11 +122,25 @@ class ProjectImport extends Command @@ -109,11 +122,25 @@ class ProjectImport extends Command
109 122
110 $task->status = ImportTask::STATUS_COM;//导入完成 123 $task->status = ImportTask::STATUS_COM;//导入完成
111 $task->total_count = $total_count; 124 $task->total_count = $total_count;
112 - $task->success_count = $success_count; 125 + $task->success_count += $success_count;
113 $task->save(); 126 $task->save();
114 127
115 echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', import end, total count: ' . $total_count . ', success count: ' . $success_count . PHP_EOL; 128 echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', import end, total count: ' . $total_count . ', success count: ' . $success_count . PHP_EOL;
116 129
117 sleep(2); 130 sleep(2);
118 } 131 }
  132 +
  133 + //判断编码格式
  134 + function get_code_type($file)
  135 + {
  136 + $list = array('GBK', 'UTF-8');
  137 + $str = file_get_contents($file);
  138 + foreach ($list as $item) {
  139 + $tmp = mb_convert_encoding($str, $item, $item);
  140 + if (md5($tmp) == md5($str)) {
  141 + return $item;
  142 + }
  143 + }
  144 + return false;
  145 + }
119 } 146 }
@@ -270,15 +270,16 @@ class BlogLogic extends BaseLogic @@ -270,15 +270,16 @@ class BlogLogic extends BaseLogic
270 */ 270 */
271 public function importBlog($project_id, $user_id, $data) 271 public function importBlog($project_id, $user_id, $data)
272 { 272 {
273 - $category_id = '';  
274 - if ($data[2]) {  
275 - //处理分类  
276 - $blogCategoryLogic = new BlogCategoryLogic();  
277 - $category_id = $blogCategoryLogic->importBlogCategory($project_id, $user_id, $data[2]);  
278 - }  
279 -  
280 $blog = $this->model->read(['name' => $data[0]]); 273 $blog = $this->model->read(['name' => $data[0]]);
281 if (!$blog) { 274 if (!$blog) {
  275 +
  276 + $category_id = '';
  277 + if ($data[2]) {
  278 + //处理分类
  279 + $blogCategoryLogic = new BlogCategoryLogic();
  280 + $category_id = $blogCategoryLogic->importBlogCategory($project_id, $user_id, $data[2]);
  281 + }
  282 +
282 $id = $this->model->addReturnId( 283 $id = $this->model->addReturnId(
283 [ 284 [
284 'name' => $data[0], 285 'name' => $data[0],
@@ -269,15 +269,16 @@ class NewsLogic extends BaseLogic @@ -269,15 +269,16 @@ class NewsLogic extends BaseLogic
269 */ 269 */
270 public function importNews($project_id, $user_id, $data) 270 public function importNews($project_id, $user_id, $data)
271 { 271 {
272 - $category_id = '';  
273 - if ($data[2]) {  
274 - //处理分类  
275 - $newsCategoryLogic = new NewsCategoryLogic();  
276 - $category_id = $newsCategoryLogic->importNewsCategory($project_id, $user_id, $data[2]);  
277 - }  
278 -  
279 $news = $this->model->read(['name' => $data[0]]); 272 $news = $this->model->read(['name' => $data[0]]);
280 if (!$news) { 273 if (!$news) {
  274 +
  275 + $category_id = '';
  276 + if ($data[2]) {
  277 + //处理分类
  278 + $newsCategoryLogic = new NewsCategoryLogic();
  279 + $category_id = $newsCategoryLogic->importNewsCategory($project_id, $user_id, $data[2]);
  280 + }
  281 +
281 $id = $this->model->addReturnId( 282 $id = $this->model->addReturnId(
282 [ 283 [
283 'name' => $data[0], 284 'name' => $data[0],
@@ -381,65 +381,91 @@ class ProductLogic extends BaseLogic @@ -381,65 +381,91 @@ class ProductLogic extends BaseLogic
381 */ 381 */
382 public function importProduct($project_id, $user_id, $data) 382 public function importProduct($project_id, $user_id, $data)
383 { 383 {
384 - $category_id = '';  
385 - if ($data[2]) {  
386 - //处理分类  
387 - $categoryLogic = new CategoryLogic();  
388 - $category_id = $categoryLogic->importProductCategory($project_id, $data[2]);  
389 - } 384 + $product = $this->model->read(['title' => $data[0]]);
  385 + if (!$product) {
390 386
391 - $keyword_id = '';  
392 - if($data[3]){  
393 - //处理关键词  
394 - $keywordLogic = new KeywordLogic();  
395 - $keyword_id = $keywordLogic->importProductKeyword($project_id, $data[3]);  
396 - } 387 + $category_id = '';
  388 + if ($data[2]) {
  389 + //处理分类
  390 + $categoryLogic = new CategoryLogic();
  391 + $category_id = $categoryLogic->importProductCategory($project_id, $data[2]);
  392 + }
397 393
398 - $gallery = [];  
399 - $thumb = '';  
400 - if($data[7]){  
401 - //处理图片  
402 - $img_arr = explode(',',$data[7]);  
403 - foreach ($img_arr as $v_img){  
404 - if($v_img){  
405 - $one_img = CosService::uploadRemote($project_id,'image_product',$v_img);  
406 - if($one_img){  
407 - $one_gallery = [  
408 - 'alt' => '这是一张产品图',  
409 - 'url' => $one_img  
410 - ]; 394 + $keyword_id = '';
  395 + if($data[3]){
  396 + //处理关键词
  397 + $keywordLogic = new KeywordLogic();
  398 + $keyword_id = $keywordLogic->importProductKeyword($project_id, $data[3]);
  399 + }
  400 +
  401 + $gallery = [];
  402 + $thumb = '';
  403 + if($data[7]){
  404 + //处理图片
  405 + $img_arr = explode(',',$data[7]);
  406 + foreach ($img_arr as $v_img){
  407 + if($v_img){
  408 + $one_img = CosService::uploadRemote($project_id,'image_product',$v_img);
  409 + if($one_img){
  410 + $one_gallery = [
  411 + 'alt' => '这是一张产品图',
  412 + 'url' => $one_img
  413 + ];
  414 +
  415 + if(!$thumb){
  416 + $thumb = Arr::a2s($one_gallery);
  417 + }
411 418
412 - if(!$thumb){  
413 - $thumb = Arr::a2s($one_gallery); 419 + $gallery[] = $one_gallery;
414 } 420 }
  421 + }
  422 + }
  423 + }
415 424
416 - $gallery[] = $one_gallery; 425 + $intro = '';
  426 + if($data[5]){
  427 + //处理短描述中的图片
  428 + $pattern = '<img src="(.*?)">';
  429 + preg_match_all($pattern, $data[5], $result_intro);
  430 + if($result_intro[1]){
  431 + foreach ($result_intro[1] as $vi_img){
  432 + $data[5] = str_replace($vi_img,getImageUrl(CosService::uploadRemote($project_id,'image_product',$vi_img)),$data[5]);
417 } 433 }
418 } 434 }
  435 + $intro = $data[5];
419 } 436 }
420 - }  
421 437
  438 + $content = '';
  439 + if($data[6]){
  440 + //处理内容中的图片
  441 + $pattern = '<img src="(.*?)">';
  442 + preg_match_all($pattern, $data[6], $result_content);
  443 + if($result_content[1]){
  444 + foreach ($result_content[1] as $vc_img){
  445 + $data[6] = str_replace($vc_img,getImageUrl(CosService::uploadRemote($project_id,'image_product',$vc_img)),$data[6]);
  446 + }
  447 + }
  448 + $content = $data[6];
  449 + }
422 450
423 - //处理seo  
424 - $seo_mate = [  
425 - 'title' => $data[8]??'',  
426 - 'keyword' => $data[9]??'',  
427 - 'description' => $data[10]??''  
428 - ]; 451 + //处理seo
  452 + $seo_mate = [
  453 + 'title' => $data[8]??'',
  454 + 'keyword' => $data[9]??'',
  455 + 'description' => $data[10]??''
  456 + ];
429 457
430 - $product = $this->model->read(['title' => $data[0]]);  
431 - if (!$product) {  
432 $id = $this->model->addReturnId( 458 $id = $this->model->addReturnId(
433 [ 459 [
434 'project_id' => $project_id, 460 'project_id' => $project_id,
435 'title' => $data[0], 461 'title' => $data[0],
436 'thumb' => $thumb, 462 'thumb' => $thumb,
437 'gallery' => Arr::a2s($gallery), 463 'gallery' => Arr::a2s($gallery),
438 - 'attrs' => $data[4] ? $data[4] : Arr::a2s([]), 464 + 'attrs' => trim($data[4]) ? $data[4] : Arr::a2s([]),
439 'category_id' => $category_id, 465 'category_id' => $category_id,
440 'keyword_id' => $keyword_id, 466 'keyword_id' => $keyword_id,
441 - 'intro' => $data[5] ?? '',  
442 - 'content' => $data[6] ?? '', 467 + 'intro' => $intro,
  468 + 'content' => $content,
443 'seo_mate' => Arr::a2s($seo_mate), 469 'seo_mate' => Arr::a2s($seo_mate),
444 'created_uid' => $user_id, 470 'created_uid' => $user_id,
445 'status' => Product::STATUS_ON 471 'status' => Product::STATUS_ON
@@ -74,7 +74,6 @@ class CosService @@ -74,7 +74,6 @@ class CosService
74 */ 74 */
75 public static function uploadRemote($project_id,$image_type,$file_url) 75 public static function uploadRemote($project_id,$image_type,$file_url)
76 { 76 {
77 - return '';  
78 $ext = explode('.',$file_url); 77 $ext = explode('.',$file_url);
79 78
80 $filename = uniqid().rand(10000,99999).'.'.end($ext); 79 $filename = uniqid().rand(10000,99999).'.'.end($ext);