From 835479d840a4a1e1b62894184ce1cec688491729 Mon Sep 17 00:00:00 2001 From: King Date: Wed, 31 Jul 2019 09:29:19 +0800 Subject: [PATCH] =?UTF-8?q?api=E6=A8=A1=E5=9D=97=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E7=99=BB=E5=87=BA=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/config/main.php | 1 + api/modules/v1/controllers/SiteController.php | 14 ++++++++++++ common/models/api/AccessToken.php | 3 ++- services/api/AccessTokenService.php | 22 +++++++++++++++++-- 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/api/config/main.php b/api/config/main.php index 0036c0d46..b9ca18f22 100644 --- a/api/config/main.php +++ b/api/config/main.php @@ -87,6 +87,7 @@ 'pluralize' => false, // 是否启用复数形式,注意index的复数indices,开启后不直观 'extraPatterns' => [ 'POST login' => 'login', // 登录获取token + 'POST logout' => 'logout', // 登出 'POST refresh' => 'refresh', // 重置token 'POST sms-code' => 'sms-code', // 获取验证码 'POST register' => 'register', // 注册 diff --git a/api/modules/v1/controllers/SiteController.php b/api/modules/v1/controllers/SiteController.php index 260237d2c..23075a7e4 100644 --- a/api/modules/v1/controllers/SiteController.php +++ b/api/modules/v1/controllers/SiteController.php @@ -54,6 +54,20 @@ public function actionLogin() return ResultDataHelper::api(422, $this->getError($model)); } + /** + * 登出 + * + * @return array|mixed + */ + public function actionLogout() + { + if (Yii::$app->services->apiAccessToken->disableToken(Yii::$app->user->identity->access_token)) { + return ResultDataHelper::api(200, '成功'); + } else { + return ResultDataHelper::api(500, '失败'); + } + } + /** * 重置令牌 * diff --git a/common/models/api/AccessToken.php b/common/models/api/AccessToken.php index abcc68706..0a61f5fbe 100644 --- a/common/models/api/AccessToken.php +++ b/common/models/api/AccessToken.php @@ -2,6 +2,7 @@ namespace common\models\api; +use common\enums\StatusEnum; use Yii; use yii\behaviors\BlameableBehavior; use yii\db\ActiveRecord; @@ -112,7 +113,7 @@ public static function findIdentityByAccessToken($token, $type = null) */ public static function findIdentityByRefreshToken($token, $group = null) { - return static::findOne(['group' => $group, 'refresh_token' => $token]); + return static::findOne(['group' => $group, 'refresh_token' => $token, 'status' => StatusEnum::ENABLED]); } /** diff --git a/services/api/AccessTokenService.php b/services/api/AccessTokenService.php index 50f5b94ed..6d7ab66ac 100644 --- a/services/api/AccessTokenService.php +++ b/services/api/AccessTokenService.php @@ -2,6 +2,7 @@ namespace services\api; +use common\enums\StatusEnum; use Yii; use yii\db\ActiveRecord; use yii\web\UnprocessableEntityHttpException; @@ -48,6 +49,7 @@ public function getAccessToken(Member $member, $group, $cycle_index = 1) !empty($model->access_token) && Yii::$app->cache->delete(CacheKeyEnum::API_ACCESS_TOKEN . $model->access_token); $model->refresh_token = Yii::$app->security->generateRandomString() . '_' . time(); $model->access_token = Yii::$app->security->generateRandomString() . '_' . time(); + $model->status = StatusEnum::ENABLED; if (!$model->save()) { if ($cycle_index <= 3) { @@ -108,11 +110,27 @@ public function getTokenToCache($token, $type) public function getTokenByAccessToken($token) { return AccessToken::find() - ->where(['access_token' => $token]) + ->where(['access_token' => $token, 'status' => StatusEnum::ENABLED]) ->andFilterWhere(['merchant_id' => $this->getMerchantId()]) ->one(); } + /** + * 禁用token + * @param $access_token + * @return bool + */ + public function disableToken($access_token) + { + if ($this->cache === true) { + Yii::$app->cache->delete(CacheKeyEnum::API_ACCESS_TOKEN . $access_token); + } + + $model = $this->getTokenByAccessToken($access_token); + $model->status = StatusEnum::DISABLED; + return $model->save(); + } + /** * 返回模型 * @@ -132,4 +150,4 @@ protected function findModel($member_id, $group) return $model; } -} \ No newline at end of file +}