address-edit.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <template>
  2. <div>
  3. <nav-bar title="编辑地址" @click-left="$router.back()" />
  4. <div class="address-add">
  5. <van-form>
  6. <van-field v-model="formData.name" label="姓名" placeholder="请输入姓名" />
  7. <van-field v-model="formData.tel" label="手机号" placeholder="请输入手机号" maxlength="11"/>
  8. <van-field
  9. readonly
  10. clickable
  11. label="城市"
  12. :value="formData.town"
  13. placeholder="选择省/市/区"
  14. @click="showPicker = true"
  15. />
  16. <van-field v-model="formData.address" label="详细地址" placeholder="请输入详细地址" />
  17. <van-field name="switch" label="是否选为默认地址">
  18. <template #input>
  19. <van-switch v-model="formData.isDefault" size="20" />
  20. </template>
  21. </van-field>
  22. </van-form>
  23. <div class="btn">
  24. <van-button color="#FF5B00" @click="onSave">保存</van-button>
  25. </div>
  26. <div class="btn" v-if="formData.id">
  27. <van-button @click="onDelete">删除</van-button>
  28. </div>
  29. </div>
  30. <van-popup v-model="showPicker" round position="bottom">
  31. <van-picker
  32. show-toolbar
  33. :columns="columns"
  34. @cancel="showPicker = false"
  35. @confirm="onConfirm"
  36. value-key="name"
  37. :loading="loading"
  38. />
  39. </van-popup>
  40. </div>
  41. </template>
  42. <script>
  43. import { addressSave, addressDelete } from '@/api/institutionApi/address'
  44. export default {
  45. data () {
  46. return {
  47. formData: {
  48. town: '',
  49. address: '',
  50. name: '',
  51. tel: '',
  52. isDefault: true,
  53. addressId: ''
  54. },
  55. showPicker: false,
  56. loading: false,
  57. columns: this.$store.getters.addressAll,
  58. townId: 0
  59. }
  60. },
  61. mounted () {
  62. if (this.$route.query.addressInfo) {
  63. const form = JSON.parse(this.$route.query.addressInfo)
  64. this.formData = form
  65. this.townId = form.townId
  66. this.formData.town = `${form.province}/${form.city}/${form.town}`
  67. this.formData.address = form.addressMine
  68. }
  69. },
  70. methods: {
  71. async onSave () {
  72. const form = {
  73. addressId: this.formData.id || '',
  74. userId: this.$store.getters.userId,
  75. townId: this.townId,
  76. receiver: this.formData.name,
  77. mobile: this.formData.tel,
  78. address: this.formData.address,
  79. defaultFlag: this.formData.isDefault ? 1 : 0
  80. }
  81. const data = await addressSave(form)
  82. console.log(data)
  83. this.$router.back()
  84. },
  85. async onDelete () {
  86. try {
  87. await addressDelete({ userId: this.$store.getters.userId, addressId: this.formData.id })
  88. this.$router.back()
  89. } catch (error) {
  90. console.log(error)
  91. }
  92. },
  93. onConfirm ($event, value) {
  94. console.log($event, value)
  95. this.formData.town = $event.join('/')
  96. this.townId = this.columns[value[0]].children[value[1]].children[value[2]].id
  97. console.log(this.townId)
  98. this.showPicker = false
  99. }
  100. }
  101. }
  102. </script>
  103. <style lang="scss" scoped>
  104. .address-add {
  105. padding: 3vw;
  106. min-height: 100vh;
  107. ::v-deep .van-form {
  108. border-radius: 1.2vw;
  109. overflow: hidden;
  110. margin-bottom: 4vw;
  111. }
  112. ::v-deep .van-field__label {
  113. white-space: nowrap;
  114. }
  115. .van-switch {
  116. margin-left: 20vw;
  117. }
  118. .van-button {
  119. margin-bottom: 4vw;
  120. border-radius: 10vw;
  121. width: 80vw;
  122. }
  123. .btn {
  124. width: 100%;
  125. display: flex;
  126. justify-content: center;
  127. }
  128. }
  129. </style>