70 lines
1.8 KiB
JavaScript
70 lines
1.8 KiB
JavaScript
const app = getApp()
|
|
|
|
Page({
|
|
data: {
|
|
statusBarHeight: 44,
|
|
navBarHeight: 88,
|
|
query: '',
|
|
results: [],
|
|
keywords: [],
|
|
isLoading: false,
|
|
hotKeywords: ['私域', '流量', '赚钱', '电商', 'AI', '社群']
|
|
},
|
|
|
|
onLoad() {
|
|
const statusBarHeight = app.globalData.statusBarHeight || 44
|
|
const navBarHeight = app.globalData.navBarHeight || (statusBarHeight + 44)
|
|
this.setData({ statusBarHeight, navBarHeight })
|
|
},
|
|
|
|
onInput(e) {
|
|
const query = (e.detail && e.detail.value) || ''
|
|
this.setData({ query })
|
|
if (!query.trim()) {
|
|
this.setData({ results: [], keywords: [] })
|
|
return
|
|
}
|
|
this.debounceSearch(query)
|
|
},
|
|
|
|
_searchTimer: null,
|
|
debounceSearch(query) {
|
|
if (this._searchTimer) clearTimeout(this._searchTimer)
|
|
this._searchTimer = setTimeout(() => this.doSearch(query), 300)
|
|
},
|
|
|
|
doSearch(q) {
|
|
if (!q || !q.trim()) return
|
|
this.setData({ isLoading: true })
|
|
app.request('/api/search?q=' + encodeURIComponent(q.trim()) + '&type=all')
|
|
.then((res) => {
|
|
const results = (res && res.data && res.data.results) ? res.data.results : []
|
|
const keywords = (res && res.data && res.data.keywords) ? res.data.keywords : []
|
|
this.setData({ results, keywords, isLoading: false })
|
|
})
|
|
.catch(() => {
|
|
this.setData({ results: [], keywords: [], isLoading: false })
|
|
})
|
|
},
|
|
|
|
onKeywordTap(e) {
|
|
const keyword = e.currentTarget.dataset.keyword
|
|
this.setData({ query: keyword })
|
|
this.doSearch(keyword)
|
|
},
|
|
|
|
clearQuery() {
|
|
this.setData({ query: '', results: [], keywords: [] })
|
|
},
|
|
|
|
goToRead(e) {
|
|
const id = e.currentTarget.dataset.id
|
|
if (!id) return
|
|
wx.navigateTo({ url: '/pages/read/read?id=' + encodeURIComponent(id) })
|
|
},
|
|
|
|
goBack() {
|
|
wx.navigateBack({ fail: () => wx.switchTab({ url: '/pages/index/index' }) })
|
|
}
|
|
})
|