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 SystemAdmin
- * @package app\models\system
- */
- class SystemBadmin extends BaseModel
- {
- /**
- * 数据表主键
- * @var string
- */
- protected $pk = 'id';
- /**
- * 模型名称
- * @var string
- */
- protected $name = 'system_badmin';
- /**
- * token令牌桶名
- * @var string
- */
- protected static $tokenPrefix = 'badmin_token';
- use ModelTrait;
- use JwtAuthModelTrait;
- protected $insert = ['add_time'];
- /**
- * 获取token缓存前缀
- * @return string
- */
- public static function getTokenPrefix()
- {
- return self::$tokenPrefix;
- }
- public static function getRolesAttr($value)
- {
- return explode(',', $value);
- }
- /**
- * 获取token并放入令牌桶
- * @param SystemAdmin $adminInfo
- * @param $type
- * @return array|bool
- */
- 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;
- }
- /**
- * 从令牌桶中删除token
- * @param string $token
- * @return bool
- * @throws InvalidArgumentException
- */
- public static function delTokenBucket(string $token)
- {
- if (Cache::has($token)) {
- return Cache::delete($token);
- }
- return true;
- }
- /**
- * 用户登陆
- * @param $account
- * @param $pwd
- * @return bool|SystemBadmin
- */
- 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;
- }
- }
|