ProductController.php 3.5 KB
<?php

namespace App\Http\Controllers\Bside\Product;

use App\Enums\Common\Code;
use App\Exceptions\BsideGlobalException;
use App\Helper\Arr;
use App\Http\Controllers\Bside\BaseController;
use App\Http\Logic\Bside\Product\ProductLogic;
use App\Http\Requests\Bside\product\ProductRequest;
use App\Rules\Ids;
use Illuminate\Http\Request;

/**
 * Class ProductController
 * @package App\Http\Controllers\Bside
 * @author zbj
 * @date 2023/4/17
 */
class ProductController extends BaseController
{

    public function index(ProductLogic $logic)
    {
        $map = [];
        if(!empty($this->param['search'])){
            $map[] = ['title', 'like', "%{$this->param['search']}%"];
        }
        if(!empty($this->param['created_at'])){
            $map[] = ['created_at', 'between', $this->param['created_at']];
        }
        $sort = ['id' => 'desc'];
        $data = $logic->getList($map, $sort, ['id', 'title', 'thumb', 'category_id', 'keywords', 'status', 'created_at', 'updated_at']);
        return $this->success($data);
    }

    public function info(Request $request, ProductLogic $logic){
        $request->validate([
            'id'=>'required'
        ],[
            'id.required' => 'ID不能为空'
        ]);
        $data = $logic->getInfo($this->param['id']);
        return $this->success(Arr::twoKeepKeys($data, ['id', 'title', 'gallery', 'attrs', 'category_id', 'keywords', 'intro', 'content',
            'describe', 'seo_mate', 'related_product_id', 'status']));
    }

    public function save(ProductRequest $request, ProductLogic $logic)
    {
        //验证图片集格式
        foreach ($this->param['gallery'] as $v){
            if(empty($v['url'])){
                throw new BsideGlobalException(Code::SYSTEM_ERROR, '图片链接不能为空');
            }
        }
        //验证产品参数
        foreach ($this->param['attrs'] as $v){
            if(empty($v['key'])){
                throw new BsideGlobalException(Code::SYSTEM_ERROR, '产品属性名不能为空');
            }
            if(empty($v['value'])){
                throw new BsideGlobalException(Code::SYSTEM_ERROR, '产品属性值不能为空');
            }
        }
        //验证seo_mate参数
        if(empty($this->param['seo_mate']['title'])){
            throw new BsideGlobalException(Code::SYSTEM_ERROR, 'SEO标题不能为空');
        }
        if(empty($this->param['seo_mate']['description'])){
            throw new BsideGlobalException(Code::SYSTEM_ERROR, 'SEO描述不能为空');
        }
        if(empty($this->param['seo_mate']['keyword'])){
            throw new BsideGlobalException(Code::SYSTEM_ERROR, 'SEO关键词不能为空');
        }

        //关联产品 最多16个
        $this->param['related_product_id'] = array_filter(Arr::splitFilterToArray($this->param['related_product_id']), 'intval');
        if(count($this->param['related_product_id']) > 16){
            throw new BsideGlobalException(Code::SYSTEM_ERROR, '关联产品不能超过16个');
        }

        //封面取第一个图片
        $this->param['thumb'] = $this->param['gallery'][0] ?? '';

        $data = $logic->save($this->param);
        return $this->success($data);
    }

    public function delete(Request $request, ProductLogic $logic)
    {
        $request->validate([
            'ids'=>['required', new Ids()]
        ],[
            'ids.required' => 'ID不能为空'
        ]);

        $data = $logic->delete($this->param['ids']);
        return $this->success($data);
    }

}