fix:搜索改成走后端 keyword 模糊查询(不再前端拉全量),外加修了空态闪现和顶部总人数被搜索缩水

This commit is contained in:
jacobxu666
2026-10-09 10:54:06 +08:00
parent 9276bd6140
commit b48c8e1e55
2 changed files with 78 additions and 89 deletions
+65 -89
View File
@@ -39,7 +39,7 @@
<view class="search-area">
<view class="search-bar">
<u-icon name="search" color="#8e8e93" size="36"></u-icon>
<input class="search-input" v-model="keyword" placeholder="搜索姓名/手机号" confirm-type="search" @confirm="onSearch" />
<input class="search-input" v-model="keyword" placeholder="搜索姓名/手机号/身份证号" confirm-type="search" @confirm="onSearch" />
<view class="search-btn" @click="onSearch">搜索</view>
</view>
</view>
@@ -62,7 +62,10 @@
<u-loadmore :status="loadStatus" :loading-text="'加载中...'" :loadmore-text="'上拉加载更多'" :nomore-text="'没有更多了'" />
</view>
</template>
<!-- xuchao: 无名单时展示空态 -->
<!-- xuchao: 无名单时展示空态;全量拉取/首屏预取进行中显示查询态,避免"暂无数据"闪现 -->
<view class="loadmore-area" v-else-if="!datesLoaded || loadStatus === 'loading'">
<u-loadmore status="loading" loading-text="查询中..." />
</view>
<view class="empty-area" v-else>
<u-icon name="empty" size="60" color="#c8c8c8"></u-icon>
<text class="empty-text">暂无数据</text>
@@ -81,27 +84,25 @@ export default {
// xuchao: 页码改为按期维护(原单一 pageNo 在切期后会被沿用,导致同一页重复追加/跳页)
pageNos: {},
pageSize: 10,
// xuchao: 搜索触发的全量拉取用大页尺寸(列表接口仅支持分页参数,无姓名检索)
pageSizeFull: 500,
totals: {},
fullLoaded: {},
loadingAll: {},
// xuchao: 各期全量人数(无关键字),仅用于顶部"已产生"总数;搜索态不影响该统计
periodTotals: {},
// xuchao: 各期当前生效的查询关键字(loadMore 续拉用它,而非输入框实时值)
queryKeywords: {},
// xuchao: 各期请求序号,丢弃乱序返回的旧响应
reqSeqs: {},
totalWinners: 0,
inFlight: 0,
loadStatusMap: {},
// xuchao: 期次日期接口是否已回,首屏未回前显示查询态而非空态
datesLoaded: false,
keyword: ''
}
},
computed: {
// xuchao: 服务端已按关键字模糊过滤,前端直接渲染当期列表
currentList() {
const list = this.winnerData[this.currentTab] || []
if (!this.keyword) return list
const kw = this.keyword.trim().toLowerCase()
if (!kw) return list
return list.filter(item =>
(item.memberName && item.memberName.toLowerCase().includes(kw)) ||
(item.memberPhone && item.memberPhone.includes(kw))
)
return this.winnerData[this.currentTab] || []
},
// xuchao: 各期独立的加载状态,避免并行请求互相覆盖
loadStatus() {
@@ -109,12 +110,10 @@ export default {
}
},
watch: {
// xuchao: 输入关键字即后台拉全当期名单,修复"没翻到的人物搜索无结果"
keyword(val) {
// xuchao: 关键字防抖 400ms 后走服务端模糊搜索(姓名/手机号/身份证任意片段)
keyword() {
clearTimeout(this._kwTimer)
if (val && val.trim() && !this.fullLoaded[this.currentTab]) {
this._kwTimer = setTimeout(() => this.loadAll(this.currentTab), 400)
}
this._kwTimer = setTimeout(() => this.search(), 400)
}
},
onLoad() {
@@ -126,6 +125,7 @@ export default {
methods: {
loadDates() {
this.$u.api.getWinnerDates().then(res => {
this.datesLoaded = true
if (res.errcode === 0 && res.data) {
const dates = res.data
this.tabs = dates.map((date, index) => ({
@@ -137,112 +137,87 @@ export default {
data[index] = []
})
this.winnerData = data
// xuchao: 预置各期 key,保证 totals/pageNos/loadStatusMap 的响应式
// xuchao: 预置各期 key,保证响应式
this.totals = {}
this.pageNos = {}
this.loadStatusMap = {}
this.periodTotals = {}
this.queryKeywords = {}
this.reqSeqs = {}
dates.forEach((date, index) => {
this.$set(this.totals, index, 0)
this.$set(this.pageNos, index, 1)
this.$set(this.loadStatusMap, index, 'loading')
this.$set(this.periodTotals, index, 0)
this.$set(this.queryKeywords, index, '')
this.$set(this.reqSeqs, index, 0)
})
// xuchao: 预取各期第一页(无关键字),汇总已产生总人数
this.tabs.forEach((tab, index) => {
this.loadTab(index, 1, '')
})
// xuchao: 预取各期第一页,汇总已产生总人数
if (this.tabs.length > 0) {
this.tabs.forEach((tab, index) => {
this.loadWinnerList(index, 1, true)
})
}
}
})
}).catch(() => { this.datesLoaded = true })
},
loadWinnerList(tabIndex, pageNo, allowConcurrent) {
// xuchao: 拉取某期某页;keyword 走服务端 LIKE(姓名/手机/身份证任意片段),空串=不过滤
loadTab(tabIndex, pageNo, keyword) {
if (!this.tabs[tabIndex]) return
if (this.inFlight > 0 && !allowConcurrent) return
// xuchao: 本期页码独立计算,缺省续拉该期下一页
const page = pageNo || (this.pageNos[tabIndex] || 1)
const kw = keyword || ''
// xuchao: 递增序号,返回时仅最新请求生效,丢弃连续输入产生的乱序旧响应
const seq = (this.reqSeqs[tabIndex] || 0) + 1
this.$set(this.reqSeqs, tabIndex, seq)
this.inFlight++
this.$set(this.loadStatusMap, tabIndex, 'loading')
const drawDate = this.tabs[tabIndex].date
this.$u.api.getWinnerList({
pageNo: page,
pageNo: pageNo,
pageSize: this.pageSize,
drawDate: drawDate
drawDate: this.tabs[tabIndex].date,
keyword: kw
}).then(res => {
// xuchao: 全量拉取已就绪/进行中时,旧的10条分页响应直接丢弃,避免覆盖全量列表
if (this.fullLoaded[tabIndex] || this.loadingAll[tabIndex]) return
if (seq !== this.reqSeqs[tabIndex]) return
if (res.errcode === 0 && res.data) {
const list = res.data.list || []
const prev = this.winnerData[tabIndex] || []
this.$set(this.pageNos, tabIndex, page)
if (page === 1) {
this.$set(this.winnerData, tabIndex, list)
} else {
this.$set(this.winnerData, tabIndex, prev.concat(list))
}
const prev = pageNo > 1 ? (this.winnerData[tabIndex] || []) : []
this.$set(this.winnerData, tabIndex, prev.concat(list))
this.$set(this.pageNos, tabIndex, pageNo)
this.$set(this.queryKeywords, tabIndex, kw)
this.$set(this.totals, tabIndex, res.data.total || 0)
// xuchao: 各期总人数求和,用于顶部幸运持卡人数
this.totalWinners = Object.values(this.totals).reduce((sum, n) => sum + n, 0)
// xuchao: 顶部总数只累计"无关键字"的全量人数,搜索态不会让它缩水
if (!kw) {
this.$set(this.periodTotals, tabIndex, res.data.total || 0)
this.totalWinners = Object.values(this.periodTotals).reduce((s, n) => s + n, 0)
}
}
}).finally(() => {
this.inFlight--
if (seq !== this.reqSeqs[tabIndex]) return
const len = (this.winnerData[tabIndex] || []).length
const total = this.totals[tabIndex] || 0
this.$set(this.loadStatusMap, tabIndex, len === 0 || len >= total ? 'nomore' : 'loadmore')
})
},
// xuchao: 执行一次关键字搜索(当期回到第1页);输入防抖与点搜索按钮都走这里
search() {
this.loadTab(this.currentTab, 1, (this.keyword || '').trim())
},
loadMore() {
const tab = this.currentTab
if (this.loadingAll[tab]) return
const total = this.totals[tab] || 0
const len = (this.winnerData[tab] || []).length
if (len < total && this.inFlight === 0) {
this.loadWinnerList(tab, (this.pageNos[tab] || 1) + 1)
this.loadTab(tab, (this.pageNos[tab] || 1) + 1, this.queryKeywords[tab] || '')
}
},
// xuchao: 按大页把当期名单一次拉全,搜索即可命中未翻到的人;完成后整期替换并标记 fullLoaded
loadAll(tabIndex) {
const tab = this.tabs[tabIndex]
if (!tab || this.fullLoaded[tabIndex] || this.loadingAll[tabIndex]) return
this.$set(this.loadingAll, tabIndex, true)
this.$set(this.loadStatusMap, tabIndex, 'loading')
const acc = []
const pull = (pageNo) => this.$u.api.getWinnerList({
pageNo: pageNo,
pageSize: this.pageSizeFull,
drawDate: tab.date
}).then(res => {
if (res.errcode !== 0 || !res.data) return
const list = res.data.list || []
const total = res.data.total || 0
acc.push(...list)
this.$set(this.totals, tabIndex, total)
if (list.length > 0 && acc.length < total && pageNo < 50) return pull(pageNo + 1)
})
pull(1).then(() => {
if (acc.length > 0) {
this.$set(this.winnerData, tabIndex, acc)
this.$set(this.fullLoaded, tabIndex, true)
this.$set(this.pageNos, tabIndex, Math.ceil(acc.length / this.pageSizeFull))
}
this.totalWinners = Object.values(this.totals).reduce((s, n) => s + n, 0)
}).catch(() => {
// 拉全失败静默降级为已加载部分可搜
}).finally(() => {
this.$set(this.loadingAll, tabIndex, false)
const len = (this.winnerData[tabIndex] || []).length
const total = this.totals[tabIndex] || 0
this.$set(this.loadStatusMap, tabIndex, len === 0 || len >= total ? 'nomore' : 'loadmore')
})
},
switchTab(index) {
this.currentTab = index
// xuchao: 不再重置全局 pageNo(各期游标自带);空列表补拉首页时允许并发,
// 避免首屏预取仍在途时切期被丢弃后一直显示“暂无数据”
if ((this.winnerData[index] || []).length === 0) {
this.loadWinnerList(index, 1, true)
// xuchao: 关键字作用于"正在看的这期"——若输入关键字与该期已生效查询不同则重查,否则复用已加载结果
const kw = (this.keyword || '').trim()
if (kw !== (this.queryKeywords[index] || '')) {
this.loadTab(index, 1, kw)
} else {
const total = this.totals[index] || 0
this.$set(this.loadStatusMap, index, this.winnerData[index].length >= total ? 'nomore' : 'loadmore')
const len = (this.winnerData[index] || []).length
this.$set(this.loadStatusMap, index, len === 0 || len >= total ? 'nomore' : 'loadmore')
}
},
goBack() {
@@ -255,8 +230,9 @@ export default {
}
},
onSearch() {
// xuchao: 点搜索立即确保当期全量已拉取(过滤仍在前端本地完成)
if (!this.fullLoaded[this.currentTab]) this.loadAll(this.currentTab)
// xuchao: 点搜索/键盘确认 → 立即搜索,取消待执行的防抖
clearTimeout(this._kwTimer)
this.search()
}
}
}