Files
PetCommunity/miniprogram/pages/search/search.ts
chenwu b02e26f602 chore: 清理冗余代码并补全私信/搜索/故事/通知功能
- 删除未使用的空组件目录、死代码导出(clearAuth/calcDistance/COLORS/
  AVATAR_GRADIENTS/Message/TabBarItem/deletePost/uploadImage/getFileURL/
  _initLogin),确保程序仍可正常运行
- 新增 8 个云函数并补全对应页面逻辑:getMessages/sendMessage(私信聊天)、
  getHotTopics/searchPosts/searchUsers(搜索)、getStory(故事,复用 posts
  集合)、getNotifications/markAllRead(基于点赞评论聚合的通知系统)
- 在 cloudbaserc.json 中注册全部新云函数,并通过 --deployMode zip 完成部署

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-07 16:27:28 +08:00

75 lines
1.8 KiB
TypeScript

import { api } from '../../utils/api'
import { getAvatarGradient } from '../../utils/format'
Page({
data: {
statusBarHeight: 0,
keyword: '',
resultTab: 'posts' as 'posts' | 'users',
postResults: [] as any[],
userResults: [] as any[],
hotTopics: [] as any[],
loading: false,
},
onLoad() {
const info = wx.getSystemInfoSync()
this.setData({ statusBarHeight: info.statusBarHeight })
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() },
})