103 lines
2.2 KiB
PHP
103 lines
2.2 KiB
PHP
<?php
|
|
/**
|
|
* This service file is part of item.
|
|
*
|
|
* @author ctexthuang
|
|
* @contact ctexthuang@qq.com
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Service\ServiceTrait\Common;
|
|
|
|
use App\Exception\ErrException;
|
|
use App\Model\OssObject;
|
|
use Hyperf\Di\Annotation\Inject;
|
|
use Hyperf\Tappable\HigherOrderTapProxy;
|
|
|
|
trait OssTrait
|
|
{
|
|
/**
|
|
* oss资源表
|
|
* @var OssObject
|
|
*/
|
|
#[Inject]
|
|
protected OssObject $ossObjectModel;
|
|
|
|
|
|
/**
|
|
* 确认资源
|
|
* @param array $ossIds
|
|
* @return void
|
|
*/
|
|
public function checkOssObjects(array $ossIds): void
|
|
{
|
|
$data = $this->ossObjectModel->getIdListByIds($ossIds);
|
|
if (empty($data)){
|
|
throw new ErrException('资源不存在');
|
|
}
|
|
|
|
$data = $data->toArray();
|
|
|
|
if (count($data) != count($ossIds)) {
|
|
throw new ErrException('资源不存在【' . implode('|', array_diff($ossIds, $data)) . '】');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 更改资源属性 -> Enable
|
|
* @param array $ossIds
|
|
* @return void
|
|
*/
|
|
public function updateOssObjects(array $ossIds): void
|
|
{
|
|
$this->checkOssObjects($ossIds);
|
|
|
|
$this->ossObjectModel->updateEnabledByIds($ossIds);
|
|
}
|
|
|
|
/**
|
|
* 更改资源属性 -> Disable
|
|
* @param array $ossIds
|
|
* @return void
|
|
*/
|
|
public function updateOssObjectsDisable(array $ossIds): void
|
|
{
|
|
$this->ossObjectModel->updateDisableByIds($ossIds);
|
|
}
|
|
|
|
/**
|
|
* 获取所有数据
|
|
* @param array $ossIds
|
|
* @return array
|
|
*/
|
|
public function getOssObjects(array $ossIds): array
|
|
{
|
|
$data = $this->ossObjectModel->getInfoByOssIds($ossIds);
|
|
if (empty($data)){
|
|
return [];
|
|
}
|
|
|
|
$res = [];
|
|
foreach ($data->toArray() as $one)
|
|
{
|
|
$res[$one['id']] = $one;
|
|
}
|
|
|
|
return $res;
|
|
}
|
|
|
|
/**
|
|
* @param int $ossId
|
|
* @return HigherOrderTapProxy|mixed|string|null
|
|
*/
|
|
public function getOssObjectById(int $ossId): mixed
|
|
{
|
|
$data = $this->ossObjectModel->getInfoById($ossId);
|
|
if (empty($data)){
|
|
return '';
|
|
}
|
|
|
|
return $data->url ?? '';
|
|
}
|
|
} |