Load role-permission on first visit to drive menus, role-based layout, store card, and location reporting with account-scoped persistence and unit tests. Co-authored-by: Cursor <cursoragent@cursor.com>
51 lines
1.7 KiB
Swift
51 lines
1.7 KiB
Swift
//
|
||
// HomeCommonMenuStore.swift
|
||
// suixinkan
|
||
//
|
||
|
||
import Foundation
|
||
|
||
/// 常用应用 URI 持久化,按账号与角色作用域存储。
|
||
final class HomeCommonMenuStore {
|
||
|
||
private let defaults: UserDefaults
|
||
|
||
init(defaults: UserDefaults = .standard) {
|
||
self.defaults = defaults
|
||
}
|
||
|
||
/// 读取已保存的常用 URI;无记录时返回空数组。
|
||
func savedCommonURIs(accountScope: String, roleCode: String) -> [String] {
|
||
defaults.stringArray(forKey: storageKey(accountScope: accountScope, roleCode: roleCode)) ?? []
|
||
}
|
||
|
||
/// 保存常用 URI 列表。
|
||
func saveCommonURIs(_ uris: [String], accountScope: String, roleCode: String) {
|
||
defaults.set(uris, forKey: storageKey(accountScope: accountScope, roleCode: roleCode))
|
||
}
|
||
|
||
/// 根据权限生成默认常用 URI(最多 4 个)。
|
||
static func defaultCommonURIs(from permissions: [HomePermissionItem]) -> [String] {
|
||
let uris = permissions.map(\.uri)
|
||
return uris.count > 4 ? Array(uris.prefix(4)) : uris
|
||
}
|
||
|
||
/// 构建常用菜单列表。
|
||
func commonMenus(
|
||
from allMenus: [HomeMenuItem],
|
||
permissions: [HomePermissionItem],
|
||
accountScope: String,
|
||
roleCode: String
|
||
) -> [HomeMenuItem] {
|
||
let saved = savedCommonURIs(accountScope: accountScope, roleCode: roleCode)
|
||
let defaultURIs = Self.defaultCommonURIs(from: permissions)
|
||
let commonURIs = saved.isEmpty ? defaultURIs : saved
|
||
let commonSet = Set(commonURIs)
|
||
return allMenus.filter { commonSet.contains($0.uri) }
|
||
}
|
||
|
||
private func storageKey(accountScope: String, roleCode: String) -> String {
|
||
"\(accountScope)_role_\(roleCode)_common_uris"
|
||
}
|
||
}
|