import { api } from '../../utils/api' import { BREED_OPTIONS } from '../../utils/constants' import { getPetEmoji } from '../../utils/format' const SPECIES_LABELS: Record = { dog: '狗狗', cat: '猫猫', other: '其他' } const PET_TAGS = ['活泼', '温顺', '黏人', '独立', '爱玩', '怕生', '聪明', '贪吃', '爱运动', '安静'] const app = getApp<{ userInfo: any }>() Page({ data: { statusBarHeight: 0, isEdit: false, petId: '', form: { name: '', species: 'dog' as 'dog' | 'cat' | 'other', breed: '', gender: 'female' as 'female' | 'male', birthday: '', bio: '', tags: [] as string[], emoji: '🐶', }, speciesLabel: '狗狗', allTags: PET_TAGS, }, onLoad(query: { id?: string }) { const info = wx.getSystemInfoSync() this.setData({ statusBarHeight: info.statusBarHeight }) if (query.id) { this.setData({ isEdit: true, petId: query.id }) this.loadPet(query.id) } }, loadPet(id: string) { const user = app.globalData?.userInfo const pet = user?.pets?.find((p: any) => p._id === id) if (!pet) return this.setData({ form: { name: pet.name, species: pet.species, breed: pet.breed, gender: pet.gender, birthday: pet.birthday || '', bio: pet.bio || '', tags: pet.tags || [], emoji: pet.emoji, }, speciesLabel: SPECIES_LABELS[pet.species] || '狗狗', }) }, onField(e: WechatMiniprogram.CustomEvent) { const key = e.currentTarget.dataset.key as string this.setData({ [`form.${key}`]: e.detail.value }) }, onSpecies() { wx.showActionSheet({ itemList: ['狗狗', '猫猫', '其他'], success: res => { const species = (['dog', 'cat', 'other'] as const)[res.tapIndex] this.setData({ 'form.species': species, 'form.breed': '', speciesLabel: SPECIES_LABELS[species], 'form.emoji': getPetEmoji(species), }) }, }) }, onBreed() { const breeds = BREED_OPTIONS[this.data.form.species] || BREED_OPTIONS.dog wx.showActionSheet({ itemList: breeds, success: res => { const breed = breeds[res.tapIndex] this.setData({ 'form.breed': breed, 'form.emoji': getPetEmoji(this.data.form.species, breed), }) }, }) }, onGender(e: WechatMiniprogram.CustomEvent) { this.setData({ 'form.gender': e.currentTarget.dataset.v }) }, onBirthday(e: WechatMiniprogram.CustomEvent) { const dob = e.detail.value as string const diffMs = Date.now() - new Date(dob).getTime() const years = Math.floor(diffMs / (365.25 * 24 * 3600 * 1000)) const months = Math.floor((diffMs % (365.25 * 24 * 3600 * 1000)) / (30.44 * 24 * 3600 * 1000)) const age = years > 0 ? `${years}岁${months > 0 ? months + '个月' : ''}` : `${months}个月` this.setData({ 'form.birthday': dob, 'form.age': age }) }, onTagToggle(e: WechatMiniprogram.CustomEvent) { const tag = e.currentTarget.dataset.tag as string const tags = [...this.data.form.tags] const idx = tags.indexOf(tag) if (idx >= 0) tags.splice(idx, 1) else if (tags.length < 5) tags.push(tag) this.setData({ 'form.tags': tags }) }, async onChoosePhoto() { wx.chooseMedia({ count: 1, mediaType: ['image'], sourceType: ['album', 'camera'], success: async res => { const path = res.tempFiles[0].tempFilePath wx.showLoading({ title: '上传中...' }) try { const cloudPath = `pet-avatars/${Date.now()}.jpg` const r = await wx.cloud.uploadFile({ cloudPath, filePath: path }) this.setData({ 'form.avatarUrl': r.fileID }) wx.hideLoading() } catch { wx.hideLoading() wx.showToast({ title: '上传失败', icon: 'none' }) } }, }) }, async onSave() { const { form, isEdit, petId } = this.data if (!form.name.trim()) { wx.showToast({ title: '请填写宠物名字', icon: 'none' }) return } wx.showLoading({ title: '保存中...' }) try { await api.updatePet(isEdit ? { ...form, _id: petId } : form) wx.hideLoading() wx.showToast({ title: '保存成功', icon: 'success' }) setTimeout(() => wx.navigateBack(), 800) } catch { wx.hideLoading() wx.showToast({ title: '保存失败', icon: 'none' }) } }, onDelete() { wx.showModal({ title: '删除宠物档案', content: '确定要删除吗?', confirmText: '删除', confirmColor: '#ff4f91', success: res => { if (res.confirm) { wx.showToast({ title: '已删除', icon: 'success' }) setTimeout(() => wx.navigateBack(), 800) } }, }) }, onBack() { wx.navigateBack() }, })