- 新增 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 报错
157 lines
4.3 KiB
TypeScript
157 lines
4.3 KiB
TypeScript
import { api } from '../../utils/api'
|
|
import { formatTime, getAvatarGradient } from '../../utils/format'
|
|
import { AppGlobalData } from '../../types/index'
|
|
|
|
const app = getApp<{ globalData: AppGlobalData }>()
|
|
|
|
function normalizeAuthor(author: any, authorId = '') {
|
|
return {
|
|
...(author || {}),
|
|
nickName: author?.nickName || '匿名用户',
|
|
avatarUrl: author?.avatarUrl || '',
|
|
openid: author?.openid || authorId,
|
|
}
|
|
}
|
|
|
|
function normalizePost(post: any) {
|
|
return {
|
|
authorId: '',
|
|
...(post || {}),
|
|
author: normalizeAuthor(post?.author, post?.authorId),
|
|
content: post?.content || '',
|
|
images: Array.isArray(post?.images) ? post.images : [],
|
|
hashtags: Array.isArray(post?.hashtags) ? post.hashtags : [],
|
|
likeCount: typeof post?.likeCount === 'number' ? post.likeCount : 0,
|
|
commentCount: typeof post?.commentCount === 'number' ? post.commentCount : 0,
|
|
createdAt: post?.createdAt || '',
|
|
}
|
|
}
|
|
|
|
function normalizeComment(comment: any) {
|
|
return {
|
|
authorId: '',
|
|
...(comment || {}),
|
|
author: normalizeAuthor(comment?.author, comment?.authorId),
|
|
content: comment?.content || '',
|
|
createdAt: comment?.createdAt || '',
|
|
gradient: getAvatarGradient(comment?.authorId || ''),
|
|
timeText: comment?.createdAt ? formatTime(comment.createdAt) : '',
|
|
}
|
|
}
|
|
|
|
Page({
|
|
data: {
|
|
statusBarHeight: 0,
|
|
postId: '',
|
|
post: null as any,
|
|
comments: [] as any[],
|
|
avatarGradient: '',
|
|
timeText: '',
|
|
likeCount: 0,
|
|
commentText: '',
|
|
focusInput: false,
|
|
pendingFocusComment: false,
|
|
},
|
|
|
|
onLoad(query: { id?: string; focus?: string }) {
|
|
const info = wx.getSystemInfoSync()
|
|
this.setData({
|
|
statusBarHeight: info.statusBarHeight,
|
|
postId: query.id || '',
|
|
pendingFocusComment: query.focus === 'comment',
|
|
})
|
|
this.loadPost(query.id || '')
|
|
},
|
|
|
|
async loadPost(id: string) {
|
|
if (!id) return
|
|
try {
|
|
const res = await wx.cloud.callFunction({
|
|
name: 'getPost',
|
|
data: { postId: id },
|
|
}) as any
|
|
const post = normalizePost(res.result?.data)
|
|
if (!post?._id) return
|
|
this.setData({
|
|
post,
|
|
avatarGradient: getAvatarGradient(post.authorId || ''),
|
|
timeText: post.createdAt ? formatTime(post.createdAt) : '',
|
|
likeCount: post.likeCount,
|
|
})
|
|
if (this.data.pendingFocusComment) {
|
|
wx.nextTick(() => {
|
|
this.setData({ focusInput: true, pendingFocusComment: false })
|
|
})
|
|
}
|
|
this.loadComments(id)
|
|
} catch {}
|
|
},
|
|
|
|
async loadComments(postId: string) {
|
|
try {
|
|
const res = await api.getComments(postId)
|
|
const comments = res.list.map(normalizeComment)
|
|
this.setData({ comments })
|
|
} catch {}
|
|
},
|
|
|
|
async onLike() {
|
|
const post = this.data.post
|
|
if (!post) return
|
|
const was = post.isLiked
|
|
this.setData({ 'post.isLiked': !was, likeCount: was ? this.data.likeCount - 1 : this.data.likeCount + 1 })
|
|
try {
|
|
const res = await api.likePost(post._id)
|
|
this.setData({ likeCount: res.count, 'post.isLiked': res.liked })
|
|
} catch {
|
|
this.setData({ 'post.isLiked': was, likeCount: post.likeCount })
|
|
}
|
|
},
|
|
|
|
onCommentInput(e: WechatMiniprogram.CustomEvent) {
|
|
this.setData({ commentText: e.detail.value })
|
|
},
|
|
|
|
onCommentBlur() {
|
|
this.setData({ focusInput: false })
|
|
},
|
|
|
|
async onSend() {
|
|
const text = this.data.commentText.trim()
|
|
if (!text || !this.data.postId) return
|
|
this.setData({ commentText: '' })
|
|
try {
|
|
await api.addComment(this.data.postId, text)
|
|
const comments = await api.getComments(this.data.postId)
|
|
this.setData({
|
|
comments: comments.list.map(normalizeComment),
|
|
})
|
|
} catch {
|
|
wx.showToast({ title: '发送失败', icon: 'none' })
|
|
}
|
|
},
|
|
|
|
focusComment() {
|
|
this.setData({ focusInput: true })
|
|
},
|
|
|
|
onAuthorTap() {
|
|
const post = this.data.post
|
|
if (post?.authorId) wx.navigateTo({ url: `/pages/pet-detail/pet-detail?userId=${post.authorId}` })
|
|
},
|
|
|
|
onImgTap(e: WechatMiniprogram.CustomEvent) {
|
|
const post = this.data.post
|
|
wx.previewImage({ current: post.images[e.currentTarget.dataset.idx], urls: post.images })
|
|
},
|
|
|
|
onMore() {
|
|
wx.showActionSheet({
|
|
itemList: ['举报'],
|
|
success: () => wx.showToast({ title: '已提交举报', icon: 'success' }),
|
|
})
|
|
},
|
|
|
|
onBack() { wx.navigateBack() },
|
|
})
|