mall-app-t/peach/hooks/useGoods.js

184 lines
4.8 KiB
JavaScript
Raw Normal View History

2024-05-28 18:22:58 +08:00
import { ref } from 'vue'
import dayjs from 'dayjs'
2024-06-03 18:35:53 +08:00
import { formatDate } from '@/peach/utils'
/**
* 格式化数字
* @param {string} prefix 前缀
* @param {'exact' | string} type 格式类型exact=精确值其它=大致数量
* @param {number} num 销量
* @return {string} 格式化后的销量字符串
*/
export function formatNum(prefix, type, num) {
num = num || 0
// 情况一:精确数值
if (type === 'exact') {
return prefix + num
}
// 情况二:小于等于 10
if (num < 10) {
return `${prefix}≤10`
}
// 情况三:大于 10除第一位外其它位都显示为0
// 例如100 - 199 显示为 100+
// 9000 - 9999 显示为 9000+
const numStr = num.toString()
const first = numStr[0]
const other = '0'.repeat(numStr.length - 1)
return `${prefix}${first}${other}+`
}
2024-05-27 00:51:03 +08:00
/**
* 将分转成元
*
* @param price 例如说 100
* @returns {string} 例如说 1.00
*/
export function fen2yuan(price) {
2024-05-28 18:22:58 +08:00
return (price / 100.0).toFixed(2)
2024-05-27 00:51:03 +08:00
}
/**
* 格式化销量
* @param {'exact' | string} type 格式类型exact=精确值其它=大致数量
* @param {number} num 销量
* @return {string} 格式化后的销量字符串
*/
export function formatSales(type, num) {
2024-05-28 18:22:58 +08:00
let prefix = type !== 'exact' && num < 10 ? '销量' : '已售'
return formatNum(prefix, type, num)
2024-05-27 00:51:03 +08:00
}
/**
* 格式化库存
* @param {'exact' | any} type 格式类型exact=精确值其它=大致数量
* @param {number} num 销量
* @return {string} 格式化后的销量字符串
*/
export function formatStock(type, num) {
2024-05-28 18:22:58 +08:00
return formatNum('库存', type, num)
}
2024-06-03 18:35:53 +08:00
export function formatOrderStatusDescription(order) {
if (order.status === 0) {
if (!order.payExpireTime) {
return '当前支付已过期'
}
return `请在 ${formatDate(order.payExpireTime)} 前完成支付`
}
if (order.status === 10) {
return '商家未发货,请耐心等待'
}
if (order.status === 20) {
return '商家已发货,请耐心等待'
}
if (order.status === 30 && !order.commentStatus) {
2024-08-21 18:09:07 +08:00
return '已收货,待评价'
2024-06-03 18:35:53 +08:00
}
if (order.status === 30 && order.commentStatus) {
return '交易完成,感谢您的支持'
}
if (order.status === 50) {
return '请及时到店核销商品'
}
2024-08-21 18:09:07 +08:00
if (order.status === 60) {
return '商家已接单,待配送'
}
2024-06-03 18:35:53 +08:00
return '交易关闭'
}
2024-05-28 18:22:58 +08:00
export function formatOrderStatus(order) {
2024-08-22 16:32:15 +08:00
if (order.refundStatus === 10) {
return '部分退款'
}
if (order.refundStatus === 20) {
return '整单退款'
}
2024-05-28 18:22:58 +08:00
if (order.status === 0) {
return '待付款'
}
if (order.status === 10 && order.deliveryType === 1) {
return '待发货'
}
if (order.status === 10 && order.deliveryType === 2) {
return '待核销'
}
if (order.status === 20) {
return '待收货'
}
if (order.status === 30 && !order.commentStatus) {
return '待评价'
}
if (order.status === 30 && order.commentStatus) {
2024-08-22 16:32:15 +08:00
return '已评价'
2024-05-28 18:22:58 +08:00
}
if (order.status === 50) {
return '待核销'
}
if (order.status === 60) {
return '待配送'
}
return '已关闭'
}
export function formatOrderColor(order) {
2024-08-22 16:32:15 +08:00
if (order.refundStatus === 10 || order.refundStatus === 20) {
return 'danger-color'
}
2024-05-28 18:22:58 +08:00
if (order.status === 0) {
return 'info-color'
}
if (
order.status === 10 ||
order.status === 20 ||
(order.status === 30 && !order.commentStatus) ||
order.status === 50 ||
order.status === 60
) {
return 'warning-color'
}
if (order.status === 30 && order.commentStatus) {
return 'success-color'
}
return 'danger-color'
}
export function handleOrderButtons(order) {
order.buttons = []
if (order.type === 3) {
// 查看拼团
order.buttons.push('combination')
}
if (order.status === 20) {
// 确认收货
order.buttons.push('confirm')
}
if (order.logisticsId > 0) {
// 查看物流
order.buttons.push('express')
}
if (order.status === 0) {
// 取消订单 / 发起支付
order.buttons.push('cancel')
// order.buttons.push('pay');
}
2024-08-21 18:09:07 +08:00
// if (order.status === 30 && !order.commentStatus) {
// // 发起评价
// order.buttons.push('comment')
// }
2024-05-28 18:22:58 +08:00
if (order.status === 40) {
// 删除订单
2024-06-03 18:35:53 +08:00
// order.buttons.push('delete')
}
2024-08-21 18:09:07 +08:00
// if (order.status === 50) {
// // 核销订单
// order.buttons.push('verification')
// }
2024-06-03 18:35:53 +08:00
if (order.status === 60) {
// 配送完成
order.buttons.push('delivery')
2024-05-28 18:22:58 +08:00
}
2024-05-27 00:51:03 +08:00
}