useGoods.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. import { ref } from 'vue';
  2. import dayjs from 'dayjs';
  3. // import $url from '@/sheep/url';
  4. import { formatDate } from '@/utils/date';
  5. /**
  6. * 格式化销量
  7. * @param {'exact' | string} type 格式类型:exact=精确值,其它=大致数量
  8. * @param {number} num 销量
  9. * @return {string} 格式化后的销量字符串
  10. */
  11. export function formatSales(type, num) {
  12. let prefix = type !== 'exact' && num < 10 ? '销量' : '已售';
  13. return formatNum(prefix, type, num);
  14. }
  15. /**
  16. * 格式化兑换量
  17. * @param {'exact' | string} type 格式类型:exact=精确值,其它=大致数量
  18. * @param {number} num 销量
  19. * @return {string} 格式化后的销量字符串
  20. */
  21. export function formatExchange(type, num) {
  22. return formatNum('已兑换', type, num);
  23. }
  24. /**
  25. * 格式化库存
  26. * @param {'exact' | any} type 格式类型:exact=精确值,其它=大致数量
  27. * @param {number} num 销量
  28. * @return {string} 格式化后的销量字符串
  29. */
  30. export function formatStock(type, num) {
  31. return formatNum('库存', type, num);
  32. }
  33. /**
  34. * 格式化数字
  35. * @param {string} prefix 前缀
  36. * @param {'exact' | string} type 格式类型:exact=精确值,其它=大致数量
  37. * @param {number} num 销量
  38. * @return {string} 格式化后的销量字符串
  39. */
  40. export function formatNum(prefix, type, num) {
  41. num = num || 0;
  42. // 情况一:精确数值
  43. if (type === 'exact') {
  44. return prefix + num;
  45. }
  46. // 情况二:小于等于 10
  47. if (num < 10) {
  48. return `${prefix}≤10`;
  49. }
  50. // 情况三:大于 10,除第一位外,其它位都显示为0
  51. // 例如:100 - 199 显示为 100+
  52. // 9000 - 9999 显示为 9000+
  53. const numStr = num.toString();
  54. const first = numStr[0];
  55. const other = '0'.repeat(numStr.length - 1);
  56. return `${prefix}${first}${other}+`;
  57. }
  58. // 格式化价格
  59. export function formatPrice(e) {
  60. return e.length === 1 ? e[0] : e.join('~');
  61. }
  62. // 视频格式后缀列表
  63. const VIDEO_SUFFIX_LIST = ['.avi', '.mp4'];
  64. /**
  65. * 转换商品轮播的链接列表:根据链接的后缀,判断是视频链接还是图片链接
  66. *
  67. * @param {string[]} urlList 链接列表
  68. * @return {{src: string, type: 'video' | 'image' }[]} 转换后的链接列表
  69. */
  70. // export function formatGoodsSwiper(urlList) {
  71. // return (
  72. // urlList
  73. // ?.filter((url) => url)
  74. // .map((url, key) => {
  75. // const isVideo = VIDEO_SUFFIX_LIST.some((suffix) => url.includes(suffix));
  76. // const type = isVideo ? 'video' : 'image';
  77. // const src = $url.cdn(url);
  78. // return {
  79. // type,
  80. // src,
  81. // };
  82. // }) || []
  83. // );
  84. // }
  85. /**
  86. * 格式化订单状态的颜色
  87. *
  88. * @param order 订单
  89. * @return {string} 颜色的 class 名称
  90. */
  91. export function formatOrderColor(order) {
  92. if (order.status === 0) {
  93. return 'color-333';
  94. }
  95. if (order.status === 10 || order.status === 20 || (order.status === 30 && !order.commentStatus)) {
  96. return 'color-warning';
  97. }
  98. if (order.status === 30 && order.commentStatus) {
  99. return 'color-success';
  100. }
  101. return 'color-error';
  102. }
  103. /**
  104. * 格式化订单状态
  105. *
  106. * @param order 订单
  107. */
  108. export function formatOrderStatus(order) {
  109. if (order.status === 0) {
  110. return '待付款';
  111. }
  112. // if (order.status === 10 && order.items.every(k => k.spuType !== '0')) {
  113. // return '已完成'
  114. // }
  115. if (order.status === 10 && order.deliveryType === 1) {
  116. return '待发货';
  117. }
  118. if (order.status === 10 && order.deliveryType === 2) {
  119. return '待核销';
  120. }
  121. if (order.status === 20) {
  122. return '待收货';
  123. }
  124. if (order.status === 30 && !order.commentStatus) {
  125. return '待评价';
  126. }
  127. if (order.status === 30 && order.commentStatus) {
  128. return '已完成';
  129. }
  130. return '已关闭';
  131. }
  132. /**
  133. * 格式化订单状态的描述
  134. *
  135. * @param order 订单
  136. */
  137. export function formatOrderStatusDescription(order) {
  138. if (order.status === 0) {
  139. return `请在 ${formatDate(order.payExpireTime)} 前完成支付`;
  140. }
  141. if (order.status === 10) {
  142. return '商家未发货,请耐心等待';
  143. }
  144. if (order.status === 20) {
  145. return '商家已发货,请耐心等待';
  146. }
  147. if (order.status === 30 && !order.commentStatus) {
  148. return '已收货,快去评价一下吧';
  149. }
  150. if (order.status === 30 && order.commentStatus) {
  151. return '交易完成,感谢您的支持';
  152. }
  153. return '交易关闭';
  154. }
  155. /**
  156. * 处理订单的 button 操作按钮数组
  157. *
  158. * @param order 订单
  159. */
  160. export function handleOrderButtons(order) {
  161. order.buttons = [];
  162. if (order.type === 3) {
  163. // 查看拼团
  164. order.buttons.push('combination');
  165. }
  166. if (order.status === 20) {
  167. // 确认收货
  168. order.buttons.push('confirm');
  169. }
  170. if (order.logisticsId > 0) {
  171. // 查看物流
  172. order.buttons.push('express');
  173. }
  174. if (order.status === 0) {
  175. // 取消订单 / 发起支付
  176. order.buttons.push('cancel');
  177. order.buttons.push('pay');
  178. }
  179. if (order.status === 30 && !order.commentStatus) {
  180. // 发起评价
  181. order.buttons.push('comment');
  182. }
  183. if (order.status === 40) {
  184. // 删除订单
  185. order.buttons.push('delete');
  186. }
  187. }
  188. /**
  189. * 格式化售后状态
  190. *
  191. * @param afterSale 售后
  192. */
  193. export function formatAfterSaleStatus(afterSale) {
  194. if (afterSale.status === 10) {
  195. return '申请售后';
  196. }
  197. if (afterSale.status === 20) {
  198. return '商品待退货';
  199. }
  200. if (afterSale.status === 30) {
  201. return '商家待收货';
  202. }
  203. if (afterSale.status === 40) {
  204. return '等待退款';
  205. }
  206. if (afterSale.status === 50) {
  207. return '退款成功';
  208. }
  209. if (afterSale.status === 61) {
  210. return '买家取消';
  211. }
  212. if (afterSale.status === 62) {
  213. return '商家拒绝';
  214. }
  215. if (afterSale.status === 63) {
  216. return '商家拒收货';
  217. }
  218. return '未知状态';
  219. }
  220. /**
  221. * 格式化售后状态的描述
  222. *
  223. * @param afterSale 售后
  224. */
  225. export function formatAfterSaleStatusDescription(afterSale) {
  226. if (afterSale.status === 10) {
  227. return '退款申请待商家处理';
  228. }
  229. if (afterSale.status === 20) {
  230. return '请退货并填写物流信息';
  231. }
  232. if (afterSale.status === 30) {
  233. return '退货退款申请待商家处理';
  234. }
  235. if (afterSale.status === 40) {
  236. return '等待退款';
  237. }
  238. if (afterSale.status === 50) {
  239. return '退款成功';
  240. }
  241. if (afterSale.status === 61) {
  242. return '退款关闭';
  243. }
  244. if (afterSale.status === 62) {
  245. return `商家不同意退款申请,拒绝原因:${afterSale.auditReason}`;
  246. }
  247. if (afterSale.status === 63) {
  248. return `商家拒绝收货,不同意退款,拒绝原因:${afterSale.auditReason}`;
  249. }
  250. return '未知状态';
  251. }
  252. /**
  253. * 处理售后的 button 操作按钮数组
  254. *
  255. * @param afterSale 售后
  256. */
  257. export function handleAfterSaleButtons(afterSale) {
  258. afterSale.buttons = [];
  259. if ([10, 20, 30].includes(afterSale.status)) {
  260. // 取消订单
  261. afterSale.buttons.push('cancel');
  262. }
  263. if (afterSale.status === 20) {
  264. // 退货信息
  265. afterSale.buttons.push('delivery');
  266. }
  267. }
  268. /**
  269. * 倒计时
  270. * @param toTime 截止时间
  271. * @param fromTime 起始时间,默认当前时间
  272. * @return {{s: string, ms: number, h: string, m: string}} 持续时间
  273. */
  274. export function useDurationTime(toTime, fromTime = '') {
  275. toTime = getDayjsTime(toTime);
  276. if (fromTime === '') {
  277. fromTime = dayjs();
  278. }
  279. let duration = ref(toTime - fromTime);
  280. if (duration.value > 0) {
  281. setTimeout(() => {
  282. if (duration.value > 0) {
  283. duration.value -= 1000;
  284. }
  285. }, 1000);
  286. }
  287. let durationTime = dayjs.duration(duration.value);
  288. return {
  289. h: (durationTime.months() * 30 * 24 + durationTime.days() * 24 + durationTime.hours())
  290. .toString()
  291. .padStart(2, '0'),
  292. m: durationTime.minutes().toString().padStart(2, '0'),
  293. s: durationTime.seconds().toString().padStart(2, '0'),
  294. ms: durationTime.$ms,
  295. };
  296. }
  297. /**
  298. * 转换为 Dayjs
  299. * @param {any} time 时间
  300. * @return {dayjs.Dayjs}
  301. */
  302. function getDayjsTime(time) {
  303. time = time.toString();
  304. if (time.indexOf('-') > 0) {
  305. // 'date'
  306. return dayjs(time);
  307. }
  308. if (time.length > 10) {
  309. // 'timestamp'
  310. return dayjs(parseInt(time));
  311. }
  312. if (time.length === 10) {
  313. // 'unixTime'
  314. return dayjs.unix(parseInt(time));
  315. }
  316. }
  317. /**
  318. * 将分转成元
  319. *
  320. * @param price 分,例如说 100 分
  321. * @returns {string} 元,例如说 1.00 元
  322. */
  323. export function fen2yuan(price) {
  324. return (price / 100.0).toFixed(2);
  325. }
  326. /**
  327. * 将分转成元
  328. *
  329. * 如果没有小数点,则不展示小数点部分
  330. *
  331. * @param price 分,例如说 100 分
  332. * @returns {string} 元,例如说 1 元
  333. */
  334. export function fen2yuanSimple(price) {
  335. return fen2yuan(price).replace(/\.?0+$/, '');
  336. }
  337. /**
  338. * 将折扣百分比转化为“打x者”的 x 部分
  339. *
  340. * @param discountPercent
  341. */
  342. export function formatDiscountPercent(discountPercent) {
  343. return (discountPercent / 10.0).toFixed(1).replace(/\.?0+$/, '');
  344. }
  345. /**
  346. * 从商品 SKU 数组中,转换出商品属性的数组
  347. *
  348. * 类似结构:[{
  349. * id: // 属性的编号
  350. * name: // 属性的名字
  351. * values: [{
  352. * id: // 属性值的编号
  353. * name: // 属性值的名字
  354. * }]
  355. * }]
  356. *
  357. * @param skus 商品 SKU 数组
  358. */
  359. export function convertProductPropertyList(skus) {
  360. let result = [];
  361. for (const sku of skus) {
  362. if (!sku.properties) {
  363. continue;
  364. }
  365. for (const property of sku.properties) {
  366. // ① 先处理属性
  367. let resultProperty = result.find((item) => item.id === property.propertyId);
  368. if (!resultProperty) {
  369. resultProperty = {
  370. id: property.propertyId,
  371. name: property.propertyName,
  372. values: [],
  373. };
  374. result.push(resultProperty);
  375. }
  376. // ② 再处理属性值
  377. let resultValue = resultProperty.values.find((item) => item.id === property.valueId);
  378. if (!resultValue) {
  379. resultProperty.values.push({
  380. id: property.valueId,
  381. name: property.valueName,
  382. });
  383. }
  384. }
  385. }
  386. return result;
  387. }
  388. export function appendSettlementProduct(spus, settlementInfos) {
  389. if (!settlementInfos || settlementInfos.length === 0) {
  390. return;
  391. }
  392. for (const spu of spus) {
  393. const settlementInfo = settlementInfos.find((info) => info.spuId === spu.id);
  394. if (!settlementInfo) {
  395. return;
  396. }
  397. // 选择价格最小的 SKU 设置到 SPU 上
  398. const settlementSku = settlementInfo.skus
  399. .filter((sku) => sku.promotionPrice > 0)
  400. .reduce((prev, curr) => (prev.promotionPrice < curr.promotionPrice ? prev : curr), []);
  401. if (settlementSku) {
  402. spu.promotionType = settlementSku.promotionType;
  403. spu.promotionPrice = settlementSku.promotionPrice;
  404. }
  405. // 设置【满减送】活动
  406. if (settlementInfo.rewardActivity) {
  407. spu.rewardActivity = settlementInfo.rewardActivity;
  408. }
  409. }
  410. }
  411. // 获得满减送活动的规则描述(group)
  412. export function getRewardActivityRuleGroupDescriptions(activity) {
  413. if (!activity || !activity.rules || activity.rules.length === 0) {
  414. return [];
  415. }
  416. const result = [
  417. { name: '满减', values: [] },
  418. { name: '赠品', values: [] },
  419. { name: '包邮', values: [] },
  420. ];
  421. activity.rules.forEach((rule) => {
  422. const conditionTypeStr =
  423. activity.conditionType === 10 ? `满 ${fen2yuanSimple(rule.limit)} 元` : `满 ${rule.limit} 件`;
  424. // 满减
  425. if (rule.limit) {
  426. result[0].values.push(`${conditionTypeStr} 减 ${fen2yuanSimple(rule.discountPrice)} 元`);
  427. }
  428. // 赠品
  429. if (rule.point || (rule.giveCouponTemplateCounts && rule.giveCouponTemplateCounts.length > 0)) {
  430. let tips = [];
  431. if (rule.point) {
  432. tips.push(`送 ${rule.point} 积分`);
  433. }
  434. if (rule.giveCouponTemplateCounts && rule.giveCouponTemplateCounts.length > 0) {
  435. tips.push(`送 ${rule.giveCouponTemplateCounts.length} 张优惠券`);
  436. }
  437. result[1].values.push(`${conditionTypeStr} ${tips.join('、')}`);
  438. }
  439. // 包邮
  440. if (rule.freeDelivery) {
  441. result[2].values.push(`${conditionTypeStr} 包邮`);
  442. }
  443. });
  444. // 移除 values 为空的元素
  445. result.forEach((item) => {
  446. if (item.values.length === 0) {
  447. result.splice(result.indexOf(item), 1);
  448. }
  449. });
  450. return result;
  451. }
  452. // 获得满减送活动的规则描述(item)
  453. export function getRewardActivityRuleItemDescriptions(activity) {
  454. if (!activity || !activity.rules || activity.rules.length === 0) {
  455. return [];
  456. }
  457. const result = [];
  458. activity.rules.forEach((rule) => {
  459. const conditionTypeStr =
  460. activity.conditionType === 10 ? `满${fen2yuanSimple(rule.limit)}元` : `满${rule.limit}件`;
  461. // 满减
  462. if (rule.limit) {
  463. result.push(`${conditionTypeStr}减${fen2yuanSimple(rule.discountPrice)}元`);
  464. }
  465. // 赠品
  466. if (rule.point) {
  467. result.push(`${conditionTypeStr}送${rule.point}积分`);
  468. }
  469. if (rule.giveCouponTemplateCounts && rule.giveCouponTemplateCounts.length > 0) {
  470. result.push(`${conditionTypeStr}送${rule.giveCouponTemplateCounts.length}张优惠券`);
  471. }
  472. // 包邮
  473. if (rule.freeDelivery) {
  474. result.push(`${conditionTypeStr}包邮`);
  475. }
  476. });
  477. return result;
  478. }