123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- <?php
- namespace App\Services;
- use App\Traits\CommonTrait;
- use Illuminate\Support\Facades\Cache;
- class LoginService
- {
- /**
- * 存放通用的 缓存key和tags
- */
- use CommonTrait;
- protected ?string $uniqueIdentifier;
- public int $expiredTime = 24 * 3600 * 7;
- public ?string $authorizationEncrypt;
- public function __construct($siteAliasName)
- {
- $this->siteAliasName = $siteAliasName;
- $this->uniqueIdentifier = $this->getUniqueIdentifier();
- // 用户登录的token信息
- $this->authorizationEncrypt = $this->getToken();
- }
- /**
- * 设置登录,返回登录token
- *
- * @param $userInfo
- * @param int $time
- * @return array
- */
- public function setLoginCookie($userInfo, int $time = 24 * 3600 * 7): array
- {
- $this->expiredTime = $time = $time ?: config('session.lifetime') * 60;
- //设置随机值
- $encrypt = md5(sha1(md5(microtime() . mt_rand())));
- //设置登录的用户id
- $keyArr = $this->getUserIdCacheKey($encrypt);
- Cache::tags($keyArr['tags'])->put($keyArr['key'], $userInfo['id'], $time);
- // 存储用户信息
- $keyArr = $this->getUserInfoCacheKey($encrypt);
- Cache::tags($keyArr['tags'])->put($keyArr['key'], $userInfo, $time);
- return ['token' => $encrypt, 'expire' => $time];
- }
- /**
- * 用户退出登录,清除已经保存的登录信息
- *
- * @param string $encrypt
- * @return bool
- */
- public function clearLoginInfo(string $encrypt = ''): bool
- {
- $this->authorizationEncrypt = $encrypt ?: $this->authorizationEncrypt;
- $keyArr = $this->getUserIdCacheKey($this->authorizationEncrypt);
- Cache::tags($keyArr['tags'])->forget($keyArr['key']);
- return true;
- }
- /**
- * @param $yzm
- * @param int $time
- */
- public function saveImgYzm($yzm, int $time = 300)
- {
- $keyArr = $this->getYzmCacheKey();
- Cache::tags($keyArr['tags'])->put($keyArr['key'], $yzm, $time);
- }
- /**
- * @param $yzm
- * @return bool
- */
- public function checkImgYzm($yzm): bool
- {
- $keyArr = $this->getYzmCacheKey();
- return $yzm == Cache::tags($keyArr['tags'])->get($keyArr['key']);
- }
- public function clearImgYzm()
- {
- $keyArr = $this->getYzmCacheKey();
- Cache::tags($keyArr['tags'])->forget($keyArr['key']);
- }
- }
|