2022-11-22 07:45:36 +00:00
|
|
|
|
import { ref } from 'vue';
|
|
|
|
|
import dayjs from 'dayjs';
|
|
|
|
|
import $url from '@/sheep/url';
|
|
|
|
|
|
2023-12-01 12:24:54 +00:00
|
|
|
|
/**
|
|
|
|
|
* 格式化销量
|
|
|
|
|
* @param {'exact' | string} type 格式类型:exact=精确值,其它=大致数量
|
|
|
|
|
* @param {number} num 销量
|
|
|
|
|
* @return {string} 格式化后的销量字符串
|
|
|
|
|
*/
|
2022-11-22 07:45:36 +00:00
|
|
|
|
export function formatSales(type, num) {
|
2023-12-01 12:24:54 +00:00
|
|
|
|
let prefix = type!=='exact' && num<10 ? '销量': '已售';
|
|
|
|
|
return formatNum(prefix, type, num)
|
2022-11-22 07:45:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-01 12:24:54 +00:00
|
|
|
|
/**
|
|
|
|
|
* 格式化兑换量
|
|
|
|
|
* @param {'exact' | string} type 格式类型:exact=精确值,其它=大致数量
|
|
|
|
|
* @param {number} num 销量
|
|
|
|
|
* @return {string} 格式化后的销量字符串
|
|
|
|
|
*/
|
2022-11-22 07:45:36 +00:00
|
|
|
|
export function formatExchange(type, num) {
|
2023-12-01 12:24:54 +00:00
|
|
|
|
return formatNum('已兑换', type, num)
|
2022-11-22 07:45:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-01 12:24:54 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 格式化库存
|
|
|
|
|
* @param {'exact' | any} type 格式类型:exact=精确值,其它=大致数量
|
|
|
|
|
* @param {number} num 销量
|
|
|
|
|
* @return {string} 格式化后的销量字符串
|
|
|
|
|
*/
|
2022-11-22 07:45:36 +00:00
|
|
|
|
export function formatStock(type, num) {
|
2023-12-01 12:24:54 +00:00
|
|
|
|
return formatNum('库存', type, num)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 格式化数字
|
|
|
|
|
* @param {string} prefix 前缀
|
|
|
|
|
* @param {'exact' | string} type 格式类型:exact=精确值,其它=大致数量
|
|
|
|
|
* @param {number} num 销量
|
|
|
|
|
* @return {string} 格式化后的销量字符串
|
|
|
|
|
*/
|
|
|
|
|
export function formatNum(prefix, type, num) {
|
|
|
|
|
num = (num || 0);
|
|
|
|
|
// 情况一:精确数值
|
2022-11-22 07:45:36 +00:00
|
|
|
|
if (type === 'exact') {
|
2023-12-01 12:24:54 +00:00
|
|
|
|
return prefix + num;
|
|
|
|
|
}
|
|
|
|
|
// 情况二:小于等于 10
|
|
|
|
|
if (num < 10) {
|
|
|
|
|
return `${prefix}≤10`;
|
2022-11-22 07:45:36 +00:00
|
|
|
|
}
|
2023-12-01 12:24:54 +00:00
|
|
|
|
// 情况三:大于 10,除第一位外,其它位都显示为0
|
|
|
|
|
// 例如:100 - 199 显示为 100+
|
|
|
|
|
// 9000 - 9999 显示为 9000+
|
|
|
|
|
let pow = Math.pow(10, `${num}`.length - 1);
|
|
|
|
|
return `${prefix}${(num / pow) * pow}+`;
|
2022-11-22 07:45:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 格式化价格
|
|
|
|
|
export function formatPrice(e) {
|
|
|
|
|
return e.length === 1 ? e[0] : e.join('~');
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-01 12:24:54 +00:00
|
|
|
|
// 视频格式后缀列表
|
|
|
|
|
const VIDEO_SUFFIX_LIST = ['.avi', '.mp4']
|
|
|
|
|
/**
|
|
|
|
|
* 转换商品轮播的链接列表:根据链接的后缀,判断是视频链接还是图片链接
|
|
|
|
|
*
|
|
|
|
|
* @param {string[]} urlList 链接列表
|
|
|
|
|
* @return {{src: string, type: 'video' | 'image' }[]} 转换后的链接列表
|
|
|
|
|
*/
|
|
|
|
|
export function formatGoodsSwiper(urlList) {
|
|
|
|
|
return urlList.map((url, key) => {
|
|
|
|
|
const isVideo = VIDEO_SUFFIX_LIST.some(suffix => url.includes(suffix));
|
|
|
|
|
const type = isVideo ? 'video' :'image'
|
|
|
|
|
const src = $url.cdn(url);
|
|
|
|
|
return { type, src }
|
2022-11-22 07:45:36 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-01 12:24:54 +00:00
|
|
|
|
/**
|
|
|
|
|
* 格式化订单状态的颜色
|
|
|
|
|
* @param type 订单类型
|
|
|
|
|
* @return {string} 颜色的 class 名称
|
|
|
|
|
*/
|
2022-11-22 07:45:36 +00:00
|
|
|
|
export function formatOrderColor(type) {
|
2023-12-01 12:24:54 +00:00
|
|
|
|
switch (type) {
|
|
|
|
|
case 'apply_refund':
|
|
|
|
|
case 'groupon_ing':
|
|
|
|
|
case 'nocomment':
|
|
|
|
|
case 'noget':
|
|
|
|
|
case 'nosend':
|
|
|
|
|
return 'warning-color';
|
|
|
|
|
case 'closed':
|
|
|
|
|
case 'groupon_invalid':
|
|
|
|
|
case 'cancel':
|
|
|
|
|
case 'refund_agree':
|
|
|
|
|
return 'danger-color';
|
|
|
|
|
case 'completed':
|
|
|
|
|
return 'success-color';
|
|
|
|
|
case 'unpaid':
|
|
|
|
|
return 'info-color';
|
2022-11-22 07:45:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-01 12:24:54 +00:00
|
|
|
|
/**
|
|
|
|
|
* 倒计时
|
|
|
|
|
* @param toTime 截止时间
|
|
|
|
|
* @param fromTime 起始时间,默认当前时间
|
|
|
|
|
* @return {{s: string, ms: number, h: string, m: string}} 持续时间
|
|
|
|
|
*/
|
2022-11-22 07:45:36 +00:00
|
|
|
|
export function useDurationTime(toTime, fromTime = '') {
|
|
|
|
|
toTime = getDayjsTime(toTime);
|
|
|
|
|
if (fromTime === '') {
|
|
|
|
|
fromTime = dayjs();
|
|
|
|
|
}
|
|
|
|
|
let duration = ref(toTime - fromTime);
|
|
|
|
|
if (duration.value > 0) {
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
if (duration.value > 0) {
|
|
|
|
|
duration.value -= 1000;
|
|
|
|
|
}
|
|
|
|
|
}, 1000);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let durationTime = dayjs.duration(duration.value);
|
|
|
|
|
return {
|
|
|
|
|
h: (durationTime.months() * 30 * 24 + durationTime.days() * 24 + durationTime.hours())
|
|
|
|
|
.toString()
|
|
|
|
|
.padStart(2, '0'),
|
|
|
|
|
m: durationTime.minutes().toString().padStart(2, '0'),
|
|
|
|
|
s: durationTime.seconds().toString().padStart(2, '0'),
|
|
|
|
|
ms: durationTime.$ms,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-01 12:24:54 +00:00
|
|
|
|
/**
|
|
|
|
|
* 转换为 Dayjs
|
|
|
|
|
* @param {any} time 时间
|
|
|
|
|
* @return {dayjs.Dayjs}
|
|
|
|
|
*/
|
2022-11-22 07:45:36 +00:00
|
|
|
|
function getDayjsTime(time) {
|
|
|
|
|
time = time.toString();
|
|
|
|
|
if (time.indexOf('-') > 0) {
|
|
|
|
|
// 'date'
|
|
|
|
|
return dayjs(time);
|
|
|
|
|
}
|
|
|
|
|
if (time.length > 10) {
|
|
|
|
|
// 'timestamp'
|
|
|
|
|
return dayjs(parseInt(time));
|
|
|
|
|
}
|
|
|
|
|
if (time.length === 10) {
|
2023-12-01 12:24:54 +00:00
|
|
|
|
// 'unixTime'
|
2022-11-22 07:45:36 +00:00
|
|
|
|
return dayjs.unix(parseInt(time));
|
|
|
|
|
}
|
|
|
|
|
}
|