useGoods.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import { ref } from 'vue';
  2. import dayjs from 'dayjs';
  3. import $url from '@/sheep/url';
  4. /**
  5. * 格式化销量
  6. * @param {'exact' | string} type 格式类型:exact=精确值,其它=大致数量
  7. * @param {number} num 销量
  8. * @return {string} 格式化后的销量字符串
  9. */
  10. export function formatSales(type, num) {
  11. let prefix = type!=='exact' && num<10 ? '销量': '已售';
  12. return formatNum(prefix, type, num)
  13. }
  14. /**
  15. * 格式化兑换量
  16. * @param {'exact' | string} type 格式类型:exact=精确值,其它=大致数量
  17. * @param {number} num 销量
  18. * @return {string} 格式化后的销量字符串
  19. */
  20. export function formatExchange(type, num) {
  21. return formatNum('已兑换', type, num)
  22. }
  23. /**
  24. * 格式化库存
  25. * @param {'exact' | any} type 格式类型:exact=精确值,其它=大致数量
  26. * @param {number} num 销量
  27. * @return {string} 格式化后的销量字符串
  28. */
  29. export function formatStock(type, num) {
  30. return formatNum('库存', type, num)
  31. }
  32. /**
  33. * 格式化数字
  34. * @param {string} prefix 前缀
  35. * @param {'exact' | string} type 格式类型:exact=精确值,其它=大致数量
  36. * @param {number} num 销量
  37. * @return {string} 格式化后的销量字符串
  38. */
  39. export function formatNum(prefix, type, num) {
  40. num = (num || 0);
  41. // 情况一:精确数值
  42. if (type === 'exact') {
  43. return prefix + num;
  44. }
  45. // 情况二:小于等于 10
  46. if (num < 10) {
  47. return `${prefix}≤10`;
  48. }
  49. // 情况三:大于 10,除第一位外,其它位都显示为0
  50. // 例如:100 - 199 显示为 100+
  51. // 9000 - 9999 显示为 9000+
  52. let pow = Math.pow(10, `${num}`.length - 1);
  53. return `${prefix}${(num / pow) * pow}+`;
  54. }
  55. // 格式化价格
  56. export function formatPrice(e) {
  57. return e.length === 1 ? e[0] : e.join('~');
  58. }
  59. // 视频格式后缀列表
  60. const VIDEO_SUFFIX_LIST = ['.avi', '.mp4']
  61. /**
  62. * 转换商品轮播的链接列表:根据链接的后缀,判断是视频链接还是图片链接
  63. *
  64. * @param {string[]} urlList 链接列表
  65. * @return {{src: string, type: 'video' | 'image' }[]} 转换后的链接列表
  66. */
  67. export function formatGoodsSwiper(urlList) {
  68. return urlList.map((url, key) => {
  69. const isVideo = VIDEO_SUFFIX_LIST.some(suffix => url.includes(suffix));
  70. const type = isVideo ? 'video' :'image'
  71. const src = $url.cdn(url);
  72. return { type, src }
  73. });
  74. }
  75. /**
  76. * 格式化订单状态的颜色
  77. * @param type 订单类型
  78. * @return {string} 颜色的 class 名称
  79. */
  80. export function formatOrderColor(type) {
  81. switch (type) {
  82. case 'apply_refund':
  83. case 'groupon_ing':
  84. case 'nocomment':
  85. case 'noget':
  86. case 'nosend':
  87. return 'warning-color';
  88. case 'closed':
  89. case 'groupon_invalid':
  90. case 'cancel':
  91. case 'refund_agree':
  92. return 'danger-color';
  93. case 'completed':
  94. return 'success-color';
  95. case 'unpaid':
  96. return 'info-color';
  97. }
  98. }
  99. /**
  100. * 倒计时
  101. * @param toTime 截止时间
  102. * @param fromTime 起始时间,默认当前时间
  103. * @return {{s: string, ms: number, h: string, m: string}} 持续时间
  104. */
  105. export function useDurationTime(toTime, fromTime = '') {
  106. toTime = getDayjsTime(toTime);
  107. if (fromTime === '') {
  108. fromTime = dayjs();
  109. }
  110. let duration = ref(toTime - fromTime);
  111. if (duration.value > 0) {
  112. setTimeout(() => {
  113. if (duration.value > 0) {
  114. duration.value -= 1000;
  115. }
  116. }, 1000);
  117. }
  118. let durationTime = dayjs.duration(duration.value);
  119. return {
  120. h: (durationTime.months() * 30 * 24 + durationTime.days() * 24 + durationTime.hours())
  121. .toString()
  122. .padStart(2, '0'),
  123. m: durationTime.minutes().toString().padStart(2, '0'),
  124. s: durationTime.seconds().toString().padStart(2, '0'),
  125. ms: durationTime.$ms,
  126. };
  127. }
  128. /**
  129. * 转换为 Dayjs
  130. * @param {any} time 时间
  131. * @return {dayjs.Dayjs}
  132. */
  133. function getDayjsTime(time) {
  134. time = time.toString();
  135. if (time.indexOf('-') > 0) {
  136. // 'date'
  137. return dayjs(time);
  138. }
  139. if (time.length > 10) {
  140. // 'timestamp'
  141. return dayjs(parseInt(time));
  142. }
  143. if (time.length === 10) {
  144. // 'unixTime'
  145. return dayjs.unix(parseInt(time));
  146. }
  147. }