useGoods.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { ref } from 'vue';
  2. import dayjs from 'dayjs';
  3. import $url from '@/sheep/url';
  4. // 格式化销量
  5. export function formatSales(type, num) {
  6. num = num + '';
  7. if (type === 'exact') {
  8. return '已售' + num;
  9. } else {
  10. if (num < 10) {
  11. return '销量≤10';
  12. } else {
  13. let a = Math.pow(10, num.length - 1);
  14. return '已售' + parseInt(num / a) * a + '+';
  15. }
  16. }
  17. }
  18. // 格式化兑换量
  19. export function formatExchange(type, num) {
  20. num = num + '';
  21. if (type === 'exact') {
  22. return '已兑换' + num;
  23. } else {
  24. if (num < 10) {
  25. return '已兑换≤10';
  26. } else {
  27. let a = Math.pow(10, num.length - 1);
  28. return '已兑换' + parseInt(num / a) * a + '+';
  29. }
  30. }
  31. }
  32. // 格式化库存
  33. export function formatStock(type, num) {
  34. num = num + '';
  35. if (type === 'exact') {
  36. return '库存' + num;
  37. } else {
  38. if (num < 10) {
  39. return '库存≤10';
  40. } else {
  41. let a = Math.pow(10, num.length - 1);
  42. return '库存 ' + parseInt(num / a) * a + '+';
  43. }
  44. }
  45. }
  46. // 格式化价格
  47. export function formatPrice(e) {
  48. return e.length === 1 ? e[0] : e.join('~');
  49. }
  50. // 格式化商品轮播
  51. export function formatGoodsSwiper(list) {
  52. let swiper = [];
  53. list.forEach((item, key) => {
  54. if (item.indexOf('.avi') !== -1 || item.indexOf('.mp4') !== -1) {
  55. swiper.push({
  56. src: $url.cdn(item),
  57. type: 'video',
  58. });
  59. } else {
  60. swiper.push({
  61. src: $url.cdn(item),
  62. type: 'image',
  63. });
  64. }
  65. });
  66. return swiper;
  67. }
  68. export function formatOrderColor(type) {
  69. if (
  70. type === 'apply_refund' ||
  71. type === 'groupon_ing' ||
  72. type === 'nocomment' ||
  73. type === 'noget' ||
  74. type === 'nosend'
  75. ) {
  76. return 'warning-color';
  77. } else if (
  78. type === 'closed' ||
  79. type === 'groupon_invalid' ||
  80. type === 'cancel' ||
  81. type === 'refund_agree'
  82. ) {
  83. return 'danger-color';
  84. } else if (type === 'completed') {
  85. return 'success-color';
  86. } else if (type === 'unpaid') {
  87. return 'info-color';
  88. }
  89. }
  90. // 计算相隔时间
  91. export function useDurationTime(toTime, fromTime = '') {
  92. toTime = getDayjsTime(toTime);
  93. if (fromTime === '') {
  94. fromTime = dayjs();
  95. }
  96. let duration = ref(toTime - fromTime);
  97. if (duration.value > 0) {
  98. setTimeout(() => {
  99. if (duration.value > 0) {
  100. duration.value -= 1000;
  101. }
  102. }, 1000);
  103. }
  104. let durationTime = dayjs.duration(duration.value);
  105. return {
  106. h: (durationTime.months() * 30 * 24 + durationTime.days() * 24 + durationTime.hours())
  107. .toString()
  108. .padStart(2, '0'),
  109. m: durationTime.minutes().toString().padStart(2, '0'),
  110. s: durationTime.seconds().toString().padStart(2, '0'),
  111. ms: durationTime.$ms,
  112. };
  113. }
  114. function getDayjsTime(time) {
  115. time = time.toString();
  116. if (time.indexOf('-') > 0) {
  117. // 'date'
  118. return dayjs(time);
  119. }
  120. if (time.length > 10) {
  121. // 'timestamp'
  122. return dayjs(parseInt(time));
  123. }
  124. if (time.length === 10) {
  125. // 'unixtime'
  126. return dayjs.unix(parseInt(time));
  127. }
  128. }