作者 张关杰

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

... ... @@ -3,7 +3,10 @@
namespace App\Http\Controllers\Api;
use App\Enums\Common\Code;
use App\Helper\Translate;
use App\Models\Product\Category;
use App\Models\Product\Product;
use App\Models\RouteMap\RouteMap;
use App\Services\CosService;
use App\Services\ProjectServer;
use App\Utils\LogUtils;
... ... @@ -52,12 +55,16 @@ class ProductController extends BaseController
* @time :2024/3/20 11:09
*/
public function saveProduct(){
$api_key = '8242LYUGaOfUQ1koc4Rq6MhEEOG7NW68oRaB7iO9coJDjG5L5gA1Q';
if($this->request->header('api-key') != $api_key){
$this->response('非法请求',Code::SYSTEM_ERROR);
}
$this->request->validate([
'project_id'=>'required',
'title'=>'required',
],[
'project_id.required' => 'project_id不能为空',
'title.required' => 'keyword不能为空',
'title.required' => 'title不能为空',
]);
$project = ProjectServer::useProject($this->param['project_id']);
if (!$project) {
... ... @@ -65,7 +72,7 @@ class ProductController extends BaseController
}
$gallery = [];
$thumb = [];
if(!empty($this->param['image']) && in_array($this->param['image'])){
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);
... ... @@ -75,17 +82,48 @@ class ProductController extends BaseController
$gallery[] = ['url'=>$url,'alt'=>''];
}
}
//处理分类
$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.',';
}
try {
$param = [
'project_id'=>$this->param['project_id'],
'title'=>$this->param['title'],
'intro'=>$this->param['intro'] ?? '',
'content'=>$this->param['content'] ?? '',
'thumb'=>json_encode($thumb,true),
'gallery'=>json_encode($gallery,true),
];
$productModel = new Product();
$productModel->add($param);
$productInfo = $productModel->read(['title'=>$this->param['title']]);
if($productInfo === false){
$productData = [
'project_id'=>$this->param['project_id'],
'title'=>$this->param['title'],
'intro'=>$this->param['intro'] ?? '',
'content'=>$this->param['content'] ?? '',
'thumb'=>json_encode($thumb,true),
'gallery'=>json_encode($gallery,true),
'created_at'=>date('Y-m-d H:i:s'),
'updated_at'=>date('Y-m-d H:i:s')
];
if(!empty($category_id)){
$productData['category_id'] = $category_id;
}
$product_id = $productModel->addReturnId($productData);
$route = RouteMap::setRoute($productData['title'], RouteMap::SOURCE_PRODUCT, $product_id, $this->param['project_id']);
$productModel->edit(['route'=>$route],['id'=>$cate_id]);
}
} catch (\Exception $e) {
LogUtils::error('Project Id: ' . $this->param['project_id'] . ' saveProduct error:' . $e->getMessage());
$this->response('保存失败,请联系管理员',Code::SYSTEM_ERROR);
... ...