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

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

77 lines
2.9 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.

//
// RolePermissionMatching.swift
// suixinkan
//
// Created by Codex on 2026/6/29.
//
import Foundation
/// role_code legacy id
enum RolePermissionMatching {
/// App session
static func resolveRoleCode(
roleCode: String?,
roleId: Int?,
roleName: String?,
permissions: [RolePermissionResponse] = []
) -> AppRoleCode? {
if let code = AppRoleCode.fromCode(roleCode) { return code }
if let legacyId = roleId, legacyId > 0, let code = AppRoleCode.fromLegacyRoleId(legacyId) {
return code
}
if let code = AppRoleCode.fromRoleName(roleName) { return code }
return inferFromPermissionList(roleId: roleId, roleName: roleName, permissions: permissions)
}
/// role_code
static func match(
in permissions: [RolePermissionResponse],
roleCode: String?,
roleId: Int?,
roleName: String?
) -> RolePermissionResponse? {
if let code = AppRoleCode.fromCode(roleCode) {
if let matched = permissions.first(where: { AppRoleCode.fromCode($0.role.roleCode) == code }) {
return matched
}
}
if let roleId, roleId > 0,
let matched = permissions.first(where: { $0.role.id == roleId }) {
return matched
}
let trimmedName = roleName?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
if !trimmedName.isEmpty {
if let matched = permissions.first(where: { item in
let name = item.role.name.trimmingCharacters(in: .whitespacesAndNewlines)
return !name.isEmpty && (name.contains(trimmedName) || trimmedName.contains(name))
}) {
return matched
}
}
return nil
}
/// code
private static func inferFromPermissionList(
roleId: Int?,
roleName: String?,
permissions: [RolePermissionResponse]
) -> AppRoleCode? {
guard !permissions.isEmpty else { return nil }
if let roleId, roleId > 0,
let item = permissions.first(where: { $0.role.id == roleId }) {
return AppRoleCode.fromCode(item.role.roleCode) ?? AppRoleCode.fromRoleName(item.role.name)
}
let trimmedName = roleName?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
if !trimmedName.isEmpty,
let item = permissions.first(where: { item in
let name = item.role.name.trimmingCharacters(in: .whitespacesAndNewlines)
return !name.isEmpty && (name.contains(trimmedName) || trimmedName.contains(name))
}) {
return AppRoleCode.fromCode(item.role.roleCode) ?? AppRoleCode.fromRoleName(item.role.name)
}
return nil
}
}