create.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 { createAddress } from '../../api/address'
  33. export default {
  34. data() {
  35. return {
  36. regionVisible: false,
  37. defaultRegion: ['110000', '110100', '110101'],
  38. typeList: [
  39. {
  40. name: '是',
  41. value: 1
  42. },
  43. {
  44. name: '否',
  45. value: 2
  46. }
  47. ],
  48. formData: {
  49. name: '',
  50. mobile: '',
  51. areaText: '',
  52. areaCode: '',
  53. detail: '',
  54. detailAddress: '',
  55. type: 1
  56. },
  57. rules: {
  58. name: [
  59. {
  60. type: 'string',
  61. min: 2,
  62. max: 12,
  63. required: true,
  64. message: '请填写收件人名称',
  65. trigger: ['blur', 'change']
  66. },
  67. {
  68. message: '收件人名称不能为空',
  69. // 触发器可以同时用blur和change
  70. trigger: ['change', 'blur']
  71. }
  72. ],
  73. mobile: [
  74. {
  75. type: 'integer',
  76. required: true,
  77. message: '请填写手机号',
  78. trigger: ['blur', 'change']
  79. },
  80. {
  81. // 自定义验证函数,见上说明
  82. validator: (rule, value, callback) => {
  83. // 上面有说,返回true表示校验通过,返回false表示不通过
  84. // uni.$u.test.mobile()就是返回true或者false的
  85. return uni.$u.test.mobile(value)
  86. },
  87. message: '手机号码不正确',
  88. // 触发器可以同时用blur和change
  89. trigger: ['change', 'blur']
  90. }
  91. ],
  92. areaText: {
  93. type: 'string',
  94. required: true,
  95. message: '请选择省市地区',
  96. trigger: ['blur', 'change']
  97. },
  98. detailAddress: {
  99. type: 'string',
  100. min: 6,
  101. max: 30,
  102. required: true,
  103. message: '请填写详细地址',
  104. trigger: ['blur', 'change']
  105. }
  106. }
  107. }
  108. },
  109. onLoad() {},
  110. methods: {
  111. onConfirm(res) {
  112. this.formData.areaText = res.result
  113. this.formData.areaCode = res.value[2]
  114. },
  115. onCancel() {},
  116. hideKeyboard() {
  117. uni.hideKeyboard()
  118. },
  119. handleSubmit() {
  120. this.$refs.form.validate().then(res => {
  121. this.formData.detailAddress = this.formData.areaText + this.formData.detail
  122. console.log(this.formData)
  123. createAddress(this.formData).then(res => {
  124. uni.$u.toast('地址已保存')
  125. setTimeout(() => {
  126. uni.navigateBack()
  127. }, 300)
  128. })
  129. })
  130. }
  131. }
  132. }
  133. </script>
  134. <style lang="scss" scoped>
  135. .address-box {
  136. width: 690rpx;
  137. margin: 0 auto;
  138. padding-top: 30rpx;
  139. }
  140. </style>