Files
hyperf_service/app/Service/Admin/Good/CategoryService.php
2025-01-13 15:03:17 +08:00

71 lines
1.6 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' => []]);
$res = $list->toArray();
$ossIds = array_column($res['data'],'image_id');
$ossInfo = $this->getOssObjects($ossIds);
foreach ($res['data'] as &$one)
{
$one['url'] = $ossInfo[$one['image_id']]['url'];
}
return $this->return->success('success',['list' => $res]);
}
/**
* 添加
* @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();
}
}