diff --git a/app/BaseApp/BaseService.php b/app/BaseApp/BaseService.php index 16f35c25..63f1e616 100755 --- a/app/BaseApp/BaseService.php +++ b/app/BaseApp/BaseService.php @@ -96,6 +96,13 @@ class BaseService if ($this->isAuth) { $this->userInfo = JWTService::getInstance()->getToken()->getUserInfo(); $this->userId = $this->userInfo['id'] ?? 0; + } else { + try { + $this->userInfo = JWTService::getInstance()->getToken()->getUserInfo(); + $this->userId = $this->userInfo['id'] ?? 0; + } catch (Exception $e) { + $this->userId = 0; + } } $this->utils = UtilsService::getInstance(); } diff --git a/app/Http/Controllers/CollectionController.php b/app/Http/Controllers/CollectionController.php index 15f95321..241b0882 100644 --- a/app/Http/Controllers/CollectionController.php +++ b/app/Http/Controllers/CollectionController.php @@ -4,6 +4,7 @@ namespace App\Http\Controllers; use App\BaseApp\BaseController; use App\Service\CollectionService; +use Illuminate\Http\JsonResponse; class CollectionController extends BaseController { @@ -15,4 +16,20 @@ class CollectionController extends BaseController $this->service = CollectionService::getInstance(); $this->insertField = ['project_id']; } + + /** + * 取消收藏 + * @return JsonResponse + */ + public function unCollection(): JsonResponse + { + $projectId = request()->post('project_id'); + if (empty($projectId)) { + return jerr('参数错误'); + } + return jok( + $this->service->unCollection($projectId), + '取消成功' + ); + } } diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 2e0acbbe..5fa20d7d 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -28,11 +28,16 @@ class UserController extends BaseController */ public function changePassword(): JsonResponse { - $this->insertField = ['password', 'new_password']; + $this->insertField = ['old_password', 'new_password', 'confirm_password']; $params = $this->checkRequiredFields(request()->post()); + if ($params['old_password'] === $params['new_password']) { + return jerr('新密码不能与旧密码相同'); + } elseif ($params['new_password'] !== $params['confirm_password']) { + return jerr('新密码与确认密码不一致'); + } return jok( - $this->service->changePassword($params['password'], $params['new_password']), + $this->service->changePassword($params['old_password'], $params['new_password']), '重置密码成功' ); } diff --git a/app/Service/CollectionService.php b/app/Service/CollectionService.php index ad427d04..e15444e8 100644 --- a/app/Service/CollectionService.php +++ b/app/Service/CollectionService.php @@ -61,9 +61,31 @@ class CollectionService extends BaseService */ public function create($params): mixed { + $myCollectionModel = $this->model::where('user_id', $this->userId)->where('project_id', $params['project_id'])->first(); + if ($myCollectionModel) { + if (!empty($myCollectionModel['deleted_at'])) { + return $this->model::where('id', $myCollectionModel['id'])->update([ + 'deleted_at' => 0 + ]); + } + $this->utils->errorThrow('您已收藏过该项目'); + } + $params['user_id'] = $this->userId; return $this->insert($params); } + /** + * 取消收藏 + * @param $projectId + * @return mixed + */ + public function unCollection($projectId) + { + return $this->model::where('user_id', $this->userId)->where('project_id', $projectId)->update([ + 'deleted_at' => get_time() + ]); + } + /** * 编辑用户收藏 * @param $id diff --git a/app/Service/HomeService.php b/app/Service/HomeService.php index e99d3680..9ace3626 100644 --- a/app/Service/HomeService.php +++ b/app/Service/HomeService.php @@ -5,6 +5,7 @@ namespace App\Service; use App\BaseApp\BaseService; use App\Enum\ProjectStatusEnum; use App\Enum\StatusEnum; +use App\Models\CollectionModel; use App\Models\FeatureModel; use App\Models\ProjectLabelRelationModel; use App\Models\ProjectModel; @@ -71,6 +72,12 @@ class HomeService extends BaseService 'features:id,project_id,name', 'author:id,nick_name,avatar,email' ]; - return $this->getDetail($id); + $result = $this->getDetail($id); + if (!empty($this->userId)) { + $result['is_collection'] = CollectionModel::where('project_id', $id)->where('user_id', $this->userId)->exists(); + } else { + $result['is_collection'] = false; + } + return $result; } } diff --git a/app/Service/ProjectService.php b/app/Service/ProjectService.php index 074993f6..70712773 100644 --- a/app/Service/ProjectService.php +++ b/app/Service/ProjectService.php @@ -5,6 +5,7 @@ namespace App\Service; use App\BaseApp\BaseService; use App\Enum\ProjectStatusEnum; use App\Enum\StatusEnum; +use App\Models\CollectionModel; use App\Models\FeatureModel; use App\Models\ProjectLabelRelationModel; use App\Models\ProjectModel; @@ -60,6 +61,7 @@ class ProjectService extends BaseService '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 aa2b05c0..038ce039 100644 --- a/app/Service/UserService.php +++ b/app/Service/UserService.php @@ -17,7 +17,7 @@ class UserService extends BaseService public function __construct() { parent::__construct(); - $this->selectField = [ 'id', 'account', 'nick_name', 'avatar', 'email', 'balance', 'qr_code', 'role_id', 'ip', 'ip_table', 'status', 'last_reset_password_at', 'created_at', 'updated_at', 'deleted_at', ]; + $this->selectField = [ 'id', 'account', 'nick_name', 'avatar', 'email', 'balance', 'is_sys_notifications', 'is_collection_notifications', 'is_marketing_notifications', 'qr_code', 'role_id', 'ip', 'ip_table', 'status', 'last_reset_password_at', 'created_at', 'updated_at', 'deleted_at', ]; $this->model = UserModel::class; $this->queryField = [ 'account' => 'like', diff --git a/app/Service/common/UtilsService.php b/app/Service/common/UtilsService.php index 167dbc2f..4bb2f686 100755 --- a/app/Service/common/UtilsService.php +++ b/app/Service/common/UtilsService.php @@ -61,7 +61,7 @@ class UtilsService */ public function notAuth($msg): mixed { - exit(json_encode(['msg' => $msg, 'code' => ErrorEnum::NOT_AUTH->value])); + return self::errorThrow($msg, ErrorEnum::NOT_AUTH); } /**