Files
lgp-admin-plus-api/app/Service/business/ImageService.php
LQ bcf54c2727 初始化
缺陷:主题配色需要优化整体的同风格
2026-08-14 23:21:21 +08:00

152 lines
4.1 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Service\business;
use App\BaseApp\BaseService;
use App\Models\business\CatalogueModel;
use App\Models\business\ImageModel;
use App\Service\common\MediaUrlService;
use Exception;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
/**
* 商品相册(渲染图 / 实物图)
*/
class ImageService extends BaseService
{
private MediaUrlService $media;
public function __construct()
{
parent::__construct();
$this->model = ImageModel::class;
$this->selectField = ['id', 'catalogue_id', 'url', 'type', 'status', 'created_at', 'updated_at'];
$this->queryField = ['catalogue_id' => '=', 'type' => '=', 'status' => '='];
$this->orderBy = ['name' => 'id', 'sort' => 'asc'];
$this->media = MediaUrlService::getInstance();
}
/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function list(): array
{
$result = $this->getPageList();
$this->media->publicEach($result['items'], ['url']);
return $result;
}
public function option(): mixed
{
$this->optionField = ['id', 'url as name'];
return $this->getOption();
}
/**
* @throws Exception
*/
public function detail($id): mixed
{
$info = $this->getDetail($id);
$info->url = $this->media->toPublic($info->url);
return $info;
}
/**
* 渲染图列表(老接口 image/get-render-graph入参是 catalogue_id
*/
public function renderGraph(int $catalogueId): array
{
return $this->listByCatalogue($catalogueId, ImageModel::TYPE_RENDER);
}
/**
* 实物图列表(老接口 image/get-physical-drawing入参是 catalogue_id
*/
public function physicalDrawing(int $catalogueId): array
{
return $this->listByCatalogue($catalogueId, ImageModel::TYPE_PHYSICAL);
}
/**
* 新增前端图片组件一次能选多张url 传数组时逐张入库
* @throws Exception
*/
public function create($params): mixed
{
$catalogueId = (int) ($params['catalogue_id'] ?? 0);
if (!CatalogueModel::where('id', $catalogueId)->where('deleted_at', 0)->exists()) {
$this->utils->errorThrow('商品不存在');
}
$type = (int) ($params['type'] ?? ImageModel::TYPE_RENDER);
$urls = $params['url'] ?? '';
$urls = is_array($urls) ? $urls : [$urls];
$now = time();
$rows = [];
foreach ($urls as $url) {
$url = $this->media->toStorage(is_string($url) ? $url : '');
if ($url === '') {
continue;
}
$rows[] = [
'catalogue_id' => $catalogueId,
'url' => $url,
'type' => $type,
'created_at' => $now,
];
}
if (empty($rows)) {
$this->utils->errorThrow('请上传图片');
}
if (count($rows) === 1) {
return $this->insert($rows[0]);
}
return ImageModel::insert($rows);
}
/**
* @throws Exception
*/
public function update($id, $params): mixed
{
if (array_key_exists('url', $params)) {
$params['url'] = $this->media->firstOf($params['url']);
}
return $this->save($id, $params);
}
/**
* @throws Exception
*/
public function delete($id): mixed
{
return $this->del(is_array($id) ? $id : [$id]);
}
/**
* @throws Exception
*/
public function status($id, $status): mixed
{
return $this->save($id, ['status' => (int) $status]);
}
private function listByCatalogue(int $catalogueId, int $type): array
{
if ($catalogueId <= 0) {
return [];
}
$rows = ImageModel::where('catalogue_id', $catalogueId)
->where('type', $type)
->where('deleted_at', 0)
->orderBy('id')
->get(['id', 'catalogue_id', 'url', 'type', 'status'])
->toArray();
$this->media->publicEach($rows, ['url']);
return $rows;
}
}