Files
MTKJPAY-FRONT/src/utils/helpers.js

124 lines
2.6 KiB
JavaScript
Raw Normal View History

/**
* 工具函数
*/
/**
* 格式化金额
*/
export function formatAmount(amount, currency = 'USD') {
if (!amount) return '0.00'
return parseFloat(amount).toFixed(2)
}
/**
* 格式化日期时间
*/
export function formatDateTime(dateTime, format = 'YYYY-MM-DD HH:mm:ss') {
if (!dateTime) return ''
const date = new Date(dateTime)
const year = date.getFullYear()
const month = String(date.getMonth() + 1).padStart(2, '0')
const day = String(date.getDate()).padStart(2, '0')
const hours = String(date.getHours()).padStart(2, '0')
const minutes = String(date.getMinutes()).padStart(2, '0')
const seconds = String(date.getSeconds()).padStart(2, '0')
return format
.replace('YYYY', year)
.replace('MM', month)
.replace('DD', day)
.replace('HH', hours)
.replace('mm', minutes)
.replace('ss', seconds)
}
/**
* 生成订单号
*/
export function generateOrderId() {
const timestamp = Date.now()
const random = Math.floor(Math.random() * 10000)
return `MTN${timestamp}${random.toString().padStart(4, '0')}`
}
/**
* 获取订单状态标签类型
*/
export function getStatusTagType(status) {
if (!status) return 'info'
const statusUpper = status.toUpperCase()
if (statusUpper === 'SUCCESS' || statusUpper === 'SUCCESSFUL') {
return 'success'
} else if (statusUpper === 'FAILED' || statusUpper === 'FAILURE') {
return 'danger'
} else if (statusUpper === 'REVIEW') {
return 'warning'
}
return 'info'
}
/**
* 获取订单状态文本
*/
export function getStatusText(status) {
if (!status) return '未知'
const statusMap = {
'PENDING': '待支付',
'SUCCESS': '支付成功',
'SUCCESSFUL': '支付成功',
'FAILED': '支付失败',
'FAILURE': '支付失败',
'REVIEW': '审核中',
'CANCELLED': '已取消',
'CANCEL': '已取消'
}
return statusMap[status.toUpperCase()] || status
}
/**
* 验证邮箱
*/
export function validateEmail(email) {
const re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
return re.test(email)
}
/**
* 验证手机号简单验证
*/
export function validatePhone(phone) {
const re = /^1[3-9]\d{9}$/
return re.test(phone)
}
/**
* 防抖函数
*/
export function debounce(func, wait) {
let timeout
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout)
func(...args)
}
clearTimeout(timeout)
timeout = setTimeout(later, wait)
}
}
/**
* 节流函数
*/
export function throttle(func, limit) {
let inThrottle
return function(...args) {
if (!inThrottle) {
func.apply(this, args)
inThrottle = true
setTimeout(() => inThrottle = false, limit)
}
}
}