修复账号切换后「我的」页当前账号展示错误,并统一举报摄影师首页 URI。
切换账号时以用户选中账号为准写入展示标题与业务作用域,避免沿用旧门店名称;同时将首页举报摄影师入口 URI 对齐为 report_photographer。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@ -63,11 +63,22 @@ struct BusinessScope: Codable, Equatable, Identifiable {
|
||||
final class AccountContext: ObservableObject {
|
||||
@Published private(set) var profile: AccountProfile?
|
||||
@Published private(set) var accountType: String?
|
||||
@Published private(set) var businessUserId: Int?
|
||||
/// 当前账号在「我的」页面展示的标题,来源于登录/切换时选中的账号名称。
|
||||
@Published private(set) var currentAccountDisplayTitle: String?
|
||||
@Published private(set) var scenicScopes: [BusinessScope] = []
|
||||
@Published private(set) var storeScopes: [BusinessScope] = []
|
||||
@Published var currentScenic: BusinessScope?
|
||||
@Published var currentStore: BusinessScope?
|
||||
|
||||
/// 当前账号标识,与 AccountSwitchAccount.id 规则一致。
|
||||
var currentAccountIdentifier: String? {
|
||||
guard let businessUserId, businessUserId > 0 else { return nil }
|
||||
let type = accountType?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
|
||||
guard !type.isEmpty else { return nil }
|
||||
return "\(type)_\(businessUserId)"
|
||||
}
|
||||
|
||||
/// 当前账号缓存前缀,与 Android 的账号级缓存作用域保持一致。
|
||||
var accountCachePrefix: String? {
|
||||
let userId = profile?.userId.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
|
||||
@ -77,9 +88,20 @@ final class AccountContext: ObservableObject {
|
||||
}
|
||||
|
||||
/// 应用登录成功后写入账号资料。
|
||||
func applyLogin(profile: AccountProfile? = nil, accountType: String? = nil) {
|
||||
func applyLogin(
|
||||
profile: AccountProfile? = nil,
|
||||
accountType: String? = nil,
|
||||
businessUserId: Int? = nil,
|
||||
displayTitle: String? = nil
|
||||
) {
|
||||
self.profile = profile
|
||||
self.accountType = accountType?.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
if let businessUserId, businessUserId > 0 {
|
||||
self.businessUserId = businessUserId
|
||||
}
|
||||
if let displayTitle = normalizedText(displayTitle) {
|
||||
currentAccountDisplayTitle = displayTitle
|
||||
}
|
||||
}
|
||||
|
||||
/// 替换当前账号资料,通常用于用户信息刷新后同步全局展示。
|
||||
@ -92,10 +114,29 @@ final class AccountContext: ObservableObject {
|
||||
scenic: [BusinessScope],
|
||||
stores: [BusinessScope],
|
||||
currentScenicId: Int? = nil,
|
||||
currentStoreId: Int? = nil
|
||||
currentStoreId: Int? = nil,
|
||||
explicitSelection: Bool = false
|
||||
) {
|
||||
scenicScopes = scenic
|
||||
storeScopes = stores
|
||||
|
||||
if explicitSelection {
|
||||
currentScenic = currentScenicId.flatMap { id in
|
||||
scenic.first { $0.id == id }
|
||||
} ?? scenic.first
|
||||
if let storeId = currentStoreId, storeId > 0 {
|
||||
currentStore = resolvedStore(
|
||||
in: stores,
|
||||
currentScenic: currentScenic,
|
||||
preferredStoreId: storeId,
|
||||
allowFallbackToFirst: false
|
||||
)
|
||||
} else {
|
||||
currentStore = nil
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
currentScenic = currentScenicId.flatMap { id in
|
||||
scenic.first { $0.id == id }
|
||||
} ?? currentScenic.flatMap { current in
|
||||
@ -104,7 +145,8 @@ final class AccountContext: ObservableObject {
|
||||
currentStore = resolvedStore(
|
||||
in: stores,
|
||||
currentScenic: currentScenic,
|
||||
preferredStoreId: currentStoreId ?? currentStore?.id
|
||||
preferredStoreId: currentStoreId ?? currentStore?.id,
|
||||
allowFallbackToFirst: true
|
||||
)
|
||||
}
|
||||
|
||||
@ -115,7 +157,8 @@ final class AccountContext: ObservableObject {
|
||||
currentStore = resolvedStore(
|
||||
in: storeScopes,
|
||||
currentScenic: scenic,
|
||||
preferredStoreId: currentStore?.id
|
||||
preferredStoreId: currentStore?.id,
|
||||
allowFallbackToFirst: true
|
||||
)
|
||||
}
|
||||
|
||||
@ -134,6 +177,8 @@ final class AccountContext: ObservableObject {
|
||||
func reset() {
|
||||
profile = nil
|
||||
accountType = nil
|
||||
businessUserId = nil
|
||||
currentAccountDisplayTitle = nil
|
||||
scenicScopes = []
|
||||
storeScopes = []
|
||||
currentScenic = nil
|
||||
@ -144,7 +189,8 @@ final class AccountContext: ObservableObject {
|
||||
private func resolvedStore(
|
||||
in stores: [BusinessScope],
|
||||
currentScenic: BusinessScope?,
|
||||
preferredStoreId: Int?
|
||||
preferredStoreId: Int?,
|
||||
allowFallbackToFirst: Bool
|
||||
) -> BusinessScope? {
|
||||
let scenicId = currentScenic?.id
|
||||
let storesForScenic = stores.filter { store in
|
||||
@ -155,6 +201,13 @@ final class AccountContext: ObservableObject {
|
||||
let store = storesForScenic.first(where: { $0.id == preferredStoreId }) {
|
||||
return store
|
||||
}
|
||||
guard allowFallbackToFirst else { return nil }
|
||||
return storesForScenic.first
|
||||
}
|
||||
|
||||
/// 返回去空白后的非空文本。
|
||||
private func normalizedText(_ value: String?) -> String? {
|
||||
let text = value?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
|
||||
return text.isEmpty ? nil : text
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user