goods.helper.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import { fetchProductDetail } from '@/services/api/goods.js'
  2. import { fetchCouponListByProductId } from '@/services/api/coupon.js'
  3. import store from '@/store/index.js'
  4. /* 获取产品活动类型 (拼团 活动价 限时特价) */
  5. export function generateActivityType(productData) {
  6. const { collageStatus = 0, activeStatus = 0, discountStatus = 0 } = productData
  7. // 拼团价
  8. if (collageStatus > 0) {
  9. return 'group' // 拼团价
  10. }
  11. // 限时活动
  12. else if (discountStatus > 0) {
  13. return 'time-limit'
  14. }
  15. // 普通活动价
  16. else if (activeStatus > 0) {
  17. return 'activity'
  18. }
  19. // 普通商品
  20. return 'normal' // 普通价
  21. }
  22. /* 获取产品价格类型 */
  23. export function generatePriceType(productData) {
  24. const { couponStatus = 0, collageStatus = 0, activeStatus = 0, discountStatus = 0, couponId } = productData
  25. // 拼团价
  26. if (collageStatus > 0) {
  27. if (couponStatus === 1 && couponId) {
  28. return 'groupWithCoupon' // 拼团券后价
  29. } else {
  30. return 'group' // 拼团价
  31. }
  32. }
  33. // 限时活动
  34. else if (discountStatus > 0 || activeStatus > 0) {
  35. if (couponStatus === 1 && couponId) {
  36. return 'activityWithCoupon' // 券后价
  37. } else {
  38. return 'normal' // 限时活动价格
  39. }
  40. }
  41. // 无活动价
  42. else {
  43. if (couponStatus === 1 && couponId) {
  44. return 'normalWithCoupon' // 普通券后价
  45. } else {
  46. return 'normal' // 普通价
  47. }
  48. }
  49. }
  50. /* 导航栏按钮类别 */
  51. const navbarButtonGroup = {
  52. // 仅拼团
  53. group: {
  54. left: ['单独购买', '¥1000.00'],
  55. right: ['拼团购买', '¥1000.00']
  56. },
  57. // 拼团 + 优惠券
  58. groupWithCoupon: {
  59. left: ['领券单独购买', '¥1000.00'],
  60. right: ['领券拼团购买', '¥1000.00']
  61. },
  62. // 限时活动 / 活动价 + 优惠券
  63. activityWithCoupon: {
  64. left: ['加入购物车'],
  65. right: ['领券购买', '¥1000.00']
  66. },
  67. // 限时活动 / 活动价 + 优惠券
  68. normalWithCoupon: {
  69. left: ['加入购物车'],
  70. right: ['领券购买', '¥1000.00']
  71. },
  72. // 普通方式 不使用优惠券
  73. normal: {
  74. left: ['加入购物车'],
  75. right: ['立即购买']
  76. }
  77. }
  78. /* 导航按钮文本 */
  79. export function generateNavbarButtonText(productData) {
  80. const { priceType } = productData
  81. const navbarButton = navbarButtonGroup[priceType]
  82. // 拼团券后价购买
  83. if (priceType === 'groupWithCoupon') {
  84. navbarButton.left[1] = `¥${productData.normalCouponPrice.toFixed(2)}`
  85. navbarButton.right[1] = `¥${productData.couponPrice.toFixed(2)}`
  86. }
  87. // 拼团价购买
  88. else if (priceType === 'group') {
  89. navbarButton.left[1] = `¥${productData.normalPrice.toFixed(2)}`
  90. navbarButton.right[1] = `¥${productData.price.toFixed(2)}`
  91. }
  92. // 活动价券后价购买(限时特价|普通活动)
  93. else if (priceType === 'activityWithCoupon') {
  94. navbarButton.right[1] = `¥${productData.couponPrice.toFixed(2)}`
  95. }
  96. // 普通价券后价购买
  97. else if (priceType === 'normalWithCoupon') {
  98. navbarButton.right[1] = `¥${productData.couponPrice.toFixed(2)}`
  99. } else {
  100. navbarButton.right[1] = ''
  101. }
  102. return navbarButton
  103. }
  104. /* 生成导航菜单类型 */
  105. export function generateNavbarType(productInfo) {
  106. const { couponStatus = 0, collageStatus = 0, activeStatus = 0, discountStatus = 0 } = productInfo
  107. // 拼团价
  108. if (collageStatus > 0) {
  109. if (couponStatus === 1) {
  110. return 'groupWithCoupon' // 拼团券后价
  111. } else {
  112. return 'group' // 拼团价
  113. }
  114. }
  115. // 限时活动
  116. else if (discountStatus > 0 || activeStatus > 0) {
  117. if (couponStatus === 1) {
  118. return 'activityWithCoupon' // 券后价
  119. } else {
  120. return 'normal' // 限时活动价格
  121. }
  122. }
  123. // 无活动价
  124. else {
  125. if (couponStatus === 1) {
  126. return 'normalWithCoupon' // 普通券后价
  127. } else {
  128. return 'normal' // 普通价
  129. }
  130. }
  131. }
  132. /* 处理商品信息 */
  133. function generateProductInfo(product) {
  134. if (!product) return product
  135. // 商品活动类型
  136. product.activityType = generateActivityType(product)
  137. product.priceType = generatePriceType(product)
  138. product.skus = product.skus.map(sku => {
  139. sku.activityType = generateActivityType(sku)
  140. sku.priceType = generatePriceType(sku)
  141. return sku
  142. })
  143. return product
  144. }
  145. /* 创建优惠券 */
  146. function generateCoupon(coupon) {
  147. const obj = Object.assign({}, coupon)
  148. // 添加标题
  149. if (coupon.noThresholdFlag > 0) {
  150. obj.couponTitle = `减¥${coupon.couponAmount}元`
  151. } else {
  152. obj.couponTitle = `满¥${coupon.touchPrice}元减¥${coupon.couponAmount}元`
  153. }
  154. // 添加优惠券状态
  155. if (obj.useStatus === 0) {
  156. obj.controlType = 'receive'
  157. } else if (obj.useStatus === 1) {
  158. obj.couponStatus = 'received'
  159. obj.controlType = 'search'
  160. }
  161. return obj
  162. }
  163. /* 获取商品详情 */
  164. export async function fetchPorductInfo(productId) {
  165. try {
  166. const userId = store.getters.userId
  167. const res = await fetchProductDetail({ productId, userId })
  168. return generateProductInfo(res.data)
  169. } catch (e) {
  170. console.log(e)
  171. }
  172. }
  173. /* 获取商品可用优惠券 */
  174. export async function fetchCouponListByProduct(productId) {
  175. try {
  176. const userId = store.getters.userId
  177. const res = await fetchCouponListByProductId({ productId, userId })
  178. return res.data.map(coupon => generateCoupon(coupon))
  179. } catch (e) {
  180. console.log(e)
  181. }
  182. }