
主要变更: 1. 在 .gitignore 中添加了 Docs/ 文件夹,以忽略文档相关文件。 2. 删除了多个过时的文档,包括构建指南、编译修复指南和当前状态报告等。 此更新旨在清理项目文件,确保版本控制的整洁性。
150 lines
5.5 KiB
Swift
150 lines
5.5 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)
|
||
|
||
// 设置为根控制器
|
||
if let window = getKeyWindow() {
|
||
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 = getKeyWindow() {
|
||
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
|
||
|
||
/// 获取 keyWindow(iOS 13+ 兼容)
|
||
private static func getKeyWindow() -> UIWindow? {
|
||
if #available(iOS 13.0, *) {
|
||
for windowScene in UIApplication.shared.connectedScenes {
|
||
if let windowScene = windowScene as? UIWindowScene,
|
||
windowScene.activationState == .foregroundActive {
|
||
for window in windowScene.windows {
|
||
if window.isKeyWindow {
|
||
return window
|
||
}
|
||
}
|
||
// 如果没有 keyWindow,返回第一个 window
|
||
return windowScene.windows.first
|
||
}
|
||
}
|
||
} else {
|
||
// iOS 13 以下,使用旧方法(已废弃但仍然可用)
|
||
return UIApplication.shared.keyWindow
|
||
}
|
||
return nil
|
||
}
|
||
|
||
/// 检查并显示专属颜色引导页
|
||
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
|
||
}
|
||
}
|
||
|