feat : ali

This commit is contained in:
2024-11-06 16:53:45 +08:00
parent 7b6f9e4aa7
commit 8d30a65528
9 changed files with 443 additions and 36 deletions

View File

@@ -5,10 +5,10 @@ namespace App\Cache\Redis\Common;
class CommonRedisKey
{
/**
* @var $type
* @return string
*@var string $type
*/
public static function getSystemRegionList($type = 'all')
public static function getSystemRegionList(string $type = 'all'): string
{
return '__system:address:list:'.$type;
}

View File

@@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
namespace App\Controller\Common;
use App\Service\Common\OssCallbackService;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use RedisException;
#[Controller]
class ThirdCallbackController
{
/**
* oss回调处理
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
#[RequestMapping(path: "/common/oss/ossCallBack", methods: "post")]
public function callback()
{
return (new OssCallbackService)->process();
}
}

View File

@@ -10,14 +10,57 @@ declare(strict_types=1);
namespace App\Cron\Oss;
use App\Cache\Redis\Common\CommonRedisKey;
use App\Cache\Redis\RedisCache;
use App\Extend\DateUtil;
use App\Lib\Log;
use App\Model\OssObject;
use Hyperf\Crontab\Annotation\Crontab;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use RedisException;
#[Crontab(rule: "* * * * *", name: "OssDelByDisableTask", singleton: true , callback: "execute", memo: "这是一个示例的定时任务")]
#[Crontab(rule: "0 5 * * *", name: "OssDelByDisableTask", singleton: true , callback: "execute", memo: "定时删除未使用的oss资源")]
class OssDelByDisableTask
{
public function execute()
/**
* 日志
* @var Log $log
*/
#[Inject]
protected Log $log;
/**
* 缓存
* @var RedisCache $redis
*/
#[Inject]
protected RedisCache $redis;
/**
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function execute(): void
{
//todo Write logic
var_dump(date('Y-m-d H:i:s', time()));
$ossObjectModel = new OssObject();
$time = date('Y-m-d H:i:s',time() - DateUtil::DAY);
$list = $ossObjectModel->getOssIdListByIsEnabled($time);
if (empty($list)){
$this->log->notice(__CLASS__.'昨日没有无效的资源');
return;
}
foreach ($list->toArray() as $item) {
$this->redis->lPush(CommonRedisKey::getDeleteOssImgListByOssId(), (string)$item,'system');
}
$this->log->notice(__CLASS__.'异步删除昨日无效图片');
}
}

View File

@@ -10,14 +10,90 @@ declare(strict_types=1);
namespace App\Cron\Oss;
use App\Cache\Redis\Common\CommonRedisKey;
use App\Cache\Redis\RedisCache;
use App\Lib\Log;
use App\Model\OssObject;
use Hyperf\Crontab\Annotation\Crontab;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
#[Crontab(rule: "* * * * *", name: "OssDelByOssIdTask", singleton: true , callback: "execute", memo: "这是一个示例的定时任务")]
#[Crontab(rule: "* * * * *", name: "OssDelByOssIdTask", singleton: true , callback: "execute", memo: "根据id删除oss的逻辑")]
class OssDelByOssIdTask
{
public function execute()
/**
* 日志
* @var Log $log
*/
#[Inject]
protected Log $log;
/**
* 缓存
* @var RedisCache $redis
*/
#[Inject]
protected RedisCache $redis;
/**
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function execute(): void
{
//todo Write logic
var_dump(date('Y-m-d H:i:s', time()));
try {
$key = CommonRedisKey::getDeleteOssImgListByOssId();
$delNum = 0;
$ossIds = [];
for ($i = 1; $i < 50; $i++) {
$one = $this->redis->rPop($key);
if (empty($one)){
continue;
}
$ossIds[] = $one;
}
if (count($ossIds) == 0) {
$this->log->notice(__CLASS__.':success:无根据id删除的列表');
return;
}
$ossObjectModel = new OssObject();
//获取url列表用于删除oss
$urlList = $ossObjectModel->whereIn('id', $ossIds)->pluck('url');
if (empty($urlList)){
$this->log->error(__CLASS__.':删除内容关联失败,无法获取url');
return;
}
if(count($urlList->toArray()) != count($ossIds))
{
$this->log->error(__CLASS__.':删除内容关联失败url数量与id数量不一致');
return;
}
// 删除oss图片
$res = $ossObjectModel->whereIn('id', $ossIds)->delete();
if (!$res) {
$this->log->error(__CLASS__.':删除内容关联失败,删除sql执行失败');
return;
}
//删除oss资源
//把图片地址丢到删除redis队列
foreach ($urlList as $item) {
$this->redis->lPush(CommonRedisKey::getDeleteOssImgListByUrl(), $item);
}
$this->log->notice(__CLASS__.':success:删除oss文件个数:' . $delNum);
}catch (\Exception $e){
$this->log->error(__CLASS__.':'.$e->getMessage());
return;
}
}
}

View File

@@ -10,14 +10,66 @@ declare(strict_types=1);
namespace App\Cron\Oss;
use App\Cache\Redis\Common\CommonRedisKey;
use App\Cache\Redis\RedisCache;
use App\Lib\Log;
use Hyperf\Crontab\Annotation\Crontab;
use Hyperf\Di\Annotation\Inject;
use OSS\Core\OssException;
use OSS\Http\RequestCore_Exception;
use OSS\OssClient;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use RedisException;
use function Hyperf\Config\config;
#[Crontab(rule: "* * * * *", name: "OssDelByUrlTask", singleton: true , callback: "execute", memo: "这是一个示例的定时任务")]
#[Crontab(rule: "* * * * *", name: "OssDelByUrlTask", singleton: true , callback: "execute", memo: "根据url删除oss的逻辑")]
class OssDelByUrlTask
{
public function execute()
/**
* 日志
* @var Log $log
*/
#[Inject]
protected Log $log;
/**
* 缓存
* @var RedisCache $redis
*/
#[Inject]
protected RedisCache $redis;
/**
* @return void
* @throws OssException
* @throws RequestCore_Exception
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function execute(): void
{
//todo Write logic
var_dump(date('Y-m-d H:i:s', time()));
$key = CommonRedisKey::getDeleteOssImgListByUrl();
// 阿里云oss上传
$OssClient = new OssClient(
config('ali.access_key_id'),
config('ali.access_key_secret'),
config('ali.intranet_endpoint')
);
$bucket = config('ali.bucket');
$delNum = 0;
for ($i = 1; $i < 50; $i++) {
$url = $this->redis->rPop($key);
if (!empty($url)) {
$delNum++;
$OssClient->deleteObject($bucket, $url);
}
}
$this->log->notice(__CLASS__.':success:删除oss文件个数:' . $delNum);
}
}

99
app/Extend/DateUtil.php Normal file
View File

@@ -0,0 +1,99 @@
<?php
namespace App\Extend;
class DateUtil
{
public const MINUTE = 60; // 分
public const HOUR = 3600; // 小时
public const DAY = 86400; // 天
public const WEEK = 604800; // 周
public const MONTH = 2592000; // 月
public const YEAR = 311040000; // 年
/**
* 获取当前时间到今日结束的秒数
* @return int
*/
static function getTodayEnds(): int
{
return 86400 - (time() + 8 * 3600) % 86400;
}
/**
* 获取当前时间
* @return string
*/
static function getCurrentDatetime(): string
{
return date('Y-m-d H:i:s');
}
/**
* 获取今日开始的时间
* @return string
*/
static function getTodayStartDate(): string
{
return date("Y-m-d 00:00:00");
}
/**
* 获取今日结束的时间
* @return string
*/
static function getTodayEndDate(): string
{
return date("Y-m-d 23:59:59");
}
/**
* 获取当前日期
* @return string
*/
static function getCurrentDate(): string
{
return date('Y-m-d');
}
/**
* 获取某天开始的时间
* @param int $i
* @return string
*/
static function getStartDate(int $i = 1): string
{
return date("Y-m-d 00:00:00", strtotime("-{$i} day"));
}
/**
* 获取某日结束的时间
* @param int $i
* @return string
*/
static function getEndDate(int $i = 1): string
{
return date("Y-m-d 23:59:59", strtotime("-{$i} day"));
}
/**
* 获取某天前日期
* @param int $i
* @return string
*/
static function getDate(int $i = 1): string
{
return date('Y-m-d', strtotime("-{$i} day"));
}
/**
* 获取当前的毫秒时间戳(13位)
* @return string
*/
static function getCurrentMsTime(): string
{
list($ms, $sec) = explode(' ', microtime());
$msTime = (float)sprintf('%.0f', (floatval($ms) + floatval($sec)) * 1000);
return substr($msTime,0,13);
}
}

107
app/Model/OssObject.php Normal file
View File

@@ -0,0 +1,107 @@
<?php
declare(strict_types=1);
namespace App\Model;
use Hyperf\Collection\Collection;
use Hyperf\DbConnection\Model\Model;
use function Hyperf\Config\config;
/**
* @property int $id
* @property string $url
* @property int $size
* @property int $height
* @property int $width
* @property int $audio_second
* @property int $video_duration
* @property int $is_enabled
* @property string $create_time
* @property int $type
*/
class OssObject extends Model
{
/**
* The table associated with the model.
*/
protected ?string $table = 'oss_object';
/**
* The attributes that are mass assignable.
*/
protected array $fillable = [];
protected array $guarded = [];
/**
* The attributes that should be cast to native types.
*/
protected array $casts = ['id' => 'integer', 'size' => 'integer','height' => 'integer', 'width' => 'integer', 'audio_second' => 'integer', 'video_duration' => 'integer', 'is_enabled' => 'integer', 'type' => 'integer'];
const CREATED_AT = 'create_time';
const UPDATED_AT = null;
/**
* url访问器
* @param $value
* @return string
*/
public function getUrlAttribute($value)
{
return config('ali.oss_url').$value;
}
/**
* 根据ids获取资源id列表
* @param array $ids
* @return Collection
*/
public function getIdListByIds(array $ids): Collection
{
return $this->whereIn('id', $ids)->pluck('id');
}
/**
* 根据ids更新oss Enable状态
*/
public function updateEnabledByIds(array $ids): int
{
return $this->whereIn('id', $ids)->update(['is_enabled' => 1]);
}
/**
* 根据ids 更新oss Disable状态
* @param array $ids
* @return int
*/
public function updateDisableByIds(array $ids): int
{
return $this->whereIn('id', $ids)->update(['is_enabled' => 0]);
}
/**
* 根据id获取信息
* @param array $ids
* @return Collection
*/
public function getInfoByOssIds(array $ids): Collection
{
return $this->whereIn('id', $ids)->get();
}
/**
* 根据is_enabled获取传入时间前无效资源id
* @param $time
* @return Collection|false
*/
public function getOssIdListByIsEnabled($time): Collection|false
{
return $this->where([
['is_enabled', '=', 0],
['create_time', '<',$time]
])->pluck('id') ?? false;
}
}

View File

@@ -10,13 +10,12 @@ declare(strict_types=1);
namespace App\Service\Common;
use AlibabaCloud\SDK\Sts\V20150401\Sts;
use App\Cache\Redis\Common\CommonRedisKey;
use App\Cache\Redis\RedisCache;
use App\Exception\AdminException;
use App\Lib\AdminReturn;
use App\Lib\Log;
use App\Service\ServiceTrait\Common\AliStsTrait;
use App\Model\OssObject;
use Exception;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Contract\RequestInterface;
@@ -28,7 +27,6 @@ use function Hyperf\Config\config;
class OssCallbackService
{
use AliStsTrait;
/**
* 请求对象
@@ -116,7 +114,7 @@ class OssCallbackService
$this->ossClient = new OssClient(
config('ali.access_key_id'),
config('ali.access_key_secret'),
config('oss.sts_endpoint')
config('ali.intranet_endpoint')
);
$this->bucket = config('ali.bucket');
@@ -167,7 +165,8 @@ class OssCallbackService
return $this->adminReturn->success('上传成功',[
'id' => $this->newId,
'url' => config('ali.oss_url') . $this->newObjectPath, 'old_file_name' => $this->fileName
'url' => config('ali.oss_url') . $this->newObjectPath,
'old_file_name' => $this->fileName
]);
}
@@ -277,23 +276,23 @@ class OssCallbackService
break;
}
// $ossObjectModel = new OssObject();
//
// $ossObjectModel->url = $this->newObjectPath;
//// $ossObjectModel->url = urldecode($this->data['object']);
// $ossObjectModel->width = $this->data['imageInfo_width'] ?? 0;
// $ossObjectModel->height = $this->data['imageInfo_height'] ?? 0;
// $ossObjectModel->audio_second = $this->data['audio_second'] ?? 0;
// $ossObjectModel->video_duration = $this->data['video_duration'] ?? 0;
// $ossObjectModel->size = $this->data['size'] ?? 0;
//
// $ossObjectModel->type = $type;
//
// if (!$ossObjectModel->save()){
// throw new AdminException('保存图片失败');
// }
$ossObjectModel = new OssObject();
// $this->newId = $ossObjectModel->id;
$ossObjectModel->url = $this->newObjectPath;
// $ossObjectModel->url = urldecode($this->data['object']);
$ossObjectModel->width = $this->data['imageInfo_width'] ?? 0;
$ossObjectModel->height = $this->data['imageInfo_height'] ?? 0;
$ossObjectModel->audio_second = $this->data['audio_second'] ?? 0;
$ossObjectModel->video_duration = $this->data['video_duration'] ?? 0;
$ossObjectModel->size = $this->data['size'] ?? 0;
$ossObjectModel->type = $type;
if (!$ossObjectModel->save()){
throw new AdminException('保存图片失败');
}
$this->newId = $ossObjectModel->id;
}
/**
@@ -305,7 +304,7 @@ class OssCallbackService
private function deleteOssObject(): void
{
//删除旧的文件
$this->redisCache->lPush(CommonRedisKey::getDeleteOssImgListByUrl(), $this->data['object']);
$this->redisCache->lPush(CommonRedisKey::getDeleteOssImgListByUrl(), $this->data['object'],'system');
}
}