Files
JayuCloud/YumiNail-Admin-Program/utils/format.js
T

149 lines
2.8 KiB
JavaScript

function toIdString(value) {
return value === undefined || value === null ? '' : String(value)
}
function sameId(left, right) {
return toIdString(left) === toIdString(right)
}
function splitCsv(value) {
return String(value || '')
.split(',')
.map(item => item.trim())
.filter(Boolean)
}
function formatPrice(value) {
return Number(value || 0).toFixed(2)
}
function formatDateTime(value) {
if (!value) {
return ''
}
return String(value).replace('T', ' ').slice(0, 16)
}
function formatDate(value) {
return formatDateTime(value).slice(0, 10)
}
function maskPhone(phone) {
const value = String(phone || '')
if (value.length !== 11) {
return value
}
return `${value.slice(0, 3)}****${value.slice(7)}`
}
function truncateText(value, limit) {
const text = String(value || '')
if (!limit || text.length <= limit) {
return text
}
return `${text.slice(0, limit)}...`
}
function buildShopName(record) {
if (!record) {
return '未绑定店铺'
}
if (Array.isArray(record.shopNames) && record.shopNames.length) {
return record.shopNames.join(' / ')
}
if (record.shopName) {
return record.shopName
}
return '未绑定店铺'
}
function getUserStatusText(status) {
return Number(status) === 0 ? '已停用' : '正常'
}
function getUserTypeText(type) {
const value = Number(type)
if (value === 4) {
return '平台管理员'
}
if (value === 2) {
return '店长'
}
if (value === 3) {
return '美甲师'
}
return '普通用户'
}
function getOrderStatusText(order) {
if (order && order.statusText) {
return order.statusText
}
const status = Number(order && order.status)
if (status === 1) {
return '待服务'
}
if (status === 2) {
return '已完成'
}
return '已取消'
}
function getCouponTypeText(type) {
return Number(type) === 1 ? '折扣券' : '满减券'
}
function getCouponValueText(coupon) {
if (Number(coupon && coupon.couponType) === 1) {
return `${coupon && coupon.discountValue ? coupon.discountValue : '--'} 折`
}
return `满 ${formatPrice(coupon && coupon.minAmount)} 减 ${formatPrice(coupon && coupon.reduceAmount)}`
}
function parseDateTimeToPicker(value) {
const text = formatDateTime(value)
if (!text) {
return {
date: '',
time: ''
}
}
const parts = text.split(' ')
return {
date: parts[0] || '',
time: (parts[1] || '').slice(0, 5)
}
}
function buildDateTime(date, time) {
if (!date || !time) {
return ''
}
return `${date} ${time}:00`
}
module.exports = {
toIdString,
sameId,
splitCsv,
formatPrice,
formatDateTime,
formatDate,
maskPhone,
truncateText,
buildShopName,
getUserStatusText,
getUserTypeText,
getOrderStatusText,
getCouponTypeText,
getCouponValueText,
parseDateTimeToPicker,
buildDateTime
}