作者 lyh

gx

@@ -71,39 +71,12 @@ class ProductController extends BaseController @@ -71,39 +71,12 @@ class ProductController extends BaseController
71 if (!$project) { 71 if (!$project) {
72 $this->response('项目不存在', Code::SYSTEM_ERROR); 72 $this->response('项目不存在', Code::SYSTEM_ERROR);
73 } 73 }
74 - $gallery = [];  
75 - $thumb = [];  
76 - if(!empty($this->param['image']) && is_array($this->param['image'])){  
77 - foreach ($this->param['image'] as $k => $v){  
78 - //TODO::图片转存  
79 - $url = CosService::uploadRemote($this->param['project_id'],'image_product',$v);  
80 - if($k == 0){  
81 - $thumb = ['url'=>$url,'alt'=>''];  
82 - }  
83 - $gallery[] = ['url'=>$url,'alt'=>''];  
84 - }  
85 - } 74 + //处理图片
  75 + $imageInfo = $this->handleImage($this->param['image'],$this->param['project_id']);
  76 + $thumb = $imageInfo['thumb'];
  77 + $gallery = $imageInfo['gallery'];
86 //处理分类 78 //处理分类
87 - $category_id = '';  
88 - if(isset($this->param['category_name']) && !empty($this->param['category_name'])){  
89 - $categoryModel = new Category();  
90 - $cateInfo = $categoryModel->read(['title'=>$this->param['category_name']]);  
91 - if($cateInfo === false){  
92 - $cateData =[  
93 - 'project_id'=>$this->param['project_id'],  
94 - 'title'=>$this->param['category_name'],  
95 - 'pid'=>0,  
96 - 'created_at'=>date('Y-m-d H:i:s'),  
97 - 'updated_at'=>date('Y-m-d H:i:s')  
98 - ];  
99 - $cate_id = $categoryModel->addReturnId($cateData);  
100 - $route = RouteMap::setRoute($cateData['title'], RouteMap::SOURCE_PRODUCT_CATE, $cate_id, $this->param['project_id']);  
101 - $categoryModel->edit(['route'=>$route],['id'=>$cate_id]);  
102 - }else{  
103 - $cate_id = $cateInfo['id'];  
104 - }  
105 - $category_id = $cate_id;  
106 - } 79 + $category_id = $this->handleCategory($this->param['category_name'],$this->param['project_id']);
107 try { 80 try {
108 $productModel = new Product(); 81 $productModel = new Product();
109 $productInfo = $productModel->read(['title'=>$this->param['title']]); 82 $productInfo = $productModel->read(['title'=>$this->param['title']]);
@@ -125,7 +98,7 @@ class ProductController extends BaseController @@ -125,7 +98,7 @@ class ProductController extends BaseController
125 $route = RouteMap::setRoute($productData['title'], RouteMap::SOURCE_PRODUCT, $product_id, $this->param['project_id']); 98 $route = RouteMap::setRoute($productData['title'], RouteMap::SOURCE_PRODUCT, $product_id, $this->param['project_id']);
126 $productModel->edit(['route'=>$route],['id'=>$product_id]); 99 $productModel->edit(['route'=>$route],['id'=>$product_id]);
127 //添加到关联分类 100 //添加到关联分类
128 - CategoryRelated::saveRelated($product_id, [$cate_id]); 101 + CategoryRelated::saveRelated($product_id, [$category_id]);
129 } 102 }
130 } catch (\Exception $e) { 103 } catch (\Exception $e) {
131 LogUtils::error('Project Id: ' . $this->param['project_id'] . ' saveProduct error:' . $e->getMessage()); 104 LogUtils::error('Project Id: ' . $this->param['project_id'] . ' saveProduct error:' . $e->getMessage());
@@ -133,4 +106,58 @@ class ProductController extends BaseController @@ -133,4 +106,58 @@ class ProductController extends BaseController
133 } 106 }
134 $this->response('success'); 107 $this->response('success');
135 } 108 }
  109 +
  110 + /**
  111 + * @remark :处理图片
  112 + * @name :handleImage
  113 + * @author :lyh
  114 + * @method :post
  115 + * @time :2024/3/20 14:44
  116 + */
  117 + public function handleImage($image,$project_id){
  118 + $gallery = [];
  119 + $thumb = [];
  120 + if(!empty($image) && is_array($image)){
  121 + foreach ($image as $k => $v){
  122 + //TODO::图片转存
  123 + $url = CosService::uploadRemote($project_id,'image_product',$v);
  124 + if($k == 0){
  125 + $thumb = ['url'=>$url,'alt'=>''];
  126 + }
  127 + $gallery[] = ['url'=>$url,'alt'=>''];
  128 + }
  129 + }
  130 + return ['thumb'=>$thumb,'gallery'=>$gallery];
  131 + }
  132 +
  133 + /**
  134 + * @remark :处理分类
  135 + * @name :handleCategory
  136 + * @author :lyh
  137 + * @method :post
  138 + * @time :2024/3/20 14:48
  139 + */
  140 + public function handleCategory($category_name,$project_id){
  141 + $category_id = '';
  142 + if(isset($category_name) && !empty($category_name)){
  143 + $categoryModel = new Category();
  144 + $cateInfo = $categoryModel->read(['title'=>$this->param['category_name']]);
  145 + if($cateInfo === false){
  146 + $cateData =[
  147 + 'project_id'=>$project_id,
  148 + 'title'=>$category_name,
  149 + 'pid'=>0,
  150 + 'created_at'=>date('Y-m-d H:i:s'),
  151 + 'updated_at'=>date('Y-m-d H:i:s')
  152 + ];
  153 + $cate_id = $categoryModel->addReturnId($cateData);
  154 + $route = RouteMap::setRoute($cateData['title'], RouteMap::SOURCE_PRODUCT_CATE, $cate_id, $project_id);
  155 + $categoryModel->edit(['route'=>$route],['id'=>$cate_id]);
  156 + }else{
  157 + $cate_id = $cateInfo['id'];
  158 + }
  159 + $category_id = $cate_id;
  160 + }
  161 + return $category_id;
  162 + }
136 } 163 }