feat: polish internal value displays

This commit is contained in:
JetSprow
2026-04-30 16:38:38 +10:00
parent abc2d4aa72
commit 042c5b34ab
25 changed files with 498 additions and 163 deletions

198
src/lib/audit-display.ts Normal file
View File

@@ -0,0 +1,198 @@
import {
booleanAppSettingLabels,
getBooleanAppSettingLabel,
getPaymentProviderLabel,
getTaskKindLabel,
getUserRoleLabel,
nodeStatusLabels,
orderStatusLabels,
paymentProviderLabels,
subscriptionStatusLabels,
subscriptionTypeLabels,
taskKindLabels,
userStatusLabels,
} from "@/lib/domain-labels";
export const auditActionFilterOptions = [
{ label: "全部动作", value: "" },
{ label: "用户操作", value: "user." },
{ label: "订单操作", value: "order." },
{ label: "订阅操作", value: "subscription." },
{ label: "套餐操作", value: "plan." },
{ label: "服务操作", value: "service." },
{ label: "节点操作", value: "node." },
{ label: "入站操作", value: "inbound." },
{ label: "任务操作", value: "task." },
{ label: "风控操作", value: "risk." },
{ label: "系统设置", value: "settings." },
{ label: "支付配置", value: "payment." },
{ label: "公告操作", value: "announcement." },
{ label: "工单操作", value: "support." },
{ label: "优惠规则", value: "coupon." },
{ label: "满减规则", value: "promotion." },
{ label: "备份恢复", value: "backup." },
{ label: "流量同步", value: "traffic." },
{ label: "流媒体槽位", value: "streaming-slot." },
];
const auditActionLabels: Record<string, string> = {
"announcement.create": "创建公告",
"announcement.update": "更新公告",
"announcement.enable": "启用公告",
"announcement.disable": "停用公告",
"announcement.delete": "删除公告",
"backup.restore": "恢复数据库",
"coupon.create": "创建优惠券",
"coupon.toggle": "切换优惠券状态",
"inbound.delete": "删除线路入口",
"inbound.display_name.update": "更新线路名称",
"node.create": "创建节点",
"node.update": "更新节点",
"node.delete": "删除节点",
"node.test": "同步节点入站",
"node.probe_token.generate": "生成探测 Token",
"node.probe_token.revoke": "撤销探测 Token",
"order.confirm": "确认订单",
"order.cancel": "取消订单",
"order.review": "更新订单审查",
"payment.config": "更新支付配置",
"plan.create": "创建套餐",
"plan.update": "更新套餐",
"plan.enable": "上架套餐",
"plan.disable": "下架套餐",
"plan.delete": "删除套餐",
"plan.batch_enable": "批量上架套餐",
"plan.batch_disable": "批量下架套餐",
"promotion.create": "创建满减规则",
"promotion.toggle": "切换满减规则状态",
"risk.node_access.suspend": "节点风控暂停订阅",
"risk.node_access.warning": "记录节点访问警告",
"risk.subscription.finalize": "完成风控处置",
"risk.subscription.report.generate": "生成风控报告",
"risk.subscription.report.send": "发送风控通知",
"risk.subscription.review": "更新订阅风控事件",
"risk.subscription.suspend": "风控暂停订阅",
"risk.subscription.warning": "记录订阅风险警告",
"service.create": "创建流媒体服务",
"service.update": "更新流媒体服务",
"service.delete": "删除流媒体服务",
"service.enable": "启用流媒体服务",
"service.disable": "停用流媒体服务",
"service.batch_enable": "批量启用流媒体服务",
"service.batch_disable": "批量停用流媒体服务",
"settings.toggle": "切换系统开关",
"settings.update": "更新系统设置",
"streaming-slot.reassign": "调配流媒体槽位",
"subscription.activate": "恢复订阅",
"subscription.auto_suspend": "自动暂停订阅",
"subscription.cancel": "取消订阅",
"subscription.create": "创建订阅",
"subscription.delete": "删除订阅",
"subscription.renew": "续费订阅",
"subscription.rotate_access": "重置订阅访问密钥",
"subscription.suspend": "暂停订阅",
"subscription.topup": "追加流量",
"support.close": "关闭工单",
"support.delete": "删除工单",
"support.reply": "回复工单",
"support.update": "更新工单",
"task.retry": "重试任务",
"task.run": "执行任务",
"traffic.sync": "同步流量视图",
"user.batch_status": "批量更新用户状态",
"user.create": "创建用户",
"user.force_delete": "强制删除用户",
"user.status": "更新用户状态",
"user.update": "更新用户",
};
const auditTargetTypeLabels: Record<string, string> = {
Announcement: "公告",
AppConfig: "系统设置",
Coupon: "优惠券",
Database: "数据库",
NodeInbound: "线路入口",
NodeServer: "节点",
Order: "订单",
PaymentConfig: "支付配置",
PromotionRule: "满减规则",
StreamingService: "流媒体服务",
StreamingSlot: "流媒体槽位",
SubscriptionPlan: "套餐",
SupportTicket: "工单",
TaskRun: "任务",
TrafficSync: "流量同步",
User: "用户",
UserSubscription: "订阅",
};
const tokenLabels: Record<string, string> = {
...booleanAppSettingLabels,
...nodeStatusLabels,
...orderStatusLabels,
...paymentProviderLabels,
...subscriptionStatusLabels,
...subscriptionTypeLabels,
...taskKindLabels,
...userStatusLabels,
};
function escapeRegExp(value: string) {
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
function replaceKnownTokens(value: string) {
return Object.entries(tokenLabels)
.sort(([left], [right]) => right.length - left.length)
.reduce((text, [token, label]) => {
const pattern = new RegExp(`\\b${escapeRegExp(token)}\\b`, "g");
return text.replace(pattern, label);
}, value);
}
export function formatAuditAction(action: string) {
const exact = auditActionLabels[action];
if (exact) return exact;
const prefix = auditActionFilterOptions.find((option) => option.value && action.startsWith(option.value));
return prefix?.label ?? "系统操作";
}
export function formatAuditTargetType(targetType: string | null | undefined) {
if (!targetType) return "系统";
return auditTargetTypeLabels[targetType] ?? "业务对象";
}
export function formatAuditTargetLabel({
targetType,
targetLabel,
targetId,
}: {
targetType: string | null | undefined;
targetLabel?: string | null;
targetId?: string | null;
}) {
if (targetLabel) {
if (targetType === "PaymentConfig") return getPaymentProviderLabel(targetLabel);
if (targetType === "TaskRun") return getTaskKindLabel(targetLabel);
return replaceKnownTokens(targetLabel);
}
if (targetType === "Database") return "数据库";
if (targetType === "TrafficSync") return "全站流量";
if (targetId) return `ID ${targetId.slice(0, 8)}`;
return "—";
}
export function formatAuditActorRole(role: string | null | undefined) {
if (!role) return "系统";
return getUserRoleLabel(role);
}
export function formatAuditMessage(message: string) {
const withSettingLabels = message.replace(
/系统开关\s+([A-Za-z][A-Za-z0-9_]*)/g,
(_match, field: string) => `${getBooleanAppSettingLabel(field)}开关`,
);
return replaceKnownTokens(withSettingLabels);
}

168
src/lib/domain-labels.ts Normal file
View File

@@ -0,0 +1,168 @@
import type {
AnnouncementAudience,
AnnouncementDisplayType,
OrderKind,
OrderReviewStatus,
OrderStatus,
Role,
SubscriptionStatus,
SubscriptionType,
TaskKind,
TaskStatus,
UserStatus,
} from "@prisma/client";
export const orderStatusLabels: Record<OrderStatus, string> = {
PENDING: "待确认",
PAID: "已支付",
CANCELLED: "已取消",
REFUNDED: "已退款",
};
export const orderKindLabels: Record<OrderKind, string> = {
NEW_PURCHASE: "新购",
RENEWAL: "续费",
TRAFFIC_TOPUP: "增流量",
};
export const orderReviewStatusLabels: Record<OrderReviewStatus, string> = {
NORMAL: "正常",
FLAGGED: "异常",
RESOLVED: "已解决",
};
export const subscriptionStatusLabels: Record<SubscriptionStatus, string> = {
ACTIVE: "活跃",
EXPIRED: "已过期",
CANCELLED: "已取消",
SUSPENDED: "已暂停",
};
export const subscriptionTypeLabels: Record<SubscriptionType, string> = {
PROXY: "代理",
STREAMING: "流媒体",
};
export const userRoleLabels: Record<Role, string> = {
ADMIN: "管理员",
USER: "用户",
};
export const userStatusLabels: Record<UserStatus, string> = {
ACTIVE: "正常",
PENDING_EMAIL: "待邮箱验证",
DISABLED: "禁用",
BANNED: "封禁",
};
export const taskKindLabels: Record<TaskKind, string> = {
REMINDER_DISPATCH: "提醒派发",
ORDER_PROVISION_RETRY: "订单重试",
};
export const taskStatusLabels: Record<TaskStatus, string> = {
PENDING: "待执行",
RUNNING: "运行中",
SUCCESS: "成功",
FAILED: "失败",
};
export const announcementAudienceLabels: Record<AnnouncementAudience, string> = {
PUBLIC: "公开",
USERS: "全部用户",
ADMINS: "全部管理员",
SPECIFIC_USER: "指定用户",
};
export const announcementDisplayTypeLabels: Record<AnnouncementDisplayType, string> = {
INLINE: "普通公告",
BIG: "大公告",
POPUP: "弹窗公告",
};
export const booleanAppSettingLabels = {
allowRegistration: "开放注册",
emailVerificationRequired: "注册邮箱验证",
requireInviteCode: "邀请码注册",
autoReminderDispatchEnabled: "自动提醒派发",
trafficSyncEnabled: "3x-ui 流量定时同步",
networkRecommendationsEnabled: "三网推荐",
networkInsightsEnabled: "线路体验",
subscriptionRiskEnabled: "订阅访问风控",
subscriptionRiskAutoSuspend: "风控自动暂停",
nodeAccessRiskEnabled: "节点日志风控",
inviteRewardEnabled: "自动发放奖励",
smtpEnabled: "邮件服务",
smtpSecure: "SMTP SSL 直连",
} as const;
export type BooleanAppSettingField = keyof typeof booleanAppSettingLabels;
export const booleanAppSettingFields = Object.keys(booleanAppSettingLabels) as [
BooleanAppSettingField,
...BooleanAppSettingField[],
];
export const nodeStatusLabels: Record<string, string> = {
active: "已启用",
inactive: "已停用",
disabled: "已停用",
error: "异常",
offline: "离线",
};
export const paymentProviderLabels: Record<string, string> = {
epay: "易支付",
alipay_f2f: "支付宝当面付",
usdt_trc20: "USDT (TRC20)",
};
export const paymentChannelLabels: Record<string, string> = {
alipay: "支付宝",
wxpay: "微信支付",
};
function labelFromMap(map: Partial<Record<string, string>>, value: string | null | undefined, fallback: string) {
if (!value) return fallback;
return map[value] ?? fallback;
}
export function getOrderStatusLabel(status: string | null | undefined) {
return labelFromMap(orderStatusLabels, status, "未知订单状态");
}
export function getOrderKindLabel(kind: string | null | undefined) {
return labelFromMap(orderKindLabels, kind, "未知订单类型");
}
export function getSubscriptionStatusLabel(status: string | null | undefined) {
return labelFromMap(subscriptionStatusLabels, status, "未知订阅状态");
}
export function getSubscriptionTypeLabel(type: string | null | undefined) {
return labelFromMap(subscriptionTypeLabels, type, "未知套餐类型");
}
export function getUserStatusLabel(status: string | null | undefined) {
return labelFromMap(userStatusLabels, status, "未知用户状态");
}
export function getUserRoleLabel(role: string | null | undefined) {
return labelFromMap(userRoleLabels, role, "未知角色");
}
export function getTaskKindLabel(kind: string | null | undefined) {
return labelFromMap(taskKindLabels, kind, "任务");
}
export function getNodeStatusLabel(status: string | null | undefined) {
return labelFromMap(nodeStatusLabels, status, "未知状态");
}
export function getPaymentProviderLabel(provider: string | null | undefined) {
return labelFromMap(paymentProviderLabels, provider, "支付方式");
}
export function getBooleanAppSettingLabel(field: string | null | undefined) {
return labelFromMap(booleanAppSettingLabels, field, "系统开关");
}