import { api } from '../../utils/api' import { formatTime, getAvatarGradient } from '../../utils/format' const app = getApp<{ userInfo: any }>() Page({ data: { statusBarHeight: 0, postId: '', post: null as any, comments: [] as any[], avatarGradient: '', timeText: '', likeCount: 0, commentText: '', focusInput: false, }, onLoad(query: { id?: string; focus?: string }) { const info = wx.getSystemInfoSync() this.setData({ statusBarHeight: info.statusBarHeight, postId: query.id || '' }) this.loadPost(query.id || '') if (query.focus === 'comment') { this.setData({ focusInput: true }) } }, async loadPost(id: string) { if (!id) return try { const res = await wx.cloud.callFunction({ name: 'getPost', data: { postId: id }, }) as any const post = res.result?.data if (!post) return this.setData({ post, avatarGradient: getAvatarGradient(post.authorId), timeText: formatTime(post.createdAt), likeCount: post.likeCount, }) this.loadComments(id) } catch {} }, async loadComments(postId: string) { try { const res = await api.getComments(postId) const comments = res.list.map((c: any) => ({ ...c, gradient: getAvatarGradient(c.authorId), timeText: formatTime(c.createdAt), })) 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 }) }, 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((c: any) => ({ ...c, gradient: getAvatarGradient(c.authorId), timeText: formatTime(c.createdAt), })), }) } 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() }, })