Skip to content

Commit

Permalink
Merge pull request #23 from 0xDarker/master
Browse files Browse the repository at this point in the history
api模块增加登出接口
  • Loading branch information
jianyan74 authored Jul 31, 2019
2 parents 733a083 + 835479d commit 2cb592b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
1 change: 1 addition & 0 deletions api/config/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', // 注册
Expand Down
14 changes: 14 additions & 0 deletions api/modules/v1/controllers/SiteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, '失败');
}
}

/**
* 重置令牌
*
Expand Down
3 changes: 2 additions & 1 deletion common/models/api/AccessToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace common\models\api;

use common\enums\StatusEnum;
use Yii;
use yii\behaviors\BlameableBehavior;
use yii\db\ActiveRecord;
Expand Down Expand Up @@ -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]);
}

/**
Expand Down
22 changes: 20 additions & 2 deletions services/api/AccessTokenService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace services\api;

use common\enums\StatusEnum;
use Yii;
use yii\db\ActiveRecord;
use yii\web\UnprocessableEntityHttpException;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
}

/**
* 返回模型
*
Expand All @@ -132,4 +150,4 @@ protected function findModel($member_id, $group)

return $model;
}
}
}

0 comments on commit 2cb592b

Please sign in to comment.