diff --git a/server/app/api/controller/CryptoController.php b/server/app/api/controller/CryptoController.php index 2a23fa5..358cc5b 100644 --- a/server/app/api/controller/CryptoController.php +++ b/server/app/api/controller/CryptoController.php @@ -29,6 +29,9 @@ class CryptoController extends BaseApiController */ public function favorites() { + if ($this->userId <= 0) { + return $this->fail('请先登录', [], 401); + } return $this->data(['symbols' => CryptoLogic::favorites($this->userId)]); } @@ -37,6 +40,9 @@ class CryptoController extends BaseApiController */ public function addFavorite() { + if ($this->userId <= 0) { + return $this->fail('请先登录', [], 401); + } $params = (new CryptoValidate())->post()->goCheck('favorite'); if (!CryptoLogic::addFavorite($this->userId, (string) $params['symbol'])) { return $this->fail(CryptoLogic::getError()); @@ -49,6 +55,9 @@ class CryptoController extends BaseApiController */ public function cancelFavorite() { + if ($this->userId <= 0) { + return $this->fail('请先登录', [], 401); + } $params = (new CryptoValidate())->post()->goCheck('favorite'); if (!CryptoLogic::cancelFavorite($this->userId, (string) $params['symbol'])) { return $this->fail(CryptoLogic::getError()); diff --git a/server/app/api/logic/CryptoLogic.php b/server/app/api/logic/CryptoLogic.php index b4c2c7a..8799d42 100644 --- a/server/app/api/logic/CryptoLogic.php +++ b/server/app/api/logic/CryptoLogic.php @@ -16,6 +16,9 @@ class CryptoLogic extends BaseLogic public static function favorites(int $userId): array { + if ($userId <= 0) { + return []; + } $symbols = CryptoFavorite::where([ 'user_id' => $userId, 'status' => YesNoEnum::YES, @@ -26,6 +29,10 @@ class CryptoLogic extends BaseLogic public static function addFavorite(int $userId, string $symbol): bool { + if ($userId <= 0) { + self::setError('请先登录'); + return false; + } $symbol = strtoupper(trim($symbol)); if (!CryptoMarketService::isSupportedSymbol($symbol)) { self::setError('暂不支持该币种'); @@ -54,6 +61,10 @@ class CryptoLogic extends BaseLogic public static function cancelFavorite(int $userId, string $symbol): bool { + if ($userId <= 0) { + self::setError('请先登录'); + return false; + } try { CryptoFavorite::update( ['status' => YesNoEnum::NO],