where('member_id', $memberId) ->where('game_id', $gameId) ->field('id, recent_devices') ->find(); if (empty($subInfo)) { return false; } // 解析现有设备列表 $recentDevices = []; if (!empty($subInfo['recent_devices'])) { $recentDevices = json_decode($subInfo['recent_devices'], true); if (!is_array($recentDevices)) { $recentDevices = []; } } // 检查设备是否已存在 $existsIndex = $this->findDeviceIndex($recentDevices, $imei); $now = time(); if ($existsIndex !== false) { // 设备已存在,更新最后登录时间 $recentDevices[$existsIndex]['last_login'] = $now; } else { // 设备不存在 // 仅更新模式:不添加新设备 if ($onlyUpdateExisting) { return false; } // 判断是否需要添加 if (count($recentDevices) >= self::MAX_RECENT_DEVICES) { // 列表已满,且未强制替换,不处理 if (!$forceReplace) { return false; } // 强制替换:移除最旧的设备 $oldestIndex = $this->findOldestDeviceIndex($recentDevices); if ($oldestIndex !== false) { unset($recentDevices[$oldestIndex]); $recentDevices = array_values($recentDevices); } } // 添加新设备 $recentDevices[] = [ 'imei' => $imei, 'device_type' => (int)$deviceType, 'last_login' => $now, ]; } // 更新数据库 $result = Db::name('nw_subaccount') ->where('id', $subInfo['id']) ->update([ 'recent_devices' => json_encode($recentDevices), 'update_time' => $now, ]); return $result !== false; } /** * 判断是否为最近登录的设备 * * @param int $memberId 玩家ID * @param int $gameId 游戏ID * @param string $imei 设备IMEI * @return bool true=是常用设备,false=不是常用设备 */ public function isRecentDevice($memberId, $gameId, $imei) { if (empty($memberId) || empty($gameId) || empty($imei)) { return false; } // 查询当前常用设备列表 $subInfo = Db::name('nw_subaccount') ->where('member_id', $memberId) ->where('game_id', $gameId) ->field('recent_devices') ->find(); if (empty($subInfo) || empty($subInfo['recent_devices'])) { return false; } // 解析设备列表 $recentDevices = json_decode($subInfo['recent_devices'], true); if (!is_array($recentDevices) || empty($recentDevices)) { return false; } // 查找设备 return $this->findDeviceIndex($recentDevices, $imei) !== false; } /** * 查找设备在列表中的索引 * * @param array $devices 设备列表 * @param string $imei 设备IMEI * @return int|false 找到返回索引,未找到返回false */ private function findDeviceIndex($devices, $imei) { foreach ($devices as $index => $device) { if (isset($device['imei']) && $device['imei'] === $imei) { return $index; } } return false; } /** * 查找最旧设备的索引 * * @param array $devices 设备列表 * @return int|false */ private function findOldestDeviceIndex($devices) { if (empty($devices)) { return false; } $oldestIndex = 0; $oldestTime = PHP_INT_MAX; foreach ($devices as $index => $device) { if (isset($device['last_login']) && $device['last_login'] < $oldestTime) { $oldestTime = $device['last_login']; $oldestIndex = $index; } } return $oldestIndex; } }