['id'=>..., 'name'=>..., 'rfid'=>...]] */ private array $sessions = []; /** * 人员刷卡记录缓存记录多久 */ public int $expire = 0; /** * 人员刷卡记录记录哪些读卡器的 * 设置为空则表示所有读卡器 */ public array $recordReaders = []; private function __construct() { $this->expire = Config::getInstance()->openCardRecordCacheTime; $this->recordReaders = Config::getInstance()->openCardRecordReaders; } public static function getInstance(): self { if (self::$instance === null) { self::$instance = new self(); } return self::$instance; } /** * 重置单例(测试用) */ public static function resetInstance(): void { self::$instance = null; } /** * 记录某读卡器上的操作员信息 * * @param string $readerId 读卡器ID * @param string $userId 操作员ID * @param string $userName 操作员姓名 * @param string $userRfid 操作员RFID */ public function setOperator(string $readerId, string $userId, string $userName, string $userRfid): void { $this->sessions[$readerId] = [ 'id' => $userId, 'name' => $userName, 'rfid' => $userRfid, 'time' => time(), ]; } /** * 获取某读卡器上记录的操作员信息 * * @param string $readerId 读卡器ID * @return array|null ['id'=>..., 'name'=>..., 'rfid'=>...] 或 null(未登记) */ public function getOperator(string $readerId, string $readerType): ?array { $cache = $this->sessions[$readerId] ?? null; $time = $cache['time']; Logger::debug("OperatorSessionManager::getOperator($readerId, $readerType)"); if (empty($cache)) return null; if ($this->expire > 0 && isset($time) && (time() - $time) <= $this->expire) { if (in_array($readerType, $this->recordReaders) && empty($this->recordReaders)) { return $cache; } } return $this->pop($readerId); } /** * 判断某读卡器是否已有操作员登记 */ public function hasOperator(string $readerId): bool { $cache = $this->sessions[$readerId] ?? null; if (empty($cache)) return false; // 如果时间过去了,返回没有 if ($this->expire > 0 && isset($cache['time']) && (time() - $cache['time']) > $this->expire) { return false; } return true; } /** * 清除某读卡器的操作员会话(内镜卡刷卡后立即调用) */ public function clearOperator(string $readerId): void { unset($this->sessions[$readerId]); } /** * 清除所有会话(测试用) */ public function clearAll(): void { $this->sessions = []; } public function pop(string $readerId): ?array { $op = $this->sessions[$readerId] ?? null; $this->clearOperator($readerId); return $op; } }