初始化仓库
This commit is contained in:
64
common/models/ServiceUserToken.php
Normal file
64
common/models/ServiceUserToken.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
namespace common\models;
|
||||
|
||||
use yii\base\Exception;
|
||||
use yii\behaviors\TimestampBehavior;
|
||||
use yii\db\ActiveRecord;
|
||||
|
||||
class ServiceUserToken extends \common\modelsgii\ServiceUserToken
|
||||
{
|
||||
public function behaviors()
|
||||
{
|
||||
return [
|
||||
[
|
||||
'class' => 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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user