|
|
|
@ -316,7 +316,27 @@ import QPlayAutoSDK |
|
|
|
} |
|
|
|
|
|
|
|
// MARK: - 登录状态管理 |
|
|
|
// 在第206行之前添加新的方法 |
|
|
|
// Add the error enum at the top of the class |
|
|
|
enum QPlayAutoError: Error { |
|
|
|
case qqMusicNotInstalled |
|
|
|
case loginFailed |
|
|
|
case connectionFailed |
|
|
|
case searchFailed(String) // 新增搜索失败错误 |
|
|
|
|
|
|
|
var localizedDescription: String { |
|
|
|
switch self { |
|
|
|
case .qqMusicNotInstalled: |
|
|
|
return "QQ音乐未安装" |
|
|
|
case .loginFailed: |
|
|
|
return "登录失败" |
|
|
|
case .connectionFailed: |
|
|
|
return "连接失败" |
|
|
|
case .searchFailed(let reason): |
|
|
|
return "搜索失败: \(reason)" |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private func ensureQQMusicInstalledAndLoginAndExecute( |
|
|
|
_ operation: @escaping () -> Void, |
|
|
|
onQQMusicNotInstalled: (() -> Void)? = nil, |
|
|
|
@ -330,15 +350,37 @@ import QPlayAutoSDK |
|
|
|
} |
|
|
|
|
|
|
|
// QQ音乐已安装,继续检查登录状态 |
|
|
|
ensureLoginAndExecute(operation, onLoginFailed: onLoginFailed) |
|
|
|
ensureLoginAndExecute(operation) { error in |
|
|
|
switch error { |
|
|
|
case .loginFailed: |
|
|
|
onLoginFailed?() |
|
|
|
case .qqMusicNotInstalled: |
|
|
|
// This shouldn't happen since we already checked above |
|
|
|
onQQMusicNotInstalled?() |
|
|
|
default: |
|
|
|
onLoginFailed?() |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 修改现有的ensureLoginAndExecute方法(第206行开始) |
|
|
|
private func ensureLoginAndExecute(_ operation: @escaping () -> Void, onLoginFailed: (() -> Void)? = nil) { |
|
|
|
// 修改现有的ensureLoginAndExecute方法 |
|
|
|
private func ensureLoginAndExecute( |
|
|
|
_ operation: @escaping () -> Void, |
|
|
|
onFailure: ((QPlayAutoError) -> Void)? = nil |
|
|
|
) { |
|
|
|
// 首先检查QQ音乐是否已安装 |
|
|
|
guard isQQMusicInstalled else { |
|
|
|
print("QPlayAutoManager: QQ音乐未安装,无法执行操作") |
|
|
|
onFailure?(.qqMusicNotInstalled) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
if isLoginOK { |
|
|
|
// 已登录,直接执行操作 |
|
|
|
operation() |
|
|
|
} else { |
|
|
|
|
|
|
|
|
|
|
|
// 未登录,先登录 |
|
|
|
print("QPlayAutoManager: 未登录,正在尝试登录...") |
|
|
|
performLogin { [weak self] success in |
|
|
|
@ -347,7 +389,7 @@ import QPlayAutoSDK |
|
|
|
operation() |
|
|
|
} else { |
|
|
|
print("QPlayAutoManager: 登录失败,无法执行操作") |
|
|
|
onLoginFailed?() |
|
|
|
onFailure?(.loginFailed) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
@ -533,33 +575,34 @@ import QPlayAutoSDK |
|
|
|
} |
|
|
|
|
|
|
|
@objc public func pause() { |
|
|
|
ensureLoginAndExecute { |
|
|
|
ensureLoginAndExecute({ |
|
|
|
QPlayAutoSDK.playerPlayPause() |
|
|
|
} onLoginFailed: { |
|
|
|
print("QPlayAutoManager: 暂停失败 - 登录失败") |
|
|
|
} |
|
|
|
}, onFailure: { error in |
|
|
|
print("QPlayAutoManager: 暂停失败 - \(error)") |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
// Fix seek method (lines 571-578) |
|
|
|
@objc public func seek(to position: TimeInterval) { |
|
|
|
ensureLoginAndExecute { |
|
|
|
QPlayAutoSDK.playerSeek(Int(position)) |
|
|
|
} onLoginFailed: { |
|
|
|
print("QPlayAutoManager: 跳转播放位置失败 - 登录失败") |
|
|
|
} |
|
|
|
ensureLoginAndExecute { |
|
|
|
QPlayAutoSDK.playerSeek(Int(position)) |
|
|
|
} onFailure: { error in |
|
|
|
print("QPlayAutoManager: 跳转播放位置失败 - \(error)") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// MARK: - 扩展功能方法(需要添加登录检查) |
|
|
|
|
|
|
|
/// 设置播放模式 |
|
|
|
@objc public func setPlayMode(_ playMode: QPlayAutoPlayMode, completion: @escaping (Bool) -> Void) { |
|
|
|
ensureLoginAndExecute { |
|
|
|
QPlayAutoSDK.setPlayMode(playMode) { (success, dict) in |
|
|
|
completion(success) |
|
|
|
} |
|
|
|
} onLoginFailed: { |
|
|
|
print("QPlayAutoManager: 设置播放模式失败 - 登录失败") |
|
|
|
completion(false) |
|
|
|
} |
|
|
|
ensureLoginAndExecute { |
|
|
|
QPlayAutoSDK.setPlayMode(playMode) { (success, dict) in |
|
|
|
completion(success) |
|
|
|
} |
|
|
|
} onFailure: { error in |
|
|
|
print("QPlayAutoManager: 设置播放模式失败 - \(error)") |
|
|
|
completion(false) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// 收藏/取消收藏歌曲 |
|
|
|
@ -568,11 +611,12 @@ import QPlayAutoSDK |
|
|
|
QPlayAutoSDK.setFavoriteStateWithSong(song, isFavorite: isFavorite) { errorCode in |
|
|
|
completion(errorCode == 0) |
|
|
|
} |
|
|
|
} onLoginFailed: { |
|
|
|
print("QPlayAutoManager: 设置收藏状态失败 - 登录失败") |
|
|
|
completion(false) |
|
|
|
}onFailure: { error in |
|
|
|
print("QPlayAutoManager: 设置收藏状态失败 - 登录失败 - \(error)") |
|
|
|
completion(false) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// 获取相似歌曲 |
|
|
|
@objc public func requestSimilarSongs(song: QPlayAutoListItem, completion: @escaping ([QPlayAutoListItem]?, Error?) -> Void) { |
|
|
|
@ -585,12 +629,12 @@ import QPlayAutoSDK |
|
|
|
completion(nil, error) |
|
|
|
} |
|
|
|
} |
|
|
|
} onLoginFailed: { |
|
|
|
print("QPlayAutoManager: 请求相似歌曲失败 - 登录失败") |
|
|
|
let error = NSError(domain: "QPlayAutoManager", code: -1, userInfo: [NSLocalizedDescriptionKey: "QQ音乐未安装"]) |
|
|
|
}onFailure: { error in |
|
|
|
print("QPlayAutoManager: 请求相似歌曲失败 - 登录失败 - \(error)") |
|
|
|
completion(nil, error) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// 获取歌词 |
|
|
|
@objc public func requestLyric(song: QPlayAutoListItem, completion: @escaping (QPlayAutoLyric?, Error?) -> Void) { |
|
|
|
@ -603,12 +647,11 @@ import QPlayAutoSDK |
|
|
|
completion(nil, error) |
|
|
|
} |
|
|
|
} |
|
|
|
} onLoginFailed: { |
|
|
|
print("QPlayAutoManager: 请求歌词失败 - 登录失败") |
|
|
|
let error = NSError(domain: "QPlayAutoManager", code: -1, userInfo: [NSLocalizedDescriptionKey: "QQ音乐未安装"]) |
|
|
|
} onFailure: { error in |
|
|
|
print("QPlayAutoManager: 请求相似歌曲失败 - 登录失败 - \(error)") |
|
|
|
completion(nil, error) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// 获取歌曲列表 |
|
|
|
@objc public func requestSonglist(type: QPlayAutoSongListType, completion: @escaping ([QPlayAutoListItem]?, Error?) -> Void) { |
|
|
|
@ -621,12 +664,11 @@ import QPlayAutoSDK |
|
|
|
completion(nil, error) |
|
|
|
} |
|
|
|
} |
|
|
|
} onLoginFailed: { |
|
|
|
print("QPlayAutoManager: 请求歌单失败 - 登录失败") |
|
|
|
let error = NSError(domain: "QPlayAutoManager", code: -1, userInfo: [NSLocalizedDescriptionKey: "QQ音乐未安装"]) |
|
|
|
} onFailure: { error in |
|
|
|
print("QPlayAutoManager: 请求相似歌曲失败 - 登录失败 - \(error)") |
|
|
|
completion(nil, error) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// 播放指定歌曲列表 |
|
|
|
@objc public func playAtIndex(songList: [QPlayAutoListItem], playIndex: Int, completion: @escaping (Bool) -> Void) { |
|
|
|
@ -634,11 +676,11 @@ import QPlayAutoSDK |
|
|
|
QPlayAutoSDK.play(atIndex: songList, play: playIndex) { errorCode in |
|
|
|
completion(errorCode == 0) |
|
|
|
} |
|
|
|
} onLoginFailed: { |
|
|
|
print("QPlayAutoManager: 播放指定索引歌曲失败 - QQ音乐未安装") |
|
|
|
} onFailure: { error in |
|
|
|
print("QPlayAutoManager: 设置收藏状态失败 - 登录失败 - \(error)") |
|
|
|
completion(false) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// MARK: - 状态查询 |
|
|
|
@objc public var isConnected: Bool { |
|
|
|
@ -729,17 +771,21 @@ import QPlayAutoSDK |
|
|
|
@objc public func search( |
|
|
|
keyword: String, |
|
|
|
searchType: QPlayAutoSearchType, |
|
|
|
completion: @escaping ([QPlayAutoListItem]?) -> Void |
|
|
|
completion: @escaping ([QPlayAutoListItem]?) -> Void, |
|
|
|
onError: ((Error?) -> Void)? = nil |
|
|
|
) { |
|
|
|
ensureLoginAndExecute { |
|
|
|
QPlayAutoSDK.search(keyword, type: searchType, firstPage: true) { (error, items) in |
|
|
|
completion(items) |
|
|
|
} |
|
|
|
} onLoginFailed: { |
|
|
|
print("QPlayAutoManager: 搜索失败 - 登录失败") |
|
|
|
completion(nil) |
|
|
|
}onFailure: { error in |
|
|
|
print("QPlayAutoManager: 搜索失败 - 登录失败 - \(error)") |
|
|
|
let loginError = NSError(domain: "QPlayAutoManager", code: -1, userInfo: [ |
|
|
|
NSLocalizedDescriptionKey: "登录失败: \(error)" |
|
|
|
]) |
|
|
|
onError?(loginError) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@objc public func getItems( |
|
|
|
playlistId: String, |
|
|
|
@ -756,11 +802,12 @@ import QPlayAutoSDK |
|
|
|
completion(nil) |
|
|
|
} |
|
|
|
} |
|
|
|
} onLoginFailed: { |
|
|
|
print("QPlayAutoManager: 获取播放列表失败 - 登录失败") |
|
|
|
} onFailure: { error in |
|
|
|
print("QPlayAutoManager: 设置收藏状态失败 - 登录失败 - \(error)") |
|
|
|
completion(nil) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 添加公开的登录方法 |
|
|
|
@objc public func login(completion: @escaping (Bool) -> Void) { |
|
|
|
|