index.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. <template>
  2. <view class="pet-edit-page">
  3. <nav-bar title="编辑宠物档案"></nav-bar>
  4. <!-- 宠物预览/头像修改 @Author: Antigravity -->
  5. <view class="avatar-section" @click="handleChooseAvatar">
  6. <image :src="tempAvatarUrl || '/static/images/profile.png'" class="avatar-img" mode="aspectFill"></image>
  7. <view class="avatar-tip">点击修改宠物头像</view>
  8. </view>
  9. <view v-if="loading" class="loading-state">
  10. <!-- CSS 原生 Loading @Author: Antigravity -->
  11. <view class="spinner"></view>
  12. <text class="loading-txt">加载中...</text>
  13. </view>
  14. <block v-else>
  15. <!-- 基础信息 -->
  16. <view class="section-title">基础信息</view>
  17. <view class="form-card">
  18. <view class="form-item">
  19. <text class="form-label require">宠物名称</text>
  20. <input class="form-input" v-model="form.name" placeholder="请输入宠物名称" />
  21. </view>
  22. <view class="form-item" @click="showTypeSelect = true">
  23. <text class="form-label require">宠物类型</text>
  24. <view class="picker-value">{{ getTypeLabel }}</view>
  25. <view class="right-arrow"></view>
  26. </view>
  27. <view class="form-item" @click="showCustomerSelect = true">
  28. <text class="form-label require">所属主人</text>
  29. <view class="picker-value" :class="{'placeholder': !form.userId}">{{ getCustomerLabel }}</view>
  30. <view class="right-arrow"></view>
  31. </view>
  32. <view class="form-item" @click="showBreedPicker = true">
  33. <text class="form-label require">品种</text>
  34. <view class="picker-value" :class="{'placeholder': !form.breed}">{{ form.breed || '可选择或自填品种' }}</view>
  35. <view class="right-arrow"></view>
  36. </view>
  37. <view class="form-item" @click="showGenderSelect = true">
  38. <text class="form-label">性别</text>
  39. <view class="picker-value">{{ getGenderLabel }}</view>
  40. <view class="right-arrow"></view>
  41. </view>
  42. <view class="form-item" @click="showSizeSelect = true">
  43. <text class="form-label require">体型</text>
  44. <view class="picker-value" :class="{'placeholder': !form.size}">{{ getSizeLabel }}</view>
  45. <view class="right-arrow"></view>
  46. </view>
  47. <view class="form-item">
  48. <text class="form-label require">年龄(岁)</text>
  49. <input class="form-input" v-model="form.age" type="number" placeholder="请输入年龄" />
  50. </view>
  51. <view class="form-item">
  52. <text class="form-label require">体重(kg)</text>
  53. <input class="form-input" v-model="form.weight" type="digit" placeholder="请输入体重" />
  54. </view>
  55. </view>
  56. <!-- 性格特征 -->
  57. <view class="section-title">性格特征</view>
  58. <view class="form-card">
  59. <view class="form-item">
  60. <text class="form-label">性格关键词</text>
  61. <input class="form-input" v-model="form.personality" placeholder="例如活泼、粘人" />
  62. </view>
  63. <view class="form-item vertical">
  64. <text class="form-label">萌宠性格描述</text>
  65. <textarea class="form-textarea" v-model="form.cutePersonality" placeholder="详细描述宠物的性格"></textarea>
  66. </view>
  67. </view>
  68. <!-- 家庭信息 -->
  69. <view class="section-title">家庭信息</view>
  70. <view class="form-card">
  71. <view class="form-item" @click="showHouseTypeSelect = true">
  72. <text class="form-label require">房屋类型</text>
  73. <view class="picker-value" :class="{'placeholder': !form.houseType}">{{ getHouseTypeLabel }}</view>
  74. <view class="right-arrow"></view>
  75. </view>
  76. <view class="form-item" @click="showEntryMethodSelect = true">
  77. <text class="form-label require">入门方式</text>
  78. <view class="picker-value" :class="{'placeholder': !form.entryMethod}">{{ getEntryMethodLabel }}</view>
  79. <view class="right-arrow"></view>
  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" @click="showHealthSelect = true">
  94. <text class="form-label require">健康状态</text>
  95. <view class="picker-value">{{ form.healthStatus || '请选择' }}</view>
  96. <view class="right-arrow"></view>
  97. </view>
  98. <view class="form-item" @click="showVaccineSelect = true">
  99. <text class="form-label require">疫苗接种</text>
  100. <view class="picker-value">{{ form.vaccineStatus || '请选择' }}</view>
  101. <view class="right-arrow"></view>
  102. </view>
  103. <view class="form-item" @click="showAggressionSelect = true">
  104. <text class="form-label require">攻击倾向</text>
  105. <view class="picker-value">{{ getAggressionLabel }}</view>
  106. <view class="right-arrow"></view>
  107. </view>
  108. <view class="form-item vertical">
  109. <text class="form-label require">既往病史</text>
  110. <textarea class="form-textarea" v-model="form.medicalHistory" placeholder="如有病史请务必记录"></textarea>
  111. </view>
  112. <view class="form-item vertical">
  113. <text class="form-label require">过敏史</text>
  114. <textarea class="form-textarea" v-model="form.allergies" placeholder="如有过敏源请务必记录"></textarea>
  115. </view>
  116. <view class="form-item vertical">
  117. <text class="form-label">补充备注</text>
  118. <textarea class="form-textarea" v-model="form.remark" placeholder="请输入其他备注信息"></textarea>
  119. </view>
  120. </view>
  121. <!-- 底部固定操作栏 -->
  122. <view class="footer-bar">
  123. <button class="save-btn" @click="onSave">保存修改</button>
  124. </view>
  125. <!-- 品种选择中心弹窗 @Author: Antigravity -->
  126. <view class="center-modal-mask" v-if="showBreedPicker" @click="closeBreedPicker">
  127. <view class="center-modal-content" @click.stop>
  128. <view class="modal-header">
  129. <view class="search-box">
  130. <view class="search-icon"></view>
  131. <input class="search-input" v-model="breedSearchKeyword" placeholder="搜索品种..." focus />
  132. </view>
  133. </view>
  134. <scroll-view scroll-y class="customer-list-scroll">
  135. <view class="customer-item" v-for="(item, index) in filteredBreeds" :key="index" @click="selectBreed(item)">
  136. <text class="name">{{ item }}</text>
  137. <view class="checkmark" v-if="form.breed === item"></view>
  138. </view>
  139. </scroll-view>
  140. <view class="breed-footer">
  141. <input class="breed-custom-input" v-model="customBreedText" placeholder="或手动录入新品种" />
  142. <view class="add-confirm-btn" @click="confirmCustomBreed">更新</view>
  143. </view>
  144. </view>
  145. </view>
  146. <!-- 居中选择器实例群 @Author: Antigravity -->
  147. <center-select v-model="showTypeSelect" title="宠物类型" :options="typeOptions" :value="form.type" @select="(item) => form.type = item.value" />
  148. <center-select v-model="showCustomerSelect" title="所属主人" :options="customerOptions" labelKey="name" valueKey="id" :value="form.userId" @select="(item) => form.userId = item.id" />
  149. <center-select v-model="showGenderSelect" title="选择性别" :options="genderOptions" :value="form.gender" @select="(item) => form.gender = item.value" />
  150. <center-select v-model="showSizeSelect" title="选择体型" :options="sizeOptions" :value="form.size" @select="(item) => form.size = item.value" />
  151. <center-select v-model="showHouseTypeSelect" title="房屋类型" :options="houseTypeOptions" :value="form.houseType" @select="(item) => form.houseType = item.value" />
  152. <center-select v-model="showEntryMethodSelect" title="入门方式" :options="entryMethodOptions" :value="form.entryMethod" @select="(item) => form.entryMethod = item.value" />
  153. <center-select v-model="showHealthSelect" title="当前健康状态" :options="healthStatusOptions" :value="form.healthStatus" @select="(item) => form.healthStatus = item" />
  154. <center-select v-model="showVaccineSelect" title="疫苗接种状态" :options="vaccineOptions" :value="form.vaccineStatus" @select="(item) => form.vaccineStatus = item" />
  155. <center-select v-model="showAggressionSelect" title="是否有攻击倾向" :options="aggressionOptions" :value="form.aggression" @select="(item) => form.aggression = item.value" />
  156. </block>
  157. </view>
  158. </template>
  159. <script setup>
  160. /**
  161. * @Author: Antigravity
  162. */
  163. import { ref, reactive, computed } from 'vue'
  164. import { onLoad } from '@dcloudio/uni-app'
  165. import { getPet, updatePet } from '@/api/archieves/pet'
  166. import { listAllCustomer } from '@/api/archieves/customer'
  167. import { getDicts } from '@/api/system/dict/data'
  168. import { uploadFile } from '@/api/system/oss'
  169. import navBar from '@/components/nav-bar/index.vue'
  170. import centerSelect from '@/components/center-select/index.vue'
  171. import customerEnums from '@/json/customer.json'
  172. const loading = ref(true)
  173. // 弹窗状态控制
  174. const showTypeSelect = ref(false)
  175. const showCustomerSelect = ref(false)
  176. const showGenderSelect = ref(false)
  177. const showSizeSelect = ref(false)
  178. const showHouseTypeSelect = ref(false)
  179. const showEntryMethodSelect = ref(false)
  180. const showHealthSelect = ref(false)
  181. const showVaccineSelect = ref(false)
  182. const showAggressionSelect = ref(false)
  183. const showBreedPicker = ref(false)
  184. const tempAvatarUrl = ref('')
  185. const typeOptions = [{ label: '猫', value: 1 }, { label: '狗', value: 2 }, { label: '其他', value: 3 }]
  186. const genderOptions = [{ label: '未知', value: 0 }, { label: '公', value: 1 }, { label: '母', value: 2 }]
  187. const sizeOptions = [{ label: '小型(0-10kg)', value: 'small' }, { label: '中型(10-25kg)', value: 'medium' }, { label: '大型(25kg+)', value: 'large' }]
  188. const { houseTypeOptions, entryMethodOptions } = customerEnums
  189. const healthStatusOptions = ['健康', '亚健康', '疾病']
  190. const vaccineOptions = ['无', '已打1次', '已打2次', '已打3次']
  191. const aggressionOptions = [{ label: '否', value: 0 }, { label: '是', value: 1 }]
  192. const customerOptions = ref([])
  193. const breedCandidates = ref([])
  194. const petId = ref(null)
  195. const breedSearchKeyword = ref('')
  196. const customBreedText = ref('')
  197. const form = reactive({
  198. id: '', avatar: '', name: '', userId: '', type: 1, breed: '', gender: 0, age: '', weight: '', size: '',
  199. arrivalTime: '', houseType: '', entryMethod: '', entryPassword: '', keyLocation: '',
  200. personality: '', cutePersonality: '', healthStatus: '健康', aggression: 0,
  201. vaccineStatus: '无', medicalHistory: '', allergies: '', remark: ''
  202. })
  203. onLoad(async (options) => {
  204. if (options.id) {
  205. petId.value = options.id
  206. await initData()
  207. await fetchDetail()
  208. } else {
  209. uni.showToast({ title: '参数错误', icon: 'none' })
  210. setTimeout(() => uni.navigateBack(), 1500)
  211. }
  212. })
  213. const initData = async () => {
  214. try {
  215. const [custRes, dictRes] = await Promise.all([
  216. listAllCustomer({ status: 0 }),
  217. getDicts('sys_pet_breed')
  218. ])
  219. const records = Array.isArray(custRes) ? custRes : (custRes?.data || [])
  220. customerOptions.value = records.map(item => ({
  221. id: item.id || item.userId,
  222. name: item.name || item.customerName
  223. }))
  224. const dictList = Array.isArray(dictRes) ? dictRes : (dictRes?.data || [])
  225. breedCandidates.value = dictList.map(item => item.dictLabel || item.dictValue || item.label || item.value)
  226. } catch(e) { console.error('初始化数据失败', e) }
  227. }
  228. const fetchDetail = async () => {
  229. try {
  230. loading.value = true
  231. const res = await getPet(petId.value)
  232. if (res) {
  233. const data = res.data || res
  234. Object.assign(form, data)
  235. tempAvatarUrl.value = data.avatarUrl || '' // 回显全路径 URL
  236. }
  237. } catch (e) {
  238. console.error('获取宠物详情失败', e)
  239. } finally {
  240. loading.value = false
  241. }
  242. }
  243. const getTypeLabel = computed(() => typeOptions.find(o => o.value === form.type)?.label || '请选择')
  244. const getCustomerLabel = computed(() => {
  245. const opt = customerOptions.value.find(o => String(o.id) === String(form.userId))
  246. return opt ? opt.name : '请选择主人'
  247. })
  248. const getGenderLabel = computed(() => genderOptions.find(o => o.value === form.gender)?.label || '未知')
  249. const getSizeLabel = computed(() => sizeOptions.find(o => o.value === form.size)?.label || '请选择')
  250. const getHouseTypeLabel = computed(() => houseTypeOptions.find(o => o.value === form.houseType)?.label || '请选择')
  251. const getEntryMethodLabel = computed(() => entryMethodOptions.find(o => o.value === form.entryMethod)?.label || '请选择')
  252. const getAggressionLabel = computed(() => aggressionOptions.find(o => o.value === form.aggression)?.label || '否')
  253. const filteredBreeds = computed(() => {
  254. if (!breedSearchKeyword.value.trim()) return breedCandidates.value
  255. const kw = breedSearchKeyword.value.toLowerCase().trim()
  256. return breedCandidates.value.filter(item => item.toLowerCase().includes(kw))
  257. })
  258. const selectBreed = (breed) => {
  259. form.breed = breed
  260. showBreedPicker.value = false
  261. breedSearchKeyword.value = ''
  262. }
  263. const closeBreedPicker = () => {
  264. showBreedPicker.value = false
  265. breedSearchKeyword.value = ''
  266. customBreedText.value = ''
  267. }
  268. // 选择并上传/修改宠物头像
  269. // @Author: Antigravity
  270. const handleChooseAvatar = () => {
  271. uni.chooseImage({
  272. count: 1,
  273. sizeType: ['compressed'],
  274. sourceType: ['album', 'camera'],
  275. success: async (res) => {
  276. try {
  277. uni.showLoading({ title: '上传中...' });
  278. const uploadRes = await uploadFile(res.tempFilePaths[0]);
  279. form.avatar = uploadRes.ossId; // 提交给后端的 ID
  280. tempAvatarUrl.value = uploadRes.url; // 用于预览的全路径 URL
  281. uni.hideLoading();
  282. uni.showToast({ title: '头像上传成功', icon: 'success' });
  283. } catch (e) {
  284. uni.hideLoading();
  285. console.error('上传头像失败', e);
  286. }
  287. }
  288. });
  289. }
  290. const confirmCustomBreed = () => {
  291. const val = customBreedText.value.trim()
  292. if (!val) return
  293. form.breed = val
  294. if (!breedCandidates.value.includes(val)) { breedCandidates.value.push(val) }
  295. showBreedPicker.value = false
  296. breedSearchKeyword.value = ''
  297. customBreedText.value = ''
  298. }
  299. const onSave = async () => {
  300. if (!form.name) return uni.showToast({ title: '请输入宠物名称', icon: 'none' })
  301. if (!form.userId) return uni.showToast({ title: '请选择所属主人', icon: 'none' })
  302. if (!form.breed) return uni.showToast({ title: '请输入或选择品种', icon: 'none' })
  303. if (!form.size) return uni.showToast({ title: '请选择体型', icon: 'none' })
  304. if (!form.age) return uni.showToast({ title: '请输入年龄', icon: 'none' })
  305. try {
  306. uni.showLoading({ title: '保存中' })
  307. await updatePet(form)
  308. uni.hideLoading()
  309. uni.showToast({ title: '保存成功', icon: 'success' })
  310. setTimeout(() => uni.navigateBack(), 1000)
  311. } catch (error) { uni.hideLoading() }
  312. }
  313. </script>
  314. <style lang="scss" scoped>
  315. .pet-edit-page { min-height: 100vh; background: #f7f8fa; padding-bottom: calc(140rpx + env(safe-area-inset-bottom)); }
  316. .avatar-section {
  317. display: flex;
  318. flex-direction: column;
  319. align-items: center;
  320. padding: 40rpx 0;
  321. background-color: #fff;
  322. margin-bottom: 20rpx;
  323. border-bottom: 2rpx solid #f0f0f0;
  324. .avatar-img {
  325. width: 160rpx;
  326. height: 160rpx;
  327. border-radius: 80rpx;
  328. background-color: #f7f8fa;
  329. border: 4rpx solid #fff;
  330. box-shadow: 0 4rpx 16rpx rgba(0,0,0,0.06);
  331. }
  332. .avatar-tip {
  333. margin-top: 16rpx;
  334. font-size: 24rpx;
  335. color: #ff9500;
  336. font-weight: 500;
  337. }
  338. }
  339. /* CSS 原生 Loading 方案 @Author: Antigravity */
  340. .loading-state {
  341. padding-top: 15vh; display: flex; flex-direction: column; align-items: center; justify-content: center;
  342. .loading-txt { font-size: 24rpx; color: #999; margin-top: 20rpx; }
  343. }
  344. .spinner {
  345. width: 40rpx; height: 40rpx; border: 4rpx solid #f3f3f3; border-top: 4rpx solid #ff9500;
  346. border-radius: 50%; animation: spin 1s linear infinite;
  347. }
  348. @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }
  349. .section-title { font-size: 30rpx; font-weight: bold; color: #333; padding: 32rpx 32rpx 16rpx; }
  350. .form-card { background: #fff; border-radius: 24rpx; padding: 8rpx 32rpx; margin: 0 24rpx; }
  351. .form-item { display: flex; align-items: center; padding: 28rpx 0; border-bottom: 2rpx solid #EEEEEE; position: relative; }
  352. .form-item:last-child { border-bottom: none; }
  353. .form-item.vertical { flex-direction: column; align-items: stretch; border-bottom: none; gap: 0; }
  354. .form-item.vertical .form-textarea { width: 100%; height: 160rpx; margin-top: 16rpx; background: #f9f9f9; padding: 20rpx; box-sizing: border-box; border-radius: 12rpx; font-size: 28rpx; color: #333; line-height: 1.6; }
  355. .form-label { width: 220rpx; font-size: 28rpx; color: #333; flex-shrink: 0; }
  356. .form-label.require::before { content: '*'; color: #f56c6c; margin-right: 4rpx; }
  357. .form-input { flex: 1; font-size: 28rpx; color: #333; text-align: right; }
  358. .picker-value { flex: 1; font-size: 28rpx; color: #333; text-align: right; margin-right: 16rpx; min-height: 40rpx; }
  359. .picker-value.placeholder { color: #ccc; }
  360. /* CSS 绘制图标群 @Author: Antigravity */
  361. .right-arrow {
  362. width: 14rpx; height: 14rpx; border-right: 3rpx solid #ccc; border-top: 3rpx solid #ccc; transform: rotate(45deg);
  363. }
  364. .checkmark {
  365. width: 12rpx; height: 24rpx; border-right: 4rpx solid #ff9500; border-bottom: 4rpx solid #ff9500; transform: rotate(45deg); margin-right: 10rpx;
  366. }
  367. .search-icon {
  368. width: 24rpx; height: 24rpx; border: 3rpx solid #999; border-radius: 50%; position: relative;
  369. &::after { content: ''; position: absolute; right: -8rpx; bottom: -8rpx; width: 12rpx; height: 3rpx; background: #999; transform: rotate(45deg); }
  370. }
  371. .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; }
  372. .save-btn { width: 100%; height: 88rpx; background: linear-gradient(90deg, #ffd53f, #ff9500); color: #fff; border: none; border-radius: 44rpx; font-size: 32rpx; font-weight: bold; line-height: 88rpx; &::after { border: none; } }
  373. /* 居中弹窗样式 @Author: Antigravity */
  374. .center-modal-mask {
  375. position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: rgba(0,0,0,0.6); z-index: 1000; display: flex; align-items: center; justify-content: center; backdrop-filter: blur(2px);
  376. }
  377. .center-modal-content {
  378. width: 620rpx; max-height: 80vh; background: #fff; border-radius: 32rpx; display: flex; flex-direction: column; overflow: hidden; animation: popIn 0.3s ease-out;
  379. }
  380. @keyframes popIn { from { transform: scale(0.8); opacity: 0; } to { transform: scale(1); opacity: 1; } }
  381. .modal-header { padding: 32rpx; border-bottom: 2rpx solid #f2f2f2; }
  382. .search-box {
  383. height: 72rpx; background: #f5f5f5; border-radius: 36rpx; display: flex; align-items: center; padding: 0 24rpx; gap: 12rpx;
  384. }
  385. .search-input { flex: 1; font-size: 28rpx; color: #333; }
  386. .customer-list-scroll { flex: 1; padding: 0 32rpx; max-height: 50vh; }
  387. .customer-item { display: flex; align-items: center; justify-content: space-between; padding: 30rpx 0; border-bottom: 2rpx solid #f9f9f9; }
  388. .name { font-size: 32rpx; color: #333; font-weight: bold; }
  389. .breed-footer {
  390. padding: 24rpx 32rpx; background: #fafafa; border-top: 2rpx solid #f2f2f2; display: flex; gap: 16rpx; align-items: center;
  391. }
  392. .breed-custom-input { flex: 1; height: 72rpx; background: #eee; border-radius: 12rpx; padding: 0 20rpx; font-size: 26rpx; }
  393. .add-confirm-btn { padding: 0 30rpx; height: 72rpx; line-height: 72rpx; background: #ff9500; color: #fff; border-radius: 12rpx; font-size: 26rpx; font-weight: bold; }
  394. </style>