From d3bb8380e25ad0fee59522c2d08140e78714e032 Mon Sep 17 00:00:00 2001 From: lq Date: Wed, 7 May 2025 14:48:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=89=8D=E5=8F=B0=E9=83=A8=E5=88=86=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Enum/ProjectStatusEnum.php | 25 ++++++ app/Http/Controllers/CollectionController.php | 18 ++++ app/Http/Controllers/FileController.php | 21 +++++ app/Http/Controllers/HomeController.php | 36 ++++++++ app/Http/Controllers/UserController.php | 28 ++++++ app/Models/CollectionModel.php | 36 ++++++++ app/Models/FileModel.php | 27 ++++++ .../ProjectTechnologyStacksRelationModel.php | 16 ---- app/Models/TechnologyStackModel.php | 26 ------ app/Models/UserPayModel.php | 36 ++++++++ app/Service/CollectionService.php | 90 +++++++++++++++++++ app/Service/FileService.php | 83 +++++++++++++++++ app/Service/HomeService.php | 76 ++++++++++++++++ app/Service/LoginService.php | 20 +++-- app/Service/ProjectService.php | 9 +- app/Service/UserService.php | 47 ++++++++++ app/Service/common/JWTService.php | 2 +- app/Service/common/UploadService.php | 39 ++++++-- routes/api.php | 2 + 19 files changed, 575 insertions(+), 62 deletions(-) create mode 100755 app/Enum/ProjectStatusEnum.php create mode 100644 app/Http/Controllers/CollectionController.php create mode 100644 app/Http/Controllers/FileController.php create mode 100644 app/Http/Controllers/HomeController.php create mode 100644 app/Models/CollectionModel.php create mode 100644 app/Models/FileModel.php delete mode 100644 app/Models/ProjectTechnologyStacksRelationModel.php delete mode 100644 app/Models/TechnologyStackModel.php create mode 100644 app/Models/UserPayModel.php create mode 100644 app/Service/CollectionService.php create mode 100644 app/Service/FileService.php create mode 100644 app/Service/HomeService.php diff --git a/app/Enum/ProjectStatusEnum.php b/app/Enum/ProjectStatusEnum.php new file mode 100755 index 00000000..f4eca704 --- /dev/null +++ b/app/Enum/ProjectStatusEnum.php @@ -0,0 +1,25 @@ + '草稿', + self::ON_SHELF => '上架', + self::OFF_SHELF => '下架', + self::REBUILD => '重构中', + default => '未知状态', + }; + } +} diff --git a/app/Http/Controllers/CollectionController.php b/app/Http/Controllers/CollectionController.php new file mode 100644 index 00000000..15f95321 --- /dev/null +++ b/app/Http/Controllers/CollectionController.php @@ -0,0 +1,18 @@ +service = CollectionService::getInstance(); + $this->insertField = ['project_id']; + } +} diff --git a/app/Http/Controllers/FileController.php b/app/Http/Controllers/FileController.php new file mode 100644 index 00000000..1becc4af --- /dev/null +++ b/app/Http/Controllers/FileController.php @@ -0,0 +1,21 @@ +service = FileService::getInstance(); + $this->insertField = ['url', 's']; + $this->updateField = ['id', 'url', 's']; + } +} diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php new file mode 100644 index 00000000..7bd56920 --- /dev/null +++ b/app/Http/Controllers/HomeController.php @@ -0,0 +1,36 @@ +service = HomeService::getInstance(); + } + + /** + * 轮播图列表 + * @Method GET + * @return JsonResponse + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function carousel(): JsonResponse + { + return jok( + $this->service->carousel(), + '轮播图列表' + ); + } +} diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 70080246..2e0acbbe 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -53,4 +53,32 @@ class UserController extends BaseController '重置密码成功' ); } + + /** + * 获取当前用户信息 + * @Method GET + * @return JsonResponse + * @throws Exception + */ + public function myInfo(): JsonResponse + { + return jok( + $this->service->myInfo(), + '获取成功' + ); + } + + /** + * 获取当前用户信息 + * @Method GET + * @return JsonResponse + * @throws Exception + */ + public function userStats(): JsonResponse + { + return jok( + $this->service->userStats(), + '获取成功' + ); + } } diff --git a/app/Models/CollectionModel.php b/app/Models/CollectionModel.php new file mode 100644 index 00000000..6446c56f --- /dev/null +++ b/app/Models/CollectionModel.php @@ -0,0 +1,36 @@ +hasOne(UserModel::class, 'id', 'user_id'); + } + + /** + * 项目关联 + * @return HasOne + */ + public function project(): HasOne + { + return $this->hasOne(ProjectModel::class, 'id', 'project_id'); + } +} diff --git a/app/Models/FileModel.php b/app/Models/FileModel.php new file mode 100644 index 00000000..6b0f8a20 --- /dev/null +++ b/app/Models/FileModel.php @@ -0,0 +1,27 @@ +hasOne(UserModel::class, 'id', 'user_id'); + } +} diff --git a/app/Models/ProjectTechnologyStacksRelationModel.php b/app/Models/ProjectTechnologyStacksRelationModel.php deleted file mode 100644 index 18ca3737..00000000 --- a/app/Models/ProjectTechnologyStacksRelationModel.php +++ /dev/null @@ -1,16 +0,0 @@ -belongsToMany(ProjectModel::class, 'project_technology_stack_relations', 'technology_stack_id', 'project_id'); - } -} diff --git a/app/Models/UserPayModel.php b/app/Models/UserPayModel.php new file mode 100644 index 00000000..d7185c71 --- /dev/null +++ b/app/Models/UserPayModel.php @@ -0,0 +1,36 @@ +hasOne(UserModel::class, 'id', 'user_id'); + } + + /** + * 项目关联 + * @return HasOne + */ + public function project(): HasOne + { + return $this->hasOne(ProjectModel::class, 'id', 'project_id'); + } +} diff --git a/app/Service/CollectionService.php b/app/Service/CollectionService.php new file mode 100644 index 00000000..ad427d04 --- /dev/null +++ b/app/Service/CollectionService.php @@ -0,0 +1,90 @@ +selectField = ['id', 'user_id', 'project_id', 'created_at', 'updated_at', 'deleted_at']; + $this->model = CollectionModel::class; + $this->where[] = ['user_id', '=', $this->userId]; + } + + /** + * 获取用户收藏列表 + * @return array + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function list(): array + { + $this->with = [ + 'project:id,title,cover,category_id,description,price,preferential_price,front,service,deploy,status,created_at,updated_at', + 'project.category:id,name', + 'project.labels:id,name', + ]; + return $this->getPageList(); + } + + public function detail($id) + { + return $this->getDetail($id); + } + + /** + * 用户收藏下拉列表 + * @return mixed + */ + public function option() + { + $this->optionField = ['id', 'url']; + 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/FileService.php b/app/Service/FileService.php new file mode 100644 index 00000000..282ee607 --- /dev/null +++ b/app/Service/FileService.php @@ -0,0 +1,83 @@ +selectField = ['id', 'user_id', 'url', 'type', 'created_at', 'updated_at', 'deleted_at']; + $this->model = FileModel::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', 'url']; + 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/HomeService.php b/app/Service/HomeService.php new file mode 100644 index 00000000..e99d3680 --- /dev/null +++ b/app/Service/HomeService.php @@ -0,0 +1,76 @@ +isAuth = false; + parent::__construct(); + $this->selectField = [ 'id', 'user_id', 'title', 'cover', 'category_id', 'description', 'video_url', 'author_id', 'price', 'preferential_price', 'front', 'service', 'deploy', 'status', 'created_at', 'updated_at', 'deleted_at', ]; + $this->model = ProjectModel::class; + } + + /** + * 获取项目列表 + * @return array + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function list(): array + { + $this->where[] = [ 'is_selected', '=', 1 ]; + $this->where[] = [ 'status', '=', 1 ]; + $this->with = [ + 'frameworks:id,name', + 'labels:id,name', + 'category:id,name', + ]; + return $this->getPageList(); + } + + /** + * 获取项目列表 + * @return array + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function carousel(): array + { + $this->where[] = [ 'is_carousel', '=', 1 ]; + $this->where[] = [ 'status', '=', 1 ]; + $this->with = [ + 'frameworks:id,name', + 'labels:id,name', + 'category:id,name', + ]; + return $this->getPageList(); + } + + public function detail($id) + { + $this->with = [ + 'frameworks:id,name', + 'labels:id,name', + 'category:id,name', + 'screenshots:id,url,project_id', + 'features:id,project_id,name', + 'author:id,nick_name,avatar,email' + ]; + return $this->getDetail($id); + } +} diff --git a/app/Service/LoginService.php b/app/Service/LoginService.php index 2067c461..5cb70c2a 100644 --- a/app/Service/LoginService.php +++ b/app/Service/LoginService.php @@ -114,16 +114,18 @@ class LoginService UtilsService::getInstance()->errorThrow('注册失败!'); } + $userInfo = UserModel::with(['roles:id,name,value'])->where('id', $createModel->id)->first(); + $result = [ - 'id' => $createModel->id, - 'account' => $createModel->account, - 'nick_name' => $createModel->nick_name, - 'avatar' => $createModel->avatar, - 'email' => $userModel->email?? '', - 'role_name' => $userModel->roles->name, - 'role_value' => $userModel->roles->value, - 'ip' => $createModel->ip, - 'ip_table' => $createModel->ip_table, + 'id' => $userInfo->id, + 'account' => $userInfo->account, + 'nick_name' => $userInfo->nick_name, + 'avatar' => $userInfo->avatar, + 'email' => $userInfo->email?? '', + 'role_name' => $userInfo->roles->name, + 'role_value' => $userInfo->roles->value, + 'ip' => $userInfo->ip, + 'ip_table' => $userInfo->ip_table, ]; $token = JWTService::getInstance()->generateToken($result); $result['token'] = $token; diff --git a/app/Service/ProjectService.php b/app/Service/ProjectService.php index 8bcde3d9..074993f6 100644 --- a/app/Service/ProjectService.php +++ b/app/Service/ProjectService.php @@ -3,6 +3,7 @@ namespace App\Service; use App\BaseApp\BaseService; +use App\Enum\ProjectStatusEnum; use App\Enum\StatusEnum; use App\Models\FeatureModel; use App\Models\ProjectLabelRelationModel; @@ -36,12 +37,13 @@ class ProjectService extends BaseService 'labels:id,name', 'category:id,name', 'screenshots:id,url,project_id', -// 'features:id,project_id,name' +// 'features:id,project_id,name', + 'author:id,nick_name,avatar' ]; $result = $this->getPageList(); foreach ($result['items'] as &$item) { - $item['status_text'] = StatusEnum::from($item['status'])->description(); + $item['status_text'] = ProjectStatusEnum::from($item['status'])->description(); $item['label_ids'] = array_column($item['labels'], 'id'); $item['screenshots'] = array_column($item['screenshots'], 'url'); } @@ -55,7 +57,8 @@ class ProjectService extends BaseService 'labels:id,name', 'category:id,name', 'screenshots:id,url,project_id', - 'features:id,project_id,name' + 'features:id,project_id,name', + 'author:id,nick_name,avatar' ]; return $this->getDetail($id); } diff --git a/app/Service/UserService.php b/app/Service/UserService.php index b8f4f177..aa2b05c0 100644 --- a/app/Service/UserService.php +++ b/app/Service/UserService.php @@ -4,7 +4,10 @@ namespace App\Service; use App\BaseApp\BaseService; use App\Enum\UserStatusEnum; +use App\Models\CollectionModel; +use App\Models\RoleModel; use App\Models\UserModel; +use App\Models\UserPayModel; use Exception; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; @@ -160,4 +163,48 @@ class UserService extends BaseService } return $result; } + + /** + * 获取用户信息 + * @return mixed + * @throws Exception + */ + public function myInfo(): mixed + { + $this->with = [ + 'roles' + ]; + $result = $this->getDetail($this->userId); + $result['created_day_at'] = format_time(strtotime($result['created_at']), 'Y-m-d'); + $result['account'] = desensitization($result['account']); + return $result; + } + + /** + * 用户统计 + * @return array[] + */ + public function userStats() + { + $payProject = UserPayModel::where('user_id', $this->userId)->count(); + $collection = CollectionModel::where('user_id', $this->userId)->count(); + $role = $this->userInfo['role_name']; + return [ + 'pay_project' => [ + 'label' => '已购项目', + 'value' => $payProject, + 'icon' => 'shopping-bag' + ], + 'collection' => [ + 'label' => '我的收藏', + 'value' => $collection, + 'icon' => 'heart' + ], + 'role' => [ + 'label' => '用户类型', + 'value' => $role, + 'icon' => 'user' + ], + ]; + } } diff --git a/app/Service/common/JWTService.php b/app/Service/common/JWTService.php index 6432c66d..0001bba8 100755 --- a/app/Service/common/JWTService.php +++ b/app/Service/common/JWTService.php @@ -55,7 +55,7 @@ class JWTService 'exp' => $expirationTime, 'data' => $data ]; - + RedisService::getInstance()->init(config('cc.redis.jwt'))->set($data['id'], json_encode($data)); return JWT::encode($payload, $this->secretKey, 'HS256'); } diff --git a/app/Service/common/UploadService.php b/app/Service/common/UploadService.php index 6027f041..9866c05c 100644 --- a/app/Service/common/UploadService.php +++ b/app/Service/common/UploadService.php @@ -7,6 +7,7 @@ use App\Models\ProjectModel; use App\Models\RoleModel; use App\Models\UserModel; use App\Service\common\upload\QiniuStorageService; +use App\Service\FileService; use Exception; use Illuminate\Support\Str; use Psr\Container\ContainerExceptionInterface; @@ -20,28 +21,52 @@ class UploadService extends BaseService $this->model = RoleModel::class; } - public function uploadImage($file) + /** + * 上传OSS图片 + * @param $file + * @return array|bool + * @throws Exception + */ + public function uploadImage($file): array|bool { // 获取文件后缀 $ext = $file->getClientOriginalExtension(); $key = 'spa/image/'. date('Ymd') . '/' . 'cc_upload_'. Str::random(). uniqid() . '.' . $ext; - return [ +// $result = QiniuStorageService::getInstance()->uploadVideo($file, $key); + $result = [ '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); + FileService::getInstance()->create([ + 'user_id' => $this->userId, + 'url' => $result['url'], + ]); + + return $result; } - public function uploadVideo($file) + /** + * 上传OSS视频 + * @param $file + * @return array|bool + * @throws Exception + */ + public function uploadVideo($file): array|bool { // 获取文件后缀 $ext = $file->getClientOriginalExtension(); $key = 'spa/video/'. date('Ymd') . '/' . 'cc_upload_'. Str::random(). uniqid() . '.' . $ext; - return [ +// $result = QiniuStorageService::getInstance()->uploadVideo($file, $key); + $result = [ 'key' => $key, - 'url' => 'http://d-jy.nailaoyun.cn/storage/video//20250410/ca1bbbb3af5853c7fc4ef23b8a918922.mp4?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) ]; - return QiniuStorageService::getInstance()->uploadVideo($file, $key); + FileService::getInstance()->create([ + 'user_id' => $this->userId, + 'url' => $result['url'], + ]); + + return $result; } } diff --git a/routes/api.php b/routes/api.php index b31fe2d5..f05a04ce 100644 --- a/routes/api.php +++ b/routes/api.php @@ -12,6 +12,7 @@ Route::get('/', function () { */ UtilsService::class::getInstance()->autoRouteRegister([ '' => \App\Http\Controllers\LoginController::class, // 登录控制器 + 'home' => \App\Http\Controllers\HomeController::class, // 首页控制器 ]); Route::group([], function () { @@ -22,5 +23,6 @@ Route::group([], function () { 'upload' => \App\Http\Controllers\UploadController::class, // 上传控制器 'label' => \App\Http\Controllers\LabelController::class, // 标签控制器 'category' => \App\Http\Controllers\CategoryController::class, // 分类控制器 + 'collection' => \App\Http\Controllers\CollectionController::class, // 收藏控制器 ]); });