作者 lyh

gx

@@ -3,7 +3,10 @@ @@ -3,7 +3,10 @@
3 namespace App\Http\Controllers\Api; 3 namespace App\Http\Controllers\Api;
4 4
5 use App\Enums\Common\Code; 5 use App\Enums\Common\Code;
  6 +use App\Helper\Translate;
  7 +use App\Models\Product\Category;
6 use App\Models\Product\Product; 8 use App\Models\Product\Product;
  9 +use App\Models\RouteMap\RouteMap;
7 use App\Services\CosService; 10 use App\Services\CosService;
8 use App\Services\ProjectServer; 11 use App\Services\ProjectServer;
9 use App\Utils\LogUtils; 12 use App\Utils\LogUtils;
@@ -79,19 +82,45 @@ class ProductController extends BaseController @@ -79,19 +82,45 @@ class ProductController extends BaseController
79 $gallery[] = ['url'=>$url,'alt'=>'']; 82 $gallery[] = ['url'=>$url,'alt'=>''];
80 } 83 }
81 } 84 }
  85 + //处理分类
  86 + $category_id = '';
  87 + if(isset($this->param['category_name']) && !empty($this->param['category_name'])){
  88 + $categoryModel = new Category();
  89 + $cateInfo = $categoryModel->read(['title'=>$this->param['category_name']]);
  90 + if($cateInfo === false){
  91 + $cateData =[
  92 + 'project_id'=>$this->param['project_id'],
  93 + 'title'=>$this->param['category_name'],
  94 + 'pid'=>0,
  95 + 'created_at'=>date('Y-m-d H:i:s'),
  96 + 'updated_at'=>date('Y-m-d H:i:s')
  97 + ];
  98 + $cate_id = $categoryModel->addReturnId($cateData);
  99 + $route = RouteMap::setRoute($cateData['title'], RouteMap::SOURCE_PRODUCT_CATE, $cate_id, $this->param['project_id']);
  100 + $categoryModel->edit(['route'=>$route],['id'=>$cate_id]);
  101 + }else{
  102 + $cate_id = $cateInfo['id'];
  103 + }
  104 + $category_id = ','.$cate_id.',';
  105 + }
82 try { 106 try {
83 - $product = Product::where('title', $this->param['title'])->first();  
84 - if(!$product){  
85 - $param = [ 107 + $productModel = new Product();
  108 + $productInfo = $productModel->read(['title'=>$this->param['title']]);
  109 + if($productInfo === false){
  110 + $productData = [
86 'project_id'=>$this->param['project_id'], 111 'project_id'=>$this->param['project_id'],
87 'title'=>$this->param['title'], 112 'title'=>$this->param['title'],
88 'intro'=>$this->param['intro'] ?? '', 113 'intro'=>$this->param['intro'] ?? '',
89 'content'=>$this->param['content'] ?? '', 114 'content'=>$this->param['content'] ?? '',
90 'thumb'=>json_encode($thumb,true), 115 'thumb'=>json_encode($thumb,true),
91 'gallery'=>json_encode($gallery,true), 116 'gallery'=>json_encode($gallery,true),
  117 + 'category_id'=>$category_id,
  118 + 'created_at'=>date('Y-m-d H:i:s'),
  119 + 'updated_at'=>date('Y-m-d H:i:s')
92 ]; 120 ];
93 - $productModel = new Product();  
94 - $productModel->add($param); 121 + $product_id = $productModel->addReturnId($productData);
  122 + $route = RouteMap::setRoute($productData['title'], RouteMap::SOURCE_PRODUCT, $product_id, $this->param['project_id']);
  123 + $productModel->edit(['route'=>$route],['id'=>$cate_id]);
95 } 124 }
96 } catch (\Exception $e) { 125 } catch (\Exception $e) {
97 LogUtils::error('Project Id: ' . $this->param['project_id'] . ' saveProduct error:' . $e->getMessage()); 126 LogUtils::error('Project Id: ' . $this->param['project_id'] . ' saveProduct error:' . $e->getMessage());