Files
xk-api-yii/common/models/ServiceUserToken.php
2024-09-19 11:32:39 +08:00

64 lines
1.6 KiB
PHP

<?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();
}
}