feat : oss

This commit is contained in:
2024-11-12 11:14:48 +08:00
parent a9f81888b7
commit 86b94d168c
7 changed files with 74 additions and 12 deletions

View File

@@ -0,0 +1,19 @@
<?php
namespace App\Constants\Common;
class OssObjectCode
{
/**
* 状态 1=有效 2=无效
*/
const int ENABLE = 1;
const int DISABLE = 2;
/**
* 类型 1=图片 2=音频 3=视频
*/
const int TYPE_IMAGE = 1;
const int TYPE_AUDIO = 2;
const int TYPE_VIDEO = 3;
}

View File

@@ -49,7 +49,7 @@ class OssDelByOssIdTask
$delNum = 0; $delNum = 0;
$ossIds = []; $ossIds = [];
for ($i = 1; $i < 50; $i++) { for ($i = 1; $i < 50; $i++) {
$one = $this->redis->rPop($key); $one = $this->redis->rPop($key,'system');
if (empty($one)){ if (empty($one)){
continue; continue;
} }
@@ -87,7 +87,7 @@ class OssDelByOssIdTask
//删除oss资源 //删除oss资源
//把图片地址丢到删除redis队列 //把图片地址丢到删除redis队列
foreach ($urlList as $item) { foreach ($urlList as $item) {
$this->redis->lPush(CommonRedisKey::getDeleteOssImgListByUrl(), $item); $this->redis->lPush(CommonRedisKey::getDeleteOssImgListByUrl(), $item,'system');
} }
$this->log->notice(__CLASS__.':success:删除oss文件个数:' . $delNum); $this->log->notice(__CLASS__.':success:删除oss文件个数:' . $delNum);

View File

@@ -62,7 +62,7 @@ class OssDelByUrlTask
$delNum = 0; $delNum = 0;
for ($i = 1; $i < 50; $i++) { for ($i = 1; $i < 50; $i++) {
$url = $this->redis->rPop($key); $url = $this->redis->rPop($key,'system');
if (!empty($url)) { if (!empty($url)) {
$delNum++; $delNum++;

View File

@@ -34,8 +34,7 @@ class ErrExceptionHandler extends ExceptionHandler
public function handle(Throwable $throwable, ResponseInterface $response): ResponseInterface public function handle(Throwable $throwable, ResponseInterface $response): ResponseInterface
{ {
if ($throwable instanceof ErrException) { if ($throwable instanceof ErrException) {
$url = $this->request->path(); $urlArr = explode('/',$this->request->path());
$urlArr = explode('/',$url);
$result = match ($urlArr[0]) { $result = match ($urlArr[0]) {
'api' => $this->apiReturn->error($throwable->getMessage(),$throwable->getCode()), 'api' => $this->apiReturn->error($throwable->getMessage(),$throwable->getCode()),

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Model; namespace App\Model;
use App\Constants\Common\OssObjectCode;
use Hyperf\Collection\Collection; use Hyperf\Collection\Collection;
use Hyperf\DbConnection\Model\Model; use Hyperf\DbConnection\Model\Model;
use function Hyperf\Config\config; use function Hyperf\Config\config;
@@ -69,7 +70,7 @@ class OssObject extends Model
*/ */
public function updateEnabledByIds(array $ids): int 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 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 public function getOssIdListByIsEnabled($time): Collection|false
{ {
return $this->where([ return $this->where([
['is_enabled', '=', 0], ['is_enabled', '=', OssObjectCode::DISABLE],
['create_time', '<',$time] ['create_time', '<',$time]
])->pluck('id') ?? false; ])->pluck('id') ?? false;
} }

View File

@@ -12,6 +12,7 @@ namespace App\Service\Common;
use App\Cache\Redis\Common\CommonRedisKey; use App\Cache\Redis\Common\CommonRedisKey;
use App\Cache\Redis\RedisCache; use App\Cache\Redis\RedisCache;
use App\Constants\Common\OssObjectCode;
use App\Exception\ErrException; use App\Exception\ErrException;
use App\Lib\AdminReturn; use App\Lib\AdminReturn;
use App\Lib\Log; use App\Lib\Log;
@@ -265,14 +266,14 @@ class OssCallbackService
case 'jpeg': case 'jpeg':
case 'png': case 'png':
case 'bmp': case 'bmp':
$type = 1; $type = OssObjectCode::TYPE_IMAGE;
break; break;
case 'mp3': case 'mp3':
$type = 2; $type = OssObjectCode::TYPE_AUDIO;
break; break;
case 'mp4': case 'mp4':
case 'swf': case 'swf':
$type = 3; $type = OssObjectCode::TYPE_VIDEO;
break; break;
} }
@@ -285,7 +286,7 @@ class OssCallbackService
$ossObjectModel->audio_second = $this->data['audio_second'] ?? 0; $ossObjectModel->audio_second = $this->data['audio_second'] ?? 0;
$ossObjectModel->video_duration = $this->data['video_duration'] ?? 0; $ossObjectModel->video_duration = $this->data['video_duration'] ?? 0;
$ossObjectModel->size = $this->data['size'] ?? 0; $ossObjectModel->size = $this->data['size'] ?? 0;
$ossObjectModel->is_enabled = OssObjectCode::DISABLE;
$ossObjectModel->type = $type; $ossObjectModel->type = $type;
if (!$ossObjectModel->save()){ if (!$ossObjectModel->save()){

View File

@@ -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;
}
} }