OSS封装
This commit is contained in:
@@ -88,6 +88,9 @@ class ProjectService extends BaseService
|
|||||||
$screenshots = $params['screenshots'];
|
$screenshots = $params['screenshots'];
|
||||||
$labels = $params['labels'];
|
$labels = $params['labels'];
|
||||||
$features = $params['features'];
|
$features = $params['features'];
|
||||||
|
if (empty($params['preferential_price'])) {
|
||||||
|
$params['preferential_price'] = $params['price'];
|
||||||
|
}
|
||||||
unset($params['screenshots'], $params['labels'], $params['features']);
|
unset($params['screenshots'], $params['labels'], $params['features']);
|
||||||
DB::beginTransaction();
|
DB::beginTransaction();
|
||||||
try {
|
try {
|
||||||
@@ -95,11 +98,13 @@ class ProjectService extends BaseService
|
|||||||
|
|
||||||
$labelInsertData = [];
|
$labelInsertData = [];
|
||||||
foreach ($labels as $label) {
|
foreach ($labels as $label) {
|
||||||
$labelInsertData[] = [
|
if (!empty($feature)) {
|
||||||
'project_id' => $insertProjectModel,
|
$labelInsertData[] = [
|
||||||
'label_id' => $label,
|
'project_id' => $insertProjectModel,
|
||||||
'created_at' => get_time()
|
'label_id' => $label,
|
||||||
];
|
'created_at' => get_time()
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
$insertLabelModel = ProjectLabelRelationModel::insert($labelInsertData);
|
$insertLabelModel = ProjectLabelRelationModel::insert($labelInsertData);
|
||||||
|
|
||||||
@@ -109,11 +114,13 @@ class ProjectService extends BaseService
|
|||||||
|
|
||||||
$featureInsertData = [];
|
$featureInsertData = [];
|
||||||
foreach ($features as $feature) {
|
foreach ($features as $feature) {
|
||||||
$featureInsertData[] = [
|
if (!empty($feature)) {
|
||||||
'project_id' => $insertProjectModel,
|
$featureInsertData[] = [
|
||||||
'name' => $feature,
|
'project_id' => $insertProjectModel,
|
||||||
'created_at' => get_time()
|
'name' => $feature,
|
||||||
];
|
'created_at' => get_time()
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
$insertFeatureModel = FeatureModel::insert($featureInsertData);
|
$insertFeatureModel = FeatureModel::insert($featureInsertData);
|
||||||
if (!$insertFeatureModel) {
|
if (!$insertFeatureModel) {
|
||||||
@@ -122,11 +129,13 @@ class ProjectService extends BaseService
|
|||||||
|
|
||||||
$screenInsertData = [];
|
$screenInsertData = [];
|
||||||
foreach ($screenshots as $screenshot) {
|
foreach ($screenshots as $screenshot) {
|
||||||
$screenInsertData[] = [
|
if (!empty($screenshot)) {
|
||||||
'project_id' => $insertProjectModel,
|
$screenInsertData[] = [
|
||||||
'url' => $screenshot,
|
'project_id' => $insertProjectModel,
|
||||||
'created_at' => get_time()
|
'url' => $screenshot,
|
||||||
];
|
'created_at' => get_time()
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
$insertScreenModel = ScreenshotModel::insert($screenInsertData);
|
$insertScreenModel = ScreenshotModel::insert($screenInsertData);
|
||||||
if (!$insertScreenModel) {
|
if (!$insertScreenModel) {
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ use App\BaseApp\BaseService;
|
|||||||
use App\Models\ProjectModel;
|
use App\Models\ProjectModel;
|
||||||
use App\Models\RoleModel;
|
use App\Models\RoleModel;
|
||||||
use App\Models\UserModel;
|
use App\Models\UserModel;
|
||||||
|
use App\Service\common\upload\LocalhostStorageService;
|
||||||
use App\Service\common\upload\QiniuStorageService;
|
use App\Service\common\upload\QiniuStorageService;
|
||||||
use App\Service\FileService;
|
use App\Service\FileService;
|
||||||
use Exception;
|
use Exception;
|
||||||
@@ -15,10 +16,26 @@ use Psr\Container\NotFoundExceptionInterface;
|
|||||||
|
|
||||||
class UploadService extends BaseService
|
class UploadService extends BaseService
|
||||||
{
|
{
|
||||||
|
private $uploadService;
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
$this->model = RoleModel::class;
|
$this->model = RoleModel::class;
|
||||||
|
|
||||||
|
switch (env('ECS')) {
|
||||||
|
case 'aliyun':
|
||||||
|
// $this->uploadService = AliyunStorageService::getInstance();
|
||||||
|
break;
|
||||||
|
case 'qcloud':
|
||||||
|
// $this->uploadService = QcloudStorageService::getInstance();
|
||||||
|
break;
|
||||||
|
case 'qiniu':
|
||||||
|
$this->uploadService = QiniuStorageService::getInstance();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
$this->uploadService = LocalhostStorageService::getInstance();
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -32,11 +49,11 @@ class UploadService extends BaseService
|
|||||||
// 获取文件后缀
|
// 获取文件后缀
|
||||||
$ext = $file->getClientOriginalExtension();
|
$ext = $file->getClientOriginalExtension();
|
||||||
$key = 'spa/image/'. date('Ymd') . '/' . 'cc_upload_'. Str::random(). uniqid() . '.' . $ext;
|
$key = 'spa/image/'. date('Ymd') . '/' . 'cc_upload_'. Str::random(). uniqid() . '.' . $ext;
|
||||||
// $result = QiniuStorageService::getInstance()->uploadVideo($file, $key);
|
$result = $this->uploadService->uploadVideo($file, $key);
|
||||||
$result = [
|
// $result = [
|
||||||
'key' => $key,
|
// 'key' => $key,
|
||||||
'url' => 'https://img2.baidu.com/it/u=1629222614,1358629025&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500&a='. rand(1111111, 9999999)
|
// 'url' => 'https://img2.baidu.com/it/u=1629222614,1358629025&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500&a='. rand(1111111, 9999999)
|
||||||
];
|
// ];
|
||||||
FileService::getInstance()->create([
|
FileService::getInstance()->create([
|
||||||
'user_id' => $this->userId,
|
'user_id' => $this->userId,
|
||||||
'url' => $result['url'],
|
'url' => $result['url'],
|
||||||
@@ -56,11 +73,11 @@ class UploadService extends BaseService
|
|||||||
// 获取文件后缀
|
// 获取文件后缀
|
||||||
$ext = $file->getClientOriginalExtension();
|
$ext = $file->getClientOriginalExtension();
|
||||||
$key = 'spa/video/'. date('Ymd') . '/' . 'cc_upload_'. Str::random(). uniqid() . '.' . $ext;
|
$key = 'spa/video/'. date('Ymd') . '/' . 'cc_upload_'. Str::random(). uniqid() . '.' . $ext;
|
||||||
// $result = QiniuStorageService::getInstance()->uploadVideo($file, $key);
|
$result = $this->uploadService->uploadVideo($file, $key);
|
||||||
$result = [
|
// $result = [
|
||||||
'key' => $key,
|
// 'key' => $key,
|
||||||
'url' => 'https://img2.baidu.com/it/u=1629222614,1358629025&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500&a='. rand(1111111, 9999999)
|
// 'url' => 'http://d-jy.nailaoyun.cn//storage/video//20250327/2cc1abf9bd69410ce7c69660daf54260.mp4'
|
||||||
];
|
// ];
|
||||||
FileService::getInstance()->create([
|
FileService::getInstance()->create([
|
||||||
'user_id' => $this->userId,
|
'user_id' => $this->userId,
|
||||||
'url' => $result['url'],
|
'url' => $result['url'],
|
||||||
|
|||||||
108
app/Service/common/upload/LocalhostStorageService.php
Normal file
108
app/Service/common/upload/LocalhostStorageService.php
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Service\common\upload;
|
||||||
|
|
||||||
|
use App\BaseApp\BaseService;
|
||||||
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
|
||||||
|
class LocalhostStorageService extends BaseService
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var mixed 单例实例,确保该类只有一个全局实例
|
||||||
|
*/
|
||||||
|
protected static mixed $_instance;
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
parent::__construct();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取实例
|
||||||
|
* @return null|static
|
||||||
|
*/
|
||||||
|
public static function getInstance(): null|static
|
||||||
|
{
|
||||||
|
$name = get_called_class();
|
||||||
|
if (!isset(self::$_instance[$name])) {
|
||||||
|
self::$_instance[$name] = new static();
|
||||||
|
}
|
||||||
|
|
||||||
|
return self::$_instance[$name];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上传图片
|
||||||
|
*
|
||||||
|
* @param string $filePath 文件本地路径
|
||||||
|
* @param string $key 上传到本地存储的文件名
|
||||||
|
* @return array|bool
|
||||||
|
*/
|
||||||
|
public function uploadImage($filePath, $key): bool|array
|
||||||
|
{
|
||||||
|
return $this->uploadFile($filePath, $key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上传视频
|
||||||
|
*
|
||||||
|
* @param string $filePath 文件本地路径
|
||||||
|
* @param string $key 上传到本地存储的文件名
|
||||||
|
* @return array|bool
|
||||||
|
*/
|
||||||
|
public function uploadVideo($filePath, $key): bool|array
|
||||||
|
{
|
||||||
|
return $this->uploadFile($filePath, $key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除图片
|
||||||
|
*
|
||||||
|
* @param string $key 本地存储的文件名
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function deleteImage($key): bool
|
||||||
|
{
|
||||||
|
return $this->deleteFile($key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除视频
|
||||||
|
*
|
||||||
|
* @param string $key 本地存储的文件名
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function deleteVideo($key): bool
|
||||||
|
{
|
||||||
|
return $this->deleteFile($key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上传文件
|
||||||
|
*
|
||||||
|
* @param string $filePath 文件本地路径
|
||||||
|
* @param string $key 上传到本地存储的文件名
|
||||||
|
* @return array|bool
|
||||||
|
*/
|
||||||
|
private function uploadFile(string $filePath, string $key): bool|array
|
||||||
|
{
|
||||||
|
if (Storage::put($key, file_get_contents($filePath))) {
|
||||||
|
return [
|
||||||
|
'key' => $key,
|
||||||
|
'url' => asset('/storage/' . $key)
|
||||||
|
];
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除文件
|
||||||
|
*
|
||||||
|
* @param string $key 本地存储的文件名
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
private function deleteFile(string $key): bool
|
||||||
|
{
|
||||||
|
return Storage::delete($key);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -32,7 +32,7 @@ return [
|
|||||||
|
|
||||||
'local' => [
|
'local' => [
|
||||||
'driver' => 'local',
|
'driver' => 'local',
|
||||||
'root' => storage_path('app/private'),
|
'root' => storage_path('app/public'),
|
||||||
'serve' => true,
|
'serve' => true,
|
||||||
'throw' => false,
|
'throw' => false,
|
||||||
'report' => false,
|
'report' => false,
|
||||||
|
|||||||
Reference in New Issue
Block a user