|
|
@ -228,32 +228,13 @@ import QPlayAutoSDK |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
print("QPlayAutoManager: App已注册,开始连接重试机制") |
|
|
print("QPlayAutoManager: App已注册,开始连接") |
|
|
// Add retry mechanism for socket errors |
|
|
|
|
|
connectWithRetry(forceLogin: forceLogin, retryCount: 3) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private func connectWithRetry(forceLogin: Bool, retryCount: Int) { |
|
|
|
|
|
print("QPlayAutoManager: 连接重试,剩余次数: \(retryCount), forceLogin: \(forceLogin)") |
|
|
|
|
|
|
|
|
|
|
|
guard retryCount > 0 else { |
|
|
|
|
|
print("QPlayAutoManager: ❌ 连接失败,所有重试次数已用完") |
|
|
|
|
|
print("QPlayAutoManager: 最终状态 - isConnected: \(isConnected), isLoginOK: \(isLoginOK)") |
|
|
|
|
|
|
|
|
|
|
|
// 只有在所有重试都失败且确实有待处理回调时才清理 |
|
|
|
|
|
if !loginCompletionHandlers.isEmpty { |
|
|
|
|
|
print("QPlayAutoManager: 清理登录回调队列") |
|
|
|
|
|
notifyLoginCompletion(false) |
|
|
|
|
|
} |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
let startTime = Date() |
|
|
let startTime = Date() |
|
|
print("QPlayAutoManager: 调用 QPlayAutoSDK.connectAndForceLogin(\(forceLogin))") |
|
|
print("QPlayAutoManager: 调用 QPlayAutoSDK.connectAndForceLogin(\(forceLogin))") |
|
|
QPlayAutoSDK.connectAndForceLogin(forceLogin) |
|
|
QPlayAutoSDK.connectAndForceLogin(forceLogin) |
|
|
// 增加超时时间,特别是强制登录时 |
|
|
|
|
|
|
|
|
// 设置超时检查 |
|
|
let timeoutInterval: TimeInterval = forceLogin ? 45.0 : 10.0 |
|
|
let timeoutInterval: TimeInterval = forceLogin ? 45.0 : 10.0 |
|
|
// If connection fails due to socket error, retry after cleanup |
|
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + timeoutInterval) { |
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + timeoutInterval) { |
|
|
let endTime = Date() |
|
|
let endTime = Date() |
|
|
let duration = endTime.timeIntervalSince(startTime) |
|
|
let duration = endTime.timeIntervalSince(startTime) |
|
|
@ -262,10 +243,14 @@ import QPlayAutoSDK |
|
|
print("QPlayAutoManager: 连接状态检查 - isConnected: \(self.isConnected)") |
|
|
print("QPlayAutoManager: 连接状态检查 - isConnected: \(self.isConnected)") |
|
|
|
|
|
|
|
|
if !self.isConnected { |
|
|
if !self.isConnected { |
|
|
print("QPlayAutoManager: ⚠️ 连接失败,准备重试 (剩余 \(retryCount - 1) 次)") |
|
|
print("QPlayAutoManager: ❌ 连接超时失败") |
|
|
print("QPlayAutoManager: 执行清理操作") |
|
|
print("QPlayAutoManager: 最终状态 - isConnected: \(self.isConnected), isLoginOK: \(self.isLoginOK)") |
|
|
QPlayAutoSDK.stop() |
|
|
|
|
|
self.connectWithRetry(forceLogin: forceLogin, retryCount: retryCount - 1) |
|
|
// 清理登录回调队列 |
|
|
|
|
|
if !self.loginCompletionHandlers.isEmpty { |
|
|
|
|
|
print("QPlayAutoManager: 清理登录回调队列") |
|
|
|
|
|
self.notifyLoginCompletion(false) |
|
|
|
|
|
} |
|
|
} else { |
|
|
} else { |
|
|
print("QPlayAutoManager: ✅ 连接成功") |
|
|
print("QPlayAutoManager: ✅ 连接成功") |
|
|
if forceLogin { |
|
|
if forceLogin { |
|
|
@ -382,28 +367,29 @@ import QPlayAutoSDK |
|
|
operation() |
|
|
operation() |
|
|
} else { |
|
|
} else { |
|
|
// 未连接,先连接 |
|
|
// 未连接,先连接 |
|
|
print("QPlayAutoManager: 未连接,正在尝试连接...") |
|
|
print("QPlayAutoManager: 未连接,尝试连接...") |
|
|
connect(forceLogin: false) |
|
|
connect(forceLogin: false) |
|
|
// 使用延迟检查连接状态 |
|
|
|
|
|
|
|
|
// 超时检查 |
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 10.0) { [weak self] in |
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 10.0) { [weak self] in |
|
|
guard let self = self else { return } |
|
|
guard let self = self else { return } |
|
|
if self.isConnected { |
|
|
if self.isConnected { |
|
|
print("QPlayAutoManager: 连接成功,执行操作") |
|
|
print("QPlayAutoManager: 连接成功,执行操作") |
|
|
operation() |
|
|
operation() |
|
|
} else { |
|
|
} else { |
|
|
print("QPlayAutoManager: 连接失败,无法执行操作") |
|
|
print("QPlayAutoManager: 连接超时失败,无法执行操作") |
|
|
onFailure?(.connectionFailed) |
|
|
onFailure?(.connectionFailed) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} else { |
|
|
} else { |
|
|
// 未登录,先登录 |
|
|
// 未登录,先登录 |
|
|
print("QPlayAutoManager: 未登录,正在尝试登录...") |
|
|
print("QPlayAutoManager: 未登录,尝试登录...") |
|
|
performLogin { [weak self] success in |
|
|
performLogin { [weak self] success in |
|
|
guard let self = self else { return } |
|
|
guard let self = self else { return } |
|
|
if success { |
|
|
if success { |
|
|
print("QPlayAutoManager: 登录成功,检查连接状态") |
|
|
print("QPlayAutoManager: 登录成功,执行操作") |
|
|
self.ensureLoginAndExecute(operation, onFailure: onFailure) |
|
|
operation() |
|
|
} else { |
|
|
} else { |
|
|
print("QPlayAutoManager: 登录失败,无法执行操作") |
|
|
print("QPlayAutoManager: 登录失败,无法执行操作") |
|
|
onFailure?(.loginFailed) |
|
|
onFailure?(.loginFailed) |
|
|
@ -413,49 +399,21 @@ import QPlayAutoSDK |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private func performLogin(completion: @escaping (Bool) -> Void) { |
|
|
private func performLogin(completion: @escaping (Bool) -> Void) { |
|
|
// 如果正在登录中,将回调添加到队列 |
|
|
|
|
|
loginCompletionHandlers.append(completion) |
|
|
loginCompletionHandlers.append(completion) |
|
|
|
|
|
|
|
|
// 如果已经有登录请求在处理,直接返回 |
|
|
if hasValidStoredToken() { |
|
|
if loginCompletionHandlers.count > 1 { |
|
|
// 尝试热启动连接 |
|
|
print("QPlayAutoManager: 登录请求已在队列中,当前队列长度: \(loginCompletionHandlers.count)") |
|
|
reconnect(timeout: 10.0) { [weak self] success, responseDict in |
|
|
return |
|
|
if success { |
|
|
} |
|
|
self?.notifyLoginCompletion(true) |
|
|
|
|
|
} else { |
|
|
// 首先尝试使用保存的token进行reconnect。 暂时reconnect接口无法使用先屏蔽自动登陆逻辑 |
|
|
self?.forceLogin() |
|
|
// if hasValidStoredToken() { |
|
|
} |
|
|
// let tokenInfo = getStoredTokenInfo() |
|
|
|
|
|
// print("QPlayAutoManager: 检测到有效token,开始热启动连接") |
|
|
|
|
|
// print("QPlayAutoManager: Token信息 - OpenID: \(tokenInfo?["openId"] as? String ?? "未知")") |
|
|
|
|
|
// print("QPlayAutoManager: Token信息 - 登录时间: \(tokenInfo?["loginTime"] as? Date ?? Date())") |
|
|
|
|
|
// print("QPlayAutoManager: 当前连接状态 - isConnected: \(isConnected), isConnecting: \(isConnecting)") |
|
|
|
|
|
// print("QPlayAutoManager: 当前登录状态 - isLoginOK: \(isLoginOK)") |
|
|
|
|
|
// |
|
|
|
|
|
// reconnect(timeout: 10.0) { [weak self] success, responseDict in |
|
|
|
|
|
// if success { |
|
|
|
|
|
// print("QPlayAutoManager: ✅ 热启动连接成功") |
|
|
|
|
|
// print("QPlayAutoManager: 连接响应数据: \(responseDict ?? [:])") |
|
|
|
|
|
// self?.notifyLoginCompletion(true) |
|
|
|
|
|
// } else { |
|
|
|
|
|
// print("QPlayAutoManager: ❌ 热启动连接失败") |
|
|
|
|
|
// print("QPlayAutoManager: 失败响应数据: \(responseDict ?? [:])") |
|
|
|
|
|
// print("QPlayAutoManager: 连接状态检查 - isConnected: \(self?.isConnected ?? false)") |
|
|
|
|
|
// print("QPlayAutoManager: QQ音乐安装状态: \(self?.isQQMusicInstalled ?? false)") |
|
|
|
|
|
// print("QPlayAutoManager: 开始执行强制登录流程") |
|
|
|
|
|
// self?.forceLogin() |
|
|
|
|
|
// } |
|
|
|
|
|
// } |
|
|
|
|
|
// } else { |
|
|
|
|
|
print("QPlayAutoManager: ❌ 没有有效token,直接进行强制登录") |
|
|
|
|
|
let tokenInfo = getStoredTokenInfo() |
|
|
|
|
|
if tokenInfo == nil { |
|
|
|
|
|
print("QPlayAutoManager: 原因:本地未存储任何token信息") |
|
|
|
|
|
} else { |
|
|
|
|
|
print("QPlayAutoManager: 原因:token已过期或无效") |
|
|
|
|
|
print("QPlayAutoManager: 存储的token信息: \(tokenInfo!)") |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
} else { |
|
|
|
|
|
print("QPlayAutoManager: ❌ 没有有效token,直接进行强制登录") |
|
|
forceLogin() |
|
|
forceLogin() |
|
|
// } |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private func forceLogin() { |
|
|
private func forceLogin() { |
|
|
@ -467,7 +425,7 @@ import QPlayAutoSDK |
|
|
print(" - 当前登录状态: \(isLoginOK)") |
|
|
print(" - 当前登录状态: \(isLoginOK)") |
|
|
|
|
|
|
|
|
// 强制登录,会跳转到QQ音乐 |
|
|
// 强制登录,会跳转到QQ音乐 |
|
|
connect(forceLogin: false) |
|
|
connect(forceLogin: true) |
|
|
|
|
|
|
|
|
// 设置超时监控(仅记录,不清理回调) |
|
|
// 设置超时监控(仅记录,不清理回调) |
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 60.0) { [weak self] in |
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 60.0) { [weak self] in |
|
|
@ -889,18 +847,12 @@ extension QPlayAutoManager: QPlayAutoSDKDelegate { |
|
|
} else { |
|
|
} else { |
|
|
// 登录失败时清除可能无效的token |
|
|
// 登录失败时清除可能无效的token |
|
|
clearStoredToken() |
|
|
clearStoredToken() |
|
|
print("QPlayAutoManager: ❌ 登录失败,但不立即清理回调(等待重试或最终失败)") |
|
|
print("QPlayAutoManager: ❌ 登录失败") |
|
|
// 注意:这里不立即调用 notifyLoginCompletion(false) |
|
|
|
|
|
// 让重试机制来决定是否最终失败 |
|
|
// 如果有等待的回调,立即通知失败 |
|
|
// 如果有等待的回调且不在重试过程中,则通知失败 |
|
|
|
|
|
if !loginCompletionHandlers.isEmpty { |
|
|
if !loginCompletionHandlers.isEmpty { |
|
|
// 给一个短暂的延迟,让重试机制有机会处理 |
|
|
print("QPlayAutoManager: 登录失败,清理回调队列") |
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) { |
|
|
notifyLoginCompletion(false) |
|
|
if !self.loginCompletionHandlers.isEmpty && !self.isLoginOK { |
|
|
|
|
|
print("QPlayAutoManager: 登录失败,清理回调队列") |
|
|
|
|
|
self.notifyLoginCompletion(false) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|