useGoods.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. import { ref } from 'vue';
  2. import dayjs from 'dayjs';
  3. import $url from '@/sheep/url';
  4. import { formatDate } from '@/sheep/util';
  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. let pow = Math.pow(10, `${num}`.length - 1);
  54. return `${prefix}${(num / pow) * pow}+`;
  55. }
  56. // 格式化价格
  57. export function formatPrice(e) {
  58. return e.length === 1 ? e[0] : e.join('~');
  59. }
  60. // 视频格式后缀列表
  61. const VIDEO_SUFFIX_LIST = ['.avi', '.mp4']
  62. /**
  63. * 转换商品轮播的链接列表:根据链接的后缀,判断是视频链接还是图片链接
  64. *
  65. * @param {string[]} urlList 链接列表
  66. * @return {{src: string, type: 'video' | 'image' }[]} 转换后的链接列表
  67. */
  68. export function formatGoodsSwiper(urlList) {
  69. return urlList.map((url, key) => {
  70. const isVideo = VIDEO_SUFFIX_LIST.some(suffix => url.includes(suffix));
  71. const type = isVideo ? 'video' : 'image'
  72. const src = $url.cdn(url);
  73. return { type, src }
  74. });
  75. }
  76. /**
  77. * 格式化订单状态的颜色
  78. *
  79. * @param order 订单
  80. * @return {string} 颜色的 class 名称
  81. */
  82. export function formatOrderColor(order) {
  83. if (order.status === 0) {
  84. return 'info-color';
  85. }
  86. if (order.status === 10
  87. || order.status === 20
  88. || (order.status === 30 && !order.commentStatus)) {
  89. return 'warning-color';
  90. }
  91. if (order.status === 30 && order.commentStatus) {
  92. return 'success-color';
  93. }
  94. return 'danger-color';
  95. }
  96. /**
  97. * 格式化订单状态
  98. *
  99. * @param order 订单
  100. */
  101. export function formatOrderStatus(order) {
  102. if (order.status === 0) {
  103. return '待付款';
  104. }
  105. if (order.status === 10 && order.deliveryType === 1) {
  106. return '待发货';
  107. }
  108. if (order.status === 10 && order.deliveryType === 2) {
  109. return '待核销';
  110. }
  111. if (order.status === 20) {
  112. return '待收货';
  113. }
  114. if (order.status === 30 && !order.commentStatus) {
  115. return '待评价';
  116. }
  117. if (order.status === 30 && order.commentStatus) {
  118. return '已完成';
  119. }
  120. return '已关闭';
  121. }
  122. /**
  123. * 格式化订单状态的描述
  124. *
  125. * @param order 订单
  126. */
  127. export function formatOrderStatusDescription(order) {
  128. if (order.status === 0) {
  129. return `请在 ${ formatDate(orderInfo.payExpireTime) } 前完成支付`;
  130. }
  131. if (order.status === 10) {
  132. return '商家未发货,请耐心等待';
  133. }
  134. if (order.status === 20) {
  135. return '商家已发货,请耐心等待';
  136. }
  137. if (order.status === 30 && !order.commentStatus) {
  138. return '已收货,快去评价一下吧';
  139. }
  140. if (order.status === 30 && order.commentStatus) {
  141. return '交易完成,感谢您的支持';
  142. }
  143. return '交易关闭';
  144. }
  145. /**
  146. * 处理订单的 button 操作按钮数组
  147. *
  148. * @param order 订单
  149. */
  150. export function handleOrderButtons(order) {
  151. order.buttons = []
  152. if (order.type === 3) { // 查看拼团
  153. order.buttons.push('combination');
  154. }
  155. if (order.status === 20) { // 确认收货
  156. order.buttons.push('confirm');
  157. }
  158. if (order.logisticsId > 0) { // 查看物流
  159. order.buttons.push('express');
  160. }
  161. if (order.status === 0) { // 取消订单 / 发起支付
  162. order.buttons.push('cancel');
  163. order.buttons.push('pay');
  164. }
  165. if (order.status === 30 && !order.commentStatus) { // 发起评价
  166. order.buttons.push('comment');
  167. }
  168. if (order.status === 40) { // 删除订单
  169. order.buttons.push('delete');
  170. }
  171. }
  172. /**
  173. * 倒计时
  174. * @param toTime 截止时间
  175. * @param fromTime 起始时间,默认当前时间
  176. * @return {{s: string, ms: number, h: string, m: string}} 持续时间
  177. */
  178. export function useDurationTime(toTime, fromTime = '') {
  179. toTime = getDayjsTime(toTime);
  180. if (fromTime === '') {
  181. fromTime = dayjs();
  182. }
  183. let duration = ref(toTime - fromTime);
  184. if (duration.value > 0) {
  185. setTimeout(() => {
  186. if (duration.value > 0) {
  187. duration.value -= 1000;
  188. }
  189. }, 1000);
  190. }
  191. let durationTime = dayjs.duration(duration.value);
  192. return {
  193. h: (durationTime.months() * 30 * 24 + durationTime.days() * 24 + durationTime.hours())
  194. .toString()
  195. .padStart(2, '0'),
  196. m: durationTime.minutes().toString().padStart(2, '0'),
  197. s: durationTime.seconds().toString().padStart(2, '0'),
  198. ms: durationTime.$ms,
  199. };
  200. }
  201. /**
  202. * 转换为 Dayjs
  203. * @param {any} time 时间
  204. * @return {dayjs.Dayjs}
  205. */
  206. function getDayjsTime(time) {
  207. time = time.toString();
  208. if (time.indexOf('-') > 0) {
  209. // 'date'
  210. return dayjs(time);
  211. }
  212. if (time.length > 10) {
  213. // 'timestamp'
  214. return dayjs(parseInt(time));
  215. }
  216. if (time.length === 10) {
  217. // 'unixTime'
  218. return dayjs.unix(parseInt(time));
  219. }
  220. }
  221. /**
  222. * 将分转成元
  223. *
  224. * @param price 分,例如说 100 分
  225. * @returns {string} 元,例如说 1.00 元
  226. */
  227. export function fen2yuan(price) {
  228. return (price / 100.0).toFixed(2)
  229. }
  230. /**
  231. * 从商品 SKU 数组中,转换出商品属性的数组
  232. *
  233. * 类似结构:[{
  234. * id: // 属性的编号
  235. * name: // 属性的名字
  236. * values: [{
  237. * id: // 属性值的编号
  238. * name: // 属性值的名字
  239. * }]
  240. * }]
  241. *
  242. * @param skus 商品 SKU 数组
  243. */
  244. export function convertProductPropertyList(skus) {
  245. let result = [];
  246. for (const sku of skus) {
  247. if (!sku.properties) {
  248. continue
  249. }
  250. for (const property of sku.properties) {
  251. // ① 先处理属性
  252. let resultProperty = result.find(item => item.id === property.propertyId)
  253. if (!resultProperty) {
  254. resultProperty = {
  255. id: property.propertyId,
  256. name: property.propertyName,
  257. values: []
  258. }
  259. result.push(resultProperty)
  260. }
  261. // ② 再处理属性值
  262. let resultValue = resultProperty.values.find(item => item.id === property.valueId)
  263. if (!resultValue) {
  264. resultProperty.values.push({
  265. id: property.valueId,
  266. name: property.valueName
  267. })
  268. }
  269. }
  270. }
  271. return result;
  272. }