addressEdit.vue 3.9 KB

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