ProductController.php 3.6 KB
<?php

namespace App\Http\Controllers\Api;

use App\Enums\Common\Code;
use App\Models\Product\Product;
use App\Services\CosService;
use App\Services\ProjectServer;
use App\Utils\LogUtils;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;

/**
 * Class ProductController
 * @package App\Http\Controllers\Api
 * @author zbj
 * @date 2024/2/1
 */
class ProductController extends BaseController
{
    /**
     * 获取项目随机产品的图片
     * @param Request $request
     * @return void
     */
    public function getImages(Request $request)
    {
        $project_id = $request->input('project_id');
        $project = ProjectServer::useProject($project_id);
        if (!$project) {
            $this->response('项目不存在', Code::SYSTEM_ERROR);
        }

        try {
            $info = Product::where('status', Product::STATUS_ON)->whereNotNull('gallery')->inRandomOrder()->select(['title', 'seo_mate', 'gallery'])->first();
            $info['gallery'] = array_map(function ($item) use ($project) {
                 $item['url'] = getImageUrl($item['url'], $project['storage_type'], $project['project_location']);
                 return $item;
            }, $info['gallery']);
        } catch (\Exception $e) {
            LogUtils::error('Project Id: ' . $project_id . ' getProductImages error:' . $e->getMessage());
            $info = [];
        }
        $this->response('success', Code::SUCCESS, $info);
    }

    /**
     * @remark :保存产品
     * @name   :saveProduct
     * @author :lyh
     * @method :post
     * @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' => 'title不能为空',
        ]);
        $project = ProjectServer::useProject($this->param['project_id']);
        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'=>''];
            }
        }
        try {
            $product = Product::where('title', $this->param['title'])->first();
            if(!$product){
                $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);
            }
        } catch (\Exception $e) {
            LogUtils::error('Project Id: ' . $this->param['project_id'] . ' saveProduct error:' . $e->getMessage());
            $this->response('保存失败,请联系管理员',Code::SYSTEM_ERROR);
        }
        $this->response('success');
    }
}