作者 lyh

gx

... ... @@ -71,39 +71,12 @@ class ProductController extends BaseController
if (!$project) {
$this->response('项目不存在', Code::SYSTEM_ERROR);
}
$gallery = [];
$thumb = [];
if(!empty($this->param['image']) && is_array($this->param['image'])){
foreach ($this->param['image'] as $k => $v){
//TODO::图片转存
$url = CosService::uploadRemote($this->param['project_id'],'image_product',$v);
if($k == 0){
$thumb = ['url'=>$url,'alt'=>''];
}
$gallery[] = ['url'=>$url,'alt'=>''];
}
}
//处理图片
$imageInfo = $this->handleImage($this->param['image'],$this->param['project_id']);
$thumb = $imageInfo['thumb'];
$gallery = $imageInfo['gallery'];
//处理分类
$category_id = '';
if(isset($this->param['category_name']) && !empty($this->param['category_name'])){
$categoryModel = new Category();
$cateInfo = $categoryModel->read(['title'=>$this->param['category_name']]);
if($cateInfo === false){
$cateData =[
'project_id'=>$this->param['project_id'],
'title'=>$this->param['category_name'],
'pid'=>0,
'created_at'=>date('Y-m-d H:i:s'),
'updated_at'=>date('Y-m-d H:i:s')
];
$cate_id = $categoryModel->addReturnId($cateData);
$route = RouteMap::setRoute($cateData['title'], RouteMap::SOURCE_PRODUCT_CATE, $cate_id, $this->param['project_id']);
$categoryModel->edit(['route'=>$route],['id'=>$cate_id]);
}else{
$cate_id = $cateInfo['id'];
}
$category_id = $cate_id;
}
$category_id = $this->handleCategory($this->param['category_name'],$this->param['project_id']);
try {
$productModel = new Product();
$productInfo = $productModel->read(['title'=>$this->param['title']]);
... ... @@ -125,7 +98,7 @@ class ProductController extends BaseController
$route = RouteMap::setRoute($productData['title'], RouteMap::SOURCE_PRODUCT, $product_id, $this->param['project_id']);
$productModel->edit(['route'=>$route],['id'=>$product_id]);
//添加到关联分类
CategoryRelated::saveRelated($product_id, [$cate_id]);
CategoryRelated::saveRelated($product_id, [$category_id]);
}
} catch (\Exception $e) {
LogUtils::error('Project Id: ' . $this->param['project_id'] . ' saveProduct error:' . $e->getMessage());
... ... @@ -133,4 +106,58 @@ class ProductController extends BaseController
}
$this->response('success');
}
/**
* @remark :处理图片
* @name :handleImage
* @author :lyh
* @method :post
* @time :2024/3/20 14:44
*/
public function handleImage($image,$project_id){
$gallery = [];
$thumb = [];
if(!empty($image) && is_array($image)){
foreach ($image as $k => $v){
//TODO::图片转存
$url = CosService::uploadRemote($project_id,'image_product',$v);
if($k == 0){
$thumb = ['url'=>$url,'alt'=>''];
}
$gallery[] = ['url'=>$url,'alt'=>''];
}
}
return ['thumb'=>$thumb,'gallery'=>$gallery];
}
/**
* @remark :处理分类
* @name :handleCategory
* @author :lyh
* @method :post
* @time :2024/3/20 14:48
*/
public function handleCategory($category_name,$project_id){
$category_id = '';
if(isset($category_name) && !empty($category_name)){
$categoryModel = new Category();
$cateInfo = $categoryModel->read(['title'=>$this->param['category_name']]);
if($cateInfo === false){
$cateData =[
'project_id'=>$project_id,
'title'=>$category_name,
'pid'=>0,
'created_at'=>date('Y-m-d H:i:s'),
'updated_at'=>date('Y-m-d H:i:s')
];
$cate_id = $categoryModel->addReturnId($cateData);
$route = RouteMap::setRoute($cateData['title'], RouteMap::SOURCE_PRODUCT_CATE, $cate_id, $project_id);
$categoryModel->edit(['route'=>$route],['id'=>$cate_id]);
}else{
$cate_id = $cateInfo['id'];
}
$category_id = $cate_id;
}
return $category_id;
}
}
... ...