Files
suixinkan_uikit/suixinkan/Features/Auth/AuthSessionHelper.swift
汉秋 cfcd3eee6f Integrate Amap SDK to replace CoreLocation and MapKit for all location features.
Unify GPS, reverse geocoding, and map display behind LocationProvider with privacy-gated bootstrap after login, aligned with Android.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-07 11:49:22 +08:00

123 lines
5.0 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// AuthSessionHelper.swift
// suixinkan
//
import Foundation
/// Token广
enum AuthSessionHelper {
/// Token SceneDelegate
static func completeLogin(
with response: V9AuthResponse,
username: String,
privacyAgreementAccepted: Bool,
selectedAccount: AccountSwitchAccount
) {
let token = response.token.trimmingCharacters(in: .whitespacesAndNewlines)
guard !token.isEmpty else { return }
AppStore.shared.saveToken(token)
AppStore.shared.lastLoginUsername = username
AppStore.shared.privacyAgreementAccepted = privacyAgreementAccepted
applyAccountSession(from: selectedAccount, in: response)
if privacyAgreementAccepted {
AMapBootstrap.configureIfNeeded()
}
NotificationCenter.default.post(name: NotificationName.userDidLogin, object: nil)
}
///
static func applyAccountSwitch(
with response: V9AuthResponse,
account: AccountSwitchAccount
) {
let token = response.token.trimmingCharacters(in: .whitespacesAndNewlines)
guard !token.isEmpty else { return }
AppStore.shared.saveToken(token)
applyAccountSession(from: account, in: response)
NotificationCenter.default.post(name: NotificationName.accountDidSwitch, object: nil)
}
/// v9 AppStore
static func applyAccountSession(from account: AccountSwitchAccount, in response: V9AuthResponse) {
AppStore.shared.applyAccount(account)
if let scenicUser = response.scenicUsers.first(where: { matches($0, account: account) }) {
applyScenicUser(scenicUser)
return
}
if let storeUser = response.storeUsers.first(where: { matches($0, account: account) }) {
applyStoreUser(storeUser)
}
}
/// v9 `isCurrent`
static func resolveCurrentAccount(from response: V9AuthResponse) -> AccountSwitchAccount? {
let accounts = response.accounts.filter { $0.businessUserId > 0 }
if let current = accounts.first(where: \.isCurrent) {
return current
}
return accounts.first
}
private static func applyScenicUser(_ user: V9ScenicUser) {
AppStore.shared.userId = String(user.businessUserId)
AppStore.shared.accountType = V9ScenicUser.accountTypeValue
AppStore.shared.accountDisplayName = firstNonEmpty(
user.scenicName, user.nickname, user.realName, user.username
)
AppStore.shared.userName = firstNonEmpty(user.nickname, user.username, user.realName)
AppStore.shared.realName = firstNonEmpty(user.realName, user.nickname, user.username)
AppStore.shared.phone = user.phone
AppStore.shared.currentScenicId = user.scenicId
AppStore.shared.currentScenicName = user.scenicName
AppStore.shared.currentStoreId = 0
if !user.appRoleCode.isEmpty {
AppStore.shared.roleCode = user.appRoleCode
}
if !user.appRoleName.isEmpty {
AppStore.shared.roleName = user.appRoleName
}
}
private static func applyStoreUser(_ user: V9StoreUser) {
AppStore.shared.userId = String(user.businessUserId)
AppStore.shared.accountType = V9StoreUser.accountTypeValue
AppStore.shared.accountDisplayName = firstNonEmpty(
user.storeName, user.scenicName, user.realName, user.userName, user.username
)
AppStore.shared.userName = firstNonEmpty(user.userName, user.username, user.realName)
AppStore.shared.realName = firstNonEmpty(user.realName, user.userName, user.username)
AppStore.shared.phone = user.phone
AppStore.shared.avatar = user.avatar
AppStore.shared.currentScenicId = user.scenicId
AppStore.shared.currentScenicName = user.scenicName
AppStore.shared.currentStoreId = user.storeId
if !user.appRoleCode.isEmpty {
AppStore.shared.roleCode = user.appRoleCode
}
if !user.appRoleName.isEmpty {
AppStore.shared.roleName = user.appRoleName
}
}
private static func matches(_ user: V9ScenicUser, account: AccountSwitchAccount) -> Bool {
user.businessUserId == account.businessUserId
|| account.id == "\(V9ScenicUser.accountTypeValue)_\(user.businessUserId)"
}
private static func matches(_ user: V9StoreUser, account: AccountSwitchAccount) -> Bool {
user.businessUserId == account.businessUserId
|| account.id == "\(V9StoreUser.accountTypeValue)_\(user.businessUserId)"
}
private static func firstNonEmpty(_ values: String...) -> String {
values.first { !$0.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty } ?? ""
}
}