'', 'device' => '', ]; const uidTokenList = [ 'token_1' => [ 'device' => '', 'created_time' => '', 'exp_time' => '', ], ]; function checkTokenLogin($sToken): bool { return $this->getUserInfoByToken($sToken) != null; } function getUserInfoByToken($sToken) { if (empty($sToken)) return null; $sUidInfo = Redis::get(RedisConst::TOKEN_UID . $sToken); if (empty($sUidInfo)) return null; return unserialize($sUidInfo); } function setUserInfoToToken($sToken, $iUid, $sDevice) { $sUidInfo = serialize([ 'uid' => $iUid, 'device' => $sDevice, ]); return Redis::set(RedisConst::TOKEN_UID . $sToken, $sUidInfo, RedisConst::COMMON_EXP_TIME); } function delUserInfoToToken($sToken) { return Redis::del(RedisConst::TOKEN_UID . $sToken); } function getAllTokenInfoByUid($iUid) { $sTokenList = Redis::get(RedisConst::UID_TOKENS . $iUid); if (empty($sToken)) return null; return unserialize($sTokenList); } function checkTokenInUid($iUid, $sToken): bool { $aTokenInfoList = $this->getAllTokenInfoByUid($iUid); if (empty($aTokenInfoList)) return false; $aTokenList = array_keys($aTokenInfoList); return in_array($sToken, $aTokenList); } function addTokenToUidInfo($iUid, $sToken, $sDevice) { $aTokenInfoList = $this->getAllTokenInfoByUid($iUid); if (empty($aTokenInfoList)) { $aTokenInfoList = []; } $aTokenInfoList[$sToken] = [ 'device' => $sDevice, 'created_time' => Carbon::now()->toDateTimeString(), 'exp_time' => Carbon::parse(time() + RedisConst::COMMON_EXP_TIME)->toDateTimeString(), ]; $sTokenList = serialize($aTokenInfoList); return Redis::set(RedisConst::UID_TOKENS . $iUid, $sTokenList); } function delTokenByUidInfo($iUid, $sToken) { $aTokenInfoList = $this->getAllTokenInfoByUid($iUid); if (empty($aTokenInfoList)) return false; if (!isset($aTokenInfoList[$sToken])) return false; unset($aTokenInfoList[$sToken]); $sTokenList = serialize($aTokenInfoList); return Redis::set(RedisConst::UID_TOKENS . $iUid, $sTokenList); } function getTokenFromReq(\Illuminate\Http\Request $request = null) { if ($request == null) $request = request(); $sToken = $request->header('_token'); if (!empty($sToken)) return $sToken; $sToken = $request->input('_token'); if (!empty($sToken)) return $sToken; return null; } function generateTokenStr(): string { return time() . Tools::generateRandStr(24); } //登入使用 function createTokenToUser($iUid, $sDevice): string { do { $sToken = $this->generateTokenStr(); if (!$this->checkTokenLogin($sToken)) break; } while (1); $this->setUserInfoToToken($sToken, $iUid, $sDevice); $this->addTokenToUidInfo($iUid, $sToken, $sDevice); return $sToken; } //登出使用 function delTokenToUser($iUid, $sToken): void { $this->delUserInfoToToken($sToken); $this->delTokenByUidInfo($iUid, $sToken); } function getTokenInfo() { $sToken = $this->getTokenFromReq(); if (empty($sToken)) return null; $aUserInfo = $this->getUserInfoByToken($sToken); if (empty($aUserInfo)) return null; return $aUserInfo; } function setCurrentUser(array $aUser): void { app()->singleton('customerUser',function () use ($aUser){ return $aUser; }); } function getCurrentUser() { if(app()->has('customerUser')){ return app()->get('customerUser'); } return null; } }