TimestampBehavior::class, 'attributes' => [ ActiveRecord::EVENT_BEFORE_INSERT => ['created_at','updated_at'], ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'], ], ], ]; } //登录检测token public static function checkToken($token) { $pos = strrpos($token,'_'); if(!$pos){ return false; } $tokenInfo = self::find()->where([ 'token' => $token ])->one(); if(!$tokenInfo || $tokenInfo->is_disable){ return false; } $time = substr($token,$pos+1); if($time + 30*24*60*60 < time()){ return false; } return $tokenInfo; } //创建token public static function createToken($uid) { $token = \Yii::$app->security->generateRandomString().'_'.time(); $model = new self(); $model->su_id = $uid; $model->token = $token; if(!$model->save()){ throw new Exception('登录失败'); } return $token; } public static function disableToken($token) { $model = self::find()->where([ 'token' => $token ])->one(); $model->is_disable = 1; $model->save(); } }