first commit

This commit is contained in:
annnj-company
2026-04-17 18:29:53 +08:00
parent e49fa5a215
commit 130c1026c4
5615 changed files with 1639145 additions and 0 deletions

View File

@@ -0,0 +1,158 @@
<?php
namespace app\admin\service;
use app\admin\exception\LogicException;
use think\facade\Config;
use Qcloud\Cos\Client;
use think\facade\Log;
/**
* 阿里云OSS服务
*
* Class OssService
* @package app\admin\service
*/
class OssService
{
private $client;
/**
* 不可与oss已有的bucket重名这个不区分账号
*
* @var mixed
*/
private $bucket;
public function __construct()
{
$accessKeyId = Config::get('uploadFile.access_key_id');
$accessKeySecret = Config::get('uploadFile.access_key_secret');
$endpoint = Config::get('uploadFile.endpoint');
$bucket = Config::get('uploadFile.bucket');
try {
$cos_client = new Client([
'region' => $endpoint,
'credentials' => [
'secretId' => $accessKeyId,
'secretKey' => $accessKeySecret
]
]);
if (!$cos_client->doesBucketExist($bucket)) {
$cos_client->createBucket($bucket);
}
} catch (\Exception $e) {
throw new LogicException($e->getMessage());
}
$this->bucket = $bucket;
$this->client = $cos_client;
return $this;
}
/**
* 上传单个文件eg$_FILES['img']
*
* @param string $path
* @param string $tmpFilePath
* @param string $mimeType
* @return string
* @throws LogicException
*/
public function uploadSingleFile($path, $tmpFilePath, $mimeType = 'application/octet-stream')
{
try {
$this->client->putObject([
'Bucket' => $this->bucket,
'Key' => $path,
'Body' => fopen($tmpFilePath, 'rb'),
'ContentType' => $mimeType
]);
} catch (\Exception $e) {
throw new LogicException($e->getMessage());
}
return env('uploadFile.url') . $path;
}
/**
* 上传多文件eg$_FILES['imgs']
*
* @param array $paths
* @param array $tmpPaths
* @return array
* @throws LogicException
*/
public function uploadMultiFiles(array $paths, array $tmpPaths)
{
$signedUrls = [];
foreach ($paths as $index => $path) {
$signedUrls[$index] = $this->uploadSingleFile($path, $tmpPaths[$index]);
}
return $signedUrls;
}
/**
* 获取加密的url
*
* @param $path
* @param int $timeout
* @return string
*/
public function getSignedUrl($path, $timeout = 3600)
{
try {
$url = $this->client->signUrl($this->bucket, $path, $timeout);
} catch (\Exception $e) {
return '';
}
return $url;
}
/**
*获取OSS文件
*/
public function getOSSFile($fileUrl, $localPath = '')
{
$fileUrl = str_replace("\\", "/", $fileUrl);
$path = parse_url($fileUrl, PHP_URL_PATH);
$objectKey = substr($path, 1);
try {
$config = ['Bucket' => $this->bucket, 'Key' => $objectKey];
if (!empty($localPath)) {
$config['SaveAs'] = $localPath;
}
$content = $this->client->getObject($config);
} catch (\Exception $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return "";
}
$content = base64_encode($content);
return $content;
}
/**
*删除文件
*
*/
public function deleteOSSFile($fileUrl)
{
$url = substr($fileUrl, strlen("https://pgevasys.oss-cn-shenzhen.aliyuncs.com"));
$url = str_replace("\\", "/", $url);
$objectKey = substr($url, 1);
try {
$this->client->deleteObject($this->bucket, $objectKey);
} catch (\Exception $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return false;
}
return true;
}
}