
主要变更: 1. 移除 Swift 中重复实现的 getKeyWindow() 方法,统一调用 ObjC inline 函数 kGetKeyWindow()。 2. 更新 EPLoginManager 中的相关调用,确保一致性和简洁性。 此更新旨在提升代码的可维护性,减少冗余实现,确保跨语言调用的一致性。
130 lines
4.8 KiB
Swift
130 lines
4.8 KiB
Swift
//
|
||
// EPLoginManager.swift
|
||
// YuMi
|
||
//
|
||
// Created by AI on 2025-01-27.
|
||
//
|
||
|
||
import UIKit
|
||
|
||
/// 登录管理器(Swift 版本)
|
||
/// 替代 PILoginManager,处理登录成功后的路由和初始化
|
||
@objc class EPLoginManager: NSObject {
|
||
|
||
// MARK: - Login Success Navigation
|
||
|
||
/// 登录成功后跳转首页
|
||
/// - Parameter viewController: 当前视图控制器
|
||
static func jumpToHome(from viewController: UIViewController) {
|
||
|
||
// 1. 获取当前账号信息
|
||
guard let accountModel = AccountInfoStorage.instance().getCurrentAccountInfo() else {
|
||
print("[EPLoginManager] 账号信息不完整,无法继续")
|
||
return
|
||
}
|
||
|
||
let accessToken = accountModel.access_token
|
||
guard !accessToken.isEmpty else {
|
||
print("[EPLoginManager] access_token 为空,无法继续")
|
||
return
|
||
}
|
||
|
||
// 2. 请求 ticket
|
||
let loginService = EPLoginService()
|
||
loginService.requestTicket(accessToken: accessToken) { ticket in
|
||
|
||
// 3. 保存 ticket
|
||
AccountInfoStorage.instance().saveTicket(ticket)
|
||
|
||
// 4. 切换到 EPTabBarController
|
||
DispatchQueue.main.async {
|
||
let epTabBar = EPTabBarController.create()
|
||
epTabBar.refreshTabBarWithIsLogin(true)
|
||
|
||
// 设置为根控制器(统一从 ObjC inline 函数获取)
|
||
if let window = kGetKeyWindow() {
|
||
window.rootViewController = epTabBar
|
||
window.makeKeyAndVisible()
|
||
|
||
// 延迟检查专属颜色(登录成功后引导)
|
||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.8) {
|
||
Self.checkAndShowSignatureColorGuide(in: window)
|
||
}
|
||
}
|
||
|
||
print("[EPLoginManager] 登录成功,已切换到 EPTabBarController")
|
||
}
|
||
|
||
} failure: { code, msg in
|
||
print("[EPLoginManager] 请求 Ticket 失败: \(code) - \(msg)")
|
||
|
||
// Ticket 请求失败,仍然跳转到首页(保持原有行为)
|
||
DispatchQueue.main.async {
|
||
let epTabBar = EPTabBarController.create()
|
||
epTabBar.refreshTabBarWithIsLogin(true)
|
||
|
||
if let window = kGetKeyWindow() {
|
||
window.rootViewController = epTabBar
|
||
window.makeKeyAndVisible()
|
||
|
||
// 延迟检查专属颜色(登录成功后引导)
|
||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.8) {
|
||
Self.checkAndShowSignatureColorGuide(in: window)
|
||
}
|
||
}
|
||
|
||
print("[EPLoginManager] Ticket 请求失败,仍跳转到首页")
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Apple Login 接口占位(不实现)
|
||
/// - Parameter viewController: 当前视图控制器
|
||
static func loginWithApple(from viewController: UIViewController) {
|
||
print("[EPLoginManager] Apple Login - 占位,Phase 2 实现")
|
||
// 占位,打印 log
|
||
}
|
||
|
||
// MARK: - Helper Methods
|
||
|
||
// Swift 侧不再重复实现 keyWindow 获取,统一调用 ObjC inline 函数 kGetKeyWindow()
|
||
|
||
/// 检查并显示专属颜色引导页
|
||
private static func checkAndShowSignatureColorGuide(in window: UIWindow) {
|
||
let hasSignatureColor = EPEmotionColorStorage.hasUserSignatureColor()
|
||
|
||
// #if DEBUG
|
||
print("[EPLoginManager] Debug 模式:显示专属颜色引导页(已有颜色: \(hasSignatureColor))")
|
||
|
||
let guideView = EPSignatureColorGuideView()
|
||
|
||
// 设置颜色确认回调
|
||
guideView.onColorConfirmed = { (hexColor: String) in
|
||
EPEmotionColorStorage.saveUserSignatureColor(hexColor)
|
||
print("[EPLoginManager] 用户选择专属颜色: \(hexColor)")
|
||
}
|
||
|
||
// 如果已有颜色,设置 Skip 回调
|
||
if hasSignatureColor {
|
||
guideView.onSkipTapped = {
|
||
print("[EPLoginManager] 用户跳过专属颜色选择")
|
||
}
|
||
}
|
||
|
||
// 显示引导页,已有颜色时显示 Skip 按钮
|
||
guideView.show(in: window, showSkipButton: hasSignatureColor)
|
||
|
||
// #else
|
||
// // Release 环境:仅在未设置专属颜色时显示
|
||
// if !hasSignatureColor {
|
||
// let guideView = EPSignatureColorGuideView()
|
||
// guideView.onColorConfirmed = { (hexColor: String) in
|
||
// EPEmotionColorStorage.saveUserSignatureColor(hexColor)
|
||
// }
|
||
// guideView.show(in: window)
|
||
// }
|
||
// #endif
|
||
}
|
||
}
|
||
|