- 新增 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 报错
91 lines
2.5 KiB
TypeScript
91 lines
2.5 KiB
TypeScript
import { Post } from '../../types/index'
|
|
import { formatTime, formatCount, getAvatarGradient } from '../../utils/format'
|
|
import { api } from '../../utils/api'
|
|
|
|
Component({
|
|
properties: {
|
|
post: {
|
|
type: Object,
|
|
value: null,
|
|
},
|
|
},
|
|
|
|
data: {
|
|
avatarGradient: '',
|
|
timeText: '',
|
|
likeCount: 0,
|
|
imageGridClass: 'post-img-grid-0',
|
|
},
|
|
|
|
observers: {
|
|
post(val: Post) {
|
|
if (!val) return
|
|
this.setData({
|
|
avatarGradient: getAvatarGradient(val.authorId || ''),
|
|
timeText: formatTime(val.createdAt),
|
|
likeCount: typeof val.likeCount === 'number' ? val.likeCount : 0,
|
|
imageGridClass: this.getImageGridClass(val.images),
|
|
})
|
|
},
|
|
},
|
|
|
|
methods: {
|
|
getImageGridClass(images?: string[]) {
|
|
if (!Array.isArray(images)) return 'post-img-grid-0'
|
|
return `post-img-grid-${images.length > 3 ? 'multi' : images.length}`
|
|
},
|
|
|
|
onCardTap() {
|
|
const post = this.data.post as Post
|
|
if (!post) return
|
|
wx.navigateTo({ url: `/pages/post-detail/post-detail?id=${post._id}` })
|
|
},
|
|
|
|
onAuthorTap() {
|
|
const post = this.data.post as Post
|
|
if (!post?.authorId) return
|
|
wx.navigateTo({ url: `/pages/pet-detail/pet-detail?userId=${post.authorId}` })
|
|
},
|
|
|
|
onImageTap(e: WechatMiniprogram.CustomEvent) {
|
|
const post = this.data.post as Post
|
|
if (!post?.images?.length) return
|
|
wx.previewImage({
|
|
current: post.images[e.currentTarget.dataset.idx as number],
|
|
urls: post.images,
|
|
})
|
|
},
|
|
|
|
async onLike() {
|
|
const post = this.data.post as Post
|
|
if (!post) return
|
|
const wasLiked = post.isLiked
|
|
const prevCount = this.data.likeCount
|
|
|
|
this.setData({
|
|
'post.isLiked': !wasLiked,
|
|
likeCount: wasLiked ? prevCount - 1 : prevCount + 1,
|
|
})
|
|
|
|
try {
|
|
const res = await api.likePost(post._id)
|
|
this.setData({ likeCount: res.count, 'post.isLiked': res.liked })
|
|
this.triggerEvent('likeChange', { postId: post._id, liked: res.liked, count: res.count })
|
|
} catch {
|
|
this.setData({ 'post.isLiked': wasLiked, likeCount: prevCount })
|
|
wx.showToast({ title: '操作失败', icon: 'none' })
|
|
}
|
|
},
|
|
|
|
onComment() {
|
|
const post = this.data.post as Post
|
|
if (!post) return
|
|
wx.navigateTo({ url: `/pages/post-detail/post-detail?id=${post._id}&focus=comment` })
|
|
},
|
|
|
|
onShare() {
|
|
wx.showShareMenu({ withShareTicket: true, menus: ['shareAppMessage', 'shareTimeline'] })
|
|
},
|
|
},
|
|
})
|