|
|
|
|
@ -86,124 +86,14 @@ function makeElement(tag, className = '', content) {
|
|
|
|
|
return element;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function firstText(...values) {
|
|
|
|
|
for (const value of values) {
|
|
|
|
|
if (value !== null && value !== undefined && String(value).trim() !== '') return String(value).trim();
|
|
|
|
|
}
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function formatUsd(value) {
|
|
|
|
|
const amount = Number(value);
|
|
|
|
|
if (!Number.isFinite(amount) || amount < 0) return '$—';
|
|
|
|
|
return `$${amount.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function formatDate(value) {
|
|
|
|
|
const raw = Number(value);
|
|
|
|
|
const timestamp = Number.isFinite(raw) && raw > 0
|
|
|
|
|
? (raw < 1e12 ? raw * 1000 : raw) : Date.parse(String(value || ''));
|
|
|
|
|
const date = new Date(timestamp);
|
|
|
|
|
return Number.isFinite(date.getTime())
|
|
|
|
|
? date.toLocaleString('zh-CN', { hour12: false }) : '时间未记录';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function invoicePurpose(invoice) {
|
|
|
|
|
const value = firstText(invoice?.purpose, invoice?.kind, invoice?.invoice_type, invoice?.type).toLowerCase();
|
|
|
|
|
if (['recharge', 'topup', 'top_up', 'balance', 'deposit'].includes(value)) return 'recharge';
|
|
|
|
|
if (['provision', 'provisioning', 'initial', 'create'].includes(value)) return 'provision';
|
|
|
|
|
if (['renewal', 'renew', 'subscription'].includes(value)) return 'renewal';
|
|
|
|
|
if (value === 'upgrade') return 'upgrade';
|
|
|
|
|
if (value === 'ip_change') return 'ip_change';
|
|
|
|
|
if (value === 'backup_addon') return 'backup_addon';
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function invoiceTitle(invoice) {
|
|
|
|
|
return firstText(invoice?.type_label, invoice?.title,
|
|
|
|
|
{ recharge: '余额充值', provision: '实例开通', renewal: '实例续费',
|
|
|
|
|
upgrade: '实例升级', ip_change: '更换公网 IP',
|
|
|
|
|
backup_addon: '自动备份开通' }[invoicePurpose(invoice)], '账单');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function invoiceContext(invoice) {
|
|
|
|
|
const purpose = invoicePurpose(invoice);
|
|
|
|
|
const status = String(invoice?.status || '').toLowerCase();
|
|
|
|
|
if (purpose === 'renewal') {
|
|
|
|
|
let value = `对应实例到期 ${formatDate(invoice?.renews_expires_at ?? invoice?.due_at)}`;
|
|
|
|
|
if (['open', 'overdue'].includes(status)) {
|
|
|
|
|
value += ` · 最晚支付 ${formatDate(invoice?.payment_deadline_at)}`;
|
|
|
|
|
}
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
if (purpose === 'provision') {
|
|
|
|
|
return `支付截止 ${formatDate(invoice?.payment_deadline_at ?? invoice?.due_at)}`;
|
|
|
|
|
}
|
|
|
|
|
if (['upgrade', 'ip_change'].includes(purpose)) {
|
|
|
|
|
return `目标实例 ${firstText(invoice?.instance_name,
|
|
|
|
|
invoice?.instance_id ? `#${invoice.instance_id}` : '', '实例')}`;
|
|
|
|
|
}
|
|
|
|
|
if (purpose === 'backup_addon') {
|
|
|
|
|
return `目标实例 ${firstText(invoice?.instance_name,
|
|
|
|
|
invoice?.instance_id ? `#${invoice.instance_id}` : '', '实例')}`;
|
|
|
|
|
}
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function invoiceStatusInfo(status) {
|
|
|
|
|
const values = {
|
|
|
|
|
open: ['待支付', 'warning'], overdue: ['已逾期', 'failed'], paid: ['已支付', 'running'],
|
|
|
|
|
renewal_grace: ['已到期,宽限期内可续费', 'warning'],
|
|
|
|
|
refunded: ['已退款', 'other'], void: ['已失效', 'other'], expired: ['已失效', 'failed'],
|
|
|
|
|
deleting: ['实例删除中', 'failed'], delete_failed: ['自动删除失败', 'failed'],
|
|
|
|
|
purge_pending: ['等待永久删除', 'failed'], purge_stopping: ['删除前正在关机', 'failed'],
|
|
|
|
|
purge_force_required: ['等待管理员确认强制删除', 'failed'],
|
|
|
|
|
purge_deleting: ['实例删除中', 'failed'], purge_stop_failed: ['删除前关机失败', 'failed'],
|
|
|
|
|
purge_failed: ['自动删除失败', 'failed'],
|
|
|
|
|
};
|
|
|
|
|
return values[String(status || '').toLowerCase()] || [String(status || '未知'), 'other'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function fulfillmentStatus(invoice) {
|
|
|
|
|
return firstText(invoice?.fulfillment_status).toLowerCase();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function fulfillmentStatusInfo(invoice) {
|
|
|
|
|
const purpose = invoicePurpose(invoice);
|
|
|
|
|
if (purpose === 'upgrade') {
|
|
|
|
|
const status = fulfillmentStatus(invoice);
|
|
|
|
|
const values = {
|
|
|
|
|
active: ['升级中', 'warning'], succeeded: ['已升级', 'running'],
|
|
|
|
|
failed: ['升级失败', 'failed'], cancelled: ['已取消', 'failed'],
|
|
|
|
|
};
|
|
|
|
|
return { status, display: values[status] || ['升级状态协议无效', 'failed'] };
|
|
|
|
|
}
|
|
|
|
|
if (purpose === 'ip_change') {
|
|
|
|
|
const status = fulfillmentStatus(invoice);
|
|
|
|
|
const values = {
|
|
|
|
|
active: ['更换中', 'warning'], succeeded: ['已更换', 'running'],
|
|
|
|
|
review: ['待人工核对', 'warning'],
|
|
|
|
|
failed: ['更换失败', 'failed'], cancelled: ['已取消', 'failed'],
|
|
|
|
|
};
|
|
|
|
|
return { status, display: values[status] || ['更换状态协议无效', 'failed'] };
|
|
|
|
|
}
|
|
|
|
|
const values = {
|
|
|
|
|
awaiting_payment: ['等待支付', 'warning'], active: ['创建中', 'warning'],
|
|
|
|
|
succeeded: ['已开通', 'running'],
|
|
|
|
|
failed: ['创建失败', 'failed'], refunded: ['开通失败,已退款', 'failed'],
|
|
|
|
|
expired: ['已失效', 'failed'], cancelled: ['已取消', 'failed'],
|
|
|
|
|
};
|
|
|
|
|
const status = fulfillmentStatus(invoice);
|
|
|
|
|
return { status, display: values[status] || ['履约状态协议无效', 'failed'] };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function invoiceNeedsPolling(invoice) {
|
|
|
|
|
const status = String(invoice?.status || '').toLowerCase();
|
|
|
|
|
if (['open', 'overdue'].includes(status)) return true;
|
|
|
|
|
if (invoicePurpose(invoice) === 'upgrade' && fulfillmentStatus(invoice) === 'active') return true;
|
|
|
|
|
if (invoicePurpose(invoice) === 'ip_change'
|
|
|
|
|
&& fulfillmentStatus(invoice) === 'active') return true;
|
|
|
|
|
return false;
|
|
|
|
|
return invoice?.polling_required === true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function renderInvoices() {
|
|
|
|
|
@ -218,27 +108,27 @@ function renderInvoices() {
|
|
|
|
|
for (const invoice of state.invoices) {
|
|
|
|
|
const row = makeElement('article', 'cloud-order cloud-invoice cloud-invoice-list-row');
|
|
|
|
|
const detail = makeElement('div', 'cloud-order-detail cloud-invoice-list-copy');
|
|
|
|
|
detail.append(makeElement('strong', '', invoiceTitle(invoice)),
|
|
|
|
|
makeElement('p', 'muted', `账单 #${invoice.id} · ${formatDate(invoice.created_at)}`));
|
|
|
|
|
const context = invoiceContext(invoice);
|
|
|
|
|
detail.append(makeElement('strong', '', String(invoice?.title || '账单')),
|
|
|
|
|
makeElement('p', 'muted', `账单 #${invoice.id} · ${String(
|
|
|
|
|
invoice?.created_display || '时间未记录',
|
|
|
|
|
)}`));
|
|
|
|
|
const context = String(invoice?.context_display || '').trim();
|
|
|
|
|
if (context) detail.append(makeElement('p', 'muted cloud-invoice-context', context));
|
|
|
|
|
const actions = makeElement('div', 'cloud-order-actions cloud-invoice-list-actions');
|
|
|
|
|
const invoiceStatus = String(invoice.status || '').toLowerCase();
|
|
|
|
|
const [label, className] = invoiceStatusInfo(
|
|
|
|
|
invoice.display_status ?? invoiceStatus,
|
|
|
|
|
);
|
|
|
|
|
actions.append(makeElement('span', `state-pill ${className}`, label));
|
|
|
|
|
const purpose = invoicePurpose(invoice);
|
|
|
|
|
if ((purpose === 'provision' && ['paid', 'refunded'].includes(invoiceStatus))
|
|
|
|
|
|| (['upgrade', 'ip_change'].includes(purpose) && fulfillmentStatus(invoice))) {
|
|
|
|
|
const { status, display } = fulfillmentStatusInfo(invoice);
|
|
|
|
|
if (status) actions.append(makeElement('span', `state-pill ${display[1]}`, display[0]));
|
|
|
|
|
actions.append(makeElement('span', `state-pill ${String(
|
|
|
|
|
invoice?.status_class || 'other',
|
|
|
|
|
)}`, String(invoice?.status_label || '未知')));
|
|
|
|
|
if (invoice?.show_fulfillment === true && String(invoice?.fulfillment_label || '').trim()) {
|
|
|
|
|
actions.append(makeElement('span', `state-pill ${String(
|
|
|
|
|
invoice?.fulfillment_class || 'other',
|
|
|
|
|
)}`, String(invoice.fulfillment_label)));
|
|
|
|
|
}
|
|
|
|
|
const link = makeElement('a', 'row-button', '账单详情');
|
|
|
|
|
link.href = `/user/cloud/invoices/${encodeURIComponent(String(invoice.id))}`;
|
|
|
|
|
actions.append(link);
|
|
|
|
|
const amount = makeElement('div', 'cloud-invoice-list-amount');
|
|
|
|
|
amount.append(makeElement('span', '', '金额'), makeElement('strong', '', formatUsd(invoice.amount)));
|
|
|
|
|
amount.append(makeElement('span', '', '金额'),
|
|
|
|
|
makeElement('strong', '', String(invoice?.amount_display || '$—')));
|
|
|
|
|
row.append(detail, actions, amount);
|
|
|
|
|
container.append(row);
|
|
|
|
|
}
|
|
|
|
|
|