import { fetchProductDetail } from '@/services/api/goods.js' import { fetchCouponListByProductId } from '@/services/api/coupon.js' import store from '@/store/index.js' /* 获取产品活动类型 (拼团 活动价 限时特价) */ export function generateActivityType(productData) { const { collageStatus = 0, activeStatus = 0, discountStatus = 0 } = productData // 拼团价 if (collageStatus > 0) { return 'group' // 拼团价 } // 限时活动 else if (discountStatus > 0) { return 'time-limit' } // 普通活动价 else if (activeStatus > 0) { return 'activity' } // 普通商品 return 'normal' // 普通价 } /* 获取产品价格类型 */ export function generatePriceType(productData) { const { couponStatus = 0, collageStatus = 0, activeStatus = 0, discountStatus = 0, couponId } = productData // 拼团价 if (collageStatus > 0) { if (couponStatus === 1 && couponId) { return 'groupWithCoupon' // 拼团券后价 } else { return 'group' // 拼团价 } } // 限时活动 else if (discountStatus > 0 || activeStatus > 0) { if (couponStatus === 1 && couponId) { return 'activityWithCoupon' // 券后价 } else { return 'normal' // 限时活动价格 } } // 无活动价 else { if (couponStatus === 1 && couponId) { return 'normalWithCoupon' // 普通券后价 } else { return 'normal' // 普通价 } } } /* 导航栏按钮类别 */ const navbarButtonGroup = { // 仅拼团 group: { left: ['单独购买', '¥1000.00'], right: ['拼团购买', '¥1000.00'] }, // 拼团 + 优惠券 groupWithCoupon: { left: ['领券单独购买', '¥1000.00'], right: ['领券拼团购买', '¥1000.00'] }, // 限时活动 / 活动价 + 优惠券 activityWithCoupon: { left: ['加入购物车'], right: ['领券购买', '¥1000.00'] }, // 限时活动 / 活动价 + 优惠券 normalWithCoupon: { left: ['加入购物车'], right: ['领券购买', '¥1000.00'] }, // 普通方式 不使用优惠券 normal: { left: ['加入购物车'], right: ['立即购买'] } } /* 导航按钮文本 */ export function generateNavbarButtonText(productData) { const { priceType } = productData const navbarButton = navbarButtonGroup[priceType] // 拼团券后价购买 if (priceType === 'groupWithCoupon') { navbarButton.left[1] = `¥${productData.normalCouponPrice.toFixed(2)}` navbarButton.right[1] = `¥${productData.couponPrice.toFixed(2)}` } // 拼团价购买 else if (priceType === 'group') { navbarButton.left[1] = `¥${productData.normalPrice.toFixed(2)}` navbarButton.right[1] = `¥${productData.price.toFixed(2)}` } // 活动价券后价购买(限时特价|普通活动) else if (priceType === 'activityWithCoupon') { navbarButton.right[1] = `¥${productData.couponPrice.toFixed(2)}` } // 普通价券后价购买 else if (priceType === 'normalWithCoupon') { navbarButton.right[1] = `¥${productData.couponPrice.toFixed(2)}` } else { navbarButton.right[1] = '' } return navbarButton } /* 生成导航菜单类型 */ export function generateNavbarType(productInfo) { const { couponStatus = 0, collageStatus = 0, activeStatus = 0, discountStatus = 0 } = productInfo // 拼团价 if (collageStatus > 0) { if (couponStatus === 1) { return 'groupWithCoupon' // 拼团券后价 } else { return 'group' // 拼团价 } } // 限时活动 else if (discountStatus > 0 || activeStatus > 0) { if (couponStatus === 1) { return 'activityWithCoupon' // 券后价 } else { return 'normal' // 限时活动价格 } } // 无活动价 else { if (couponStatus === 1) { return 'normalWithCoupon' // 普通券后价 } else { return 'normal' // 普通价 } } } /* 处理商品信息 */ function generateProductInfo(product) { if (!product) return product // 商品活动类型 product.activityType = generateActivityType(product) product.priceType = generatePriceType(product) product.skus = product.skus.map(sku => { sku.activityType = generateActivityType(sku) sku.priceType = generatePriceType(sku) return sku }) return product } /* 创建优惠券 */ function generateCoupon(coupon) { const obj = Object.assign({}, coupon) // 添加标题 if (coupon.noThresholdFlag > 0) { obj.couponTitle = `减¥${coupon.couponAmount}元` } else { obj.couponTitle = `满¥${coupon.touchPrice}元减¥${coupon.couponAmount}元` } // 添加优惠券状态 if (obj.useStatus === 0) { obj.controlType = 'receive' } else if (obj.useStatus === 1) { obj.couponStatus = 'received' obj.controlType = 'search' } return obj } /* 获取商品详情 */ export async function fetchPorductInfo(productId) { try { const userId = store.getters.userId const res = await fetchProductDetail({ productId, userId }) return generateProductInfo(res.data) } catch (e) { console.log(e) } } /* 获取商品可用优惠券 */ export async function fetchCouponListByProduct(productId) { try { const userId = store.getters.userId const res = await fetchCouponListByProductId({ productId, userId }) return res.data.map(coupon => generateCoupon(coupon)) } catch (e) { console.log(e) } }