作者 张关杰

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

@@ -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;
@@ -52,12 +55,16 @@ class ProductController extends BaseController @@ -52,12 +55,16 @@ class ProductController extends BaseController
52 * @time :2024/3/20 11:09 55 * @time :2024/3/20 11:09
53 */ 56 */
54 public function saveProduct(){ 57 public function saveProduct(){
  58 + $api_key = '8242LYUGaOfUQ1koc4Rq6MhEEOG7NW68oRaB7iO9coJDjG5L5gA1Q';
  59 + if($this->request->header('api-key') != $api_key){
  60 + $this->response('非法请求',Code::SYSTEM_ERROR);
  61 + }
55 $this->request->validate([ 62 $this->request->validate([
56 'project_id'=>'required', 63 'project_id'=>'required',
57 'title'=>'required', 64 'title'=>'required',
58 ],[ 65 ],[
59 'project_id.required' => 'project_id不能为空', 66 'project_id.required' => 'project_id不能为空',
60 - 'title.required' => 'keyword不能为空', 67 + 'title.required' => 'title不能为空',
61 ]); 68 ]);
62 $project = ProjectServer::useProject($this->param['project_id']); 69 $project = ProjectServer::useProject($this->param['project_id']);
63 if (!$project) { 70 if (!$project) {
@@ -65,7 +72,7 @@ class ProductController extends BaseController @@ -65,7 +72,7 @@ class ProductController extends BaseController
65 } 72 }
66 $gallery = []; 73 $gallery = [];
67 $thumb = []; 74 $thumb = [];
68 - if(!empty($this->param['image']) && in_array($this->param['image'])){ 75 + if(!empty($this->param['image']) && is_array($this->param['image'])){
69 foreach ($this->param['image'] as $k => $v){ 76 foreach ($this->param['image'] as $k => $v){
70 //TODO::图片转存 77 //TODO::图片转存
71 $url = CosService::uploadRemote($this->param['project_id'],'image_product',$v); 78 $url = CosService::uploadRemote($this->param['project_id'],'image_product',$v);
@@ -75,17 +82,48 @@ class ProductController extends BaseController @@ -75,17 +82,48 @@ class ProductController extends BaseController
75 $gallery[] = ['url'=>$url,'alt'=>'']; 82 $gallery[] = ['url'=>$url,'alt'=>''];
76 } 83 }
77 } 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 + }
78 try { 106 try {
79 - $param = [  
80 - 'project_id'=>$this->param['project_id'],  
81 - 'title'=>$this->param['title'],  
82 - 'intro'=>$this->param['intro'] ?? '',  
83 - 'content'=>$this->param['content'] ?? '',  
84 - 'thumb'=>json_encode($thumb,true),  
85 - 'gallery'=>json_encode($gallery,true),  
86 - ];  
87 $productModel = new Product(); 107 $productModel = new Product();
88 - $productModel->add($param); 108 + $productInfo = $productModel->read(['title'=>$this->param['title']]);
  109 + if($productInfo === false){
  110 + $productData = [
  111 + 'project_id'=>$this->param['project_id'],
  112 + 'title'=>$this->param['title'],
  113 + 'intro'=>$this->param['intro'] ?? '',
  114 + 'content'=>$this->param['content'] ?? '',
  115 + 'thumb'=>json_encode($thumb,true),
  116 + 'gallery'=>json_encode($gallery,true),
  117 + 'created_at'=>date('Y-m-d H:i:s'),
  118 + 'updated_at'=>date('Y-m-d H:i:s')
  119 + ];
  120 + if(!empty($category_id)){
  121 + $productData['category_id'] = $category_id;
  122 + }
  123 + $product_id = $productModel->addReturnId($productData);
  124 + $route = RouteMap::setRoute($productData['title'], RouteMap::SOURCE_PRODUCT, $product_id, $this->param['project_id']);
  125 + $productModel->edit(['route'=>$route],['id'=>$cate_id]);
  126 + }
89 } catch (\Exception $e) { 127 } catch (\Exception $e) {
90 LogUtils::error('Project Id: ' . $this->param['project_id'] . ' saveProduct error:' . $e->getMessage()); 128 LogUtils::error('Project Id: ' . $this->param['project_id'] . ' saveProduct error:' . $e->getMessage());
91 $this->response('保存失败,请联系管理员',Code::SYSTEM_ERROR); 129 $this->response('保存失败,请联系管理员',Code::SYSTEM_ERROR);