diff --git a/app/BaseApp/BaseController.php b/app/BaseApp/BaseController.php index 4f7490bf..1e091bc6 100755 --- a/app/BaseApp/BaseController.php +++ b/app/BaseApp/BaseController.php @@ -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) { diff --git a/app/Http/Controllers/CategoryController.php b/app/Http/Controllers/CategoryController.php new file mode 100644 index 00000000..0b84b633 --- /dev/null +++ b/app/Http/Controllers/CategoryController.php @@ -0,0 +1,22 @@ +service = CategoryService::getInstance(); + $this->insertField = ['name', 'pid', 'value']; + $this->updateField = ['id', 'name', 'pid', 'value']; + } +} diff --git a/app/Http/Controllers/LabelController.php b/app/Http/Controllers/LabelController.php new file mode 100644 index 00000000..3ade0e45 --- /dev/null +++ b/app/Http/Controllers/LabelController.php @@ -0,0 +1,21 @@ +service = LabelService::getInstance(); + $this->insertField = ['name', 'pid', 'value']; + $this->updateField = ['id', 'name', 'pid', 'value']; + } +} diff --git a/app/Http/Controllers/ProjectController.php b/app/Http/Controllers/ProjectController.php index e03c6406..0286684c 100644 --- a/app/Http/Controllers/ProjectController.php +++ b/app/Http/Controllers/ProjectController.php @@ -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']; } } diff --git a/app/Http/Controllers/RoleController.php b/app/Http/Controllers/RoleController.php index 5133dce9..45ea8837 100644 --- a/app/Http/Controllers/RoleController.php +++ b/app/Http/Controllers/RoleController.php @@ -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']; } } diff --git a/app/Http/Controllers/UploadController.php b/app/Http/Controllers/UploadController.php new file mode 100644 index 00000000..78656647 --- /dev/null +++ b/app/Http/Controllers/UploadController.php @@ -0,0 +1,48 @@ +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), + '上传成功' + ); + } +} diff --git a/app/Service/CategoryService.php b/app/Service/CategoryService.php new file mode 100644 index 00000000..fb2f789c --- /dev/null +++ b/app/Service/CategoryService.php @@ -0,0 +1,84 @@ +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); + } + +} diff --git a/app/Service/LabelService.php b/app/Service/LabelService.php new file mode 100644 index 00000000..89c5814a --- /dev/null +++ b/app/Service/LabelService.php @@ -0,0 +1,83 @@ +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); + } + +} diff --git a/app/Service/ProjectService.php b/app/Service/ProjectService.php index bf77d2ba..7a42d810 100644 --- a/app/Service/ProjectService.php +++ b/app/Service/ProjectService.php @@ -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; } /** diff --git a/app/Service/common/UploadService.php b/app/Service/common/UploadService.php new file mode 100644 index 00000000..6027f041 --- /dev/null +++ b/app/Service/common/UploadService.php @@ -0,0 +1,47 @@ +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); + } + +} diff --git a/app/Service/common/upload/QiniuStorageService.php b/app/Service/common/upload/QiniuStorageService.php index 83abd253..8cf9e656 100644 --- a/app/Service/common/upload/QiniuStorageService.php +++ b/app/Service/common/upload/QiniuStorageService.php @@ -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]; } /** diff --git a/composer.lock b/composer.lock index 278bb939..13f65da7 100644 --- a/composer.lock +++ b/composer.lock @@ -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", diff --git a/config/cc.php b/config/cc.php index e0828ec1..693e25e2 100755 --- a/config/cc.php +++ b/config/cc.php @@ -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', ] ], /* diff --git a/routes/api.php b/routes/api.php index 85a733c2..b31fe2d5 100644 --- a/routes/api.php +++ b/routes/api.php @@ -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, // 分类控制器 ]); });