|
@@ -2,49 +2,60 @@ import { ref } from 'vue';
|
|
|
import dayjs from 'dayjs';
|
|
|
import $url from '@/sheep/url';
|
|
|
|
|
|
-// 格式化销量
|
|
|
+/**
|
|
|
+ * 格式化销量
|
|
|
+ * @param {'exact' | string} type 格式类型:exact=精确值,其它=大致数量
|
|
|
+ * @param {number} num 销量
|
|
|
+ * @return {string} 格式化后的销量字符串
|
|
|
+ */
|
|
|
export function formatSales(type, num) {
|
|
|
- num = num + '';
|
|
|
- if (type === 'exact') {
|
|
|
- return '已售' + num;
|
|
|
- } else {
|
|
|
- if (num < 10) {
|
|
|
- return '销量≤10';
|
|
|
- } else {
|
|
|
- let a = Math.pow(10, num.length - 1);
|
|
|
- return '已售' + parseInt(num / a) * a + '+';
|
|
|
- }
|
|
|
- }
|
|
|
+ let prefix = type!=='exact' && num<10 ? '销量': '已售';
|
|
|
+ return formatNum(prefix, type, num)
|
|
|
}
|
|
|
|
|
|
-// 格式化兑换量
|
|
|
+/**
|
|
|
+ * 格式化兑换量
|
|
|
+ * @param {'exact' | string} type 格式类型:exact=精确值,其它=大致数量
|
|
|
+ * @param {number} num 销量
|
|
|
+ * @return {string} 格式化后的销量字符串
|
|
|
+ */
|
|
|
export function formatExchange(type, num) {
|
|
|
- num = num + '';
|
|
|
- if (type === 'exact') {
|
|
|
- return '已兑换' + num;
|
|
|
- } else {
|
|
|
- if (num < 10) {
|
|
|
- return '已兑换≤10';
|
|
|
- } else {
|
|
|
- let a = Math.pow(10, num.length - 1);
|
|
|
- return '已兑换' + parseInt(num / a) * a + '+';
|
|
|
- }
|
|
|
- }
|
|
|
+ return formatNum('已兑换', type, num)
|
|
|
}
|
|
|
|
|
|
-// 格式化库存
|
|
|
+
|
|
|
+/**
|
|
|
+ * 格式化库存
|
|
|
+ * @param {'exact' | any} type 格式类型:exact=精确值,其它=大致数量
|
|
|
+ * @param {number} num 销量
|
|
|
+ * @return {string} 格式化后的销量字符串
|
|
|
+ */
|
|
|
export function formatStock(type, num) {
|
|
|
- num = num + '';
|
|
|
+ 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);
|
|
|
+ // 情况一:精确数值
|
|
|
if (type === 'exact') {
|
|
|
- return '库存' + num;
|
|
|
- } else {
|
|
|
- if (num < 10) {
|
|
|
- return '库存≤10';
|
|
|
- } else {
|
|
|
- let a = Math.pow(10, num.length - 1);
|
|
|
- return '库存 ' + parseInt(num / a) * a + '+';
|
|
|
- }
|
|
|
+ return prefix + num;
|
|
|
+ }
|
|
|
+ // 情况二:小于等于 10
|
|
|
+ if (num < 10) {
|
|
|
+ return `${prefix}≤10`;
|
|
|
}
|
|
|
+ // 情况三:大于 10,除第一位外,其它位都显示为0
|
|
|
+ // 例如:100 - 199 显示为 100+
|
|
|
+ // 9000 - 9999 显示为 9000+
|
|
|
+ let pow = Math.pow(10, `${num}`.length - 1);
|
|
|
+ return `${prefix}${(num / pow) * pow}+`;
|
|
|
}
|
|
|
|
|
|
// 格式化价格
|
|
@@ -52,49 +63,54 @@ export function formatPrice(e) {
|
|
|
return e.length === 1 ? e[0] : e.join('~');
|
|
|
}
|
|
|
|
|
|
-// 格式化商品轮播
|
|
|
-export function formatGoodsSwiper(list) {
|
|
|
- let swiper = [];
|
|
|
- list.forEach((item, key) => {
|
|
|
- if (item.indexOf('.avi') !== -1 || item.indexOf('.mp4') !== -1) {
|
|
|
- swiper.push({
|
|
|
- src: $url.cdn(item),
|
|
|
- type: 'video',
|
|
|
- });
|
|
|
- } else {
|
|
|
- swiper.push({
|
|
|
- src: $url.cdn(item),
|
|
|
- type: 'image',
|
|
|
- });
|
|
|
- }
|
|
|
+// 视频格式后缀列表
|
|
|
+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 }
|
|
|
});
|
|
|
- return swiper;
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * 格式化订单状态的颜色
|
|
|
+ * @param type 订单类型
|
|
|
+ * @return {string} 颜色的 class 名称
|
|
|
+ */
|
|
|
export function formatOrderColor(type) {
|
|
|
- if (
|
|
|
- type === 'apply_refund' ||
|
|
|
- type === 'groupon_ing' ||
|
|
|
- type === 'nocomment' ||
|
|
|
- type === 'noget' ||
|
|
|
- type === 'nosend'
|
|
|
- ) {
|
|
|
- return 'warning-color';
|
|
|
- } else if (
|
|
|
- type === 'closed' ||
|
|
|
- type === 'groupon_invalid' ||
|
|
|
- type === 'cancel' ||
|
|
|
- type === 'refund_agree'
|
|
|
- ) {
|
|
|
- return 'danger-color';
|
|
|
- } else if (type === 'completed') {
|
|
|
- return 'success-color';
|
|
|
- } else if (type === 'unpaid') {
|
|
|
- return 'info-color';
|
|
|
+ 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';
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-// 计算相隔时间
|
|
|
+/**
|
|
|
+ * 倒计时
|
|
|
+ * @param toTime 截止时间
|
|
|
+ * @param fromTime 起始时间,默认当前时间
|
|
|
+ * @return {{s: string, ms: number, h: string, m: string}} 持续时间
|
|
|
+ */
|
|
|
export function useDurationTime(toTime, fromTime = '') {
|
|
|
toTime = getDayjsTime(toTime);
|
|
|
if (fromTime === '') {
|
|
@@ -120,6 +136,11 @@ export function useDurationTime(toTime, fromTime = '') {
|
|
|
};
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * 转换为 Dayjs
|
|
|
+ * @param {any} time 时间
|
|
|
+ * @return {dayjs.Dayjs}
|
|
|
+ */
|
|
|
function getDayjsTime(time) {
|
|
|
time = time.toString();
|
|
|
if (time.indexOf('-') > 0) {
|
|
@@ -131,7 +152,7 @@ function getDayjsTime(time) {
|
|
|
return dayjs(parseInt(time));
|
|
|
}
|
|
|
if (time.length === 10) {
|
|
|
- // 'unixtime'
|
|
|
+ // 'unixTime'
|
|
|
return dayjs.unix(parseInt(time));
|
|
|
}
|
|
|
}
|