Files
suixinkan_ios_new/suixinkan/Features/Home/Services/HomeCommonMenuStore.swift
汉秋 a0549e068a 彻底清理 promotion_page 残留引用,移除 hiddenHomeMenuURIs 机制。
宣传页已从白名单、菜单构建与文档中完全删除,不再保留专用隐藏逻辑。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-03 20:51:27 +08:00

221 lines
8.3 KiB
Swift
Raw Permalink 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.

//
// HomeCommonMenuStore.swift
// suixinkan
//
// Created by Codex on 2026/6/22.
//
import Foundation
/// URI
struct HomeCommonMenuStore {
static let storageKeyPrefix = "home.common.menu.uris.role."
static let accountScopedStorageKeyPrefix = "home.common.menu.uris.account."
/// Android `Constants.menuList` URI
static let androidHomeMenuURIs: Set<String> = [
"space_settings",
"wallet",
"cloud_management",
"asset_management",
"task_management",
"schedule_management",
"system_settings",
"message_center",
"checkin_points",
"sample_management",
"live_stream_management",
"verification_order",
"live_album",
"travel_album",
"pm",
"location_report",
"report_photographer",
"registration_invitation",
"store",
"fly",
"pilot_cert",
"task_management_editor",
"pm_manager",
"pilot_controller",
"/scenic-queue",
"operating-area",
"/scenic-order-manage",
"cooperation_order"
]
private let defaults: UserDefaults
/// UserDefaults
init(defaults: UserDefaults = .standard) {
self.defaults = defaults
}
/// Android URI 4 URI menuList
static func defaultCommonURIs(orderedTopLevelURIs: [String]) -> [String] {
let rawTopLevel = orderedTopLevelURIs.compactMap { uri -> String? in
let trimmed = uri.trimmingCharacters(in: .whitespacesAndNewlines)
return trimmed.isEmpty ? nil : trimmed
}
let candidates = rawTopLevel.count > 4 ? Array(rawTopLevel.prefix(4)) : rawTopLevel
return candidates.filter { isAndroidHomeMenuURI($0) }
}
/// Android menuList URI API
static func availableTopLevelMenuURIs(from orderedTopLevelURIs: [String]) -> [String] {
orderedTopLevelURIs.compactMap { uri -> String? in
let trimmed = uri.trimmingCharacters(in: .whitespacesAndNewlines)
guard !trimmed.isEmpty, isAndroidHomeMenuURI(trimmed) else { return nil }
return trimmed
}
}
/// URI saved 退
func load(
menuItems: [HomeMenuItem],
roleCode: String?,
accountScope: String? = nil,
legacyRoleId: Int? = nil,
topLevelPermissionURIs: [String]
) -> [String] {
guard let roleCode, !roleCode.isEmpty else { return [] }
migrateLegacyStorageIfNeeded(roleCode: roleCode, accountScope: accountScope, legacyRoleId: legacyRoleId)
let rawTopLevel = topLevelPermissionURIs.compactMap { uri -> String? in
let trimmed = uri.trimmingCharacters(in: .whitespacesAndNewlines)
return trimmed.isEmpty ? nil : trimmed
}
let availableTopLevel = Self.availableTopLevelMenuURIs(from: rawTopLevel)
let availableTopLevelSet = Set(availableTopLevel)
let key = Self.storageKey(roleCode: roleCode, accountScope: accountScope)
let saved = defaults.stringArray(forKey: key) ?? []
let defaultCommon = Self.defaultCommonURIs(orderedTopLevelURIs: rawTopLevel)
HomeCommonMenuDiagnostics.log(
step: "context",
fields: [
"roleCode": roleCode,
"accountScope": accountScope ?? "",
"storageKey": key
]
)
HomeCommonMenuDiagnostics.log(
step: "rawTopLevel",
fields: [
"count": String(rawTopLevel.count),
"uris": HomeCommonMenuDiagnostics.formatURIList(rawTopLevel)
]
)
HomeCommonMenuDiagnostics.log(
step: "availableFunctions",
fields: [
"count": String(availableTopLevel.count),
"uris": HomeCommonMenuDiagnostics.formatURIList(availableTopLevel)
]
)
HomeCommonMenuDiagnostics.log(
step: "savedCommon",
fields: [
"uris": HomeCommonMenuDiagnostics.formatURIList(saved)
]
)
HomeCommonMenuDiagnostics.log(
step: "defaultCommon",
fields: [
"uris": HomeCommonMenuDiagnostics.formatURIList(defaultCommon)
]
)
let finalCommon: [String]
if !saved.isEmpty {
finalCommon = saved.filter { availableTopLevelSet.contains($0) }
} else {
finalCommon = defaultCommon
save(finalCommon, roleCode: roleCode, accountScope: accountScope)
}
HomeCommonMenuDiagnostics.log(
step: "finalCommon",
fields: [
"count": String(finalCommon.count),
"uris": HomeCommonMenuDiagnostics.formatURIList(finalCommon)
]
)
_ = menuItems
return finalCommon
}
/// URI 使 URI
func add(
_ uri: String,
current: [String],
topLevelPermissionURIs: [String],
roleCode: String?,
accountScope: String? = nil
) -> [String] {
guard let roleCode, !roleCode.isEmpty else { return current }
let availableTopLevel = Set(Self.availableTopLevelMenuURIs(from: topLevelPermissionURIs))
let trimmed = uri.trimmingCharacters(in: .whitespacesAndNewlines)
guard availableTopLevel.contains(trimmed), !current.contains(trimmed) else { return current }
let next = current + [trimmed]
save(next, roleCode: roleCode, accountScope: accountScope)
return next
}
/// URI使 URI
func remove(_ uri: String, current: [String], roleCode: String?, accountScope: String? = nil) -> [String] {
guard let roleCode, !roleCode.isEmpty else { return current }
let trimmed = uri.trimmingCharacters(in: .whitespacesAndNewlines)
let next = current.filter { $0 != trimmed }
save(next, roleCode: roleCode, accountScope: accountScope)
return next
}
/// URI
func save(_ uris: [String], roleCode: String, accountScope: String? = nil) {
defaults.set(uris, forKey: Self.storageKey(roleCode: roleCode, accountScope: accountScope))
}
/// key roleCode legacy role id key
func migrateLegacyStorageIfNeeded(roleCode: String, accountScope: String? = nil, legacyRoleId: Int?) {
let newKey = Self.storageKey(roleCode: roleCode, accountScope: accountScope)
guard defaults.stringArray(forKey: newKey) == nil else { return }
let legacyKeys = [
legacyStorageKey(for: roleCode),
legacyRoleId.map { legacyStorageKey(for: $0) }
].compactMap { $0 }
for legacyKey in legacyKeys {
guard let legacyData = defaults.stringArray(forKey: legacyKey), !legacyData.isEmpty else { continue }
defaults.set(legacyData, forKey: newKey)
defaults.removeObject(forKey: legacyKey)
return
}
}
/// role_code UserDefaults key
static func storageKey(roleCode: String, accountScope: String?) -> String {
let normalizedScope = accountScope?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
if normalizedScope.isEmpty {
return storageKeyPrefix + roleCode
}
return accountScopedStorageKeyPrefix + normalizedScope + ".role." + roleCode
}
/// numeric role id key
private func legacyStorageKey(for roleId: Int) -> String {
Self.storageKeyPrefix + String(roleId)
}
/// role_code key
private func legacyStorageKey(for roleCode: String) -> String {
Self.storageKeyPrefix + roleCode
}
/// URI Android
private static func isAndroidHomeMenuURI(_ uri: String) -> Bool {
androidHomeMenuURIs.contains(uri)
}
}