From 86b94d168cd7afb62f36fe2b57fe86e32fce4fd5 Mon Sep 17 00:00:00 2001 From: ctexthuang Date: Tue, 12 Nov 2024 11:14:48 +0800 Subject: [PATCH] feat : oss --- app/Constants/Common/OssObjectCode.php | 19 +++++++++ app/Cron/Oss/OssDelByOssIdTask.php | 4 +- app/Cron/Oss/OssDelByUrlTask.php | 2 +- app/Exception/Handler/ErrExceptionHandler.php | 3 +- app/Model/OssObject.php | 7 ++-- app/Service/Common/OssCallbackService.php | 9 ++-- app/Service/ServiceTrait/Common/OssTrait.php | 42 +++++++++++++++++++ 7 files changed, 74 insertions(+), 12 deletions(-) create mode 100644 app/Constants/Common/OssObjectCode.php diff --git a/app/Constants/Common/OssObjectCode.php b/app/Constants/Common/OssObjectCode.php new file mode 100644 index 0000000..7613cdf --- /dev/null +++ b/app/Constants/Common/OssObjectCode.php @@ -0,0 +1,19 @@ +redis->rPop($key); + $one = $this->redis->rPop($key,'system'); if (empty($one)){ continue; } @@ -87,7 +87,7 @@ class OssDelByOssIdTask //删除oss资源 //把图片地址丢到删除redis队列 foreach ($urlList as $item) { - $this->redis->lPush(CommonRedisKey::getDeleteOssImgListByUrl(), $item); + $this->redis->lPush(CommonRedisKey::getDeleteOssImgListByUrl(), $item,'system'); } $this->log->notice(__CLASS__.':success:删除oss文件个数:' . $delNum); diff --git a/app/Cron/Oss/OssDelByUrlTask.php b/app/Cron/Oss/OssDelByUrlTask.php index 8d6551c..21f34d3 100644 --- a/app/Cron/Oss/OssDelByUrlTask.php +++ b/app/Cron/Oss/OssDelByUrlTask.php @@ -62,7 +62,7 @@ class OssDelByUrlTask $delNum = 0; for ($i = 1; $i < 50; $i++) { - $url = $this->redis->rPop($key); + $url = $this->redis->rPop($key,'system'); if (!empty($url)) { $delNum++; diff --git a/app/Exception/Handler/ErrExceptionHandler.php b/app/Exception/Handler/ErrExceptionHandler.php index 0277e64..75229b5 100644 --- a/app/Exception/Handler/ErrExceptionHandler.php +++ b/app/Exception/Handler/ErrExceptionHandler.php @@ -34,8 +34,7 @@ class ErrExceptionHandler extends ExceptionHandler public function handle(Throwable $throwable, ResponseInterface $response): ResponseInterface { if ($throwable instanceof ErrException) { - $url = $this->request->path(); - $urlArr = explode('/',$url); + $urlArr = explode('/',$this->request->path()); $result = match ($urlArr[0]) { 'api' => $this->apiReturn->error($throwable->getMessage(),$throwable->getCode()), diff --git a/app/Model/OssObject.php b/app/Model/OssObject.php index aea520d..8fdbdcf 100644 --- a/app/Model/OssObject.php +++ b/app/Model/OssObject.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace App\Model; +use App\Constants\Common\OssObjectCode; use Hyperf\Collection\Collection; use Hyperf\DbConnection\Model\Model; use function Hyperf\Config\config; @@ -69,7 +70,7 @@ class OssObject extends Model */ public function updateEnabledByIds(array $ids): int { - return $this->whereIn('id', $ids)->update(['is_enabled' => 1]); + return $this->whereIn('id', $ids)->update(['is_enabled' => OssObjectCode::ENABLE]); } /** @@ -79,7 +80,7 @@ class OssObject extends Model */ public function updateDisableByIds(array $ids): int { - return $this->whereIn('id', $ids)->update(['is_enabled' => 0]); + return $this->whereIn('id', $ids)->update(['is_enabled' => OssObjectCode::DISABLE]); } /** @@ -100,7 +101,7 @@ class OssObject extends Model public function getOssIdListByIsEnabled($time): Collection|false { return $this->where([ - ['is_enabled', '=', 0], + ['is_enabled', '=', OssObjectCode::DISABLE], ['create_time', '<',$time] ])->pluck('id') ?? false; } diff --git a/app/Service/Common/OssCallbackService.php b/app/Service/Common/OssCallbackService.php index 9216d40..7ecc090 100644 --- a/app/Service/Common/OssCallbackService.php +++ b/app/Service/Common/OssCallbackService.php @@ -12,6 +12,7 @@ namespace App\Service\Common; use App\Cache\Redis\Common\CommonRedisKey; use App\Cache\Redis\RedisCache; +use App\Constants\Common\OssObjectCode; use App\Exception\ErrException; use App\Lib\AdminReturn; use App\Lib\Log; @@ -265,14 +266,14 @@ class OssCallbackService case 'jpeg': case 'png': case 'bmp': - $type = 1; + $type = OssObjectCode::TYPE_IMAGE; break; case 'mp3': - $type = 2; + $type = OssObjectCode::TYPE_AUDIO; break; case 'mp4': case 'swf': - $type = 3; + $type = OssObjectCode::TYPE_VIDEO; break; } @@ -285,7 +286,7 @@ class OssCallbackService $ossObjectModel->audio_second = $this->data['audio_second'] ?? 0; $ossObjectModel->video_duration = $this->data['video_duration'] ?? 0; $ossObjectModel->size = $this->data['size'] ?? 0; - + $ossObjectModel->is_enabled = OssObjectCode::DISABLE; $ossObjectModel->type = $type; if (!$ossObjectModel->save()){ diff --git a/app/Service/ServiceTrait/Common/OssTrait.php b/app/Service/ServiceTrait/Common/OssTrait.php index 61948b0..af02a5c 100644 --- a/app/Service/ServiceTrait/Common/OssTrait.php +++ b/app/Service/ServiceTrait/Common/OssTrait.php @@ -43,4 +43,46 @@ trait OssTrait } } + /** + * 更改资源属性 -> 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; + } } \ No newline at end of file