food/src/utils/index.js
tk 61bfc7d455 【类 型】:factor 订单模块修改
【原  因】:1.弹出框影响视觉,载入页面改为loding动画。2.价格加入 价格格式化 防止浮点显示错误
【过  程】:1获取数据加入loading属性 2.写了一个格式化价格参数 保留小数点后两位
【影  响】:
2024-07-30 13:32:51 +08:00

161 lines
3.8 KiB
JavaScript

/**
* Parse the time to string
* @param {(Object|string|number)} time
* @param {string} cFormat
* @returns {string | null}
*/
export function parseTime (time, cFormat) {
if (arguments.length === 0 || !time) {
return null
}
const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
let date
if (typeof time === 'object') {
date = time
} else {
if ((typeof time === 'string')) {
if ((/^[0-9]+$/.test(time))) {
time = parseInt(time)
} else {
time = time.replace(new RegExp(/-/gm), '/')
}
}
if ((typeof time === 'number') && (time.toString().length === 10)) {
time = time * 1000
}
date = new Date(time)
}
const formatObj = {
y: date.getFullYear(),
m: date.getMonth() + 1,
d: date.getDate(),
h: date.getHours(),
i: date.getMinutes(),
s: date.getSeconds(),
a: date.getDay()
}
// eslint-disable-next-line camelcase
const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
const value = formatObj[key]
// Note: getDay() returns 0 on Sunday
if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
return value.toString().padStart(2, '0')
})
// eslint-disable-next-line camelcase
return time_str
}
/**
* @param {number} time
* @param {string} option
* @returns {string}
*/
export function formatTime (time, option) {
if (('' + time).length === 10) {
time = parseInt(time) * 1000
} else {
time = +time
}
const d = new Date(time)
const now = Date.now()
const diff = (now - d) / 1000
if (diff < 30) {
return '刚刚'
} else if (diff < 3600) {
// less 1 hour
return Math.ceil(diff / 60) + '分钟前'
} else if (diff < 3600 * 24) {
return Math.ceil(diff / 3600) + '小时前'
} else if (diff < 3600 * 24 * 2) {
return '1天前'
}
if (option) {
return parseTime(time, option)
} else {
return (
d.getMonth() +
1 +
'月' +
d.getDate() +
'日' +
d.getHours() +
'时' +
d.getMinutes() +
'分'
)
}
}
/**
* @param {string} url
* @returns {Object}
*/
export function param2Obj (url) {
const search = decodeURIComponent(url.split('?')[1]).replace(/\+/g, ' ')
if (!search) {
return {}
}
const obj = {}
const searchArr = search.split('&')
searchArr.forEach(v => {
const index = v.indexOf('=')
if (index !== -1) {
const name = v.substring(0, index)
const val = v.substring(index + 1, v.length)
obj[name] = val
}
})
return obj
}
/**
* @param {string} path
* @returns {Boolean}
*/
export function isExternal (path) {
return /^(https?:|mailto:|tel:)/.test(path)
}
/**
* @description: 统计当前被选中的ID组
* @return: 被选中记录 的ID组
* @param {*} selection 列表中被选中的记录
*/
export function countSelIdArr (selection) {
const idArr = []
selection.map((item) => idArr.push(item.id))
return idArr
}
/**
* @description: 封装文本转语音功能 responsiveVoice接口
* @param {*} text 要播放的文本
*/
export function speakText (text) {
if (!text || text.trim() === '') {
console.error('请输入要转换的文本!')
return
}
window.responsiveVoice.speak(text, 'Chinese Female')
}
/**
* 过滤价格数值 超过小数点两位得 保留两位
* @param {string} value
*/
export function formatPrice (value) {
if (isNaN(value)) return '' // 如果不是数字,则返回空字符串
// 格式化价格为保留两位小数
let formattedPrice = parseFloat(value).toFixed(2)
// 如果价格有两位小数以上,则保留两位小数,否则显示整数或者一位小数
const decimalCount = (formattedPrice.split('.')[1] || []).length
if (decimalCount > 2) {
formattedPrice = parseFloat(formattedPrice).toFixed(2)
}
return formattedPrice
}