update.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <template>
  2. <view class="container">
  3. <view class="address-box">
  4. <u--form labelPosition="left" :model="formData" :rules="rules" ref="form">
  5. <u-form-item label="收件人名称" prop="name" labelWidth="90" borderBottom ref="item-name">
  6. <u-input type="text" maxlength="11" v-model="formData.name" clearable placeholder="请填写收件人名称" border="none"></u-input>
  7. </u-form-item>
  8. <u-form-item label="手机号" prop="mobile" labelWidth="90" borderBottom ref="item-mobile">
  9. <u-input type="number" maxlength="11" v-model="formData.mobile" clearable placeholder="请填写手机号" border="none"></u-input>
  10. </u-form-item>
  11. <u-form-item label="省市地区" prop="areaText" labelWidth="90" borderBottom @click=" regionVisible = true; hideKeyboard()" ref="item-areaText">
  12. <u--input v-model="formData.areaText" disabled disabledColor="#ffffff" placeholder="请选择省市地区" border="none"></u--input>
  13. <u-icon slot="right" name="arrow-right"></u-icon>
  14. <w-picker :visible.sync="regionVisible" mode="region" :value="defaultRegion" default-type="value" :hide-area="false" @confirm="onConfirm($event, 'region')" @cancel="onCancel" ref="region"></w-picker>
  15. </u-form-item>
  16. <u-form-item label="详细地址" prop="detail" labelWidth="90" borderBottom ref="item-detail">
  17. <u--textarea placeholder="请输入街道门牌号不低于6个字" v-model="formData.detail" count></u--textarea>
  18. </u-form-item>
  19. <u-form-item label="默认地址" prop="type" labelWidth="90" borderBottom ref="item-type">
  20. <u-radio-group v-model="formData.type">
  21. <u-radio :customStyle="{ marginRight: '16px' }" v-for="(item, index) in typeList" :key="index" :label="item.name" :name="item.value"></u-radio>
  22. </u-radio-group>
  23. </u-form-item>
  24. <view class="btn-group">
  25. <u-button type="primary" text="更新地址" customStyle="margin-top: 50px" @click="handleSubmit"></u-button>
  26. </view>
  27. </u--form>
  28. </view>
  29. </view>
  30. </template>
  31. <script>
  32. import { getAddressById, updateAddress } from '../../api/address'
  33. export default {
  34. data() {
  35. return {
  36. id: '',
  37. regionVisible: false,
  38. defaultRegion: ['110000', '110100', '110101'],
  39. typeList: [
  40. {
  41. name: '是',
  42. value: 1
  43. },
  44. {
  45. name: '否',
  46. value: 2
  47. }
  48. ],
  49. formData: {
  50. id: '',
  51. name: '',
  52. mobile: '',
  53. areaText: '',
  54. areaCode: '',
  55. detail: '',
  56. detailAddress: '',
  57. type: 1
  58. },
  59. rules: {
  60. name: [
  61. {
  62. type: 'string',
  63. min: 2,
  64. max: 12,
  65. required: true,
  66. message: '请填写收件人名称',
  67. trigger: ['blur', 'change']
  68. },
  69. {
  70. message: '收件人名称不能为空',
  71. // 触发器可以同时用blur和change
  72. trigger: ['change', 'blur']
  73. }
  74. ],
  75. mobile: [
  76. {
  77. type: 'integer',
  78. required: true,
  79. message: '请填写手机号',
  80. trigger: ['blur', 'change']
  81. },
  82. {
  83. // 自定义验证函数,见上说明
  84. validator: (rule, value, callback) => {
  85. // 上面有说,返回true表示校验通过,返回false表示不通过
  86. // uni.$u.test.mobile()就是返回true或者false的
  87. return uni.$u.test.mobile(value)
  88. },
  89. message: '手机号码不正确',
  90. // 触发器可以同时用blur和change
  91. trigger: ['change', 'blur']
  92. }
  93. ],
  94. areaText: {
  95. type: 'string',
  96. required: true,
  97. message: '请选择省市地区',
  98. trigger: ['blur', 'change']
  99. },
  100. detailAddress: {
  101. type: 'string',
  102. min: 6,
  103. max: 30,
  104. required: true,
  105. message: '请填写详细地址',
  106. trigger: ['blur', 'change']
  107. }
  108. }
  109. }
  110. },
  111. onLoad(e) {
  112. if (!e.addressId) {
  113. uni.$u.toast('请求参数错误')
  114. } else {
  115. this.id = e.addressId
  116. this.loadAddressData()
  117. }
  118. },
  119. methods: {
  120. loadAddressData() {
  121. getAddressById({ id: this.id }).then(res => {
  122. this.formData = res.data
  123. this.initRegionData()
  124. })
  125. },
  126. initRegionData(){
  127. //回显 【省市地区】 和 【详细地址】 信息
  128. if (this.formData.areaCode) {
  129. const areaCode = this.formData.areaCode + ''
  130. //通过地区code反解析出【省-市-地区】code
  131. this.defaultRegion.splice(0, 3, areaCode.substring(0,2).padEnd(6, '0'), areaCode.substring(0,4).padEnd(6, '0'), areaCode)
  132. this.$nextTick(res => {
  133. let areaText = this.$refs.region._data.result.result
  134. this.formData.areaText = areaText
  135. //通过从完整详细地址除去【省-市-地区】得到后半段地址信息
  136. this.formData.detail = this.formData.detailAddress.replace(areaText, '')
  137. this.$forceUpdate();
  138. })
  139. }
  140. },
  141. onConfirm(res) {
  142. this.formData.areaText = res.result
  143. this.formData.areaCode = res.value[2]
  144. },
  145. onCancel() {},
  146. hideKeyboard() {
  147. uni.hideKeyboard()
  148. },
  149. handleSubmit() {
  150. this.$refs.form.validate().then(res => {
  151. this.formData.detailAddress = this.formData.areaText + this.formData.detail
  152. console.log(this.formData)
  153. updateAddress(this.formData).then(res => {
  154. uni.$u.toast('地址已更新')
  155. setTimeout(() => {
  156. uni.navigateBack()
  157. }, 300)
  158. })
  159. })
  160. }
  161. }
  162. }
  163. </script>
  164. <style lang="scss" scoped>
  165. .address-box {
  166. width: 690rpx;
  167. margin: 0 auto;
  168. padding-top: 30rpx;
  169. }
  170. </style>