66 lines
2.1 KiB
TypeScript
66 lines
2.1 KiB
TypeScript
import { useUserStore } from '@/store/modules/user'
|
||
|
||
export const CREATOR_DELETE_TIP = '仅创建人可删除'
|
||
|
||
/** 当前登录用户 ID(UUID 字符串) */
|
||
export function getCurrentUserId(): string {
|
||
const info = useUserStore().info as Record<string, unknown>
|
||
const id = info?.id ?? info?.userId ?? info?.user_id
|
||
if (id === undefined || id === null || id === '') return ''
|
||
return String(id)
|
||
}
|
||
|
||
/** 是否 super_admin */
|
||
export function isSuperAdmin(): boolean {
|
||
const info = useUserStore().info as Record<string, unknown>
|
||
const roles = (info?.roles as string[] | undefined) ?? []
|
||
const role = (info?.role as string | undefined) ?? ''
|
||
return role === 'super_admin' || roles.includes('super_admin')
|
||
}
|
||
|
||
export function rowCreatorId(row: Record<string, unknown> | null | undefined): string {
|
||
if (!row) return ''
|
||
const creator =
|
||
row.createBy ??
|
||
row.create_by ??
|
||
row.applicantId ??
|
||
row.applicant_id
|
||
if (creator === undefined || creator === null || creator === '') return ''
|
||
return String(creator)
|
||
}
|
||
|
||
/** 是否允许删除(仅创建人或 super_admin) */
|
||
export function canDeleteByCreator(
|
||
row: Record<string, unknown> | null | undefined,
|
||
currentUserId?: string
|
||
): boolean {
|
||
if (!row) return false
|
||
if (isSuperAdmin()) return true
|
||
const uid = (currentUserId ?? getCurrentUserId()).trim()
|
||
const creator = rowCreatorId(row)
|
||
if (!creator) return false
|
||
if (uid && creator === uid) return true
|
||
const info = useUserStore().info as Record<string, unknown>
|
||
const names = [info?.realName, info?.real_name, info?.userName, info?.username, info?.nickName]
|
||
.filter(Boolean)
|
||
.map(String)
|
||
return names.includes(creator)
|
||
}
|
||
|
||
/** 批量删除前过滤可删行 */
|
||
export function filterDeletableByCreator<T extends Record<string, unknown>>(
|
||
rows: T[],
|
||
currentUserId?: string
|
||
): T[] {
|
||
return rows.filter((row) => canDeleteByCreator(row, currentUserId))
|
||
}
|
||
|
||
/** 选中项是否全部可删 */
|
||
export function allRowsDeletableByCreator(
|
||
rows: Record<string, unknown>[],
|
||
currentUserId?: string
|
||
): boolean {
|
||
if (!rows.length) return false
|
||
return rows.every((row) => canDeleteByCreator(row, currentUserId))
|
||
}
|