useGoods.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. *
  78. * @param order 订单
  79. * @return {string} 颜色的 class 名称
  80. */
  81. export function formatOrderColor(order) {
  82. if (order.status === 0) {
  83. return 'info-color';
  84. }
  85. if (order.status === 10
  86. || order.status === 20
  87. || (order.status === 30 && !order.commentStatus)) {
  88. return 'warning-color';
  89. }
  90. if (order.status === 30 && order.commentStatus) {
  91. return 'success-color';
  92. }
  93. return 'danger-color';
  94. }
  95. /**
  96. * 格式化订单状态
  97. *
  98. * @param order 订单
  99. */
  100. export function formatOrderStatus(order) {
  101. if (order.status === 0) {
  102. return '待付款';
  103. }
  104. if (order.status === 10 && order.deliveryType === 1) {
  105. return '待发货';
  106. }
  107. if (order.status === 10 && order.deliveryType === 2) {
  108. return '待核销';
  109. }
  110. if (order.status === 20) {
  111. return '待收货';
  112. }
  113. if (order.status === 30 && !order.commentStatus) {
  114. return '待评价';
  115. }
  116. if (order.status === 30 && order.commentStatus) {
  117. return '已完成';
  118. }
  119. return '已关闭';
  120. }
  121. /**
  122. * 处理订单的 button 操作按钮数组
  123. *
  124. * @param order 订单
  125. */
  126. export function handleOrderButtons(order) {
  127. order.buttons = []
  128. if (order.type === 3) { // 查看拼团
  129. order.buttons.push('combination');
  130. }
  131. if (order.status === 20) { // 确认收货
  132. order.buttons.push('confirm');
  133. }
  134. if (order.logisticsId > 0) { // 查看物流
  135. order.buttons.push('express');
  136. }
  137. if (order.status === 0) { // 取消订单 / 发起支付
  138. order.buttons.push('cancel');
  139. order.buttons.push('pay');
  140. }
  141. if (order.status === 30 && !order.commentStatus) { // 发起评价
  142. order.buttons.push('comment');
  143. }
  144. if (order.status === 40) { // 删除订单
  145. order.buttons.push('delete');
  146. }
  147. }
  148. /**
  149. * 倒计时
  150. * @param toTime 截止时间
  151. * @param fromTime 起始时间,默认当前时间
  152. * @return {{s: string, ms: number, h: string, m: string}} 持续时间
  153. */
  154. export function useDurationTime(toTime, fromTime = '') {
  155. toTime = getDayjsTime(toTime);
  156. if (fromTime === '') {
  157. fromTime = dayjs();
  158. }
  159. let duration = ref(toTime - fromTime);
  160. if (duration.value > 0) {
  161. setTimeout(() => {
  162. if (duration.value > 0) {
  163. duration.value -= 1000;
  164. }
  165. }, 1000);
  166. }
  167. let durationTime = dayjs.duration(duration.value);
  168. return {
  169. h: (durationTime.months() * 30 * 24 + durationTime.days() * 24 + durationTime.hours())
  170. .toString()
  171. .padStart(2, '0'),
  172. m: durationTime.minutes().toString().padStart(2, '0'),
  173. s: durationTime.seconds().toString().padStart(2, '0'),
  174. ms: durationTime.$ms,
  175. };
  176. }
  177. /**
  178. * 转换为 Dayjs
  179. * @param {any} time 时间
  180. * @return {dayjs.Dayjs}
  181. */
  182. function getDayjsTime(time) {
  183. time = time.toString();
  184. if (time.indexOf('-') > 0) {
  185. // 'date'
  186. return dayjs(time);
  187. }
  188. if (time.length > 10) {
  189. // 'timestamp'
  190. return dayjs(parseInt(time));
  191. }
  192. if (time.length === 10) {
  193. // 'unixTime'
  194. return dayjs.unix(parseInt(time));
  195. }
  196. }
  197. /**
  198. * 将分转成元
  199. *
  200. * @param price 分,例如说 100 分
  201. * @returns {string} 元,例如说 1.00 元
  202. */
  203. export function fen2yuan(price) {
  204. return (price / 100.0).toFixed(2)
  205. }
  206. /**
  207. * 从商品 SKU 数组中,转换出商品属性的数组
  208. *
  209. * 类似结构:[{
  210. * id: // 属性的编号
  211. * name: // 属性的名字
  212. * values: [{
  213. * id: // 属性值的编号
  214. * name: // 属性值的名字
  215. * }]
  216. * }]
  217. *
  218. * @param skus 商品 SKU 数组
  219. */
  220. export function convertProductPropertyList(skus) {
  221. let result = [];
  222. for (const sku of skus) {
  223. if (!sku.properties) {
  224. continue
  225. }
  226. for (const property of sku.properties) {
  227. // ① 先处理属性
  228. let resultProperty = result.find(item => item.id === property.propertyId)
  229. if (!resultProperty) {
  230. resultProperty = {
  231. id: property.propertyId,
  232. name: property.propertyName,
  233. values: []
  234. }
  235. result.push(resultProperty)
  236. }
  237. // ② 再处理属性值
  238. let resultValue = resultProperty.values.find(item => item.id === property.valueId)
  239. if (!resultValue) {
  240. resultProperty.values.push({
  241. id: property.valueId,
  242. name: property.valueName
  243. })
  244. }
  245. }
  246. }
  247. return result;
  248. }