import SwiftUI /// 设置页面视图 struct AccountView: View { // MARK: - 属性 /// 环境变量 - 用于dismiss视图 @Environment(\.dismiss) private var dismiss /// 控制删除确认弹窗显示 @State private var showDeleteConfirmation = false // MARK: - 动画配置 /// 动画配置 private let animation = Animation.spring( response: 0.6, // 响应时间 dampingFraction: 0.9, // 阻尼系数 blendDuration: 0.8 // 混合时间 ) // MARK: - 主体视图 var body: some View { ZStack { // Theme background color Color.themeTextWhiteSecondary .edgesIgnoringSafeArea(.all) VStack(spacing: 0) { // 自定义导航头 SimpleNaviHeader(title: "Account & Security") { withAnimation(animation) { Router.shared.pop() } } .padding(.top, UIApplication.shared.windows.first?.safeAreaInsets.top) // 内容区域 ScrollView { VStack(spacing: 0) { // 注销登录 settingRow( title: "Log Out", action: { // 处理注销登录逻辑 self.handleLogout() } ) .padding() // 删除账号 settingRow( title: "Delete Account", action: { withAnimation { showDeleteConfirmation = true } } ) .padding() } .background(Color.white) .cornerRadius(12) .padding(.horizontal) } .background(Color.themeTextWhiteSecondary) } .frame(maxWidth: .infinity, maxHeight: .infinity) .edgesIgnoringSafeArea(.top) } .edgesIgnoringSafeArea(.all) .frame(maxWidth: .infinity, maxHeight: .infinity) .overlay( // 删除确认弹窗 Group { if showDeleteConfirmation { ZStack { // 半透明黑色遮罩 Color.black.opacity(0.6) .edgesIgnoringSafeArea(.all) .onTapGesture { withAnimation { showDeleteConfirmation = false } } // 确认弹窗内容 VStack(spacing: 20) { Text("Are you sure you want to delete this account?") .font(.headline) .font(.system(size: 14, weight: .medium)) .foregroundColor(.themeTextMessageMain) Text("Once deleted, you can’t get it back.") .multilineTextAlignment(.center) .font(.subheadline) .font(.system(size: 12)) .foregroundColor(.themeTextMessage) HStack(spacing: 20) { Button(action: { withAnimation { showDeleteConfirmation = false } }) { Text("Cancel") .font(Typography.font(for: .subtitle, family: .quicksandBold)) .foregroundColor(.themeTextMessageMain) .frame(maxWidth: .infinity) .padding() .background(Color.themePrimary) .cornerRadius(32) } Button(action: { // 处理删除账号逻辑 NetworkService.shared.delete(path: "/iam/delete-user", parameters: nil) { (result: Result) in switch result { case .success(let data): print("删除账号成功: \(data)") case .failure(let error): print("删除账号失败: \(error)") } } // 关闭弹窗 withAnimation { showDeleteConfirmation = false } }) { Text("Confirm") .foregroundColor(.themeTextWhite) .font(.system(size: 12)) .frame(maxWidth: .infinity) .padding() .background(Color.themeTextWhiteSecondary) .cornerRadius(32) } } } .padding() .frame(width: 300) .background(Color.white) .cornerRadius(12) .shadow(radius: 10) } .transition(.opacity) } } ) .navigationBarBackButtonHidden(true) // 隐藏默认返回按钮 .navigationBarHidden(true) // 隐藏导航栏 } // MARK: - 私有方法 /// 处理用户登出逻辑 private func handleLogout() { // 1. 清除认证token TokenManager.shared.clearTokens() // 2. 清除本地存储的用户信息 UserDefaults.standard.removeObject(forKey: "lastLoginUser") // 3. 更新认证状态 AuthState.shared.logout() // 4. 跳转到登录页面 Router.shared.popToRoot() Router.shared.navigate(to: .login) } /// 配置TableView外观 private func configureTableView() { // 移除列表底部分隔线 UITableView.appearance().tableFooterView = UIView() // 移除列表顶部分隔线 UITableView.appearance().tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: CGFloat.leastNonzeroMagnitude)) // 移除分隔线缩进 UITableView.appearance().separatorInset = .zero // 移除列表顶部额外间距 UITableView.appearance().contentInset = .zero } /// 创建设置项行视图 /// - Parameters: /// - icon: 图标名称 /// - title: 标题 /// - action: 点击动作 /// - Returns: 返回设置项行视图 private func settingRow(title: String, action: @escaping () -> Void) -> some View { Button(action: action) { HStack { // 标题 Text(title) .foregroundColor(.primary) Spacer() // 右侧箭头 Image(systemName: "chevron.right") .font(.system(size: 14)) .foregroundColor(.gray) } .padding(.vertical, 12) // 增加垂直内边距 .background(Color.white) } .buttonStyle(PlainButtonStyle()) .listRowBackground(Color.white) .listRowSeparator(.hidden) } } // MARK: - 预览 #Preview { NavigationView { AccountView() } }