address-edit.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <template>
  2. <view class="family-address">
  3. <view class="page-title">家庭地址设置</view>
  4. <view class="form-section">
  5. <view class="section-title">所在街道</view>
  6. <address-picker
  7. v-model="formData.streetId"
  8. @change="onAddressChange"
  9. ref="addressPicker"
  10. />
  11. </view>
  12. <view class="form-section" v-if="formData.fullName">
  13. <view class="section-title">当前地址</view>
  14. <view class="current-address">
  15. <text>{{ formData.fullName }}</text>
  16. </view>
  17. </view>
  18. <button class="save-btn" @click="saveAddress" :loading="loading">保存地址</button>
  19. </view>
  20. </template>
  21. <script>
  22. import AddressPicker from '@/components/address-picker.vue'
  23. import { getFamilyDetail, updateFamilyAddress } from '@/utils/api.js'
  24. export default {
  25. components: {
  26. AddressPicker
  27. },
  28. data() {
  29. return {
  30. loading: false,
  31. familyId: null,
  32. formData: {
  33. streetId: null,
  34. province: null,
  35. city: null,
  36. district: null,
  37. street: null,
  38. fullName: ''
  39. }
  40. }
  41. },
  42. onLoad(options) {
  43. if (options.familyId) {
  44. this.familyId = parseInt(options.familyId)
  45. this.loadFamilyInfo()
  46. }
  47. },
  48. methods: {
  49. async loadFamilyInfo() {
  50. try {
  51. const res = await getFamilyDetail(this.familyId)
  52. const family = res.data
  53. if (family) {
  54. this.formData.streetId = family.streetId
  55. this.formData.province = family.province
  56. this.formData.city = family.city
  57. this.formData.district = family.district
  58. this.formData.street = family.street
  59. this.formData.fullName = `${family.province || ''}${family.city || ''}${family.district || ''}${family.street || ''}`
  60. }
  61. } catch (e) {
  62. console.error('加载家庭信息失败', e)
  63. }
  64. },
  65. onAddressChange(data) {
  66. this.formData.streetId = data.streetId
  67. this.formData.province = data.province
  68. this.formData.city = data.city
  69. this.formData.district = data.district
  70. this.formData.street = data.street
  71. this.formData.fullName = data.fullName || ''
  72. },
  73. async saveAddress() {
  74. if (!this.formData.streetId) {
  75. uni.showToast({ title: '请选择街道地址', icon: 'none' })
  76. return
  77. }
  78. this.loading = true
  79. try {
  80. await updateFamilyAddress(this.familyId, {
  81. streetId: this.formData.streetId,
  82. province: this.formData.province,
  83. city: this.formData.city,
  84. district: this.formData.district,
  85. street: this.formData.street
  86. })
  87. uni.showToast({ title: '保存成功', icon: 'success' })
  88. setTimeout(() => {
  89. uni.navigateBack()
  90. }, 1500)
  91. } catch (e) {
  92. console.error('保存地址失败', e)
  93. uni.showToast({ title: '保存失败', icon: 'none' })
  94. } finally {
  95. this.loading = false
  96. }
  97. }
  98. }
  99. }
  100. </script>
  101. <style scoped>
  102. .family-address {
  103. min-height: 100vh;
  104. background: #f5f5f5;
  105. padding: 20rpx;
  106. }
  107. .page-title {
  108. font-size: 36rpx;
  109. font-weight: bold;
  110. color: #333;
  111. padding: 30rpx 0;
  112. }
  113. .form-section {
  114. background: #fff;
  115. border-radius: 16rpx;
  116. margin-bottom: 20rpx;
  117. padding: 20rpx;
  118. }
  119. .section-title {
  120. font-size: 30rpx;
  121. color: #333;
  122. font-weight: bold;
  123. margin-bottom: 20rpx;
  124. }
  125. .current-address {
  126. background: #f9f9f9;
  127. padding: 20rpx;
  128. border-radius: 8rpx;
  129. }
  130. .current-address text {
  131. font-size: 28rpx;
  132. color: #666;
  133. }
  134. .save-btn {
  135. margin-top: 40rpx;
  136. width: 100%;
  137. height: 88rpx;
  138. background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  139. color: #fff;
  140. border-radius: 44rpx;
  141. font-size: 32rpx;
  142. display: flex;
  143. align-items: center;
  144. justify-content: center;
  145. }
  146. </style>