修改bug,优化导航
This commit is contained in:
@@ -119,7 +119,8 @@ const getCommunityList = async (pageNum = 1, keyword = '') => {
|
||||
isLoading.value = true
|
||||
|
||||
try {
|
||||
keyword = decodeURIComponent(keyword)
|
||||
// keyword 来自用户输入时是普通文本,无需 decodeURIComponent;
|
||||
// 仅当 keyword 来自 URL 参数时才需要解码(已在外部处理)
|
||||
const params = {
|
||||
pageNum,
|
||||
pageSize,
|
||||
@@ -129,8 +130,10 @@ const getCommunityList = async (pageNum = 1, keyword = '') => {
|
||||
const res = await getVillageList(params)
|
||||
|
||||
if (res.code === 200) {
|
||||
const listData = res.data || []
|
||||
const total = res.data?.total || listData.length
|
||||
// 兼容两种返回格式:数组 或 { rows, total }
|
||||
const resData = res.data || {}
|
||||
const listData = Array.isArray(resData) ? resData : (resData.rows || [])
|
||||
const total = Array.isArray(resData) ? resData.length : (resData.total || listData.length)
|
||||
|
||||
if (pageNum === 1) {
|
||||
displayedCommunities.value = listData
|
||||
@@ -142,20 +145,32 @@ const getCommunityList = async (pageNum = 1, keyword = '') => {
|
||||
hasMoreData.value = displayedCommunities.value.length < totalCount.value
|
||||
|
||||
// 默认选中:优先匹配传入的 villageId,否则选第一条(仅第一页时)
|
||||
// 搜索场景下不覆盖用户已手动选择的项
|
||||
if (pageNum === 1 && displayedCommunities.value.length > 0) {
|
||||
const targetId = currentVillageIdFromHome.value || uni.getStorageSync('currentVillageId') || ''
|
||||
if (targetId) {
|
||||
const matched = displayedCommunities.value.find(
|
||||
item => String(item.villageId) === String(targetId)
|
||||
const userSelectedInCurrentList = selectedItem.value &&
|
||||
displayedCommunities.value.some(
|
||||
item => String(item.villageId) === String(selectedItem.value.villageId)
|
||||
)
|
||||
selectedItem.value = matched || displayedCommunities.value[0]
|
||||
} else {
|
||||
selectedItem.value = displayedCommunities.value[0]
|
||||
// 如果用户已选中且该项仍在当前列表中,保留用户选择
|
||||
if (!isSearching.value || !userSelectedInCurrentList) {
|
||||
const targetId = currentVillageIdFromHome.value || uni.getStorageSync('currentVillageId') || ''
|
||||
if (targetId) {
|
||||
const matched = displayedCommunities.value.find(
|
||||
item => String(item.villageId) === String(targetId)
|
||||
)
|
||||
selectedItem.value = matched || displayedCommunities.value[0]
|
||||
} else {
|
||||
selectedItem.value = displayedCommunities.value[0]
|
||||
}
|
||||
}
|
||||
} else if (pageNum === 1) {
|
||||
// 第一页返回空列表(搜索无结果等场景),清除选中项使确认按钮禁用
|
||||
selectedItem.value = null
|
||||
}
|
||||
} else {
|
||||
displayedCommunities.value = []
|
||||
hasMoreData.value = false
|
||||
selectedItem.value = null
|
||||
uni.showToast({
|
||||
title: res.msg || "查询失败",
|
||||
icon: 'error'
|
||||
@@ -165,6 +180,7 @@ const getCommunityList = async (pageNum = 1, keyword = '') => {
|
||||
// console.error('获取小区列表失败:', err)
|
||||
displayedCommunities.value = []
|
||||
hasMoreData.value = false
|
||||
selectedItem.value = null
|
||||
uni.showToast({
|
||||
title: err.msg || "网络异常",
|
||||
icon: 'error'
|
||||
@@ -238,7 +254,7 @@ const handleConfirm = async () => {
|
||||
uni.hideLoading()
|
||||
|
||||
if (res.code === 200) {
|
||||
if (res.data.villageId) {
|
||||
if (res.data?.villageId) {
|
||||
selectedItem.value.villageId = res.data.villageId
|
||||
}
|
||||
uni.showToast({
|
||||
|
||||
Reference in New Issue
Block a user