useGoods.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. const numStr = num.toString();
  54. const first = numStr[0];
  55. const other = '0'.repeat(numStr.length - 1);
  56. return `${prefix}${first}${other}+`;
  57. }
  58. // 格式化价格
  59. export function formatPrice(e) {
  60. return e.length === 1 ? e[0] : e.join('~');
  61. }
  62. // 视频格式后缀列表
  63. const VIDEO_SUFFIX_LIST = ['.avi', '.mp4'];
  64. /**
  65. * 转换商品轮播的链接列表:根据链接的后缀,判断是视频链接还是图片链接
  66. *
  67. * @param {string[]} urlList 链接列表
  68. * @return {{src: string, type: 'video' | 'image' }[]} 转换后的链接列表
  69. */
  70. export function formatGoodsSwiper(urlList) {
  71. return (
  72. urlList
  73. ?.filter((url) => url)
  74. .map((url, key) => {
  75. const isVideo = VIDEO_SUFFIX_LIST.some((suffix) => url.includes(suffix));
  76. const type = isVideo ? 'video' : 'image';
  77. const src = $url.cdn(url);
  78. return {
  79. type,
  80. src,
  81. };
  82. }) || []
  83. );
  84. }
  85. /**
  86. * 格式化订单状态的颜色
  87. *
  88. * @param order 订单
  89. * @return {string} 颜色的 class 名称
  90. */
  91. export function formatOrderColor(order) {
  92. if (order.status === 0) {
  93. return 'info-color';
  94. }
  95. if (order.status === 10 || order.status === 20 || (order.status === 30 && !order.commentStatus)) {
  96. return 'warning-color';
  97. }
  98. if (order.status === 30 && order.commentStatus) {
  99. return 'success-color';
  100. }
  101. return 'danger-color';
  102. }
  103. /**
  104. * 格式化订单状态
  105. *
  106. * @param order 订单
  107. */
  108. export function formatOrderStatus(order) {
  109. if (order.status === 0) {
  110. return '待付款';
  111. }
  112. if (order.status === 10 && order.deliveryType === 1) {
  113. return '待发货';
  114. }
  115. if (order.status === 10 && order.deliveryType === 2) {
  116. return '待核销';
  117. }
  118. if (order.status === 20) {
  119. return '待收货';
  120. }
  121. if (order.status === 30 && !order.commentStatus) {
  122. return '待评价';
  123. }
  124. if (order.status === 30 && order.commentStatus) {
  125. return '已完成';
  126. }
  127. return '已关闭';
  128. }
  129. /**
  130. * 格式化订单状态的描述
  131. *
  132. * @param order 订单
  133. */
  134. export function formatOrderStatusDescription(order) {
  135. if (order.status === 0) {
  136. return `请在 ${formatDate(order.payExpireTime)} 前完成支付`;
  137. }
  138. if (order.status === 10) {
  139. return '商家未发货,请耐心等待';
  140. }
  141. if (order.status === 20) {
  142. return '商家已发货,请耐心等待';
  143. }
  144. if (order.status === 30 && !order.commentStatus) {
  145. return '已收货,快去评价一下吧';
  146. }
  147. if (order.status === 30 && order.commentStatus) {
  148. return '交易完成,感谢您的支持';
  149. }
  150. return '交易关闭';
  151. }
  152. /**
  153. * 处理订单的 button 操作按钮数组
  154. *
  155. * @param order 订单
  156. */
  157. export function handleOrderButtons(order) {
  158. order.buttons = [];
  159. if (order.type === 3) {
  160. // 查看拼团
  161. order.buttons.push('combination');
  162. }
  163. if (order.status === 20) {
  164. // 确认收货
  165. order.buttons.push('confirm');
  166. }
  167. if (order.logisticsId > 0) {
  168. // 查看物流
  169. order.buttons.push('express');
  170. }
  171. if (order.status === 0) {
  172. // 取消订单 / 发起支付
  173. order.buttons.push('cancel');
  174. order.buttons.push('pay');
  175. }
  176. if (order.status === 30 && !order.commentStatus) {
  177. // 发起评价
  178. order.buttons.push('comment');
  179. }
  180. if (order.status === 40) {
  181. // 删除订单
  182. order.buttons.push('delete');
  183. }
  184. }
  185. /**
  186. * 格式化售后状态
  187. *
  188. * @param afterSale 售后
  189. */
  190. export function formatAfterSaleStatus(afterSale) {
  191. if (afterSale.status === 10) {
  192. return '申请售后';
  193. }
  194. if (afterSale.status === 20) {
  195. return '商品待退货';
  196. }
  197. if (afterSale.status === 30) {
  198. return '商家待收货';
  199. }
  200. if (afterSale.status === 40) {
  201. return '等待退款';
  202. }
  203. if (afterSale.status === 50) {
  204. return '退款成功';
  205. }
  206. if (afterSale.status === 61) {
  207. return '买家取消';
  208. }
  209. if (afterSale.status === 62) {
  210. return '商家拒绝';
  211. }
  212. if (afterSale.status === 63) {
  213. return '商家拒收货';
  214. }
  215. return '未知状态';
  216. }
  217. /**
  218. * 格式化售后状态的描述
  219. *
  220. * @param afterSale 售后
  221. */
  222. export function formatAfterSaleStatusDescription(afterSale) {
  223. if (afterSale.status === 10) {
  224. return '退款申请待商家处理';
  225. }
  226. if (afterSale.status === 20) {
  227. return '请退货并填写物流信息';
  228. }
  229. if (afterSale.status === 30) {
  230. return '退货退款申请待商家处理';
  231. }
  232. if (afterSale.status === 40) {
  233. return '等待退款';
  234. }
  235. if (afterSale.status === 50) {
  236. return '退款成功';
  237. }
  238. if (afterSale.status === 61) {
  239. return '退款关闭';
  240. }
  241. if (afterSale.status === 62) {
  242. return `商家不同意退款申请,拒绝原因:${afterSale.auditReason}`;
  243. }
  244. if (afterSale.status === 63) {
  245. return `商家拒绝收货,不同意退款,拒绝原因:${afterSale.auditReason}`;
  246. }
  247. return '未知状态';
  248. }
  249. /**
  250. * 处理售后的 button 操作按钮数组
  251. *
  252. * @param afterSale 售后
  253. */
  254. export function handleAfterSaleButtons(afterSale) {
  255. afterSale.buttons = [];
  256. if ([10, 20, 30].includes(afterSale.status)) {
  257. // 取消订单
  258. afterSale.buttons.push('cancel');
  259. }
  260. if (afterSale.status === 20) {
  261. // 退货信息
  262. afterSale.buttons.push('delivery');
  263. }
  264. }
  265. /**
  266. * 倒计时
  267. * @param toTime 截止时间
  268. * @param fromTime 起始时间,默认当前时间
  269. * @return {{s: string, ms: number, h: string, m: string}} 持续时间
  270. */
  271. export function useDurationTime(toTime, fromTime = '') {
  272. toTime = getDayjsTime(toTime);
  273. if (fromTime === '') {
  274. fromTime = dayjs();
  275. }
  276. let duration = ref(toTime - fromTime);
  277. if (duration.value > 0) {
  278. setTimeout(() => {
  279. if (duration.value > 0) {
  280. duration.value -= 1000;
  281. }
  282. }, 1000);
  283. }
  284. let durationTime = dayjs.duration(duration.value);
  285. return {
  286. h: (durationTime.months() * 30 * 24 + durationTime.days() * 24 + durationTime.hours())
  287. .toString()
  288. .padStart(2, '0'),
  289. m: durationTime.minutes().toString().padStart(2, '0'),
  290. s: durationTime.seconds().toString().padStart(2, '0'),
  291. ms: durationTime.$ms,
  292. };
  293. }
  294. /**
  295. * 转换为 Dayjs
  296. * @param {any} time 时间
  297. * @return {dayjs.Dayjs}
  298. */
  299. function getDayjsTime(time) {
  300. time = time.toString();
  301. if (time.indexOf('-') > 0) {
  302. // 'date'
  303. return dayjs(time);
  304. }
  305. if (time.length > 10) {
  306. // 'timestamp'
  307. return dayjs(parseInt(time));
  308. }
  309. if (time.length === 10) {
  310. // 'unixTime'
  311. return dayjs.unix(parseInt(time));
  312. }
  313. }
  314. /**
  315. * 将分转成元
  316. *
  317. * @param price 分,例如说 100 分
  318. * @returns {string} 元,例如说 1.00 元
  319. */
  320. export function fen2yuan(price) {
  321. return (price / 100.0).toFixed(2);
  322. }
  323. /**
  324. * 将分转成元
  325. *
  326. * 如果没有小数点,则不展示小数点部分
  327. *
  328. * @param price 分,例如说 100 分
  329. * @returns {string} 元,例如说 1 元
  330. */
  331. export function fen2yuanSimple(price) {
  332. return fen2yuan(price).replace(/\.?0+$/, '');
  333. }
  334. /**
  335. * 将折扣百分比转化为“打x者”的 x 部分
  336. *
  337. * @param discountPercent
  338. */
  339. export function formatDiscountPercent(discountPercent) {
  340. return (discountPercent / 10.0).toFixed(1).replace(/\.?0+$/, '');
  341. }
  342. /**
  343. * 从商品 SKU 数组中,转换出商品属性的数组
  344. *
  345. * 类似结构:[{
  346. * id: // 属性的编号
  347. * name: // 属性的名字
  348. * values: [{
  349. * id: // 属性值的编号
  350. * name: // 属性值的名字
  351. * }]
  352. * }]
  353. *
  354. * @param skus 商品 SKU 数组
  355. */
  356. export function convertProductPropertyList(skus) {
  357. let result = [];
  358. for (const sku of skus) {
  359. if (!sku.properties) {
  360. continue;
  361. }
  362. for (const property of sku.properties) {
  363. // ① 先处理属性
  364. let resultProperty = result.find((item) => item.id === property.propertyId);
  365. if (!resultProperty) {
  366. resultProperty = {
  367. id: property.propertyId,
  368. name: property.propertyName,
  369. values: [],
  370. };
  371. result.push(resultProperty);
  372. }
  373. // ② 再处理属性值
  374. let resultValue = resultProperty.values.find((item) => item.id === property.valueId);
  375. if (!resultValue) {
  376. resultProperty.values.push({
  377. id: property.valueId,
  378. name: property.valueName,
  379. });
  380. }
  381. }
  382. }
  383. return result;
  384. }
  385. export function appendSettlementProduct(spus, settlementInfos) {
  386. if (!settlementInfos || settlementInfos.length === 0) {
  387. return;
  388. }
  389. for (const spu of spus) {
  390. const settlementInfo = settlementInfos.find((info) => info.spuId === spu.id);
  391. if (!settlementInfo) {
  392. return;
  393. }
  394. // 选择价格最小的 SKU 设置到 SPU 上
  395. const settlementSku = settlementInfo.skus
  396. .filter((sku) => sku.promotionPrice > 0)
  397. .reduce((prev, curr) => (prev.promotionPrice < curr.promotionPrice ? prev : curr), []);
  398. if (settlementSku) {
  399. spu.promotionType = settlementSku.promotionType;
  400. spu.promotionPrice = settlementSku.promotionPrice;
  401. }
  402. // 设置【满减送】活动
  403. if (settlementInfo.rewardActivity) {
  404. spu.rewardActivity = settlementInfo.rewardActivity;
  405. }
  406. }
  407. }
  408. // 获得满减送活动的规则描述(group)
  409. export function getRewardActivityRuleGroupDescriptions(activity) {
  410. if (!activity || !activity.rules || activity.rules.length === 0) {
  411. return [];
  412. }
  413. const result = [
  414. { name: '满减', values: [] },
  415. { name: '赠品', values: [] },
  416. { name: '包邮', values: [] },
  417. ];
  418. activity.rules.forEach((rule) => {
  419. const conditionTypeStr =
  420. activity.conditionType === 10 ? `满 ${fen2yuanSimple(rule.limit)} 元` : `满 ${rule.limit} 件`;
  421. // 满减
  422. if (rule.limit) {
  423. result[0].values.push(`${conditionTypeStr} 减 ${fen2yuanSimple(rule.discountPrice)} 元`);
  424. }
  425. // 赠品
  426. if (rule.point || (rule.giveCouponTemplateCounts && rule.giveCouponTemplateCounts.length > 0)) {
  427. let tips = [];
  428. if (rule.point) {
  429. tips.push(`送 ${rule.point} 积分`);
  430. }
  431. if (rule.giveCouponTemplateCounts && rule.giveCouponTemplateCounts.length > 0) {
  432. tips.push(`送 ${rule.giveCouponTemplateCounts.length} 张优惠券`);
  433. }
  434. result[1].values.push(`${conditionTypeStr} ${tips.join('、')}`);
  435. }
  436. // 包邮
  437. if (rule.freeDelivery) {
  438. result[2].values.push(`${conditionTypeStr} 包邮`);
  439. }
  440. });
  441. // 移除 values 为空的元素
  442. result.forEach((item) => {
  443. if (item.values.length === 0) {
  444. result.splice(result.indexOf(item), 1);
  445. }
  446. });
  447. return result;
  448. }
  449. // 获得满减送活动的规则描述(item)
  450. export function getRewardActivityRuleItemDescriptions(activity) {
  451. if (!activity || !activity.rules || activity.rules.length === 0) {
  452. return [];
  453. }
  454. const result = [];
  455. activity.rules.forEach((rule) => {
  456. const conditionTypeStr =
  457. activity.conditionType === 10 ? `满${fen2yuanSimple(rule.limit)}元` : `满${rule.limit}件`;
  458. // 满减
  459. if (rule.limit) {
  460. result.push(`${conditionTypeStr}减${fen2yuanSimple(rule.discountPrice)}元`);
  461. }
  462. // 赠品
  463. if (rule.point) {
  464. result.push(`${conditionTypeStr}送${rule.point}积分`);
  465. }
  466. if (rule.giveCouponTemplateCounts && rule.giveCouponTemplateCounts.length > 0) {
  467. result.push(`${conditionTypeStr}送${rule.giveCouponTemplateCounts.length}张优惠券`);
  468. }
  469. // 包邮
  470. if (rule.freeDelivery) {
  471. result.push(`${conditionTypeStr}包邮`);
  472. }
  473. });
  474. return result;
  475. }