Initial commit

This commit is contained in:
18338781771
2025-05-14 13:19:23 +08:00
commit 1ab607f65a
313 changed files with 27595 additions and 0 deletions
@@ -0,0 +1,68 @@
<?php
namespace app\mzl\modules\upload\controller;
use support\Request;
use Shopwwi\WebmanFilesystem\Facade\Storage;
use app\mzl\modules\upload\service\OssService;
use app\mzl\common\traits\ResponseTrait;
class UploadController
{
use ResponseTrait;
public function upload(Request $request)
{
try {
$scene = $request->input('scene', 'image');
$file = $request->file('file');
if ($file && $file->isValid()) {
$pathUrl = '/uploads/'.$scene.'/'.date('Ymd').'/'.md5($file->getUploadName().time()).'.'.$file->getUploadExtension();
$fullPathUrl = public_path().$pathUrl;
$file->move($fullPathUrl);
$data = [
'domain' => getenv('MAIN_DOMAIN'),
'file_url' => $pathUrl,
'full_url' => getenv('MAIN_DOMAIN').$pathUrl,
'scene' => $scene,
];
return $this->successResponse($data, 'upload success');
}
return $this->errorResponse('file not found');
} catch (\Throwable $e) {
return $this->errorResponse($e->getMessage());
}
}
// 上传单文件图片
public function uploadImage(Request $request)
{
try {
$file = $request->file('image');
if (!$file || !$file->isValid()) {
return $this->errorResponse('上传失败');
}
$result = Storage::adapter('oss')
->path('vehicle_album/'.date('Ymd'))
->size(1024*1024*5)
->extYes(['image/jpeg','image/png'])
->extNo(['image/gif'])
->upload($file);
$ossservice = new OssService();
$data = [
'image_name' => $result->file_name,
'image_url' => $ossservice->generatePrivateUrl($result->file_name)
];
return $this->successResponse($data);
}catch (\Throwable $e){
var_dump($e->getFile().'-'.$e->getLine().'-'.$e->getMessage());
return $this->errorResponse($e->getMessage());
}
}
}
@@ -0,0 +1,33 @@
<?php
namespace app\mzl\modules\upload\service;
use support\Request;
class LocalService
{
public function __construct()
{
}
/**
* 上传文件
*/
public function upload($file, $path = '', $type = 'excel')
{
// $file = $request->file('file');
if ($file && $file->isValid()) {
$pathUrl = $path == '' ? '/uploads/'.$type.'/'.date('Ymd').'/'.md5($file->getUploadName().time()).'.'.$file->getUploadExtension() : $path;
$fullPathUrl = public_path().$pathUrl;
$file->move($fullPathUrl);
$data = [
'file_url' => $pathUrl,
'full_url' => getenv('MAIN_DOMAIN').$pathUrl
];
return $data;
}
return false;
}
}
@@ -0,0 +1,59 @@
<?php
namespace app\mzl\modules\upload\service;
use OSS\OssClient;
use OSS\Core\OssException;
use support\exception\BusinessException;
class OssService
{
private $accessKeyId;
private $accessKeySecret;
private $bucket;
private $endpoint;
private $ossClient;
public function __construct()
{
$this->accessKeyId = getenv('OSS_ACCESSID');
$this->accessKeySecret = getenv('OSS_ACCESSSECRET');
$this->bucket = getenv('OSS_BUCKET');
$this->endpoint = getenv('OSS_ENDPOINT');
$this->ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
}
/**
* 上传图片到 OSS
* @param string $filePath 本地文件路径
* @param string $objectKey OSS上存储的对象名称
* @return string 图片的 URL
*/
public function uploadImage($filePath, $objectKey)
{
try {
// 上传文件到 OSS
$this->ossClient->uploadFile($this->bucket, $objectKey, $filePath);
// 构造图片的私有 URL
$url = $this->generatePrivateUrl($objectKey);
return $url;
} catch (OssException $e) {
// 错误处理
throw new \RuntimeException('OSS Error: ' . $e->getMessage());
} catch (\Throwable $th) {
// 错误处理
throw new BusinessException('Error: ' . $th->getMessage());
}
}
/**
* 生成图片的私有访问 URL
* @param string $objectKey
* @return string
*/
public function generatePrivateUrl($objectKey, $expireTime = 3600)
{
// 生成 OSS 私有 URL(有效期为 3600 秒)
return $this->ossClient->signUrl($this->bucket, $objectKey, $expireTime);
}
}