Files
PetCommunity/miniprogram/pages/post/post.ts
chenwu 3d21a9d297 fix: 修复评论/资料/导航胶囊/宠物档案/定位等多处问题,补全相关云函数
- 新增 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 报错
2026-06-07 15:59:21 +08:00

182 lines
5.2 KiB
TypeScript

import { api } from '../../utils/api'
import { chooseAndUploadMedia } from '../../utils/auth'
import { MOOD_OPTIONS, HOT_HASHTAGS } from '../../utils/constants'
import { getPetEmoji } from '../../utils/format'
import { AppGlobalData } from '../../types/index'
const app = getApp<{ globalData: AppGlobalData }>()
Page({
data: {
statusBarHeight: 0,
navbarHeight: 44,
navbarPaddingRight: 0,
content: '',
images: [] as any[],
location: null as any,
selectedPetId: '',
selectedHashtags: [] as string[],
hashtagInput: '',
selectedMood: '',
moods: MOOD_OPTIONS,
hotHashtags: HOT_HASHTAGS.slice(0, 6),
myPets: [] as any[],
submitting: false,
canSubmit: false,
},
onLoad() {
const { statusBarHeight, navbarHeight, navbarPaddingRight } = app.globalData.navBarInfo
this.setData({ statusBarHeight, navbarHeight, navbarPaddingRight })
this.loadMyPets()
},
loadMyPets() {
const user = app.globalData?.userInfo
if (!user?.pets?.length) return
const pets = user.pets.map((p: any) => ({
...p,
emoji: getPetEmoji(p.species, p.breed),
}))
this.setData({
myPets: pets,
selectedPetId: pets[0]?._id || '',
})
},
onContentInput(e: WechatMiniprogram.CustomEvent) {
this.setData({ content: e.detail.value })
this.checkCanSubmit()
},
checkCanSubmit() {
const { content, images } = this.data
this.setData({ canSubmit: content.trim().length > 0 || images.length > 0 })
},
async onChooseImage() {
try {
const fileIDs = await chooseAndUploadMedia(9 - this.data.images.length)
const newImages = fileIDs.map(id => ({ fileID: id, tempPath: id }))
this.setData({ images: [...this.data.images, ...newImages] })
this.checkCanSubmit()
} catch (e: any) {
if (e?.errMsg?.includes('cancel')) return
wx.showToast({ title: '上传失败', icon: 'none' })
}
},
onRemoveImage(e: WechatMiniprogram.CustomEvent) {
const idx = e.currentTarget.dataset.idx as number
const images = [...this.data.images]
images.splice(idx, 1)
this.setData({ images })
this.checkCanSubmit()
},
onPreviewImage(e: WechatMiniprogram.CustomEvent) {
const idx = e.currentTarget.dataset.idx as number
wx.previewImage({
current: this.data.images[idx]?.tempPath || this.data.images[idx],
urls: this.data.images.map((img: any) => img.tempPath || img),
})
},
onChooseLocation() {
wx.chooseLocation({
success: res => {
this.setData({
location: {
name: res.name || res.address,
latitude: res.latitude,
longitude: res.longitude,
},
})
},
fail: () => {},
})
},
onRemoveLocation() {
this.setData({ location: null })
},
onSelectPet(e: WechatMiniprogram.CustomEvent) {
const id = e.currentTarget.dataset.id as string
this.setData({ selectedPetId: this.data.selectedPetId === id ? '' : id })
},
onHashtagInput(e: WechatMiniprogram.CustomEvent) {
this.setData({ hashtagInput: e.detail.value })
},
onAddHashtag() {
let tag = this.data.hashtagInput.trim().replace(/^#/, '')
if (!tag) return
const tags = [...this.data.selectedHashtags]
if (!tags.includes(tag) && tags.length < 5) {
tags.push(tag)
}
this.setData({ selectedHashtags: tags, hashtagInput: '' })
},
onRemoveHashtag(e: WechatMiniprogram.CustomEvent) {
const idx = e.currentTarget.dataset.idx as number
const tags = [...this.data.selectedHashtags]
tags.splice(idx, 1)
this.setData({ selectedHashtags: tags })
},
onHotTag(e: WechatMiniprogram.CustomEvent) {
const tag = (e.currentTarget.dataset.tag as string).replace(/^#/, '')
const tags = [...this.data.selectedHashtags]
if (!tags.includes(tag) && tags.length < 5) {
tags.push(tag)
this.setData({ selectedHashtags: tags })
}
},
onSelectMood(e: WechatMiniprogram.CustomEvent) {
const val = e.currentTarget.dataset.val as string
this.setData({ selectedMood: this.data.selectedMood === val ? '' : val })
},
async onSubmit() {
if (!this.data.canSubmit || this.data.submitting) return
this.setData({ submitting: true })
try {
const moodLabel = MOOD_OPTIONS.find(m => m.value === this.data.selectedMood)?.label
await api.createPost({
content: this.data.content.trim(),
images: this.data.images.map((img: any) => img.fileID || img),
location: this.data.location,
hashtags: this.data.selectedHashtags.map(t => `#${t}`),
mood: moodLabel,
petId: this.data.selectedPetId || undefined,
})
wx.showToast({ title: '发布成功!', icon: 'success' })
setTimeout(() => wx.navigateBack(), 1200)
} catch {
wx.showToast({ title: '发布失败,请重试', icon: 'none' })
this.setData({ submitting: false })
}
},
onCancel() {
if (this.data.content || this.data.images.length > 0) {
wx.showModal({
title: '放弃编辑?',
content: '内容将不会保存',
confirmText: '放弃',
cancelText: '继续',
success: res => { if (res.confirm) wx.navigateBack() },
})
} else {
wx.navigateBack()
}
},
})