|
|
|
|
@ -1,49 +1,23 @@
|
|
|
|
|
const root = document.querySelector('[data-admin-billing]');
|
|
|
|
|
const byId = id => document.getElementById(id);
|
|
|
|
|
|
|
|
|
|
const FINANCIAL_OPTIONS = [
|
|
|
|
|
['', '全部财务状态'], ['open', '待支付'], ['overdue', '已逾期'],
|
|
|
|
|
['paid', '已支付'], ['refunded', '已退款'], ['expired', '已失效'], ['void', '已作废'],
|
|
|
|
|
];
|
|
|
|
|
const FULFILLMENT_OPTIONS = [
|
|
|
|
|
['', '全部履约状态'], ['awaiting_payment', '等待支付'], ['active', '执行中'],
|
|
|
|
|
['succeeded', '已成功'], ['failed', '已失败'], ['expired', '已失效'],
|
|
|
|
|
['refunded', '已退款'], ['cancelled', '已取消'],
|
|
|
|
|
];
|
|
|
|
|
const ORDER_OPTIONS = FULFILLMENT_OPTIONS;
|
|
|
|
|
const PAYMENT_OPTIONS = [
|
|
|
|
|
['', '全部支付状态'], ['creating', '创建中'], ['pending', '待支付'],
|
|
|
|
|
['paid_invoice', '已入账单'], ['credited_balance', '已入余额'], ['failed', '支付失败'],
|
|
|
|
|
['expired', '已失效'],
|
|
|
|
|
];
|
|
|
|
|
const PURPOSE_OPTIONS = [
|
|
|
|
|
['', '全部账单类型'], ['provision', '实例开通'], ['renewal', '实例续费'],
|
|
|
|
|
['upgrade', '实例升级'], ['ip_change', '更换公网 IP'],
|
|
|
|
|
['backup_addon', '自动备份开通'], ['recharge', '余额充值'],
|
|
|
|
|
];
|
|
|
|
|
const PROVIDER_OPTIONS = [
|
|
|
|
|
['', '全部支付渠道'], ['balance', '账户余额'], ['usdt', 'USDT-TRC20'],
|
|
|
|
|
['yipay_alipay', '易支付 · 支付宝'], ['yipay_wxpay', '易支付 · 微信'], ['paytaro', 'Paytaro'],
|
|
|
|
|
['stripe_card', 'Stripe · 银行卡'], ['stripe_alipay', 'Stripe · 支付宝'],
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const VIEWS = {
|
|
|
|
|
invoices: {
|
|
|
|
|
title: '全部账单', description: '按财务状态与履约状态核对每一笔账单。',
|
|
|
|
|
queryPlaceholder: '账单 / 订单 / 套餐 / 用户 ID',
|
|
|
|
|
endpoint: '/api/v1/admin/billing/invoices', caption: '管理员账单列表',
|
|
|
|
|
primary: { key: 'financial_status', label: '财务状态', options: FINANCIAL_OPTIONS },
|
|
|
|
|
secondary: { key: 'fulfillment_status', label: '履约状态', options: FULFILLMENT_OPTIONS },
|
|
|
|
|
category: { key: 'purpose', label: '账单类型', options: PURPOSE_OPTIONS },
|
|
|
|
|
reference: { key: 'payment_method', label: '支付渠道', options: PROVIDER_OPTIONS },
|
|
|
|
|
primary: { key: 'financial_status', label: '财务状态', optionsKey: 'financial' },
|
|
|
|
|
secondary: { key: 'fulfillment_status', label: '履约状态', optionsKey: 'fulfillment' },
|
|
|
|
|
category: { key: 'purpose', label: '账单类型', optionsKey: 'purpose' },
|
|
|
|
|
reference: { key: 'payment_method', label: '支付渠道', optionsKey: 'provider' },
|
|
|
|
|
columns: ['账单', '用户', '项目', '金额', '财务状态', '履约状态', '支付渠道', '时间', '详情'],
|
|
|
|
|
},
|
|
|
|
|
orders: {
|
|
|
|
|
title: '开通任务', description: '跟踪支付后的实例开通队列与最终结果。',
|
|
|
|
|
queryPlaceholder: '任务 / 实例 / 套餐 / 用户 ID',
|
|
|
|
|
endpoint: '/api/v1/admin/billing/orders', caption: '管理员实例开通任务列表',
|
|
|
|
|
primary: { key: 'status', label: '履约状态', options: ORDER_OPTIONS },
|
|
|
|
|
secondary: { key: 'financial_status', label: '财务状态', options: FINANCIAL_OPTIONS },
|
|
|
|
|
primary: { key: 'status', label: '履约状态', optionsKey: 'fulfillment' },
|
|
|
|
|
secondary: { key: 'financial_status', label: '财务状态', optionsKey: 'financial' },
|
|
|
|
|
category: null, reference: null,
|
|
|
|
|
columns: ['任务', '用户', '套餐 / 宿主机', '金额', '财务状态', '履约状态', '实例 / 后台任务', '更新 / 异常', '账单'],
|
|
|
|
|
},
|
|
|
|
|
@ -51,8 +25,8 @@ const VIEWS = {
|
|
|
|
|
title: '支付流水', description: '核对支付尝试、入账去向与所属账单的财务状态。',
|
|
|
|
|
queryPlaceholder: '流水 / 账单 / 用户 ID',
|
|
|
|
|
endpoint: '/api/v1/admin/billing/payments', caption: '管理员支付流水列表',
|
|
|
|
|
primary: { key: 'status', label: '支付状态', options: PAYMENT_OPTIONS }, secondary: null,
|
|
|
|
|
category: { key: 'provider', label: '支付渠道', options: PROVIDER_OPTIONS },
|
|
|
|
|
primary: { key: 'status', label: '支付状态', optionsKey: 'payment' }, secondary: null,
|
|
|
|
|
category: { key: 'provider', label: '支付渠道', optionsKey: 'provider' },
|
|
|
|
|
reference: { key: 'invoice_id', label: '账单 ID', placeholder: '全部账单' },
|
|
|
|
|
columns: ['支付流水', '用户', '支付渠道', '金额', '支付状态', '账单财务状态', '用途', '时间', '账单'],
|
|
|
|
|
},
|
|
|
|
|
@ -63,7 +37,7 @@ const state = {
|
|
|
|
|
pagination: { page: 1, per_page: 30, total: 0, total_pages: 1, has_previous: false, has_next: false },
|
|
|
|
|
request: null, detailRequest: null, requestNumber: 0, detailInvoiceId: '', detailClosing: false,
|
|
|
|
|
detailClosePromise: null, detailOpener: null,
|
|
|
|
|
loadedView: '', loadedFilters: {}, cache: new Map(), scroll: null,
|
|
|
|
|
loadedView: '', loadedFilters: {}, cache: new Map(), scroll: null, catalog: {},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function element(tag, className = '', text) {
|
|
|
|
|
@ -80,31 +54,12 @@ function firstText(...values) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function formatUsd(value) {
|
|
|
|
|
if (value === null || value === undefined || value === '') return '—';
|
|
|
|
|
const amount = Number(value);
|
|
|
|
|
if (!Number.isFinite(amount)) return '—';
|
|
|
|
|
const sign = amount < 0 ? '-$' : '$';
|
|
|
|
|
return sign + Math.abs(amount).toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function formatInteger(value) {
|
|
|
|
|
if (value === null || value === undefined || value === '') return '—';
|
|
|
|
|
const number = Number(value);
|
|
|
|
|
return Number.isFinite(number) ? Math.trunc(number).toLocaleString('zh-CN') : '—';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function formatDate(value) {
|
|
|
|
|
if (value === null || value === undefined || value === '') return '—';
|
|
|
|
|
const numeric = Number(value);
|
|
|
|
|
if (Number.isFinite(numeric) && numeric <= 0) return '—';
|
|
|
|
|
const timestamp = Number.isFinite(numeric)
|
|
|
|
|
? (numeric < 1e12 ? numeric * 1000 : numeric) : Date.parse(String(value));
|
|
|
|
|
const date = new Date(timestamp);
|
|
|
|
|
return Number.isFinite(date.getTime())
|
|
|
|
|
? date.toLocaleString('zh-CN', { hour12: false }) : '—';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function formatBytes(value) {
|
|
|
|
|
const bytes = Number(value);
|
|
|
|
|
if (!Number.isFinite(bytes) || bytes < 0) return '—';
|
|
|
|
|
@ -114,71 +69,14 @@ function formatBytes(value) {
|
|
|
|
|
return `${(bytes / (1024 ** index)).toLocaleString('zh-CN', { maximumFractionDigits: 2 })} ${units[index]}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function purposeLabel(value) {
|
|
|
|
|
return ({ provision: '实例开通', renewal: '实例续费', upgrade: '实例升级',
|
|
|
|
|
ip_change: '更换公网 IP', backup_addon: '自动备份开通',
|
|
|
|
|
recharge: '余额充值' })[String(value || '').toLowerCase()]
|
|
|
|
|
|| firstText(value, '未分类');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function providerLabel(value) {
|
|
|
|
|
const provider = String(value || '').toLowerCase();
|
|
|
|
|
return ({ balance: '账户余额', usdt: 'USDT-TRC20', yipay_alipay: '易支付 · 支付宝',
|
|
|
|
|
yipay_wxpay: '易支付 · 微信', paytaro: 'Paytaro',
|
|
|
|
|
stripe_card: 'Stripe · 银行卡', stripe_alipay: 'Stripe · 支付宝' })[provider] || firstText(value, '未记录');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function statusInfo(kind, value) {
|
|
|
|
|
const status = String(value || '').toLowerCase();
|
|
|
|
|
if (!status && kind === 'fulfillment') return ['不适用', 'neutral'];
|
|
|
|
|
const financial = {
|
|
|
|
|
open: ['待支付', 'warning'], overdue: ['已逾期', 'danger'], paid: ['已支付', 'success'],
|
|
|
|
|
refunded: ['已退款', 'info'], expired: ['已失效', 'neutral'], void: ['已作废', 'neutral'],
|
|
|
|
|
};
|
|
|
|
|
const fulfillment = {
|
|
|
|
|
awaiting_payment: ['等待支付', 'warning'], active: ['执行中', 'info'],
|
|
|
|
|
succeeded: ['已成功', 'success'], failed: ['已失败', 'danger'],
|
|
|
|
|
expired: ['已失效', 'neutral'], refunded: ['已退款', 'info'],
|
|
|
|
|
cancelled: ['已取消', 'neutral'],
|
|
|
|
|
};
|
|
|
|
|
const payment = {
|
|
|
|
|
creating: ['创建中', 'info'], pending: ['待支付', 'warning'], paid_invoice: ['已入账单', 'success'],
|
|
|
|
|
credited_balance: ['已入余额', 'success'], failed: ['支付失败', 'danger'], expired: ['已失效', 'neutral'],
|
|
|
|
|
};
|
|
|
|
|
const table = kind === 'financial' ? financial : kind === 'payment' ? payment : fulfillment;
|
|
|
|
|
return table[status] || [status || '未记录', 'neutral'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function statusBadge(kind, value, purpose = '') {
|
|
|
|
|
let [label, tone] = statusInfo(kind, value);
|
|
|
|
|
if (kind === 'fulfillment' && purpose === 'renewal' && value === 'succeeded') label = '已续费';
|
|
|
|
|
if (kind === 'fulfillment' && purpose === 'upgrade') {
|
|
|
|
|
label = ({ active: '升级中', succeeded: '已升级', failed: '升级失败',
|
|
|
|
|
cancelled: '已取消' })[value] || label;
|
|
|
|
|
}
|
|
|
|
|
const badgeTone = {
|
|
|
|
|
success: 'bg-success-subtle text-success-emphasis',
|
|
|
|
|
warning: 'bg-warning-subtle text-warning-emphasis',
|
|
|
|
|
danger: 'bg-danger-subtle text-danger-emphasis',
|
|
|
|
|
info: 'bg-info-subtle text-info-emphasis',
|
|
|
|
|
neutral: 'bg-light text-dark',
|
|
|
|
|
}[tone] || 'bg-light text-dark';
|
|
|
|
|
return element('span', `badge rounded-pill fs-12 px-2 py-1 ${badgeTone}`, label);
|
|
|
|
|
function statusBadge(presentation) {
|
|
|
|
|
return element('span', `badge rounded-pill fs-12 px-2 py-1 ${firstText(
|
|
|
|
|
presentation?.class, 'bg-light text-dark',
|
|
|
|
|
)}`, firstText(presentation?.label, '未记录'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function taskStatusBadge(task) {
|
|
|
|
|
let label = '任务状态协议无效';
|
|
|
|
|
let tone = 'danger';
|
|
|
|
|
if (task?.completed === false
|
|
|
|
|
&& (!Object.prototype.hasOwnProperty.call(task, 'status')
|
|
|
|
|
|| task.status === 'active')) {
|
|
|
|
|
label = '执行中'; tone = 'info';
|
|
|
|
|
} else if (task?.completed === true) {
|
|
|
|
|
const info = { succeeded: ['已成功', 'success'], failed: ['已失败', 'danger'] }[task.status];
|
|
|
|
|
if (info) [label, tone] = info;
|
|
|
|
|
}
|
|
|
|
|
const classes = { success: 'bg-success-subtle text-success-emphasis', danger: 'bg-danger-subtle text-danger-emphasis', info: 'bg-info-subtle text-info-emphasis', neutral: 'bg-light text-dark' };
|
|
|
|
|
return element('span', `badge rounded-pill fs-12 px-2 py-1 ${classes[tone]}`, label);
|
|
|
|
|
return statusBadge(task?.presentation?.status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function stack(primary, secondary = '') {
|
|
|
|
|
@ -204,33 +102,40 @@ function detailButton(invoiceId, label = '查看账单') {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function invoiceCells(item) {
|
|
|
|
|
const display = item.presentation || {};
|
|
|
|
|
return [
|
|
|
|
|
stack(`#${item.id}`, purposeLabel(item.purpose)), accountCell(item),
|
|
|
|
|
stack(`#${item.id}`, firstText(display.purpose_label, '未分类')), accountCell(item),
|
|
|
|
|
stack(firstText(item.subject_name, item.plan_name, '账单项目'), item.plan_id ? `套餐 #${item.plan_id}` : ''),
|
|
|
|
|
element('span', 'admin-mono', formatUsd(item.amount)), statusBadge('financial', item.financial_status),
|
|
|
|
|
statusBadge('fulfillment', item.fulfillment_status, item.purpose), providerLabel(item.payment_method),
|
|
|
|
|
stack(formatDate(item.created_at), item.due_at ? `到期 ${formatDate(item.due_at)}` : ''), detailButton(item.id, '账单详情'),
|
|
|
|
|
element('span', 'admin-mono', firstText(display.amount_display, '—')),
|
|
|
|
|
statusBadge(display.financial_status), statusBadge(display.fulfillment_status),
|
|
|
|
|
firstText(display.provider_label, '未记录'),
|
|
|
|
|
stack(firstText(display.created_display, '—'), item.due_at ? `到期 ${firstText(display.due_display, '—')}` : ''),
|
|
|
|
|
detailButton(item.id, '账单详情'),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function orderCells(item) {
|
|
|
|
|
const display = item.presentation || {};
|
|
|
|
|
const taskDetails = [item.task_id ? `任务 #${item.task_id}` : ''].filter(Boolean).join(' · ');
|
|
|
|
|
return [
|
|
|
|
|
stack(`#${item.id}`, taskDetails), accountCell(item),
|
|
|
|
|
stack(firstText(item.plan_name, item.plan_id ? `套餐 #${item.plan_id}` : '套餐未记录'), item.host_id ? `宿主机 #${item.host_id}` : ''),
|
|
|
|
|
element('span', 'admin-mono', formatUsd(item.amount)), statusBadge('financial', item.financial_status),
|
|
|
|
|
statusBadge('fulfillment', item.fulfillment_status),
|
|
|
|
|
element('span', 'admin-mono', firstText(display.amount_display, '—')),
|
|
|
|
|
statusBadge(display.financial_status), statusBadge(display.fulfillment_status),
|
|
|
|
|
stack(item.instance_id ? `实例 #${item.instance_id}` : '实例 —', item.task_id ? `任务 #${item.task_id}` : '后台任务 —'),
|
|
|
|
|
stack(formatDate(firstText(item.updated_at, item.created_at)), firstText(item.error)), detailButton(item.invoice_id),
|
|
|
|
|
stack(firstText(display.updated_display, '—'), firstText(item.error)), detailButton(item.invoice_id),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function paymentCells(item) {
|
|
|
|
|
const display = item.presentation || {};
|
|
|
|
|
return [
|
|
|
|
|
stack(`#${item.id}`, item.invoice_id ? `账单 #${item.invoice_id}` : '未关联账单'), accountCell(item),
|
|
|
|
|
providerLabel(item.provider), element('span', 'admin-mono', formatUsd(item.amount)),
|
|
|
|
|
statusBadge('payment', item.payment_status), statusBadge('financial', item.invoice_financial_status),
|
|
|
|
|
purposeLabel(item.purpose), stack(formatDate(item.created_at), item.updated_at ? `更新 ${formatDate(item.updated_at)}` : ''),
|
|
|
|
|
firstText(display.provider_label, '未记录'),
|
|
|
|
|
element('span', 'admin-mono', firstText(display.amount_display, '—')),
|
|
|
|
|
statusBadge(display.payment_status), statusBadge(display.invoice_financial_status),
|
|
|
|
|
firstText(display.purpose_label, '未分类'),
|
|
|
|
|
stack(firstText(display.created_display, '—'), item.updated_at ? `更新 ${firstText(display.updated_display, '—')}` : ''),
|
|
|
|
|
detailButton(item.invoice_id),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
@ -245,10 +150,11 @@ function setNotice(message, tone = '') {
|
|
|
|
|
|
|
|
|
|
function fillOptions(select, options, selected = '') {
|
|
|
|
|
select.replaceChildren();
|
|
|
|
|
for (const [value, label] of options) {
|
|
|
|
|
const option = element('option', '', label);
|
|
|
|
|
for (const entry of options) {
|
|
|
|
|
const value = firstText(entry?.value);
|
|
|
|
|
const option = element('option', '', firstText(entry?.label, value));
|
|
|
|
|
option.value = value;
|
|
|
|
|
option.selected = String(value) === String(selected || '');
|
|
|
|
|
option.selected = value === String(selected || '');
|
|
|
|
|
select.append(option);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@ -263,7 +169,9 @@ function configureFilter(kind, definition) {
|
|
|
|
|
control.name = definition?.key || '';
|
|
|
|
|
if (!definition) { control.value = ''; return; }
|
|
|
|
|
label.textContent = definition.label;
|
|
|
|
|
if (control instanceof HTMLSelectElement) fillOptions(control, definition.options);
|
|
|
|
|
if (control instanceof HTMLSelectElement) {
|
|
|
|
|
fillOptions(control, state.catalog[definition.optionsKey] || []);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function configureReference(definition) {
|
|
|
|
|
@ -281,8 +189,8 @@ function configureReference(definition) {
|
|
|
|
|
input.value = '';
|
|
|
|
|
if (!definition) return;
|
|
|
|
|
byId('billing-reference-label').textContent = definition.label;
|
|
|
|
|
if (Array.isArray(definition.options)) {
|
|
|
|
|
fillOptions(select, definition.options);
|
|
|
|
|
if (definition.optionsKey) {
|
|
|
|
|
fillOptions(select, state.catalog[definition.optionsKey] || []);
|
|
|
|
|
select.hidden = false;
|
|
|
|
|
select.disabled = false;
|
|
|
|
|
select.dataset.queryKey = definition.key;
|
|
|
|
|
@ -405,17 +313,18 @@ function resetSummary(busy) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function renderSummary(summary = {}) {
|
|
|
|
|
const display = summary.presentation || {};
|
|
|
|
|
const total = Number(summary.total_records);
|
|
|
|
|
byId('billing-summary-total').textContent = Number.isFinite(total) ? formatInteger(total) : formatInteger(state.pagination.total);
|
|
|
|
|
byId('billing-summary-amount-label').textContent = state.view === 'payments'
|
|
|
|
|
? '支付尝试金额' : state.view === 'orders' ? '任务关联金额' : '账单面额';
|
|
|
|
|
byId('billing-summary-amount').textContent = formatUsd(summary.total_amount);
|
|
|
|
|
byId('billing-summary-amount').textContent = firstText(display.total_amount_display, '—');
|
|
|
|
|
if (state.view === 'invoices') {
|
|
|
|
|
byId('billing-summary-focus-label').textContent = '待收款';
|
|
|
|
|
byId('billing-summary-focus').textContent = `${formatInteger(summary.outstanding_count)} 笔 · ${formatUsd(summary.outstanding_amount)}`;
|
|
|
|
|
byId('billing-summary-focus').textContent = `${formatInteger(summary.outstanding_count)} 笔 · ${firstText(display.outstanding_amount_display, '—')}`;
|
|
|
|
|
byId('billing-summary-result-label').textContent = '已支付';
|
|
|
|
|
byId('billing-summary-result').textContent = `${formatInteger(summary.paid_count)} 笔 · ${formatUsd(summary.paid_amount)}`;
|
|
|
|
|
byId('billing-summary-refunded').textContent = `${formatInteger(summary.refunded_count)} 笔 · ${formatUsd(summary.refunded_amount)}`;
|
|
|
|
|
byId('billing-summary-result').textContent = `${formatInteger(summary.paid_count)} 笔 · ${firstText(display.paid_amount_display, '—')}`;
|
|
|
|
|
byId('billing-summary-refunded').textContent = `${formatInteger(summary.refunded_count)} 笔 · ${firstText(display.refunded_amount_display, '—')}`;
|
|
|
|
|
} else if (state.view === 'orders') {
|
|
|
|
|
const counts = summary.status_counts || {};
|
|
|
|
|
byId('billing-summary-focus-label').textContent = '处理中';
|
|
|
|
|
@ -427,7 +336,7 @@ function renderSummary(summary = {}) {
|
|
|
|
|
byId('billing-summary-focus-label').textContent = '处理中';
|
|
|
|
|
byId('billing-summary-focus').textContent = `${formatInteger(countStatuses(counts, ['creating', 'pending']))} 笔`;
|
|
|
|
|
byId('billing-summary-result-label').textContent = '入账去向';
|
|
|
|
|
byId('billing-summary-result').textContent = `账单 ${formatUsd(summary.paid_invoice_amount)} · 余额 ${formatUsd(summary.credited_balance_amount)}`;
|
|
|
|
|
byId('billing-summary-result').textContent = `账单 ${firstText(display.paid_invoice_amount_display, '—')} · 余额 ${firstText(display.credited_balance_amount_display, '—')}`;
|
|
|
|
|
}
|
|
|
|
|
byId('billing-summary').setAttribute('aria-busy', 'false');
|
|
|
|
|
byId('billing-refund-card').hidden = state.view !== 'invoices';
|
|
|
|
|
@ -562,7 +471,9 @@ function shownFilters(filters) {
|
|
|
|
|
if (filters.user_id) parts.push(`用户 #${filters.user_id}`);
|
|
|
|
|
for (const definition of [VIEWS[state.view].primary, VIEWS[state.view].secondary, VIEWS[state.view].category, VIEWS[state.view].reference]) {
|
|
|
|
|
const value = definition && filters[definition.key];
|
|
|
|
|
if (value) parts.push(`${definition.label}:${definition.options?.find(option => option[0] === value)?.[1] || value}`);
|
|
|
|
|
const options = definition?.optionsKey
|
|
|
|
|
? state.catalog[definition.optionsKey] || [] : [];
|
|
|
|
|
if (value) parts.push(`${definition.label}:${options.find(option => String(option?.value) === value)?.label || value}`);
|
|
|
|
|
}
|
|
|
|
|
if (filters.created_from || filters.created_to) parts.push(`日期 ${unixToDate(filters.created_from) || '不限'} 至 ${unixToDate(filters.created_to) || '不限'}`);
|
|
|
|
|
return parts.join(' · ') || '全部记录';
|
|
|
|
|
@ -570,6 +481,10 @@ function shownFilters(filters) {
|
|
|
|
|
|
|
|
|
|
function applyListPayload(payload, filters, replaceRows = true) {
|
|
|
|
|
if (!Array.isArray(payload?.items) || !payload?.pagination || !payload?.summary) throw new Error('账单数据格式异常,请重试。');
|
|
|
|
|
if (!payload?.presentation?.catalog || typeof payload.presentation.catalog !== 'object') {
|
|
|
|
|
throw new Error('账单展示目录缺失,请重试。');
|
|
|
|
|
}
|
|
|
|
|
state.catalog = payload.presentation.catalog;
|
|
|
|
|
state.items = payload.items;
|
|
|
|
|
state.pagination = normalizePagination(payload.pagination);
|
|
|
|
|
state.page = state.pagination.page;
|
|
|
|
|
@ -577,6 +492,10 @@ function applyListPayload(payload, filters, replaceRows = true) {
|
|
|
|
|
state.loadedView = state.view;
|
|
|
|
|
state.loadedFilters = { ...filters };
|
|
|
|
|
state.cache.set(state.view, { ...payload, filters: { ...filters }, view: state.view });
|
|
|
|
|
configureView();
|
|
|
|
|
loadFiltersFromUrl(cachedParameters({
|
|
|
|
|
filters, pagination: state.pagination,
|
|
|
|
|
}));
|
|
|
|
|
if (replaceRows) renderRows();
|
|
|
|
|
renderSummary(payload.summary);
|
|
|
|
|
renderPagination();
|
|
|
|
|
@ -693,17 +612,22 @@ function safeLink(url, label) {
|
|
|
|
|
function renderDetail(payload) {
|
|
|
|
|
const invoice = payload?.invoice;
|
|
|
|
|
if (!invoice || String(invoice.id) !== state.detailInvoiceId) throw new Error('账单详情与请求不一致,请重试。');
|
|
|
|
|
const invoiceDisplay = invoice.presentation || {};
|
|
|
|
|
detailTabs.forEach(tab => byId('billing-panel-' + tab).replaceChildren());
|
|
|
|
|
byId('billing-detail-title').textContent = '账单 #' + invoice.id;
|
|
|
|
|
const overview = byId('billing-panel-overview');
|
|
|
|
|
overview.append(detailSection('账单概览', [
|
|
|
|
|
['用户', firstText(invoice.user_name, invoice.user_email, invoice.user_id ? '用户 #' + invoice.user_id : '')],
|
|
|
|
|
['账单类型', purposeLabel(invoice.purpose)], ['项目', firstText(invoice.subject_name, invoice.plan_name)],
|
|
|
|
|
['金额', formatUsd(invoice.amount)], ['财务状态', statusBadge('financial', invoice.financial_status)],
|
|
|
|
|
['履约状态', statusBadge('fulfillment', invoice.fulfillment_status, invoice.purpose)],
|
|
|
|
|
['支付渠道', providerLabel(invoice.payment_method)], ['创建时间', formatDate(invoice.created_at)],
|
|
|
|
|
['账单到期', formatDate(invoice.due_at)], ['支付截止', formatDate(invoice.payment_deadline_at)],
|
|
|
|
|
['支付时间', formatDate(invoice.paid_at)],
|
|
|
|
|
['账单类型', firstText(invoiceDisplay.purpose_label, '未分类')],
|
|
|
|
|
['项目', firstText(invoice.subject_name, invoice.plan_name)],
|
|
|
|
|
['金额', firstText(invoiceDisplay.amount_display, '—')],
|
|
|
|
|
['财务状态', statusBadge(invoiceDisplay.financial_status)],
|
|
|
|
|
['履约状态', statusBadge(invoiceDisplay.fulfillment_status)],
|
|
|
|
|
['支付渠道', firstText(invoiceDisplay.provider_label, '未记录')],
|
|
|
|
|
['创建时间', firstText(invoiceDisplay.created_display, '—')],
|
|
|
|
|
['账单到期', firstText(invoiceDisplay.due_display, '—')],
|
|
|
|
|
['支付截止', firstText(invoiceDisplay.payment_deadline_display, '—')],
|
|
|
|
|
['支付时间', firstText(invoiceDisplay.paid_display, '—')],
|
|
|
|
|
]));
|
|
|
|
|
const specification = invoice.specification || {};
|
|
|
|
|
if (invoice.purpose !== 'recharge') overview.append(detailSection('套餐与资源快照', [
|
|
|
|
|
@ -714,19 +638,21 @@ function renderDetail(payload) {
|
|
|
|
|
['带宽', specification.bandwidth_mbps != null ? specification.bandwidth_mbps + ' Mbps' : ''],
|
|
|
|
|
['有效期', invoice.validity_days != null ? invoice.validity_days + ' 天' : ''],
|
|
|
|
|
['流量额度', invoice.traffic_quota_bytes != null ? (Number(invoice.traffic_quota_bytes) === 0 ? '不限流量' : formatBytes(invoice.traffic_quota_bytes)) : ''],
|
|
|
|
|
['续费后到期', invoice.purpose === 'renewal' ? formatDate(invoice.renews_expires_at) : ''],
|
|
|
|
|
['续费后到期', invoice.purpose === 'renewal'
|
|
|
|
|
? firstText(invoiceDisplay.renews_expires_display, '—') : ''],
|
|
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
const funds = payload.funds || {};
|
|
|
|
|
const fundsDisplay = funds.presentation || {};
|
|
|
|
|
const fundPanel = byId('billing-panel-funds');
|
|
|
|
|
fundPanel.append(element('h3', 'h6', firstText(funds.title, '资金记录')),
|
|
|
|
|
element('p', 'text-muted', firstText(funds.description, '以已确认的支付和退款记录为准。')),
|
|
|
|
|
definitionList([
|
|
|
|
|
['确认支付金额', formatUsd(funds.settled_amount)],
|
|
|
|
|
['已退款金额', funds.refund_amount != null ? formatUsd(funds.refund_amount) : '无已确认退款记录'],
|
|
|
|
|
['确认支付金额', firstText(fundsDisplay.settled_amount_display, '—')],
|
|
|
|
|
['已退款金额', firstText(fundsDisplay.refund_amount_display, '无已确认退款记录')],
|
|
|
|
|
['退款去向', funds.refund_destination === 'balance' ? '账户余额' : '—'],
|
|
|
|
|
['退款时间', formatDate(funds.refunded_at)],
|
|
|
|
|
['另行转入余额', formatUsd(funds.balance_retained_amount)],
|
|
|
|
|
['退款时间', firstText(fundsDisplay.refunded_at_display, '—')],
|
|
|
|
|
['另行转入余额', firstText(fundsDisplay.balance_retained_amount_display, '—')],
|
|
|
|
|
['转入余额笔数', formatInteger(funds.balance_retained_count)],
|
|
|
|
|
]));
|
|
|
|
|
for (const note of funds.notes || []) fundPanel.append(element('p', 'text-muted fs-13 mt-3', note));
|
|
|
|
|
@ -737,7 +663,7 @@ function renderDetail(payload) {
|
|
|
|
|
for (const entry of payload.timeline) {
|
|
|
|
|
const item = element('li');
|
|
|
|
|
item.append(element('strong', 'd-block', firstText(entry.title, entry.kind)),
|
|
|
|
|
element('span', 'text-muted fs-12 d-block', entry.at ? formatDate(entry.at) : '时间未记录'));
|
|
|
|
|
element('span', 'text-muted fs-12 d-block', firstText(entry.at_display, '时间未记录')));
|
|
|
|
|
if (entry.description) item.append(element('p', 'fs-13 mb-0', entry.description));
|
|
|
|
|
timeline.append(item);
|
|
|
|
|
}
|
|
|
|
|
@ -753,17 +679,18 @@ function renderDetail(payload) {
|
|
|
|
|
if (invoice.purpose === 'upgrade') fulfillment.append(element('p', 'alert alert-light', '升级账单在扣款时已创建后台任务,不可再次支付;请在此核对目标实例和最终任务状态。'));
|
|
|
|
|
if (order && invoice.purpose === 'provision') fulfillment.append(detailSection('开通任务', [
|
|
|
|
|
['任务 ID', order.id ? '#' + order.id : ''], ['套餐', firstText(order.plan_name)],
|
|
|
|
|
['财务状态', statusBadge('financial', firstText(order.financial_status, invoice.financial_status))],
|
|
|
|
|
['履约状态', statusBadge('fulfillment', order.fulfillment_status)],
|
|
|
|
|
['财务状态', statusBadge(order.presentation?.financial_status)],
|
|
|
|
|
['履约状态', statusBadge(order.presentation?.fulfillment_status)],
|
|
|
|
|
['实例 ID', order.instance_id ? '#' + order.instance_id : ''], ['后台任务', order.task_id ? '#' + order.task_id : ''],
|
|
|
|
|
['更新时间', formatDate(order.updated_at)], ['异常摘要', firstText(order.error)],
|
|
|
|
|
['更新时间', firstText(order.presentation?.updated_display, '—')],
|
|
|
|
|
['异常摘要', firstText(order.error)],
|
|
|
|
|
]));
|
|
|
|
|
const task = payload.task;
|
|
|
|
|
const task = payload.job;
|
|
|
|
|
if (task && ['provision', 'upgrade'].includes(invoice.purpose)) fulfillment.append(detailSection(
|
|
|
|
|
invoice.purpose === 'upgrade' ? '升级任务' : '后台任务', [
|
|
|
|
|
['任务 ID', task.id ? '#' + task.id : ''], ['类型', firstText(task.kind_label || task.action_label, task.kind)],
|
|
|
|
|
['状态', taskStatusBadge(task)], ['异常摘要', firstText(task.error)],
|
|
|
|
|
['更新时间', formatDate(task.updated_at)],
|
|
|
|
|
['更新时间', firstText(task.presentation?.updated_display, '—')],
|
|
|
|
|
]));
|
|
|
|
|
const instance = payload.instance;
|
|
|
|
|
if (instance) fulfillment.append(detailSection('关联实例', [
|
|
|
|
|
@ -771,7 +698,7 @@ function renderDetail(payload) {
|
|
|
|
|
['实例生命周期', ({ active: '在管', deleted: '已删除', deleting: '删除中', delete_failed: '删除失败' })[instance.status] || firstText(instance.status)],
|
|
|
|
|
['服务状态', ({ active: '服务正常', exempt: '免计费', expired: '已到期', expired_grace: '到期宽限期', resume_pending: '等待恢复', resume_failed: '恢复失败', purge_force_required: '等待确认强制删除', purge_deleting: '到期清理中', purge_failed: '到期清理失败' })[instance.service_status] || firstText(instance.service_status)],
|
|
|
|
|
['网络状态', ({ active: '正常', blocked_traffic: '流量耗尽已断网', blocked_expired: '到期已断网', blocking: '正在限制网络', blocking_expired: '到期断网中', block_failed: '限制网络失败', restore_pending: '等待恢复', restoring: '恢复中', restore_failed: '恢复失败' })[instance.network_status] || firstText(instance.network_status)],
|
|
|
|
|
['到期时间', formatDate(instance.expires_at)],
|
|
|
|
|
['到期时间', firstText(instance.presentation?.expires_display, '—')],
|
|
|
|
|
]));
|
|
|
|
|
if (!instance && !order && !task) fulfillment.append(element('p', 'text-muted', '当前无可用关联实例或后台任务,历史账单仍保留。'));
|
|
|
|
|
}
|
|
|
|
|
@ -782,11 +709,16 @@ function renderDetail(payload) {
|
|
|
|
|
if (payload.payment_attempts_truncated) payments.append(element('p', 'text-muted fs-13', '仅显示最近 100 条;可通过底部“全部支付记录”查看本账单完整记录。'));
|
|
|
|
|
if (!attempts.length) payments.append(element('p', 'text-muted', '暂无在线支付尝试记录。余额支付请以资金记录为准。'));
|
|
|
|
|
for (const attempt of attempts) {
|
|
|
|
|
const attemptDisplay = attempt.presentation || {};
|
|
|
|
|
const card = element('article', 'card mb-3');
|
|
|
|
|
const body = element('div', 'card-body');
|
|
|
|
|
body.append(element('strong', 'd-block mb-2', providerLabel(attempt.provider || attempt.method) + ' · ' + formatUsd(attempt.amount)),
|
|
|
|
|
statusBadge('payment', firstText(attempt.payment_status, attempt.status)),
|
|
|
|
|
element('span', 'd-block text-muted fs-13 mt-2', '创建 ' + formatDate(attempt.created_at) + (attempt.updated_at ? ' · 更新 ' + formatDate(attempt.updated_at) : '')));
|
|
|
|
|
body.append(element('strong', 'd-block mb-2', firstText(
|
|
|
|
|
attemptDisplay.provider_label, '未记录',
|
|
|
|
|
) + ' · ' + firstText(attemptDisplay.amount_display, '—')),
|
|
|
|
|
statusBadge(attemptDisplay.payment_status),
|
|
|
|
|
element('span', 'd-block text-muted fs-13 mt-2', `创建 ${firstText(
|
|
|
|
|
attemptDisplay.created_display, '—',
|
|
|
|
|
)}${attempt.updated_at ? ` · 更新 ${firstText(attemptDisplay.updated_display, '—')}` : ''}`));
|
|
|
|
|
card.append(body); payments.append(card);
|
|
|
|
|
}
|
|
|
|
|
const links = byId('billing-detail-links');
|
|
|
|
|
@ -1042,12 +974,19 @@ function initialize() {
|
|
|
|
|
if (!root) return;
|
|
|
|
|
const params = new URLSearchParams(location.search);
|
|
|
|
|
state.view = VIEWS[params.get('view')] ? params.get('view') : 'invoices';
|
|
|
|
|
let payload = null;
|
|
|
|
|
try {
|
|
|
|
|
payload = JSON.parse(byId('admin-billing-bootstrap').textContent);
|
|
|
|
|
if (payload?.presentation?.catalog
|
|
|
|
|
&& typeof payload.presentation.catalog === 'object') {
|
|
|
|
|
state.catalog = payload.presentation.catalog;
|
|
|
|
|
}
|
|
|
|
|
} catch { /* The normal refresh path handles an invalid bootstrap. */ }
|
|
|
|
|
configureView();
|
|
|
|
|
loadFiltersFromUrl();
|
|
|
|
|
bindEvents();
|
|
|
|
|
let ready = false;
|
|
|
|
|
try {
|
|
|
|
|
const payload = JSON.parse(byId('admin-billing-bootstrap').textContent);
|
|
|
|
|
if (payload?.view === state.view) {
|
|
|
|
|
applyListPayload(payload, payload.filters || activeFilters(), false);
|
|
|
|
|
ready = true;
|
|
|
|
|
|