123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- <?php
- namespace app\models\system;
- use crmeb\services\CacheService;
- use crmeb\traits\JwtAuthModelTrait;
- use crmeb\traits\ModelTrait;
- use crmeb\basic\BaseModel;
- use Psr\SimpleCache\InvalidArgumentException;
- use think\facade\Cache;
- class SystemBadmin extends BaseModel
- {
-
- protected $pk = 'id';
-
- protected $name = 'system_badmin';
-
- protected static $tokenPrefix = 'badmin_token';
- use ModelTrait;
- use JwtAuthModelTrait;
- protected $insert = ['add_time'];
-
- public static function getTokenPrefix()
- {
- return self::$tokenPrefix;
- }
- public static function getRolesAttr($value)
- {
- return explode(',', $value);
- }
-
- public static function createToken(SystemBadmin $adminInfo, string $type, array $params = [])
- {
- $tokenInfo = $adminInfo->getToken($type, $params);
- $exp = (int)bcadd($tokenInfo['params']['exp'] - $tokenInfo['params']['iat'], 60, 0);
- $res = CacheService::setTokenBucket($tokenInfo['token'], ['token' => $tokenInfo['token'], 'exp' => $exp], (int)$exp);
- if (!$res) return self::setErrorInfo('保存token失败');
- return $tokenInfo;
- }
-
- public static function delTokenBucket(string $token)
- {
- if (Cache::has($token)) {
- return Cache::delete($token);
- }
- return true;
- }
-
- public static function login($account, $pwd)
- {
- $adminInfo = self::get(compact('account'));
- if (!$adminInfo) return self::setErrorInfo('登陆的账号不存在!');
- if (!password_verify($pwd, $adminInfo['pwd'])) return self::setErrorInfo('账号或密码错误,请重新输入');
- if (!$adminInfo['status']) return self::setErrorInfo('该账号已被关闭!');
- event('SystemBAdminLoginAfter', [$adminInfo]);
- return $adminInfo;
- }
- }
|