index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <template>
  2. <view class="pet-edit-page">
  3. <nav-bar title="编辑宠物档案"></nav-bar>
  4. <view v-if="loading" class="loading-state">
  5. <uni-load-more status="loading"></uni-load-more>
  6. </view>
  7. <block v-else>
  8. <!-- 基础信息 -->
  9. <view class="section-title">基础信息</view>
  10. <view class="form-card">
  11. <view class="form-item">
  12. <text class="form-label require">宠物名称</text>
  13. <input class="form-input" v-model="form.name" placeholder="请输入宠物名称" />
  14. </view>
  15. <view class="form-item">
  16. <text class="form-label require">宠物类型</text>
  17. <picker :range="typeOptions" range-key="label" @change="onTypeChange">
  18. <view class="picker-value">{{ getTypeLabel }}</view>
  19. </picker>
  20. </view>
  21. <view class="form-item">
  22. <text class="form-label require">所属主人</text>
  23. <picker :range="customerOptions" range-key="name" @change="onCustomerChange">
  24. <view class="picker-value" :class="{'placeholder': !form.userId}">{{ getCustomerLabel }}</view>
  25. </picker>
  26. </view>
  27. <view class="form-item">
  28. <text class="form-label require">品种</text>
  29. <view class="form-combox-wrapper">
  30. <uni-combox :candidates="breedCandidates" v-model="form.breed" placeholder="请选择或输入品种" emptyTips="未找到对应品种,可直接输入"></uni-combox>
  31. </view>
  32. </view>
  33. <view class="form-item">
  34. <text class="form-label">性别</text>
  35. <picker :range="genderOptions" range-key="label" @change="onGenderChange">
  36. <view class="picker-value">{{ getGenderLabel }}</view>
  37. </picker>
  38. </view>
  39. <view class="form-item">
  40. <text class="form-label require">体型</text>
  41. <picker :range="sizeOptions" range-key="label" @change="onSizeChange">
  42. <view class="picker-value" :class="{'placeholder': !form.size}">{{ getSizeLabel }}</view>
  43. </picker>
  44. </view>
  45. <view class="form-item">
  46. <text class="form-label require">年龄(岁)</text>
  47. <input class="form-input" v-model="form.age" type="number" placeholder="请输入年龄" />
  48. </view>
  49. <view class="form-item">
  50. <text class="form-label require">体重(kg)</text>
  51. <input class="form-input" v-model="form.weight" type="digit" placeholder="请输入体重" />
  52. </view>
  53. </view>
  54. <!-- 性格特征 -->
  55. <view class="section-title">性格特征</view>
  56. <view class="form-card">
  57. <view class="form-item">
  58. <text class="form-label">性格关键词</text>
  59. <input class="form-input" v-model="form.personality" placeholder="例如活泼、粘人" />
  60. </view>
  61. <view class="form-item vertical">
  62. <text class="form-label">萌宠性格描述</text>
  63. <textarea class="form-textarea" v-model="form.cutePersonality" placeholder="详细描述宠物的性格"></textarea>
  64. </view>
  65. </view>
  66. <!-- 家庭信息 -->
  67. <view class="section-title">家庭信息</view>
  68. <view class="form-card">
  69. <view class="form-item">
  70. <text class="form-label require">房屋类型</text>
  71. <picker :range="houseTypeOptions" range-key="label" @change="onHouseTypeChange">
  72. <view class="picker-value" :class="{'placeholder': !form.houseType}">{{ getHouseTypeLabel }}</view>
  73. </picker>
  74. </view>
  75. <view class="form-item">
  76. <text class="form-label require">入门方式</text>
  77. <picker :range="entryMethodOptions" range-key="label" @change="onEntryMethodChange">
  78. <view class="picker-value" :class="{'placeholder': !form.entryMethod}">{{ getEntryMethodLabel }}</view>
  79. </picker>
  80. </view>
  81. <view class="form-item" v-if="form.entryMethod === 'password'">
  82. <text class="form-label require">门锁密码</text>
  83. <input class="form-input" v-model="form.entryPassword" placeholder="请输入门锁密码" />
  84. </view>
  85. <view class="form-item" v-if="form.entryMethod === 'key'">
  86. <text class="form-label require">钥匙存放处</text>
  87. <input class="form-input" v-model="form.keyLocation" placeholder="请输入取钥匙位置" />
  88. </view>
  89. </view>
  90. <!-- 健康状况 -->
  91. <view class="section-title">健康状况</view>
  92. <view class="form-card">
  93. <view class="form-item">
  94. <text class="form-label require">健康状态</text>
  95. <picker :range="healthStatusOptions" @change="onHealthChange">
  96. <view class="picker-value">{{ form.healthStatus || '请选择' }}</view>
  97. </picker>
  98. </view>
  99. <view class="form-item">
  100. <text class="form-label require">疫苗接种</text>
  101. <picker :range="vaccineOptions" @change="onVaccineChange">
  102. <view class="picker-value">{{ form.vaccineStatus || '请选择' }}</view>
  103. </picker>
  104. </view>
  105. <view class="form-item">
  106. <text class="form-label require">攻击倾向</text>
  107. <picker :range="aggressionOptions" range-key="label" @change="onAggressionChange">
  108. <view class="picker-value">{{ getAggressionLabel }}</view>
  109. </picker>
  110. </view>
  111. <view class="form-item vertical">
  112. <text class="form-label require">既往病史</text>
  113. <textarea class="form-textarea" v-model="form.medicalHistory" placeholder="如有病史请务必记录"></textarea>
  114. </view>
  115. <view class="form-item vertical">
  116. <text class="form-label require">过敏史</text>
  117. <textarea class="form-textarea" v-model="form.allergies" placeholder="如有过敏源请务必记录"></textarea>
  118. </view>
  119. <view class="form-item vertical">
  120. <text class="form-label">补充备注</text>
  121. <textarea class="form-textarea" v-model="form.remark" placeholder="请输入其他备注信息"></textarea>
  122. </view>
  123. </view>
  124. <!-- 底部固定操作栏 -->
  125. <view class="footer-bar">
  126. <button class="save-btn" @click="onSave">保存修改</button>
  127. </view>
  128. </block>
  129. </view>
  130. </template>
  131. <script setup>
  132. // @Author: Antigravity
  133. import { ref, reactive, computed } from 'vue'
  134. import { onLoad } from '@dcloudio/uni-app'
  135. import { getPet, updatePet } from '@/api/archieves/pet'
  136. import { listAllCustomer } from '@/api/archieves/customer'
  137. import { getDicts } from '@/api/system/dict/data'
  138. import navBar from '@/components/nav-bar/index.vue'
  139. import customerEnums from '@/json/customer.json'
  140. const loading = ref(true)
  141. const typeOptions = [{ label: '猫', value: 1 }, { label: '狗', value: 2 }, { label: '其他', value: 3 }]
  142. const genderOptions = [{ label: '未知', value: 0 }, { label: '公', value: 1 }, { label: '母', value: 2 }]
  143. const sizeOptions = [{ label: '小型(0-10kg)', value: 'small' }, { label: '中型(10-25kg)', value: 'medium' }, { label: '大型(25kg+)', value: 'large' }]
  144. const { houseTypeOptions, entryMethodOptions } = customerEnums
  145. const healthStatusOptions = ['健康', '亚健康', '疾病']
  146. const vaccineOptions = ['无', '已打1次', '已打2次', '已打3次']
  147. const aggressionOptions = [{ label: '否', value: 0 }, { label: '是', value: 1 }]
  148. const customerOptions = ref([])
  149. const breedCandidates = ref([])
  150. const petId = ref(null)
  151. const form = reactive({
  152. id: '', name: '', userId: '', type: 1, breed: '', gender: 0, age: '', weight: '', size: '',
  153. arrivalTime: '', houseType: '', entryMethod: '', entryPassword: '', keyLocation: '',
  154. personality: '', cutePersonality: '', healthStatus: '健康', aggression: 0,
  155. vaccineStatus: '无', medicalHistory: '', allergies: '', remark: ''
  156. })
  157. onLoad(async (options) => {
  158. if (options.id) {
  159. petId.value = options.id
  160. // 1. 获取所有必要的基础数据(客人列表、品种字典)
  161. await initData()
  162. // 2. 获取详情并回显
  163. await fetchDetail()
  164. } else {
  165. uni.showToast({ title: '参数错误', icon: 'none' })
  166. setTimeout(() => uni.navigateBack(), 1500)
  167. }
  168. })
  169. const initData = async () => {
  170. try {
  171. const [custRes, dictRes] = await Promise.all([
  172. listAllCustomer({ status: 0 }),
  173. getDicts('sys_pet_breed')
  174. ])
  175. customerOptions.value = Array.isArray(custRes) ? custRes : (custRes?.data || [])
  176. const dictList = Array.isArray(dictRes) ? dictRes : (dictRes?.data || [])
  177. breedCandidates.value = dictList.map(item => item.dictLabel || item.dictValue || item.label || item.value)
  178. } catch(e) {
  179. console.error('初始化数据失败', e)
  180. }
  181. }
  182. const fetchDetail = async () => {
  183. try {
  184. loading.value = true
  185. const res = await getPet(petId.value)
  186. if (res) {
  187. // 将接口返回的数据合并到响应式表单对象中
  188. Object.assign(form, res)
  189. }
  190. } catch (e) {
  191. console.error('获取宠物详情失败', e)
  192. } finally {
  193. loading.value = false
  194. }
  195. }
  196. // 标签转换逻辑
  197. const getTypeLabel = computed(() => typeOptions.find(o => o.value === form.type)?.label || '请选择')
  198. const onTypeChange = (e) => { form.type = typeOptions[e.detail.value].value }
  199. const getCustomerLabel = computed(() => {
  200. const opt = customerOptions.value.find(o => String(o.id) === String(form.userId))
  201. return opt ? opt.name : '请选择主人'
  202. })
  203. const onCustomerChange = (e) => { form.userId = customerOptions.value[e.detail.value].id }
  204. const getGenderLabel = computed(() => genderOptions.find(o => o.value === form.gender)?.label || '未知')
  205. const onGenderChange = (e) => { form.gender = genderOptions[e.detail.value].value }
  206. const getSizeLabel = computed(() => sizeOptions.find(o => o.value === form.size)?.label || '请选择')
  207. const onSizeChange = (e) => { form.size = sizeOptions[e.detail.value].value }
  208. const getHouseTypeLabel = computed(() => houseTypeOptions.find(o => o.value === form.houseType)?.label || '请选择')
  209. const onHouseTypeChange = (e) => { form.houseType = houseTypeOptions[e.detail.value].value }
  210. const getEntryMethodLabel = computed(() => entryMethodOptions.find(o => o.value === form.entryMethod)?.label || '请选择')
  211. const onEntryMethodChange = (e) => { form.entryMethod = entryMethodOptions[e.detail.value].value }
  212. const onHealthChange = (e) => { form.healthStatus = healthStatusOptions[e.detail.value] }
  213. const onVaccineChange = (e) => { form.vaccineStatus = vaccineOptions[e.detail.value] }
  214. const getAggressionLabel = computed(() => aggressionOptions.find(o => o.value === form.aggression)?.label || '否')
  215. const onAggressionChange = (e) => { form.aggression = aggressionOptions[e.detail.value].value }
  216. const onSave = async () => {
  217. // 校验逻辑
  218. if (!form.name) return uni.showToast({ title: '请输入宠物名称', icon: 'none' })
  219. if (!form.userId) return uni.showToast({ title: '请选择所属主人', icon: 'none' })
  220. if (!form.breed) return uni.showToast({ title: '请输入或选择品种', icon: 'none' })
  221. if (!form.size) return uni.showToast({ title: '请选择体型', icon: 'none' })
  222. if (!form.age) return uni.showToast({ title: '请输入年龄', icon: 'none' })
  223. if (!form.weight) return uni.showToast({ title: '请输入体重', icon: 'none' })
  224. if (!form.houseType) return uni.showToast({ title: '请选择房屋类型', icon: 'none' })
  225. if (!form.entryMethod) return uni.showToast({ title: '请选择入门方式', icon: 'none' })
  226. try {
  227. uni.showLoading({ title: '保存中' })
  228. await updatePet(form)
  229. uni.hideLoading()
  230. uni.showToast({ title: '保存成功', icon: 'success' })
  231. setTimeout(() => uni.navigateBack(), 1000)
  232. } catch (error) {
  233. uni.hideLoading()
  234. }
  235. }
  236. </script>
  237. <style lang="scss" scoped>
  238. .pet-edit-page { min-height: 100vh; background: #f7f8fa; padding-bottom: calc(140rpx + env(safe-area-inset-bottom)); }
  239. .loading-state { padding-top: 100rpx; }
  240. .section-title { font-size: 28rpx; font-weight: bold; color: #666; padding: 24rpx 32rpx 12rpx; }
  241. .form-card { background: #fff; border-radius: 24rpx; padding: 8rpx 32rpx; margin: 0 24rpx; }
  242. .form-item { display: flex; align-items: center; padding: 28rpx 0; border-bottom: 1rpx solid #f5f5f5; }
  243. .form-item:last-child { border-bottom: none; }
  244. .form-item.vertical { flex-direction: column; align-items: flex-start; border-bottom: none; }
  245. .form-item.vertical .form-textarea { width: 100%; height: 160rpx; margin-top: 16rpx; background: #f9f9f9; padding: 16rpx; box-sizing: border-box; border-radius: 12rpx; }
  246. .form-label { width: 220rpx; font-size: 28rpx; color: #333; flex-shrink: 0; }
  247. .form-label.require::before { content: '*'; color: #f56c6c; margin-right: 4rpx; }
  248. .form-input { flex: 1; font-size: 28rpx; color: #333; text-align: right; }
  249. .form-combox-wrapper { flex: 1; min-width: 0; }
  250. .picker-value { flex: 1; font-size: 28rpx; color: #333; text-align: right; min-height: 40rpx; }
  251. .picker-value.placeholder { color: #ccc; }
  252. .footer-bar { position: fixed; bottom: 0; left: 0; right: 0; background: #fff; padding: 20rpx 32rpx calc(20rpx + env(safe-area-inset-bottom)); box-shadow: 0 -4rpx 16rpx rgba(0, 0, 0, 0.05); z-index: 100; }
  253. .save-btn { width: 100%; height: 88rpx; background: linear-gradient(90deg, #ffd53f, #ff9500); color: #333; border: none; border-radius: 44rpx; font-size: 32rpx; font-weight: bold; line-height: 88rpx; }
  254. :deep(.uni-combox) { border: none !important; }
  255. :deep(.uni-combox__input-box) { display: flex; justify-content: flex-end; padding-right: 0; }
  256. :deep(.uni-combox__input) { text-align: right; font-size: 28rpx; padding-right: 0;}
  257. :deep(.uni-combox__input-plholder) { color: #ccc; text-align: right; font-size: 28rpx; padding-right: 0;}
  258. </style>