addressEdit.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <template>
  2. <view class="container">
  3. <uni-forms class="form" :rules="rules" ref="form" :modelValue="form" label-width="160rpx" label-align="right">
  4. <uni-forms-item label="姓名" required name="name">
  5. <uni-easyinput v-model="form.name" placeholder="请输入姓名" />
  6. </uni-forms-item>
  7. <uni-forms-item label="手机号" required name="tel">
  8. <uni-easyinput v-model="form.tel" placeholder="请输手机号" />
  9. </uni-forms-item>
  10. <uni-forms-item label="选择地址" required name="areaCode">
  11. <picker mode="region" @change="onchange">
  12. <!-- <uni-easyinput v-model="form.name" placeholder="请输入姓名" /> -->
  13. <view class="inputBox" v-if="form.areaCode">{{ form.province }}/{{ form.city }}/{{ form.county }}</view>
  14. <view class="inputBox" v-else>点击选择地址</view>
  15. </picker>
  16. </uni-forms-item>
  17. <uni-forms-item label="详细地址" required name="addressDetail">
  18. <uni-easyinput type="textarea" v-model="form.addressDetail" placeholder="请输详细地址" />
  19. </uni-forms-item>
  20. <uni-forms-item label="设为默认地址" required name="isDefault">
  21. <switch :checked="form.isDefault == 1" @change="switchChange" style="transform: scale(0.7)" />
  22. </uni-forms-item>
  23. </uni-forms>
  24. <view class="bottom">
  25. <button class="m_button" type="default" @click="save">保存</button>
  26. </view>
  27. </view>
  28. </template>
  29. <script>
  30. import { addressSaveApi, addressDetailApi } from '@/api/address.js'
  31. export default {
  32. data() {
  33. return {
  34. address: [],
  35. form: {
  36. id: null,
  37. areaCode: null,
  38. name: null, //姓名
  39. province: null, //省
  40. city: null, //市
  41. county: null, //县区
  42. addressDetail: null, //详细地址
  43. tel: null, //电话
  44. isDefault: 0,
  45. },
  46. rules: {
  47. name: {
  48. rules: [
  49. {
  50. required: true,
  51. errorMessage: '姓名不能为空',
  52. },
  53. ],
  54. },
  55. tel: {
  56. rules: [
  57. {
  58. required: true,
  59. errorMessage: '手机号不能为空',
  60. },
  61. ],
  62. },
  63. areaCode: {
  64. rules: [
  65. {
  66. required: true,
  67. errorMessage: '请选择地址',
  68. },
  69. ],
  70. },
  71. addressDetail: {
  72. rules: [
  73. {
  74. required: true,
  75. errorMessage: '详细地址不能为空',
  76. },
  77. ],
  78. },
  79. },
  80. }
  81. },
  82. onLoad(option) {
  83. console.log(option, 'option')
  84. if (option.id) {
  85. let params = {
  86. id: option.id,
  87. }
  88. addressDetailApi(params).then(res => {
  89. console.log(res, 'res')
  90. this.form = {
  91. ...res.data,
  92. }
  93. })
  94. }
  95. },
  96. onShow() {},
  97. methods: {
  98. onchange(value) {
  99. console.log(value, 'value')
  100. this.form.areaCode = value.detail.code[2]
  101. this.form.province = value.detail.value[0]
  102. if (value.detail.value[0] == value.detail.value[1]) {
  103. this.form.city = '市辖区'
  104. } else {
  105. this.form.city = value.detail.value[1]
  106. }
  107. this.form.county = value.detail.value[2]
  108. },
  109. switchChange(value) {
  110. console.log(value, 'value')
  111. if (value.detail.value) {
  112. this.form.isDefault = 1
  113. } else {
  114. this.form.isDefault = 0
  115. }
  116. },
  117. save() {
  118. let _this = this
  119. console.log(this.form, 'this.form')
  120. this.$refs.form
  121. .validate()
  122. .then(val => {
  123. addressSaveApi(this.form).then(res => {
  124. console.log(res, '提交新增地址')
  125. wx.redirectTo({
  126. url: '/pages/addressManage/addressManage',
  127. })
  128. wx.showToast({
  129. title: '保存成功',
  130. })
  131. })
  132. })
  133. .catch(err => {
  134. console.log('err', err)
  135. })
  136. },
  137. },
  138. }
  139. </script>
  140. <style scoped lang="scss">
  141. .container {
  142. background: #fff;
  143. padding: 40rpx;
  144. .form {
  145. margin-top: 40rpx;
  146. }
  147. .inputBox {
  148. border: 2rpx solid #f0f0f0;
  149. border-radius: 8rpx;
  150. height: 70rpx;
  151. line-height: 70rpx;
  152. padding-left: 20rpx;
  153. color: #333;
  154. }
  155. }
  156. .bottom {
  157. position: fixed;
  158. bottom: 0;
  159. padding: 40rpx 0;
  160. text-align: center;
  161. width: calc(100% - 80rpx);
  162. .m_button {
  163. height: 72rpx;
  164. width: 40vw;
  165. line-height: 72rpx;
  166. border-radius: 36rpx;
  167. font-size: 26rpx;
  168. }
  169. }
  170. </style>