fix(training): 兼容后台 training 权限标识
This commit is contained in:
@@ -18,7 +18,7 @@ final class HomeCommonMenuStore {
|
||||
func savedCommonURIs(accountScope: String, roleCode: String) -> [String] {
|
||||
guard let key = storageKey(accountScope: accountScope, roleCode: roleCode) else { return [] }
|
||||
let saved = defaults.stringArray(forKey: key) ?? []
|
||||
let sanitized = saved.filter(HomeMenuCatalog.isCustomizable(uri:))
|
||||
let sanitized = Self.normalizedCommonURIs(saved)
|
||||
if sanitized != saved {
|
||||
defaults.set(sanitized, forKey: key)
|
||||
}
|
||||
@@ -28,12 +28,12 @@ final class HomeCommonMenuStore {
|
||||
/// 保存常用 URI 列表。
|
||||
func saveCommonURIs(_ uris: [String], accountScope: String, roleCode: String) {
|
||||
guard let key = storageKey(accountScope: accountScope, roleCode: roleCode) else { return }
|
||||
defaults.set(uris.filter(HomeMenuCatalog.isCustomizable(uri:)), forKey: key)
|
||||
defaults.set(Self.normalizedCommonURIs(uris), forKey: key)
|
||||
}
|
||||
|
||||
/// 根据权限生成默认常用 URI(最多 4 个)。
|
||||
static func defaultCommonURIs(from permissions: [HomePermissionItem]) -> [String] {
|
||||
let uris = permissions.map(\.uri).filter(HomeMenuCatalog.isCustomizable(uri:))
|
||||
let uris = normalizedCommonURIs(permissions.map(\.uri))
|
||||
return uris.count > 4 ? Array(uris.prefix(4)) : uris
|
||||
}
|
||||
|
||||
@@ -55,13 +55,17 @@ final class HomeCommonMenuStore {
|
||||
from allMenus: [HomeMenuItem],
|
||||
commonURIs: [String]
|
||||
) -> (common: [HomeMenuItem], more: [HomeMenuItem]) {
|
||||
let commonSet = Set(commonURIs)
|
||||
let commonSet = Set(normalizedCommonURIs(commonURIs))
|
||||
let customizableMenus = allMenus.filter { HomeMenuCatalog.isCustomizable(uri: $0.uri) }
|
||||
let common = customizableMenus.filter { commonSet.contains($0.uri) }
|
||||
let more = customizableMenus.filter { !commonSet.contains($0.uri) }
|
||||
return (common, more)
|
||||
}
|
||||
|
||||
private static func normalizedCommonURIs(_ uris: [String]) -> [String] {
|
||||
HomeMenuCatalog.canonicalURIs(uris).filter(HomeMenuCatalog.isCustomizable(uri:))
|
||||
}
|
||||
|
||||
private func storageKey(accountScope: String, roleCode: String) -> String? {
|
||||
let normalizedScope = accountScope.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
let normalizedRoleCode = roleCode.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
|
||||
@@ -45,11 +45,16 @@ enum HomeMenuCatalog {
|
||||
HomeMenuItem(uri: "cooperation_order", title: "合作订单", iconName: "person.2"),
|
||||
]
|
||||
|
||||
/// 将服务端权限映射为可展示菜单,保持服务端顺序。
|
||||
/// 将服务端权限映射为可展示菜单;培训两种 URI 只保留首次出现的位置。
|
||||
static func visibleMenus(from permissions: [HomePermissionItem]) -> [HomeMenuItem] {
|
||||
permissions.compactMap { permission in
|
||||
var hasTraining = false
|
||||
return permissions.compactMap { permission in
|
||||
guard isCustomizable(uri: permission.uri) else { return nil }
|
||||
guard let catalog = allItems.first(where: { $0.uri == permission.uri }) else { return nil }
|
||||
guard let catalog = item(for: permission.uri) else { return nil }
|
||||
if catalog.uri == "/training" {
|
||||
guard !hasTraining else { return nil }
|
||||
hasTraining = true
|
||||
}
|
||||
return HomeMenuItem(
|
||||
uri: catalog.uri,
|
||||
title: catalog.title,
|
||||
@@ -61,7 +66,25 @@ enum HomeMenuCatalog {
|
||||
|
||||
/// 查找 catalog 项。
|
||||
static func item(for uri: String) -> HomeMenuItem? {
|
||||
allItems.first { $0.uri == uri }
|
||||
allItems.first { $0.uri == canonicalURI(for: uri) }
|
||||
}
|
||||
|
||||
/// 仅将服务端 training 别名映射为内部 /training,不泛化其它 URI。
|
||||
static func canonicalURI(for uri: String) -> String {
|
||||
uri == "training" ? "/training" : uri
|
||||
}
|
||||
|
||||
/// 常用配置统一培训 URI 并保留首项,其它菜单的顺序和重复项保持原样。
|
||||
static func canonicalURIs(_ uris: [String]) -> [String] {
|
||||
var hasTraining = false
|
||||
return uris.compactMap { uri in
|
||||
let canonical = canonicalURI(for: uri)
|
||||
if canonical == "/training" {
|
||||
guard !hasTraining else { return nil }
|
||||
hasTraining = true
|
||||
}
|
||||
return canonical
|
||||
}
|
||||
}
|
||||
|
||||
/// 判断入口是否允许出现在首页常用应用及其自定义页面中。
|
||||
|
||||
@@ -16,6 +16,7 @@ enum HomeRouteHandler {
|
||||
storeItem: StoreItem?,
|
||||
onCommonMenusChanged: (() -> Void)? = nil
|
||||
) {
|
||||
let uri = HomeMenuCatalog.canonicalURI(for: uri)
|
||||
if uri == "pilot_controller" {
|
||||
showDeveloping(from: viewController)
|
||||
return
|
||||
|
||||
@@ -72,7 +72,7 @@ final class ProfileViewModel {
|
||||
|
||||
/// 我的培训入口使用当前账号最新菜单权限,业务访问资格由点击路由和接口校验。
|
||||
var showsTrainingEntry: Bool {
|
||||
AppStore.shared.permissions.permissionItems().contains { $0.uri == "/training" }
|
||||
AppStore.shared.permissions.permissionItems().contains { HomeMenuCatalog.canonicalURI(for: $0.uri) == "/training" }
|
||||
}
|
||||
|
||||
var currentScenicName: String {
|
||||
|
||||
Reference in New Issue
Block a user