80 lines
2.0 KiB
TypeScript
80 lines
2.0 KiB
TypeScript
import { api } from '../../utils/api'
|
|
import { getAvatarGradient } from '../../utils/format'
|
|
import { AppGlobalData } from '../../types/index'
|
|
|
|
const app = getApp<{ globalData: AppGlobalData }>()
|
|
|
|
Page({
|
|
data: {
|
|
statusBarHeight: 0,
|
|
navbarHeight: 44,
|
|
navbarPaddingRight: 0,
|
|
keyword: '',
|
|
resultTab: 'posts' as 'posts' | 'users',
|
|
postResults: [] as any[],
|
|
userResults: [] as any[],
|
|
hotTopics: [] as any[],
|
|
loading: false,
|
|
},
|
|
|
|
onLoad() {
|
|
const { statusBarHeight, navbarHeight, navbarPaddingRight } = app.globalData.navBarInfo
|
|
this.setData({ statusBarHeight, navbarHeight, navbarPaddingRight })
|
|
this.loadHotTopics()
|
|
},
|
|
|
|
async loadHotTopics() {
|
|
try {
|
|
const topics = await api.getHotTopics()
|
|
this.setData({ hotTopics: topics || [] })
|
|
} catch {}
|
|
},
|
|
|
|
onInput(e: WechatMiniprogram.CustomEvent) {
|
|
this.setData({ keyword: e.detail.value })
|
|
},
|
|
|
|
onClear() {
|
|
this.setData({ keyword: '', postResults: [], userResults: [] })
|
|
},
|
|
|
|
async onSearch() {
|
|
const kw = this.data.keyword.trim()
|
|
if (!kw) return
|
|
this.setData({ loading: true })
|
|
try {
|
|
const [posts, users] = await Promise.all([
|
|
api.searchPosts(kw),
|
|
api.searchUsers(kw),
|
|
])
|
|
this.setData({
|
|
postResults: posts || [],
|
|
userResults: (users || []).map((u: any) => ({
|
|
...u,
|
|
gradient: getAvatarGradient(u._id),
|
|
})),
|
|
})
|
|
} catch {
|
|
wx.showToast({ title: '搜索失败', icon: 'none' })
|
|
} finally {
|
|
this.setData({ loading: false })
|
|
}
|
|
},
|
|
|
|
onTopicTap(e: WechatMiniprogram.CustomEvent) {
|
|
const tag = e.currentTarget.dataset.tag as string
|
|
this.setData({ keyword: tag })
|
|
this.onSearch()
|
|
},
|
|
|
|
onResultTab(e: WechatMiniprogram.CustomEvent) {
|
|
this.setData({ resultTab: e.currentTarget.dataset.t as 'posts' | 'users' })
|
|
},
|
|
|
|
onUserTap(e: WechatMiniprogram.CustomEvent) {
|
|
wx.navigateTo({ url: `/pages/pet-detail/pet-detail?userId=${e.currentTarget.dataset.uid}` })
|
|
},
|
|
|
|
onBack() { wx.navigateBack() },
|
|
})
|