useGoods.js 10 KB

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