address-edit.vue 3.7 KB

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