1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- <?php
- namespace app\common\filters;
- use app\common\helpers\Identify;
- use Yii;
- use yii\base\ActionFilter;
- //刷新token
- class FrontEndTokenFilter extends ActionFilter
- {
- public function beforeAction($action)
- {
- if(!Identify::hasLogined())return true;
- $token = \app\common\helpers\Cookie::getCookie(Yii::$app->params['access_token_name']);
- if(!empty($token))
- {
- $tokenModel = \app\modules\ucenter\models\UserToken::find()->where('token = :token and request_from=:request_from', [':token' => $token,':request_from' => REQUEST_FROM])->orderBy(['create_time'=>SORT_DESC])->one();
- if($tokenModel)
- {
- $user = \app\modules\ucenter\models\User::find()->where("user_id=".$tokenModel->user_id)->one();
- }
- }
- else
- {
- $user = \app\modules\ucenter\models\User::find()->where("user_id=".Identify::getUserInfo(null,'user_id'))->one();
- }
- if($user&&$user->refreshAccessToken($user,REQUEST_FROM))
- {
- $newTokenModel = \app\modules\ucenter\models\User::getToken($user->user_id,REQUEST_FROM);
- if($newTokenModel->token!=$token)
- {
- \app\common\helpers\Cookie::setCookie(Yii::$app->params['access_token_name'],$newTokenModel->token);
- }
- return true;
- }
- else
- {
- //如果刷新失败,说明是多端登录,清除COOKIE
- Identify::logout();
- \app\common\helpers\Cookie::deleteCookie(Yii::$app->params['access_token_name']);
- header("Location:".get_url());
- return false;
- }
- }
- }
|