Files
hyperf_service/app/Service/Admin/Good/CategoryService.php
LAPTOP-7SGDREK0\shiweijun 09d9bfaf7e fix:category
2025-01-09 16:43:11 +08:00

62 lines
1.3 KiB
PHP

<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\Admin\Good;
use App\Exception\ErrException;
use App\Model\Category;
use App\Service\Admin\BaseService;
use App\Service\ServiceTrait\Common\OssTrait;
use Exception;
use Hyperf\Di\Annotation\Inject;
class CategoryService extends BaseService
{
use OssTrait;
/**
* @var Category $categoryModel
*/
#[Inject]
protected Category $categoryModel;
/**
* @return array
*/
public function handle()
{
$list = $this->categoryModel->get(['category.id','category.name','category.image_id']);
if (empty($list)) return $this->return->success('success',['list' => []]);
return $this->return->success('success',['list' => $list->toArray()]);
}
/**
* 添加
* @return array
* @throws Exception
*/
public function add()
{
$name = $this->request->input('name');
$imageId = $this->request->input('image_id');
$this->updateOssObjects([$imageId]);
$category = new Category();
$category->name = $name;
$category->image_id = $imageId;
if (!$category->save()) throw new ErrException('商品种类添加失败');
return $this->return->success();
}
}