import SwiftUI /// 我的。未登录显示登录引导;已登录显示账户信息 + 菜单(子页 M3/M5 接) + 退出登录。 struct ProfileView: View { @Environment(SessionStore.self) private var session @State private var showLogoutConfirm = false var body: some View { NavigationStack { Group { if session.isLoggedIn { loggedInContent } else { LoginRequiredView(message: "登录后查看你的账户、比价记录与省钱战绩") } } .navigationTitle("我的") .task { await session.refreshProfile() } } } private var loggedInContent: some View { List { Section { NavigationLink { ProfileEditView() } label: { HStack(spacing: 14) { avatarView VStack(alignment: .leading, spacing: 4) { Text(session.user?.nickname ?? "傻瓜比价用户") .font(.headline) if let phone = session.user?.phone { Text(phone.maskedPhone) .font(.subheadline) .foregroundStyle(.secondary) } } } .padding(.vertical, 6) } } Section { NavigationLink { CompareRecordsView() } label: { Label("我的比价记录", systemImage: "list.bullet.rectangle") } NavigationLink { SavingsView() } label: { Label("省钱战绩", systemImage: "chart.line.uptrend.xyaxis") } } Section { NavigationLink { FeedbackView() } label: { Label("帮助与反馈", systemImage: "questionmark.circle") } NavigationLink { InviteView() } label: { Label("邀请好友", systemImage: "person.2") } NavigationLink { SettingsView() } label: { Label("设置", systemImage: "gearshape") } } Section { Button(role: .destructive) { showLogoutConfirm = true } label: { Text("退出登录").frame(maxWidth: .infinity) } } } .alert("确认退出登录?", isPresented: $showLogoutConfirm) { Button("退出", role: .destructive) { Task { await session.logout() } } Button("取消", role: .cancel) {} } } private var avatarView: some View { AsyncImage(url: AppConfig.mediaURL(session.user?.avatarUrl)) { phase in if case .success(let image) = phase { image.resizable().aspectRatio(contentMode: .fill) } else { Image(systemName: "person.crop.circle.fill") .resizable() .foregroundStyle(AppColors.gray300) } } .frame(width: 52, height: 52) .clipShape(Circle()) } }