123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- <?php
- namespace app\models\user;
- use crmeb\services\CacheService;
- use think\Model;
- class UserToken extends Model
- {
- protected $name = 'user_token';
- protected $type = [
- 'create_time' => 'datetime',
- 'login_ip' => 'string'
- ];
- protected $autoWriteTimestamp = true;
- protected $updateTime = false;
- public static function onBeforeInsert(UserToken $token)
- {
- if (!isset($token['login_ip']))
- $token['login_ip'] = app()->request->ip();
- }
- /**
- * @param User|array|Model $user
- * @param $type
- * @return array|bool
- */
- public static function createToken(User $user, $type)
- {
- $tokenInfo = $user->getToken($type);
- $expires = $tokenInfo['params']['exp'] - time() + 60;
- $res = CacheService::setTokenBucket($tokenInfo['token'], [
- 'token' => $tokenInfo['token'],
- 'exp' => $expires,
- 'uid' => $user->getData('uid'),
- 'type' => $type
- ], $expires, 'api');
- if ($res) return $tokenInfo;
- return false;
- }
- /**
- * 删除一天前的过期token
- * @return bool
- * @throws \Exception
- */
- public static function delToken()
- {
- //return self::where('expires_time', '<', date('Y-m-d H:i:s', strtotime('-1 day')))->delete();
- }
- }
|