- 新增 addComment/getComments/getFollowList/followUser/getUser/updateProfile/updatePet/deletePet 云函数并完成部署 - 统一通过 app.globalData.navBarInfo 避让系统胶囊按钮(编辑资料/编辑宠物等页面保存按钮被遮挡问题) - 编辑资料页所在城市改为省份 picker 选择 - 修复"我的"页动态为空(getUserPosts 应传 openid 而非数据库 _id) - 重构编辑宠物页:品种改用 picker 滚轮(替代有数量上限的 actionSheet),标签改用 selectedTagSet 映射 + 自定义标签输入(5字以内、仅中英文/数字、最多8个) - 接通宠物保存/删除真实云函数调用并同步本地缓存 - 移除 feed 页对不存在的 getStories 云函数调用,修复模拟器卡死 - 移除 post 页对不存在的 wx.reverseGeocoder API 的调用 - 统一扩展 AppGlobalData 类型并修正所有 getApp 调用的泛型,解决 TS 报错
94 lines
2.2 KiB
TypeScript
94 lines
2.2 KiB
TypeScript
import { api } from '../../utils/api'
|
|
import { AppGlobalData } from '../../types/index'
|
|
|
|
const app = getApp<{ globalData: AppGlobalData }>()
|
|
|
|
Page({
|
|
data: {
|
|
statusBarHeight: 0,
|
|
peerId: '',
|
|
peerName: '',
|
|
messages: [] as any[],
|
|
inputText: '',
|
|
scrollToMsg: '',
|
|
},
|
|
|
|
_watcher: null as any,
|
|
|
|
onLoad(query: { userId?: string }) {
|
|
const info = wx.getSystemInfoSync()
|
|
this.setData({ statusBarHeight: info.statusBarHeight, peerId: query.userId || '' })
|
|
this.loadMessages()
|
|
this.subscribeRealtime()
|
|
},
|
|
|
|
onUnload() {
|
|
this._watcher?.close()
|
|
},
|
|
|
|
async loadMessages() {
|
|
try {
|
|
const res = await api.getMessages()
|
|
const msgs = res.list.map((m: any) => ({
|
|
...m,
|
|
isMe: m.fromId === app.globalData?.userInfo?._id,
|
|
}))
|
|
this.setData({ messages: msgs })
|
|
this.scrollToBottom()
|
|
} catch {}
|
|
},
|
|
|
|
subscribeRealtime() {
|
|
const db = wx.cloud.database()
|
|
this._watcher = db.collection('messages')
|
|
.where({ toId: app.globalData?.userInfo?._id })
|
|
.watch({
|
|
onChange: snapshot => {
|
|
if (snapshot.type === 'add') {
|
|
const newMsgs = snapshot.docs.map((d: any) => ({ ...d, isMe: false }))
|
|
this.setData({ messages: [...this.data.messages, ...newMsgs] })
|
|
this.scrollToBottom()
|
|
}
|
|
},
|
|
onError: () => {},
|
|
})
|
|
},
|
|
|
|
scrollToBottom() {
|
|
const msgs = this.data.messages
|
|
if (msgs.length > 0) {
|
|
this.setData({ scrollToMsg: `msg-${msgs[msgs.length - 1]._id}` })
|
|
}
|
|
},
|
|
|
|
onInput(e: WechatMiniprogram.CustomEvent) {
|
|
this.setData({ inputText: e.detail.value })
|
|
},
|
|
|
|
async onSend() {
|
|
const text = this.data.inputText.trim()
|
|
if (!text) return
|
|
this.setData({ inputText: '' })
|
|
|
|
const tempMsg = {
|
|
_id: `temp_${Date.now()}`,
|
|
content: text,
|
|
isMe: true,
|
|
fromId: app.globalData?.userInfo?._id,
|
|
toId: this.data.peerId,
|
|
}
|
|
this.setData({ messages: [...this.data.messages, tempMsg] })
|
|
this.scrollToBottom()
|
|
|
|
try {
|
|
await api.sendMessage(this.data.peerId, text)
|
|
} catch {
|
|
wx.showToast({ title: '发送失败', icon: 'none' })
|
|
}
|
|
},
|
|
|
|
onBack() {
|
|
wx.navigateBack()
|
|
},
|
|
})
|