'商品列表和详情页接上接口'
This commit is contained in:
+47
-39
@@ -5,6 +5,7 @@
|
||||
<SearchBar
|
||||
:keyword="searchKeyword"
|
||||
@input="onSearchInput"
|
||||
@blur="onSearchBlur"
|
||||
@filter="toggleFilter"
|
||||
/>
|
||||
|
||||
@@ -49,7 +50,6 @@
|
||||
@sub-select="selectSubCategory"
|
||||
@product-click="goDetail"
|
||||
@page-change="loadGoods"
|
||||
@load-more="loadMore"
|
||||
/>
|
||||
</view>
|
||||
</view>
|
||||
@@ -199,29 +199,16 @@ export default {
|
||||
},
|
||||
|
||||
/**
|
||||
* 加载商品列表(分页 + 筛选)
|
||||
* 统一加载商品列表(分页 + 全部筛选条件)
|
||||
* 所有触发点(搜索失焦 / 分类点击 / 国家切换 / 标签点击 / 确认筛选 / 分页)都走这里
|
||||
*/
|
||||
async loadGoods(page) {
|
||||
try {
|
||||
const currentPage = page || this.pagination.current || 1
|
||||
const pageSize = this.pagination.pageSize || 10
|
||||
|
||||
var params = {
|
||||
page: currentPage,
|
||||
pageSize: pageSize
|
||||
}
|
||||
|
||||
if (this.selectedCountry) {
|
||||
params.countryId = this.selectedCountry
|
||||
}
|
||||
|
||||
if (this.activeCategory && this.activeCategory !== 'all') {
|
||||
params.categoryId = this.activeCategory
|
||||
}
|
||||
|
||||
if (this.searchKeyword && this.searchKeyword.trim()) {
|
||||
params.keyword = this.searchKeyword.trim()
|
||||
}
|
||||
// 统一参数对象:由 buildQueryParams 统一拼装
|
||||
const params = this.buildQueryParams(currentPage)
|
||||
|
||||
console.log('[loadGoods] params:', JSON.stringify(params))
|
||||
|
||||
@@ -274,6 +261,40 @@ export default {
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 构造统一的查询参数对象
|
||||
* - countryId / categoryId / keyword / page / pageSize 直传后端
|
||||
* - categoryId:子分类优先,其次顶级分类
|
||||
* - tags:对象数组 [{ tagGroupId, tagIds }],同组 OR、跨组 AND(后端处理)
|
||||
*/
|
||||
buildQueryParams(page) {
|
||||
const params = {
|
||||
page: page || 1,
|
||||
pageSize: this.pagination.pageSize || 10
|
||||
}
|
||||
if (this.selectedCountry) {
|
||||
params.countryId = this.selectedCountry
|
||||
}
|
||||
const catId = this.activeSubCategory || (this.activeCategory !== 'all' ? this.activeCategory : '')
|
||||
if (catId) {
|
||||
params.categoryId = catId
|
||||
}
|
||||
const kw = (this.searchKeyword || '').trim()
|
||||
if (kw) {
|
||||
params.keyword = kw
|
||||
}
|
||||
const tagsList = (this.filterGroups || [])
|
||||
.filter((g) => g.selected && g.selected.length > 0)
|
||||
.map((g) => ({
|
||||
tagGroupId: String(g.id),
|
||||
tagIds: (g.selected || []).map((id) => String(id))
|
||||
}))
|
||||
if (tagsList.length > 0) {
|
||||
params.tags = JSON.stringify(tagsList)
|
||||
}
|
||||
return params
|
||||
},
|
||||
|
||||
codeToEmoji(iconPath) {
|
||||
if (!iconPath) return '🌐'
|
||||
var match = iconPath.match(/\/([^/]+)\.\w+$/)
|
||||
@@ -295,17 +316,16 @@ export default {
|
||||
}
|
||||
}
|
||||
this.subCategories = subCats
|
||||
this.activeSubCategory = subCats.length > 0 ? subCats[0].id : ''
|
||||
this.activeSubCategory = ''
|
||||
},
|
||||
|
||||
onSearchInput(val) {
|
||||
// 仅更新关键词状态,搜索在失焦时触发
|
||||
this.searchKeyword = val
|
||||
// 防抖搜索
|
||||
if (this._searchTimer) clearTimeout(this._searchTimer)
|
||||
var self = this
|
||||
this._searchTimer = setTimeout(function () {
|
||||
self.loadGoods(1)
|
||||
}, 300)
|
||||
},
|
||||
|
||||
onSearchBlur() {
|
||||
this.loadGoods(1)
|
||||
},
|
||||
|
||||
async selectCountry(id) {
|
||||
@@ -339,8 +359,9 @@ export default {
|
||||
await this.loadGoods(1)
|
||||
},
|
||||
|
||||
selectSubCategory(id) {
|
||||
async selectSubCategory(id) {
|
||||
this.activeSubCategory = id
|
||||
await this.loadGoods(1)
|
||||
},
|
||||
|
||||
toggleFilter() {
|
||||
@@ -421,7 +442,6 @@ export default {
|
||||
var filters = []
|
||||
var countryFilter = this.activeFilters.find(function (f) { return f.key === 'country' })
|
||||
if (countryFilter) filters.push(countryFilter)
|
||||
|
||||
this.filterGroups.forEach(function (group) {
|
||||
if (group.selected.length > 0) {
|
||||
var labels = group.tags
|
||||
@@ -444,18 +464,6 @@ export default {
|
||||
uni.navigateTo({
|
||||
url: `/pages/product-detail/product-detail?id=${item.id}`
|
||||
})
|
||||
},
|
||||
|
||||
loadMore() {
|
||||
if (this.loadingMore) {
|
||||
return
|
||||
}
|
||||
const nextPage = this.pagination.current + 1
|
||||
if (nextPage > this.pagination.totalPages) {
|
||||
return
|
||||
}
|
||||
this.loadingMore = true
|
||||
this.loadGoods(nextPage)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user