This commit is contained in:
18
utils/display.js
Normal file
18
utils/display.js
Normal file
@@ -0,0 +1,18 @@
|
||||
export const formatMoney = (amount) => {
|
||||
if (amount === null || amount === undefined || amount === '') return '0.00';
|
||||
|
||||
const cents = Number(amount);
|
||||
if (!Number.isFinite(cents)) return '0.00';
|
||||
|
||||
return (cents / 100).toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ',');
|
||||
};
|
||||
|
||||
export const formatDate = (value, fallback = '-') => {
|
||||
if (!value) return fallback;
|
||||
return String(value).replace('T', ' ').slice(0, 10);
|
||||
};
|
||||
|
||||
export const formatDateTime = (value, fallback = '-') => {
|
||||
if (!value) return fallback;
|
||||
return String(value).replace('T', ' ').slice(0, 19);
|
||||
};
|
||||
21
utils/payment-methods.js
Normal file
21
utils/payment-methods.js
Normal file
@@ -0,0 +1,21 @@
|
||||
export const PAYMENT_METHOD_LABELS = {
|
||||
wechat: '微信支付',
|
||||
alipay: '支付宝支付',
|
||||
wallet: '钱包支付'
|
||||
};
|
||||
|
||||
export const normalizePaymentMethods = (methods) => {
|
||||
if (!Array.isArray(methods)) return [];
|
||||
|
||||
return [...new Set(methods.filter((method) => Object.prototype.hasOwnProperty.call(PAYMENT_METHOD_LABELS, method)))];
|
||||
};
|
||||
|
||||
export const getPaymentMethodOptions = (methods, preferredOrder = ['alipay', 'wechat', 'wallet']) => {
|
||||
const normalized = normalizePaymentMethods(methods);
|
||||
return preferredOrder
|
||||
.filter((method) => normalized.includes(method))
|
||||
.map((value) => ({ value, label: PAYMENT_METHOD_LABELS[value] }));
|
||||
};
|
||||
|
||||
export const getDefaultPaymentMethod = (methods, preferredOrder) =>
|
||||
getPaymentMethodOptions(methods, preferredOrder)[0]?.value || '';
|
||||
Reference in New Issue
Block a user