Files
suixinkan_ios_new/suixinkan/App/State/PermissionContext.swift
汉秋 1970572edd 迁移合作订单完整能力,并统一 AppRoleCode 角色权限与首页菜单展示。
新增 CooperationOrder 模块(双 Tab 列表、获客员绑定、主 Tab 扫码)、订单来源/带客单绑定及配套 API 测试;同步引入 roleCode 权限匹配与相关单元测试。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-29 13:33:00 +08:00

155 lines
6.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.

//
// PermissionContext.swift
// suixinkan
//
// Created by Codex on 2026/6/22.
//
import Foundation
import Combine
@MainActor
/// URI
final class PermissionContext: ObservableObject {
@Published private(set) var rolePermissions: [RolePermissionResponse] = []
@Published private(set) var permissionURIs: Set<String> = []
@Published var currentRole: RoleInfo?
/// App role_code legacy id
var currentAppRole: AppRoleCode? {
if let code = AppRoleCode.fromCode(currentRole?.roleCode) { return code }
if let roleId = currentRole?.id, let code = AppRoleCode.fromLegacyRoleId(roleId) { return code }
return AppRoleCode.fromRoleName(currentRole?.name)
}
/// role_code
func replaceRolePermissions(
_ rolePermissions: [RolePermissionResponse],
currentRoleCode: String? = nil,
cachedLegacyRoleId: Int? = nil,
roleName: String? = nil
) {
self.rolePermissions = rolePermissions
let resolvedCode = RolePermissionMatching.resolveRoleCode(
roleCode: currentRoleCode,
roleId: cachedLegacyRoleId,
roleName: roleName,
permissions: rolePermissions
)?.rawValue ?? currentRoleCode
if let matched = RolePermissionMatching.match(
in: rolePermissions,
roleCode: resolvedCode,
roleId: cachedLegacyRoleId,
roleName: roleName ?? currentRole?.name
) {
currentRole = matched.role
} else if let currentRole,
let matched = rolePermissions.first(where: { $0.role.id == currentRole.id }) {
self.currentRole = matched.role
} else {
currentRole = rolePermissions.first?.role
}
permissionURIs = Set(Self.flattenPermissions(currentRole?.permission ?? []))
}
///
func selectRole(code: AppRoleCode, accountContext: AccountContext) {
guard let rolePermission = rolePermissions.first(where: {
AppRoleCode.fromCode($0.role.roleCode) == code
}) else { return }
applyRoleSelection(rolePermission, accountContext: accountContext)
}
/// legacy id使 API role id
func selectRole(id roleId: Int, accountContext: AccountContext) {
guard let rolePermission = rolePermissions.first(where: { $0.role.id == roleId }) else { return }
applyRoleSelection(rolePermission, accountContext: accountContext)
}
/// URI
func canAccess(_ uri: String) -> Bool {
permissionURIs.contains(uri.trimmingCharacters(in: .whitespacesAndNewlines))
}
/// URI API
func topLevelPermissionURIs(for roleCode: String?) -> [String] {
guard let roleCode,
let rolePermission = matchedRolePermission(roleCode: roleCode) else {
return []
}
return rolePermission.role.permission.compactMap { item in
let uri = item.uri.trimmingCharacters(in: .whitespacesAndNewlines)
return uri.isEmpty ? nil : uri
}
}
///
func currentRoleScenicScopes() -> [BusinessScope] {
guard let currentRole else { return [] }
guard let rolePermission = matchedRolePermission(roleCode: currentRole.roleCode)
?? rolePermissions.first(where: { $0.role.id == currentRole.id }) else {
return []
}
return Self.uniqueScenicScopes(from: rolePermission.scenic)
}
/// 退
func reset() {
rolePermissions = []
permissionURIs = []
currentRole = nil
}
///
private func applyRoleSelection(_ rolePermission: RolePermissionResponse, accountContext: AccountContext) {
currentRole = rolePermission.role
permissionURIs = Set(Self.flattenPermissions(rolePermission.role.permission))
let scenicScopes = Self.uniqueScenicScopes(from: rolePermission.scenic)
accountContext.replaceScopes(
scenic: scenicScopes,
stores: accountContext.storeScopes,
currentScenicId: accountContext.currentScenic?.id,
currentStoreId: accountContext.currentStore?.id
)
}
/// role_code
private func matchedRolePermission(roleCode: String) -> RolePermissionResponse? {
RolePermissionMatching.match(
in: rolePermissions,
roleCode: roleCode,
roleId: nil,
roleName: nil
)
}
/// URI
private static func flattenPermissions(_ permissions: [PermissionItem]) -> [String] {
permissions.flatMap { item -> [String] in
let current = item.uri.trimmingCharacters(in: .whitespacesAndNewlines)
let children = flattenPermissions(item.children)
return current.isEmpty ? children : [current] + children
}
}
///
private static func uniqueScenicScopes(from scenics: [ScenicInfo]) -> [BusinessScope] {
var seen = Set<Int>()
return scenics.compactMap { scenic in
guard seen.insert(scenic.id).inserted else { return nil }
return BusinessScope(
id: scenic.id,
name: scenic.name,
kind: .scenic,
status: scenic.status,
address: scenic.location?.address,
latitude: scenic.location?.lat,
longitude: scenic.location?.lng,
coverURLString: scenic.coverImg
)
}
}
}