| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- import { resolveFileUrl } from '@/utils/image'
- import { formatPrice } from '@/utils/format'
- import {
- CART_INVALID_NONE,
- CART_INVALID_OUT_OF_STOCK,
- CART_INVALID_OFF_SHELF,
- CART_INVALID_SHOP_CLOSED,
- CART_INVALID_CATEGORY_HIDDEN,
- CART_INVALID_GOODS_DELETED,
- CART_INVALID_SHOP_DELETED
- } from '@/constants/cart'
- import { SHOP_STATUS_CLOSED } from '@/constants/shop'
- import { parseCartSpecText } from '@/utils/cartSpec'
- const GOODS_PLACEHOLDER = '/static/logo.png'
- const INVALID_LABEL_MAP = {
- [CART_INVALID_OUT_OF_STOCK]: '缺货',
- [CART_INVALID_OFF_SHELF]: '已下架',
- [CART_INVALID_SHOP_CLOSED]: '休息中',
- [CART_INVALID_CATEGORY_HIDDEN]: '不可购买',
- [CART_INVALID_GOODS_DELETED]: '已失效',
- [CART_INVALID_SHOP_DELETED]: '店铺失效'
- }
- export function getCartInvalidLabel(invalidType, invalidMsg) {
- if (!invalidType || invalidType === CART_INVALID_NONE) return ''
- return invalidMsg || INVALID_LABEL_MAP[invalidType] || '不可购买'
- }
- function mapCartItem(row) {
- if (!row) return null
- const invalidType = row.invalidType || CART_INVALID_NONE
- const purchasable = !!row.purchasable && invalidType === CART_INVALID_NONE
- return {
- cartItemId: row.cartItemId,
- goodsId: row.goodsId,
- goodsName: row.goodsName || '',
- displayPic: resolveFileUrl(row.mainPic) || GOODS_PLACEHOLDER,
- specText: (row.specText || '').trim() || '默认',
- specList: parseCartSpecText(row.specText),
- salePrice: row.salePrice,
- priceText: formatPrice(row.salePrice),
- quantity: Number(row.quantity) || 1,
- subtotal: row.subtotal,
- subtotalText: formatPrice(row.subtotal),
- checked: !!row.checked,
- purchasable,
- invalidType,
- invalidMsg: row.invalidMsg || '',
- invalidLabel: getCartInvalidLabel(invalidType, row.invalidMsg)
- }
- }
- function mapShopGroup(group) {
- if (!group) return null
- return {
- shopId: group.shopId,
- shopName: group.shopName || '',
- shopStatus: group.shopStatus,
- isClosed: String(group.shopStatus) === SHOP_STATUS_CLOSED,
- items: (group.items || []).map(mapCartItem).filter(Boolean)
- }
- }
- function mapCheckedSummary(summary) {
- const s = summary || {}
- const amount = s.checkedAmount
- return {
- checkedCount: Number(s.checkedCount) || 0,
- checkedQuantity: Number(s.checkedQuantity) || 0,
- checkedAmount: amount,
- checkedAmountText: formatPrice(amount)
- }
- }
- /** 列表接口 data → 页面模型 */
- export function mapCartList(data) {
- if (!data) {
- return { groups: [], checkedSummary: mapCheckedSummary(null) }
- }
- return {
- groups: (data.groups || []).map(mapShopGroup).filter(Boolean),
- checkedSummary: mapCheckedSummary(data.checkedSummary)
- }
- }
- /** 是否存在失效行 */
- export function hasInvalidCartItems(groups) {
- if (!Array.isArray(groups)) return false
- return groups.some((g) => (g.items || []).some((item) => !item.purchasable))
- }
- /** 当前勾选的可购行 shopId 集合 */
- export function getCheckedShopIds(groups) {
- const ids = new Set()
- if (!Array.isArray(groups)) return ids
- groups.forEach((g) => {
- ;(g.items || []).forEach((item) => {
- if (item.checked && item.purchasable) {
- ids.add(g.shopId)
- }
- })
- })
- return ids
- }
- /** 已勾选且可购的 cartItemId */
- export function getCheckedPurchasableIds(groups) {
- const ids = []
- if (!Array.isArray(groups)) return ids
- groups.forEach((g) => {
- ;(g.items || []).forEach((item) => {
- if (item.checked && item.purchasable) {
- ids.push(item.cartItemId)
- }
- })
- })
- return ids
- }
- /** 全部可购行 */
- export function getAllPurchasableItems(groups) {
- const list = []
- if (!Array.isArray(groups)) return list
- groups.forEach((g) => {
- ;(g.items || []).forEach((item) => {
- if (item.purchasable) {
- list.push({ ...item, shopId: g.shopId })
- }
- })
- })
- return list
- }
|