每周更新
This commit is contained in:
@@ -34,7 +34,7 @@
|
||||
<script>
|
||||
import { readCart } from '../utils/cartManager'
|
||||
import { mainNavigation, getBreadcrumb } from '../utils/navigation'
|
||||
import { getGoodsList } from '../api/shoppingCart'
|
||||
import { getGoodsListV2 } from '../api/shoppingCart'
|
||||
|
||||
export default {
|
||||
name: 'Header',
|
||||
@@ -74,47 +74,37 @@ export default {
|
||||
loadCart() {
|
||||
this.cart = readCart()
|
||||
},
|
||||
/**
|
||||
* 加载服务器购物车数量
|
||||
* 根据新接口结构:res.rows[].cartMachineInfoDtoList.length 累加
|
||||
*/
|
||||
async loadServerCartCount() {
|
||||
try {
|
||||
const res = await getGoodsList()
|
||||
// 统一提取 rows/数组
|
||||
const primary = Array.isArray(res && res.rows)
|
||||
? res.rows
|
||||
: Array.isArray(res && res.data && res.data.rows)
|
||||
? res.data.rows
|
||||
: Array.isArray(res && res.data)
|
||||
? res.data
|
||||
: (Array.isArray(res) ? res : [])
|
||||
|
||||
let groups = []
|
||||
if (Array.isArray(primary) && primary.length) {
|
||||
// 情况A:shop -> shoppingCartInfoDtoList -> productMachineDtoList
|
||||
if (Array.isArray(primary[0] && primary[0].shoppingCartInfoDtoList)) {
|
||||
primary.forEach(shop => {
|
||||
if (Array.isArray(shop && shop.shoppingCartInfoDtoList)) {
|
||||
groups.push(...shop.shoppingCartInfoDtoList)
|
||||
}
|
||||
})
|
||||
} else {
|
||||
// 情况B:直接就是商品分组数组
|
||||
groups = primary
|
||||
}
|
||||
} else if (Array.isArray(res && res.shoppingCartInfoDtoList)) {
|
||||
// 情况C:返回对象直接有 shoppingCartInfoDtoList
|
||||
groups = res.shoppingCartInfoDtoList
|
||||
}
|
||||
|
||||
const res = await getGoodsListV2()
|
||||
let total = 0
|
||||
if (groups.length) {
|
||||
total = groups.reduce((sum, g) => sum + (Array.isArray(g && g.productMachineDtoList) ? g.productMachineDtoList.length : 0), 0)
|
||||
} else if (Array.isArray(res && res.productMachineDtoList)) {
|
||||
// 情况D:根对象直接是机器列表
|
||||
total = res.productMachineDtoList.length
|
||||
|
||||
// 新接口结构:res.rows 是店铺数组,每个店铺有 cartMachineInfoDtoList
|
||||
if (Array.isArray(res && res.rows)) {
|
||||
total = res.rows.reduce((sum, shop) => {
|
||||
const machineList = Array.isArray(shop && shop.cartMachineInfoDtoList)
|
||||
? shop.cartMachineInfoDtoList
|
||||
: []
|
||||
return sum + machineList.length
|
||||
}, 0)
|
||||
} else if (Array.isArray(res && res.data && res.data.rows)) {
|
||||
// 兼容:如果数据在 res.data.rows 中
|
||||
total = res.data.rows.reduce((sum, shop) => {
|
||||
const machineList = Array.isArray(shop && shop.cartMachineInfoDtoList)
|
||||
? shop.cartMachineInfoDtoList
|
||||
: []
|
||||
return sum + machineList.length
|
||||
}, 0)
|
||||
}
|
||||
|
||||
this.cartServerCount = Number.isFinite(total) ? total : 0
|
||||
} catch (e) {
|
||||
// 忽略错误,保持当前显示
|
||||
console.error('加载购物车数量失败:', e)
|
||||
}
|
||||
},
|
||||
handleStorageChange(event) {
|
||||
|
||||
Reference in New Issue
Block a user