feat: 为商城商品列表实现服务端筛选和分页功能,并调整积分显示逻辑。
This commit is contained in:
parent
9d25477cd3
commit
470094dc75
@ -234,8 +234,16 @@ export function redeemItemCardByPoints(user_id, item_card_id, quantity = 1) {
|
||||
return authRequest({ url: `/api/app/users/${user_id}/points/redeem-item-card`, method: 'POST', data: { item_card_id, quantity } })
|
||||
}
|
||||
|
||||
export function getStoreItems(kind = 'product', page = 1, page_size = 20) {
|
||||
return authRequest({ url: '/api/app/store/items', method: 'GET', data: { kind, page, page_size } })
|
||||
export function getStoreItems(kind = 'product', page = 1, page_size = 20, filters = {}) {
|
||||
const data = { kind, page, page_size }
|
||||
if (filters.keyword) data.keyword = filters.keyword
|
||||
if (filters.price_min !== undefined && filters.price_min !== null && filters.price_min !== '') {
|
||||
data.price_min = parseInt(filters.price_min)
|
||||
}
|
||||
if (filters.price_max !== undefined && filters.price_max !== null && filters.price_max !== '') {
|
||||
data.price_max = parseInt(filters.price_max)
|
||||
}
|
||||
return authRequest({ url: '/api/app/store/items', method: 'GET', data })
|
||||
}
|
||||
|
||||
export function getTasks(page = 1, page_size = 20) {
|
||||
|
||||
@ -779,10 +779,10 @@ export default {
|
||||
return String(v || '').trim()
|
||||
},
|
||||
formatPoints(v) {
|
||||
// 后端已返回积分单位(分 * rate / 100),直接显示
|
||||
const n = Number(v) || 0
|
||||
if (n === 0) return '0.0'
|
||||
const f = n / 100
|
||||
return f.toFixed(1)
|
||||
return n.toFixed(1)
|
||||
},
|
||||
copyInviteCode() {
|
||||
const code = this.getInviteCode()
|
||||
|
||||
@ -290,7 +290,19 @@ async function loadItems(append = false) {
|
||||
if (loading.value) return
|
||||
loading.value = true
|
||||
try {
|
||||
const res = await getStoreItems(currentTab.value, page.value, pageSize)
|
||||
// 构建筛选参数
|
||||
const filters = {}
|
||||
if (keyword.value && keyword.value.trim()) {
|
||||
filters.keyword = keyword.value.trim()
|
||||
}
|
||||
if (priceMin.value !== '' && priceMin.value !== null) {
|
||||
filters.price_min = priceMin.value
|
||||
}
|
||||
if (priceMax.value !== '' && priceMax.value !== null) {
|
||||
filters.price_max = priceMax.value
|
||||
}
|
||||
|
||||
const res = await getStoreItems(currentTab.value, page.value, pageSize, filters)
|
||||
const list = res.list || res || []
|
||||
const total = res.total || 0
|
||||
const newItems = normalizeItems(list, currentTab.value)
|
||||
@ -301,21 +313,12 @@ async function loadItems(append = false) {
|
||||
allItems.value = newItems
|
||||
}
|
||||
|
||||
// 根据返回的total计算总页数
|
||||
// 直接使用服务端返回的数据(已经过筛选)
|
||||
items.value = allItems.value
|
||||
|
||||
// 检查是否还有更多数据
|
||||
const totalPages = Math.ceil(total / pageSize)
|
||||
|
||||
// 如果当前页小于总页数,继续加载下一页
|
||||
if (page.value < totalPages) {
|
||||
page.value++
|
||||
// 递归加载下一页
|
||||
loading.value = false // 临时释放loading状态,允许递归调用
|
||||
await loadItems(true)
|
||||
} else {
|
||||
// 所有数据加载完成
|
||||
hasMore.value = false
|
||||
}
|
||||
|
||||
applyFilters()
|
||||
hasMore.value = page.value < totalPages
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
hasMore.value = false
|
||||
@ -350,14 +353,22 @@ function applyFilters() {
|
||||
|
||||
function applyPriceFilter() {
|
||||
activePriceRange.value = 'custom'
|
||||
applyFilters()
|
||||
// 重新加载数据(服务端筛选)
|
||||
page.value = 1
|
||||
hasMore.value = true
|
||||
allItems.value = []
|
||||
loadItems()
|
||||
}
|
||||
|
||||
function resetPriceFilter() {
|
||||
priceMin.value = ''
|
||||
priceMax.value = ''
|
||||
activePriceRange.value = 'all'
|
||||
applyFilters()
|
||||
// 重新加载数据
|
||||
page.value = 1
|
||||
hasMore.value = true
|
||||
allItems.value = []
|
||||
loadItems()
|
||||
}
|
||||
|
||||
function selectQuickPrice(range) {
|
||||
@ -372,14 +383,24 @@ function selectQuickPrice(range) {
|
||||
} else {
|
||||
priceMax.value = ''
|
||||
}
|
||||
applyFilters()
|
||||
// 重新加载数据
|
||||
page.value = 1
|
||||
hasMore.value = true
|
||||
allItems.value = []
|
||||
loadItems()
|
||||
}
|
||||
|
||||
function isRangeActive(range) {
|
||||
return activePriceRange.value === range.key
|
||||
}
|
||||
|
||||
function onSearchConfirm() { applyFilters() }
|
||||
function onSearchConfirm() {
|
||||
// 搜索时重新加载数据
|
||||
page.value = 1
|
||||
hasMore.value = true
|
||||
allItems.value = []
|
||||
loadItems()
|
||||
}
|
||||
|
||||
function onProductTap(p) {
|
||||
if (p.kind === 'product') {
|
||||
@ -458,7 +479,11 @@ onShow(() => {
|
||||
})
|
||||
|
||||
onReachBottom(() => {
|
||||
// 所有数据已在页面加载时一次性请求完成,无需触底加载
|
||||
// 触底加载更多
|
||||
if (hasMore.value && !loading.value) {
|
||||
page.value++
|
||||
loadItems(true)
|
||||
}
|
||||
})
|
||||
|
||||
watch(keyword, () => applyFilters())
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user