useGoods.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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?.filter(url => url).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. // console.log(order.status,order.deliveryType);
  103. if (order.status === 0) {
  104. return '待付款';
  105. }
  106. // 快递发货 虚拟发货都走这条
  107. if (order.status === 10 && order.deliveryType === 1 || order.status === 10 && order.deliveryType === 3) {
  108. return '待发货';
  109. }
  110. if (order.status === 10 && order.deliveryType === 2) {
  111. return '待核销';
  112. }
  113. if (order.status === 20) {
  114. return '待收货';
  115. }
  116. if (order.status === 30 && !order.commentStatus) {
  117. return '待评价';
  118. }
  119. if (order.status === 30 && order.commentStatus) {
  120. return '已完成';
  121. }
  122. return '已关闭';
  123. }
  124. /**
  125. * 格式化订单状态的描述
  126. *
  127. * @param order 订单
  128. */
  129. export function formatOrderStatusDescription(order) {
  130. if (order.status === 0) {
  131. return `请在 ${ formatDate(order.payExpireTime) } 前完成支付`;
  132. }
  133. if (order.status === 10) {
  134. return '商家未发货,请耐心等待';
  135. }
  136. if (order.status === 20) {
  137. return '商家已发货,请耐心等待';
  138. }
  139. if (order.status === 30 && !order.commentStatus) {
  140. return '已收货,快去评价一下吧';
  141. }
  142. if (order.status === 30 && order.commentStatus) {
  143. return '交易完成,感谢您的支持';
  144. }
  145. return '交易关闭';
  146. }
  147. /**
  148. * 处理订单的 button 操作按钮数组
  149. *
  150. * @param order 订单
  151. */
  152. export function handleOrderButtons(order) {
  153. console.log()
  154. order.buttons = []
  155. if (order.type === 3) { // 查看拼团
  156. order.buttons.push('combination');
  157. }
  158. if (order.status === 20) { // 确认收货
  159. order.buttons.push('express');
  160. order.buttons.push('confirm');
  161. }
  162. if (order.logisticsId > 0) { // 查看物流
  163. order.buttons.push('express');
  164. }
  165. if (order.status === 0) { // 取消订单 / 发起支付
  166. order.buttons.push('cancel');
  167. order.buttons.push('pay');
  168. }
  169. if (order.status === 30 && !order.commentStatus) { // 发起评价
  170. order.buttons.push('comment');
  171. }
  172. if (order.status === 40) { // 删除订单
  173. order.buttons.push('delete');
  174. }
  175. if ([10, 20, 30].includes(order.items[0]?.refundStatus)) { // 取消订单
  176. order.buttons.push('aftersaleCancel');
  177. }
  178. // 如果订单是发起了售后,商家同意了。待用户发货回平台
  179. if (order.items[0]?.refundStatus === 20){
  180. order.buttons.push('aftersaleDelivery');
  181. }
  182. }
  183. /**
  184. * 格式化售后状态
  185. *
  186. * @param afterSale 售后
  187. */
  188. export function formatAfterSaleStatus(afterSale) {
  189. if (afterSale.status === 10) {
  190. return '申请售后';
  191. }
  192. if (afterSale.status === 20) {
  193. return '商品待退货';
  194. }
  195. if (afterSale.status === 30) {
  196. return '商家待收货';
  197. }
  198. if (afterSale.status === 40) {
  199. return '等待退款';
  200. }
  201. if (afterSale.status === 50) {
  202. return '退款成功';
  203. }
  204. if (afterSale.status === 61) {
  205. return '买家取消';
  206. }
  207. if (afterSale.status === 62) {
  208. return '商家拒绝';
  209. }
  210. if (afterSale.status === 63) {
  211. return '商家拒收货';
  212. }
  213. return '未知状态';
  214. }
  215. /**
  216. * 格式化售后状态的描述
  217. *
  218. * @param afterSale 售后
  219. */
  220. export function formatAfterSaleStatusDescription(afterSale) {
  221. if (afterSale.status === 10) {
  222. return '退款申请待商家处理';
  223. }
  224. if (afterSale.status === 20) {
  225. return '请退货并填写物流信息';
  226. }
  227. if (afterSale.status === 30) {
  228. return '退货退款申请待商家处理';
  229. }
  230. if (afterSale.status === 40) {
  231. return '等待退款';
  232. }
  233. if (afterSale.status === 50) {
  234. return '退款成功';
  235. }
  236. if (afterSale.status === 61) {
  237. return '退款关闭';
  238. }
  239. if (afterSale.status === 62) {
  240. return `商家不同意退款申请,拒绝原因:${afterSale.auditReason}`;
  241. }
  242. if (afterSale.status === 63) {
  243. return `商家拒绝收货,不同意退款,拒绝原因:${afterSale.auditReason}`;
  244. }
  245. return '未知状态';
  246. }
  247. /**
  248. * 处理售后的 button 操作按钮数组
  249. *
  250. * @param afterSale 售后
  251. */
  252. export function handleAfterSaleButtons(afterSale) {
  253. console.log(afterSale)
  254. afterSale.buttons = [];
  255. if ([10, 20, 30].includes(afterSale.status)) { // 取消订单
  256. afterSale.buttons.push('cancel');
  257. }
  258. if (afterSale.status === 20) { // 退货信息
  259. afterSale.buttons.push('delivery');
  260. }
  261. }
  262. /**
  263. * 倒计时
  264. * @param toTime 截止时间
  265. * @param fromTime 起始时间,默认当前时间
  266. * @return {{s: string, ms: number, h: string, m: string}} 持续时间
  267. */
  268. export function useDurationTime(toTime, fromTime = '') {
  269. toTime = getDayjsTime(toTime);
  270. if (fromTime === '') {
  271. fromTime = dayjs();
  272. }
  273. let duration = ref(toTime - fromTime);
  274. if (duration.value > 0) {
  275. setTimeout(() => {
  276. if (duration.value > 0) {
  277. duration.value -= 1000;
  278. }
  279. }, 1000);
  280. }
  281. let durationTime = dayjs.duration(duration.value);
  282. return {
  283. h: (durationTime.months() * 30 * 24 + durationTime.days() * 24 + durationTime.hours())
  284. .toString()
  285. .padStart(2, '0'),
  286. m: durationTime.minutes().toString().padStart(2, '0'),
  287. s: durationTime.seconds().toString().padStart(2, '0'),
  288. ms: durationTime.$ms,
  289. };
  290. }
  291. /**
  292. * 转换为 Dayjs
  293. * @param {any} time 时间
  294. * @return {dayjs.Dayjs}
  295. */
  296. function getDayjsTime(time) {
  297. time = time.toString();
  298. if (time.indexOf('-') > 0) {
  299. // 'date'
  300. return dayjs(time);
  301. }
  302. if (time.length > 10) {
  303. // 'timestamp'
  304. return dayjs(parseInt(time));
  305. }
  306. if (time.length === 10) {
  307. // 'unixTime'
  308. return dayjs.unix(parseInt(time));
  309. }
  310. }
  311. /**
  312. * 将分转成元
  313. *
  314. * @param price 分,例如说 100 分
  315. * @returns {string} 元,例如说 1.00 元
  316. */
  317. export function fen2yuan(price) {
  318. return (price / 100.0).toFixed(2)
  319. }
  320. /**
  321. * 将分转成元
  322. *
  323. * @param price 分,例如说 100 分
  324. * @returns {string} 元,例如说 1.000000 元
  325. */
  326. export function fen2yuan6(price) {
  327. return (price / 100.0).toFixed(6)
  328. }
  329. /**
  330. * 将后台佣金转换为可使用佣金
  331. *
  332. * @param point 分,例如说 1000000 分
  333. * @returns {string} 分,例如说 1.00 分
  334. */
  335. export function points2point(point) {
  336. return (point / 1000000.0).toFixed(3).slice(0, -1) // 干掉四舍五入 实际返回不四舍五入的两位小数
  337. }
  338. /**
  339. * 从商品 SKU 数组中,转换出商品属性的数组
  340. *
  341. * 类似结构:[{
  342. * id: // 属性的编号
  343. * name: // 属性的名字
  344. * values: [{
  345. * id: // 属性值的编号
  346. * name: // 属性值的名字
  347. * }]
  348. * }]
  349. *
  350. * @param skus 商品 SKU 数组
  351. */
  352. export function convertProductPropertyList(skus) {
  353. let result = [];
  354. for (const sku of skus) {
  355. if (!sku.properties) {
  356. continue
  357. }
  358. for (const property of sku.properties) {
  359. // ① 先处理属性
  360. let resultProperty = result.find(item => item.id === property.propertyId)
  361. if (!resultProperty) {
  362. resultProperty = {
  363. id: property.propertyId,
  364. name: property.propertyName,
  365. values: []
  366. }
  367. result.push(resultProperty)
  368. }
  369. // ② 再处理属性值
  370. let resultValue = resultProperty.values.find(item => item.id === property.valueId)
  371. if (!resultValue) {
  372. resultProperty.values.push({
  373. id: property.valueId,
  374. name: property.valueName
  375. })
  376. }
  377. }
  378. }
  379. return result;
  380. }
  381. /**
  382. * 格式化满减送活动的规则
  383. *
  384. * @param activity 活动信息
  385. * @param rule 优惠规格
  386. * @returns {string} 规格字符串
  387. */
  388. export function formatRewardActivityRule(activity, rule) {
  389. if (activity.conditionType === 10) {
  390. return `满 ${fen2yuan(rule.limit)} 元减 ${fen2yuan(rule.discountPrice)} 元`;
  391. }
  392. if (activity.conditionType === 20) {
  393. return `满 ${rule.limit} 件减 ${fen2yuan(rule.discountPrice)} 元`;
  394. }
  395. return '';
  396. }