项目相关接口、图片视频上传
This commit is contained in:
@@ -141,16 +141,18 @@ class BaseController
|
||||
$result = [];
|
||||
foreach ($checkFields as $fieldName) {
|
||||
$checkValue = $data[$fieldName] ?? '';
|
||||
if (is_array($checkValue)) {
|
||||
$checkValue = json_encode($checkValue);
|
||||
}
|
||||
if (mb_strlen(strval($checkValue)) <= 0 && !in_array($fieldName, $this->notRequest)) {
|
||||
$requiredFields[] = $fieldName;
|
||||
if (is_array($checkValue) && !empty($checkValue)) {
|
||||
$result[$fieldName] = $checkValue;
|
||||
} else {
|
||||
if (!empty($checkValue) || $checkValue === 0) {
|
||||
$result[$fieldName] = $checkValue;
|
||||
if (mb_strlen(strval($checkValue)) <= 0 && !in_array($fieldName, $this->notRequest)) {
|
||||
$requiredFields[] = $fieldName;
|
||||
} else {
|
||||
if (!empty($checkValue) || $checkValue === 0) {
|
||||
$result[$fieldName] = $checkValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if ($throw && $requiredFields) {
|
||||
|
||||
22
app/Http/Controllers/CategoryController.php
Normal file
22
app/Http/Controllers/CategoryController.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\BaseApp\BaseController;
|
||||
use App\Service\CategoryService;
|
||||
use App\Service\LabelService;
|
||||
use App\Service\ProjectService;
|
||||
use App\Service\RoleService;
|
||||
|
||||
class CategoryController extends BaseController
|
||||
{
|
||||
//
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->service = CategoryService::getInstance();
|
||||
$this->insertField = ['name', 'pid', 'value'];
|
||||
$this->updateField = ['id', 'name', 'pid', 'value'];
|
||||
}
|
||||
}
|
||||
21
app/Http/Controllers/LabelController.php
Normal file
21
app/Http/Controllers/LabelController.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\BaseApp\BaseController;
|
||||
use App\Service\LabelService;
|
||||
use App\Service\ProjectService;
|
||||
use App\Service\RoleService;
|
||||
|
||||
class LabelController extends BaseController
|
||||
{
|
||||
//
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->service = LabelService::getInstance();
|
||||
$this->insertField = ['name', 'pid', 'value'];
|
||||
$this->updateField = ['id', 'name', 'pid', 'value'];
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,8 @@ class ProjectController extends BaseController
|
||||
{
|
||||
parent::__construct();
|
||||
$this->service = ProjectService::getInstance();
|
||||
$this->insertField = [ 'user_id', 'title', 'cover', 'description', 'video_url', 'author_id', 'price', 'preferential_price', 'front', 'service', 'deploy', 'status'];
|
||||
$this->updateField = [ 'id', 'user_id', 'title', 'cover', 'description', 'video_url', 'author_id', 'price', 'preferential_price', 'front', 'service', 'deploy', 'status'];
|
||||
$this->insertField = [ 'title', 'cover', 'description', 'category_id', 'video_url', 'price', 'front', 'service', 'deploy', 'status'];
|
||||
$this->updateField = [ 'id', 'title', 'cover', 'description', 'category_id', 'video_url', 'price', 'front', 'service', 'deploy', 'status'];
|
||||
$this->notRequest = ['features', 'screenshots', 'tags'];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ class RoleController extends BaseController
|
||||
{
|
||||
parent::__construct();
|
||||
$this->service = RoleService::getInstance();
|
||||
$this->insertField = [ 'user_id', 'title', 'cover', 'description', 'video_url', 'author_id', 'price', 'preferential_price', 'front', 'service', 'deploy', 'status'];
|
||||
$this->updateField = [ 'id', 'user_id', 'title', 'cover', 'description', 'video_url', 'author_id', 'price', 'preferential_price', 'front', 'service', 'deploy', 'status'];
|
||||
$this->insertField = ['name', 'pid', 'value'];
|
||||
$this->updateField = ['id', 'name', 'pid', 'value'];
|
||||
}
|
||||
}
|
||||
|
||||
48
app/Http/Controllers/UploadController.php
Normal file
48
app/Http/Controllers/UploadController.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\BaseApp\BaseController;
|
||||
use App\Service\common\UploadService;
|
||||
use App\Service\ProjectService;
|
||||
use App\Service\RoleService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
class UploadController extends BaseController
|
||||
{
|
||||
//
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->service = UploadService::getInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
* 图片上传
|
||||
* @Method POST
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function image()
|
||||
{
|
||||
$file = request()->file('file');
|
||||
return jok(
|
||||
$this->service->uploadImage($file),
|
||||
'上传成功'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 视频上传
|
||||
* @Method POST
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function video()
|
||||
{
|
||||
$file = request()->file('file');
|
||||
return jok(
|
||||
$this->service->uploadVideo($file),
|
||||
'上传成功'
|
||||
);
|
||||
}
|
||||
}
|
||||
84
app/Service/CategoryService.php
Normal file
84
app/Service/CategoryService.php
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\BaseApp\BaseService;
|
||||
use App\Models\CategoryModel;
|
||||
use App\Models\LabelModel;
|
||||
use App\Models\ProjectModel;
|
||||
use App\Models\RoleModel;
|
||||
use App\Models\UserModel;
|
||||
use Exception;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
|
||||
class CategoryService extends BaseService
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->selectField = ['id', 'name', 'created_at', 'updated_at', 'deleted_at'];
|
||||
$this->model = CategoryModel::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取项目列表
|
||||
* @return array
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function list(): array
|
||||
{
|
||||
return $this->getPageList();
|
||||
}
|
||||
|
||||
public function detail($id)
|
||||
{
|
||||
return $this->getDetail($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 项目下拉列表
|
||||
* @return mixed
|
||||
*/
|
||||
public function option()
|
||||
{
|
||||
$this->optionField = ['id', 'name'];
|
||||
return $this->getOption();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建项目
|
||||
* @param $params
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function create($params): mixed
|
||||
{
|
||||
return $this->insert($params);
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑项目
|
||||
* @param $id
|
||||
* @param $params
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function update($id, $params): mixed
|
||||
{
|
||||
return $this->save($id, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除项目
|
||||
* @param $ids
|
||||
* @return mixed|true
|
||||
* @throws Exception
|
||||
*/
|
||||
public function delete($ids): mixed
|
||||
{
|
||||
return $this->del($ids);
|
||||
}
|
||||
|
||||
}
|
||||
83
app/Service/LabelService.php
Normal file
83
app/Service/LabelService.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\BaseApp\BaseService;
|
||||
use App\Models\LabelModel;
|
||||
use App\Models\ProjectModel;
|
||||
use App\Models\RoleModel;
|
||||
use App\Models\UserModel;
|
||||
use Exception;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
|
||||
class LabelService extends BaseService
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->selectField = ['id', 'name', 'created_at', 'updated_at', 'deleted_at'];
|
||||
$this->model = LabelModel::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取项目列表
|
||||
* @return array
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function list(): array
|
||||
{
|
||||
return $this->getPageList();
|
||||
}
|
||||
|
||||
public function detail($id)
|
||||
{
|
||||
return $this->getDetail($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 项目下拉列表
|
||||
* @return mixed
|
||||
*/
|
||||
public function option()
|
||||
{
|
||||
$this->optionField = ['id', 'name'];
|
||||
return $this->getOption();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建项目
|
||||
* @param $params
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function create($params): mixed
|
||||
{
|
||||
return $this->insert($params);
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑项目
|
||||
* @param $id
|
||||
* @param $params
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function update($id, $params): mixed
|
||||
{
|
||||
return $this->save($id, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除项目
|
||||
* @param $ids
|
||||
* @return mixed|true
|
||||
* @throws Exception
|
||||
*/
|
||||
public function delete($ids): mixed
|
||||
{
|
||||
return $this->del($ids);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,9 +3,13 @@
|
||||
namespace App\Service;
|
||||
|
||||
use App\BaseApp\BaseService;
|
||||
use App\Models\FeatureModel;
|
||||
use App\Models\ProjectLabelRelationModel;
|
||||
use App\Models\ProjectModel;
|
||||
use App\Models\ScreenshotModel;
|
||||
use App\Models\UserModel;
|
||||
use Exception;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
|
||||
@@ -66,7 +70,66 @@ class ProjectService extends BaseService
|
||||
$params['author_id'] = $params['author_id']?? $this->userId;
|
||||
// TODO 项目
|
||||
$screenshots = $params['screenshots'];
|
||||
return $this->insert($params);
|
||||
$labels = $params['tags'];
|
||||
$features = $params['features'];
|
||||
unset($params['screenshots'], $params['tags'], $params['features']);
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
$insertProjectModel = $this->insert($params);
|
||||
|
||||
$labelInsertData = [];
|
||||
foreach ($labels as $label) {
|
||||
$labelInsertData[] = [
|
||||
'project_id' => $insertProjectModel,
|
||||
'label_id' => $label,
|
||||
'created_at' => get_time()
|
||||
];
|
||||
}
|
||||
$insertLabelModel = ProjectLabelRelationModel::insert($labelInsertData);
|
||||
|
||||
if (!$insertLabelModel) {
|
||||
$this->utils->errorThrow('标签添加失败');
|
||||
}
|
||||
|
||||
$featureInsertData = [];
|
||||
foreach ($features as $feature) {
|
||||
$featureInsertData[] = [
|
||||
'project_id' => $insertProjectModel,
|
||||
'name' => $feature,
|
||||
'created_at' => get_time()
|
||||
];
|
||||
}
|
||||
$insertFeatureModel = FeatureModel::insert($featureInsertData);
|
||||
if (!$insertFeatureModel) {
|
||||
$this->utils->errorThrow('项目功能添加失败');
|
||||
}
|
||||
|
||||
$screenInsertData = [];
|
||||
foreach ($screenshots as $screenshot) {
|
||||
$screenInsertData[] = [
|
||||
'project_id' => $insertProjectModel,
|
||||
'url' => $screenshot,
|
||||
'created_at' => get_time()
|
||||
];
|
||||
}
|
||||
$insertScreenModel = ScreenshotModel::insert($screenInsertData);
|
||||
if (!$insertScreenModel) {
|
||||
$this->utils->errorThrow('项目截图添加失败');
|
||||
}
|
||||
|
||||
DB::commit();
|
||||
} catch (Exception $e) {
|
||||
DB::rollBack();
|
||||
ds([
|
||||
'message' => $e->getMessage(),
|
||||
'line' => $e->getLine(),
|
||||
'file' => $e->getFile(),
|
||||
'code' => $e->getCode(),
|
||||
]);
|
||||
$this->utils->errorThrow($e->getMessage());
|
||||
}
|
||||
|
||||
return $insertProjectModel;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
47
app/Service/common/UploadService.php
Normal file
47
app/Service/common/UploadService.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service\common;
|
||||
|
||||
use App\BaseApp\BaseService;
|
||||
use App\Models\ProjectModel;
|
||||
use App\Models\RoleModel;
|
||||
use App\Models\UserModel;
|
||||
use App\Service\common\upload\QiniuStorageService;
|
||||
use Exception;
|
||||
use Illuminate\Support\Str;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
|
||||
class UploadService extends BaseService
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->model = RoleModel::class;
|
||||
}
|
||||
|
||||
public function uploadImage($file)
|
||||
{
|
||||
// 获取文件后缀
|
||||
$ext = $file->getClientOriginalExtension();
|
||||
$key = 'spa/image/'. date('Ymd') . '/' . 'cc_upload_'. Str::random(). uniqid() . '.' . $ext;
|
||||
return [
|
||||
'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)
|
||||
];
|
||||
return QiniuStorageService::getInstance()->uploadImage($file, $key);
|
||||
}
|
||||
|
||||
public function uploadVideo($file)
|
||||
{
|
||||
// 获取文件后缀
|
||||
$ext = $file->getClientOriginalExtension();
|
||||
$key = 'spa/video/'. date('Ymd') . '/' . 'cc_upload_'. Str::random(). uniqid() . '.' . $ext;
|
||||
return [
|
||||
'key' => $key,
|
||||
'url' => 'http://d-jy.nailaoyun.cn/storage/video//20250410/ca1bbbb3af5853c7fc4ef23b8a918922.mp4?a='. rand(1111111, 9999999)
|
||||
];
|
||||
return QiniuStorageService::getInstance()->uploadVideo($file, $key);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,13 +2,18 @@
|
||||
|
||||
namespace App\Service\common\upload;
|
||||
|
||||
use App\BaseApp\BaseService;
|
||||
use Exception;
|
||||
use Qiniu\Auth;
|
||||
use Qiniu\Storage\BucketManager;
|
||||
use Qiniu\Storage\UploadManager;
|
||||
|
||||
class QiniuStorageService
|
||||
class QiniuStorageService extends BaseService
|
||||
{
|
||||
/**
|
||||
* @var mixed 单例实例,确保该类只有一个全局实例
|
||||
*/
|
||||
protected static mixed $_instance;
|
||||
private $accessKey;
|
||||
private $secretKey;
|
||||
private $bucket;
|
||||
@@ -16,10 +21,26 @@ class QiniuStorageService
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->accessKey = config('cc.oss.qiniu.access_key');
|
||||
$this->secretKey = config('cc.oss.qiniu.secret_key');
|
||||
$this->bucket = config('cc.oss.qiniu.bucket');
|
||||
$this->domain = config('cc.oss.qiniu.');
|
||||
$this->domain = config('cc.oss.qiniu.domain');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取实例
|
||||
* @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];
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
12
composer.lock
generated
12
composer.lock
generated
@@ -1520,16 +1520,16 @@
|
||||
},
|
||||
{
|
||||
"name": "league/commonmark",
|
||||
"version": "2.6.2",
|
||||
"version": "2.7.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/thephpleague/commonmark.git",
|
||||
"reference": "06c3b0bf2540338094575612f4a1778d0d2d5e94"
|
||||
"reference": "6fbb36d44824ed4091adbcf4c7d4a3923cdb3405"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/thephpleague/commonmark/zipball/06c3b0bf2540338094575612f4a1778d0d2d5e94",
|
||||
"reference": "06c3b0bf2540338094575612f4a1778d0d2d5e94",
|
||||
"url": "https://api.github.com/repos/thephpleague/commonmark/zipball/6fbb36d44824ed4091adbcf4c7d4a3923cdb3405",
|
||||
"reference": "6fbb36d44824ed4091adbcf4c7d4a3923cdb3405",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
@@ -1566,7 +1566,7 @@
|
||||
"type": "library",
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-main": "2.7-dev"
|
||||
"dev-main": "2.8-dev"
|
||||
}
|
||||
},
|
||||
"autoload": {
|
||||
@@ -1623,7 +1623,7 @@
|
||||
"type": "tidelift"
|
||||
}
|
||||
],
|
||||
"time": "2025-04-18T21:09:27+00:00"
|
||||
"time": "2025-05-05T12:20:28+00:00"
|
||||
},
|
||||
{
|
||||
"name": "league/config",
|
||||
|
||||
@@ -55,7 +55,7 @@ return [
|
||||
'access_key' => 'RPQlZUd2yrL3kR8CC4BpTGV6FhGr11npBzf6IuCT',
|
||||
'secret_key' => 'b-796uVY1InOKSVecpY8VBpO7Cdubo1Tv4RcOfMY',
|
||||
'bucket' => 'ccspa',
|
||||
'domain' => 'svso4t548.hd-bkt.clouddn.com',
|
||||
'domain' => 'http://o-spa.nailaoyun.cn',
|
||||
]
|
||||
],
|
||||
/*
|
||||
|
||||
@@ -19,5 +19,8 @@ Route::group([], function () {
|
||||
'user' => \App\Http\Controllers\UserController::class, // 用户控制器
|
||||
'project' => \App\Http\Controllers\ProjectController::class, // 项目控制器
|
||||
'role' => \App\Http\Controllers\RoleController::class, // 角色控制器
|
||||
'upload' => \App\Http\Controllers\UploadController::class, // 上传控制器
|
||||
'label' => \App\Http\Controllers\LabelController::class, // 标签控制器
|
||||
'category' => \App\Http\Controllers\CategoryController::class, // 分类控制器
|
||||
]);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user