123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444 |
- import {
- ref
- } from 'vue';
- import dayjs from 'dayjs';
- import $url from '@/sheep/url';
- import {
- formatDate
- } from '@/sheep/util';
- import {
- t
- } from '@/locale';
- /**
- * 格式化销量
- * @param {'exact' | string} type 格式类型:exact=精确值,其它=大致数量
- * @param {number} num 销量
- * @return {string} 格式化后的销量字符串
- */
- export function formatSales(type, num) {
- let prefix = type !== 'exact' && num < 10 ? t('common.sales_volume') : t('common.sold');
- return formatNum(prefix, type, num)
- }
- /**
- * 格式化兑换量
- * @param {'exact' | string} type 格式类型:exact=精确值,其它=大致数量
- * @param {number} num 销量
- * @return {string} 格式化后的销量字符串
- */
- export function formatExchange(type, num) {
- return formatNum(t('useGoods.redeemed'), type, num)
- }
- /**
- * 格式化库存
- * @param {'exact' | any} type 格式类型:exact=精确值,其它=大致数量
- * @param {number} num 销量
- * @return {string} 格式化后的销量字符串
- */
- export function formatStock(type, num) {
- return formatNum(t('useGoods.in_stock'), 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 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}+`;
- }
- // 格式化价格
- export function formatPrice(e) {
- return e.length === 1 ? e[0] : e.join('~');
- }
- // 视频格式后缀列表
- const VIDEO_SUFFIX_LIST = ['.avi', '.mp4']
- /**
- * 转换商品轮播的链接列表:根据链接的后缀,判断是视频链接还是图片链接
- *
- * @param {string[]} urlList 链接列表
- * @return {{src: string, type: 'video' | 'image' }[]} 转换后的链接列表
- */
- export function formatGoodsSwiper(urlList) {
- return urlList?.filter(url => url).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
- }
- }) || [];
- }
- /**
- * 格式化订单状态的颜色
- *
- * @param order 订单
- * @return {string} 颜色的 class 名称
- */
- export function formatOrderColor(order) {
- if (order.status === 0) {
- return 'info-color';
- }
- if (order.status === 10 ||
- order.status === 20 ||
- (order.status === 30 && !order.commentStatus)) {
- return 'warning-color';
- }
- if (order.status === 30 && order.commentStatus) {
- return 'success-color';
- }
- return 'danger-color';
- }
- /**
- * 格式化订单状态
- *
- * @param order 订单
- */
- export function formatOrderStatus(order) {
- // console.log(order.status,order.deliveryType);
- if (order.status === 0) {
- // 待付款
- return t('useGoods.pending_payment');
- }
- // 快递发货 虚拟发货都走这条
- if (order.status === 10 && order.deliveryType === 1 || order.status === 10 && order.deliveryType === 3) {
- // 待发货
- return t('useGoods.awaiting_shipment');
- }
- if (order.status === 10 && order.deliveryType === 2) {
- // 待核销
- return t('useGoods.awaiting_write_off');
- }
- if (order.status === 20) {
- // 待收货
- return t('useGoods.awaiting_delivery');
- }
- if (order.status === 30 && !order.commentStatus) {
- // 待评价
- return t('useGoods.awaiting_review');
-
- }
- if (order.status === 30 && order.commentStatus) {
- // 已完成
- return t('useGoods.completed');
- }
- // 已关闭
- return t('useGoods.closed');
- }
- /**
- * 格式化订单状态的描述
- *
- * @param order 订单
- */
- export function formatOrderStatusDescription(order) {
- if (order.status === 0) {
- return t('useGoods.complete_payment_by',{time:formatDate(order.payExpireTime)});
- }
- if (order.status === 10) {
- return t('useGoods.merchant_not_shipped');
- }
- if (order.status === 20) {
- return t('useGoods.merchant_shipped');
- }
- if (order.status === 30 && !order.commentStatus) {
- return t('useGoods.goods_received');
- }
- if (order.status === 30 && order.commentStatus) {
- return t('useGoods.transaction_completed');
- }
- return t('useGoods.transaction_closed');
- }
- /**
- * 处理订单的 button 操作按钮数组
- *
- * @param order 订单
- */
- export function handleOrderButtons(order) {
- console.log()
- order.buttons = []
- if (order.type === 3) { // 查看拼团
- order.buttons.push('combination');
- }
- if (order.status === 20) { // 确认收货
- order.buttons.push('express');
- order.buttons.push('confirm');
- }
- if (order.logisticsId > 0) { // 查看物流
- order.buttons.push('express');
- }
- if (order.status === 0) { // 取消订单 / 发起支付
- order.buttons.push('cancel');
- order.buttons.push('pay');
- }
- if (order.status === 30 && !order.commentStatus) { // 发起评价
- order.buttons.push('comment');
- }
- if (order.status === 40) { // 删除订单
- order.buttons.push('delete');
- }
- if ([10, 20, 30].includes(order.items[0]?.refundStatus)) { // 取消订单
- order.buttons.push('aftersaleCancel');
- }
- // 如果订单是发起了售后,商家同意了。待用户发货回平台
- if (order.items[0]?.refundStatus === 20) {
- order.buttons.push('aftersaleDelivery');
- }
- }
- /**
- * 格式化售后状态
- *
- * @param afterSale 售后
- */
- export function formatAfterSaleStatus(afterSale) {
- if (afterSale.status === 10) {
- return t('useGoods.apply_for_after_sales');
- }
- if (afterSale.status === 20) {
- return t('useGoods.goods_awaiting_return');
- }
- if (afterSale.status === 30) {
- return t('useGoods.merchant_awaiting_goods');
- }
- if (afterSale.status === 40) {
- return t('useGoods.waiting_for_refund');
- }
- if (afterSale.status === 50) {
- return t('useGoods.refund_successful');
- }
- if (afterSale.status === 61) {
- return t('useGoods.buyer_cancelled');
- }
- if (afterSale.status === 62) {
- return t('useGoods.merchant_refused');
- }
- if (afterSale.status === 63) {
- return t('useGoods.merchant_refused_goods');
- }
- return t('useGoods.unknown_status');
- }
- /**
- * 格式化售后状态的描述
- *
- * @param afterSale 售后
- */
- export function formatAfterSaleStatusDescription(afterSale) {
- if (afterSale.status === 10) {
- return t('useGoods.refund_request_awaiting_merchant');
- }
- if (afterSale.status === 20) {
- return t('useGoods.return_goods_fill_logistics');
- }
- if (afterSale.status === 30) {
- return t('useGoods.return_refund_request_awaiting_merchant');
- }
- if (afterSale.status === 40) {
- return t('useGoods.waiting_for_refund');
- }
- if (afterSale.status === 50) {
- return t('useGoods.refund_successful');
- }
- if (afterSale.status === 61) {
- return t('useGoods.refund_closed');
- }
- if (afterSale.status === 62) {
- return t('useGoods.merchant_disagrees_with_refund_request')+`${afterSale.auditReason}`;
- }
- if (afterSale.status === 63) {
- return t('useGoods.merchant_refuses_to_accept_goods')+`${afterSale.auditReason}`;
- }
- return t('useGoods.unknown_status');
- }
- /**
- * 处理售后的 button 操作按钮数组
- *
- * @param afterSale 售后
- */
- export function handleAfterSaleButtons(afterSale) {
- console.log(afterSale)
- afterSale.buttons = [];
- if ([10, 20, 30].includes(afterSale.status)) { // 取消订单
- afterSale.buttons.push('cancel');
- }
- if (afterSale.status === 20) { // 退货信息
- afterSale.buttons.push('delivery');
- }
- }
- /**
- * 倒计时
- * @param toTime 截止时间
- * @param fromTime 起始时间,默认当前时间
- * @return {{s: string, ms: number, h: string, m: string}} 持续时间
- */
- 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,
- };
- }
- /**
- * 转换为 Dayjs
- * @param {any} time 时间
- * @return {dayjs.Dayjs}
- */
- 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) {
- // 'unixTime'
- return dayjs.unix(parseInt(time));
- }
- }
- /**
- * 将分转成元
- *
- * @param price 分,例如说 100 分
- * @returns {string} 元,例如说 1.00 元
- */
- export function fen2yuan(price) {
- return (price / 100.0).toFixed(2)
- }
- /**
- * 将分转成元
- *
- * @param price 分,例如说 100 分
- * @returns {string} 元,例如说 1.000000 元
- */
- export function fen2yuan6(price) {
- return (price / 100.0).toFixed(6)
- }
- /**
- * 将后台佣金转换为可使用佣金
- *
- * @param point 分,例如说 1000000 分
- * @returns {string} 分,例如说 1.00 分
- */
- export function points2point(point) {
- return (point / 1000000.0).toFixed(3).slice(0, -1) // 干掉四舍五入 实际返回不四舍五入的两位小数
- }
- /**
- * 从商品 SKU 数组中,转换出商品属性的数组
- *
- * 类似结构:[{
- * id: // 属性的编号
- * name: // 属性的名字
- * values: [{
- * id: // 属性值的编号
- * name: // 属性值的名字
- * }]
- * }]
- *
- * @param skus 商品 SKU 数组
- */
- export function convertProductPropertyList(skus) {
- let result = [];
- for (const sku of skus) {
- if (!sku.properties) {
- continue
- }
- for (const property of sku.properties) {
- // ① 先处理属性
- let resultProperty = result.find(item => item.id === property.propertyId)
- if (!resultProperty) {
- resultProperty = {
- id: property.propertyId,
- name: property.propertyName,
- values: []
- }
- result.push(resultProperty)
- }
- // ② 再处理属性值
- let resultValue = resultProperty.values.find(item => item.id === property.valueId)
- if (!resultValue) {
- resultProperty.values.push({
- id: property.valueId,
- name: property.valueName
- })
- }
- }
- }
- return result;
- }
- /**
- * 格式化满减送活动的规则
- *
- * @param activity 活动信息
- * @param rule 优惠规格
- * @returns {string} 规格字符串
- */
- export function formatRewardActivityRule(activity, rule) {
- if (activity.conditionType === 10) {
- return `满 ${fen2yuan(rule.limit)} 元减 ${fen2yuan(rule.discountPrice)} 元`;
- }
- if (activity.conditionType === 20) {
- return `满 ${rule.limit} 件减 ${fen2yuan(rule.discountPrice)} 元`;
- }
- return '';
- }
|