useGoods.js 10 KB

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