Files
PetCommunity/miniprogram/pages/pet-detail/pet-detail.ts
2026-06-05 17:46:51 +08:00

79 lines
1.9 KiB
TypeScript

import { api } from '../../utils/api'
import { getAvatarGradient, getPetEmoji } from '../../utils/format'
const app = getApp<{ userInfo: any }>()
Page({
data: {
statusBarHeight: 0,
userId: '',
user: {} as any,
posts: [] as any[],
avatarGradient: '',
isMe: false,
isFollowing: false,
loading: false,
},
onLoad(query: { userId?: string }) {
const info = wx.getSystemInfoSync()
this.setData({ statusBarHeight: info.statusBarHeight })
const userId = query.userId || ''
const myId = app.globalData?.userInfo?._id || ''
this.setData({ userId, isMe: userId === myId })
this.loadUser(userId)
},
async loadUser(userId: string) {
this.setData({ loading: true })
try {
const [user, postsRes] = await Promise.all([
api.getUserProfile(userId),
api.getUserPosts(userId),
])
const enrichedPets = (user.pets || []).map((p: any) => ({
...p,
emoji: getPetEmoji(p.species, p.breed),
}))
this.setData({
user: { ...user, pets: enrichedPets },
avatarGradient: getAvatarGradient(userId),
posts: postsRes.list,
})
} catch {
wx.showToast({ title: '加载失败', icon: 'none' })
} finally {
this.setData({ loading: false })
}
},
async onFollow() {
try {
const res = await api.followUser(this.data.userId)
this.setData({ isFollowing: res.following })
} catch {
wx.showToast({ title: '操作失败', icon: 'none' })
}
},
onMessage() {
wx.navigateTo({ url: `/pages/chat/chat?userId=${this.data.userId}` })
},
onBack() {
wx.navigateBack()
},
onMore() {
wx.showActionSheet({
itemList: ['举报', '拉黑'],
success: res => {
if (res.tapIndex === 0) {
wx.showToast({ title: '已提交举报', icon: 'success' })
}
},
})
},
})