mirror of
https://gitee.com/qingqing-coconut-tea/RcKtFezyRAZX
synced 2026-07-21 10:32:32 +08:00
153 lines
3.5 KiB
JavaScript
153 lines
3.5 KiB
JavaScript
// pages/search/search.js
|
||
// 搜索逻辑处理
|
||
|
||
const app = getApp()
|
||
const api = require('../../services/api')
|
||
|
||
Page({
|
||
data: {
|
||
keyword: '',
|
||
hotSearchList: [],
|
||
historyList: [],
|
||
searchResult: [],
|
||
page: 1,
|
||
pageSize: 20,
|
||
hasMore: true,
|
||
loading: false,
|
||
showResult: false
|
||
},
|
||
|
||
onLoad() {
|
||
this.loadHotSearch()
|
||
this.loadHistorySearch()
|
||
},
|
||
|
||
onShow() {
|
||
this.loadHistorySearch()
|
||
},
|
||
|
||
// 加载热门搜索
|
||
loadHotSearch() {
|
||
api.getHotSearch().then(res => {
|
||
if (res.code === 200) {
|
||
this.setData({ hotSearchList: res.data || [] })
|
||
}
|
||
})
|
||
},
|
||
|
||
// 加载搜索历史
|
||
loadHistorySearch() {
|
||
const history = wx.getStorageSync('searchHistory') || []
|
||
this.setData({ historyList: history })
|
||
},
|
||
|
||
// 保存搜索历史
|
||
saveSearchHistory(keyword) {
|
||
if (!keyword) return
|
||
|
||
let history = wx.getStorageSync('searchHistory') || []
|
||
history = history.filter(item => item !== keyword)
|
||
history.unshift(keyword)
|
||
history = history.slice(0, 20)
|
||
wx.setStorageSync('searchHistory', history)
|
||
this.setData({ historyList: history })
|
||
},
|
||
|
||
// 搜索输入
|
||
onSearchInput(e) {
|
||
this.setData({ keyword: e.detail.value })
|
||
},
|
||
|
||
// 执行搜索
|
||
onSearch() {
|
||
const keyword = this.data.keyword.trim()
|
||
if (!keyword) {
|
||
wx.showToast({ title: '请输入搜索关键词', icon: 'none' })
|
||
return
|
||
}
|
||
|
||
this.saveSearchHistory(keyword)
|
||
this.setData({ showResult: true, page: 1, searchResult: [], hasMore: true })
|
||
this.loadSearchResult()
|
||
},
|
||
|
||
// 加载搜索结果
|
||
loadSearchResult() {
|
||
if (this.data.loading || !this.data.hasMore) return
|
||
|
||
this.setData({ loading: true })
|
||
|
||
api.search({
|
||
keyword: this.data.keyword,
|
||
page: this.data.page,
|
||
pageSize: this.data.pageSize
|
||
}).then(res => {
|
||
this.setData({ loading: false })
|
||
if (res.code === 200) {
|
||
const list = res.data.list || []
|
||
this.setData({
|
||
searchResult: this.data.page === 1 ? list : this.data.searchResult.concat(list),
|
||
hasMore: list.length >= this.data.pageSize
|
||
})
|
||
}
|
||
}).catch(() => {
|
||
this.setData({ loading: false })
|
||
})
|
||
},
|
||
|
||
// 触底加载更多
|
||
onReachBottom() {
|
||
if (this.data.showResult && this.data.hasMore && !this.data.loading) {
|
||
this.setData({ page: this.data.page + 1 })
|
||
this.loadSearchResult()
|
||
}
|
||
},
|
||
|
||
// 点击热门搜索词
|
||
onHotSearchTap(e) {
|
||
const keyword = e.currentTarget.dataset.keyword
|
||
this.setData({ keyword })
|
||
this.onSearch()
|
||
},
|
||
|
||
// 点击历史搜索词
|
||
onHistorySearchTap(e) {
|
||
const keyword = e.currentTarget.dataset.keyword
|
||
this.setData({ keyword })
|
||
this.onSearch()
|
||
},
|
||
|
||
// 清空历史记录
|
||
clearHistory() {
|
||
wx.showModal({
|
||
title: '提示',
|
||
content: '确定清空搜索历史吗?',
|
||
success: res => {
|
||
if (res.confirm) {
|
||
wx.removeStorageSync('searchHistory')
|
||
this.setData({ historyList: [] })
|
||
}
|
||
}
|
||
})
|
||
},
|
||
|
||
// 跳转到文章详情
|
||
goToDetail(e) {
|
||
const articleId = e.currentTarget.dataset.id
|
||
wx.navigateTo({
|
||
url: `/pages/article-detail/article-detail?id=${articleId}`
|
||
})
|
||
},
|
||
|
||
// 取消搜索
|
||
onCancel() {
|
||
wx.navigateBack()
|
||
}
|
||
})
|
||
|
||
/**
|
||
* @联系作者:16768118056
|
||
* @描述:(此项目非免费分享)源码百分百可用,搞定毕设不发愁,支持远程调试安装、二次开发、定制、讲解、文档类。
|
||
* @访问:https://www.notmaker.com/detail/8326a897119d4542896c863a69d7bbe7/gtt20260415,查看完整运行演示。
|
||
*/
|