editAddress.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <script>
  2. import api from '@/api'
  3. export default {
  4. data() {
  5. return {
  6. type: '',
  7. area: [],
  8. showPopup: false,
  9. form: {
  10. name: '',
  11. mobile: '',
  12. province: '', // 省id
  13. county: '', // 区id
  14. city: '', // 市id
  15. area_text: '', // 省市区拼接
  16. remark: '', // 详细地址
  17. is_default: 0
  18. },
  19. rules: {
  20. name: { required: true, message: '请输入收货人', trigger: ['blur', 'change'] },
  21. mobile: [
  22. {
  23. required: true,
  24. message: '请输入手机号',
  25. trigger: ['blur', 'change']
  26. },
  27. {
  28. pattern: /^((\+|00)86)?1\d{10}$/,
  29. message: '请输入正确的手机号',
  30. trigger: ['blur']
  31. }
  32. ],
  33. area_text: { required: true, message: '请选择地区', trigger: ['blur', 'change'] },
  34. remark: { required: true, message: '请输入详细地址', trigger: ['blur', 'change'] }
  35. }
  36. }
  37. },
  38. watch: {
  39. area(v) {
  40. if (!Object.keys(v).length) return
  41. let str = ''
  42. v.forEach(item => (str += item.name))
  43. // 这里做一下判断是因为 编辑状态下,省市区名字未 分割到 area 数组
  44. if (str) this.form.area_text = str
  45. }
  46. },
  47. methods: {
  48. save() {
  49. // this.$refs.form1.validate().then(() => {
  50. api
  51. .saveAddress({
  52. ...this.form,
  53. province: this.area[0].id,
  54. county: this.area[1].id,
  55. city: this.area[2].id
  56. })
  57. .then(() => {
  58. uni.$emit('reloadAddressList')
  59. this.$common.back()
  60. this.$nextTick(() => {
  61. this.$common.toast(`${this.type === 'add' ? '新建' : '修改'}成功`)
  62. })
  63. })
  64. // })
  65. }
  66. },
  67. onLoad() {
  68. let { type, obj } = this.$store.state.params
  69. this.type = type
  70. if (Object.keys(obj).length) {
  71. this.form = JSON.parse(JSON.stringify(obj))
  72. this.area = [
  73. { id: this.form.province, name: '' },
  74. { id: this.form.county, name: '' },
  75. { id: this.form.city, name: '' }
  76. ]
  77. }
  78. uni.setNavigationBarTitle({
  79. title: type === 'add' ? '新建地址' : '编辑地址'
  80. })
  81. }
  82. }
  83. </script>
  84. <template>
  85. <view class="m p-10">
  86. <view class="bg-white radius10 form">
  87. <u-form
  88. ref="form1"
  89. :model="form"
  90. :rules="rules"
  91. :labelStyle="{ fontSize: '28rpx', color: '#303133' }"
  92. labelAlign="left"
  93. errorType="toast"
  94. >
  95. <u-form-item prop="name" label="收货人" labelWidth="160rpx">
  96. <u-input
  97. v-model="form.name"
  98. border="none"
  99. placeholder="请输入收货人"
  100. fontSize="26rpx"
  101. ></u-input>
  102. </u-form-item>
  103. <u-form-item prop="mobile" label="手机号码" labelWidth="160rpx">
  104. <u-input
  105. v-model="form.mobile"
  106. border="none"
  107. placeholder="请输入手机号码"
  108. fontSize="26rpx"
  109. ></u-input>
  110. </u-form-item>
  111. <u-form-item
  112. prop="area_text"
  113. label="所在地区"
  114. labelWidth="160rpx"
  115. @click="showPopup = true"
  116. >
  117. <u-input
  118. disabled
  119. disabledColor="transparent"
  120. :value="form.area_text"
  121. border="none"
  122. placeholder="省市区县、乡镇等"
  123. fontSize="26rpx"
  124. ></u-input>
  125. </u-form-item>
  126. <u-form-item prop="remark">
  127. <view slot="label" class="form-label">详细地址</view>
  128. <u-textarea
  129. v-model="form.remark"
  130. placeholder="街道楼牌号等"
  131. border="none"
  132. height="50"
  133. confirmType="done"
  134. ></u-textarea>
  135. </u-form-item>
  136. </u-form>
  137. </view>
  138. <view class="bg-white radius10 mt-10 w flex pt-10 pb-10">
  139. <u-text text="默认地址" size="28rpx"></u-text>
  140. <u-switch
  141. v-model="form.is_default"
  142. :inactiveValue="0"
  143. :activeValue="1"
  144. activeColor="var(--theme)"
  145. size="20"
  146. ></u-switch>
  147. </view>
  148. <area-select :showPopup.sync="showPopup" :area.sync="area"></area-select>
  149. <u-button
  150. text="保存"
  151. color="var(--theme)"
  152. @click="save"
  153. shape="circle"
  154. customStyle="width: 650rpx;height: 98rpx; position:fixed;bottom:100rpx;left:50%;transform:translate(-50%)"
  155. ></u-button>
  156. </view>
  157. </template>
  158. <style lang="scss" scoped>
  159. .form {
  160. padding: 0 24rpx;
  161. .form-label {
  162. width: 80px;
  163. height: 40rpx;
  164. font-size: 28rpx;
  165. color: #303133;
  166. }
  167. }
  168. /deep/.u-textarea {
  169. padding: 4rpx 0 !important;
  170. }
  171. ::v-deep .u-textarea {
  172. padding: 4rpx 0 !important;
  173. /* #ifdef MP-WEIXIN */
  174. transform: translate(-10rpx, -10rpx);
  175. /* #endif */
  176. }
  177. /deep/.u-textarea textarea,
  178. ::v-deep .u-textarea textarea {
  179. font-size: 26rpx !important;
  180. }
  181. </style>