create.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <template>
  2. <view>
  3. <!-- #ifdef MP-ALIPAY -->
  4. <u-navbar :title="addrId ? '编辑地址' : '添加地址'" :border="true" :placeholder="true" :autoBack="true"
  5. bgColor="#fff" leftIconSize="0" />
  6. <!-- #endif -->
  7. <!-- #ifndef MP-ALIPAY -->
  8. <u-navbar :title="addrId ? '编辑地址' : '添加地址'" :border="true" :placeholder="true" :autoBack="true"
  9. bgColor="#fff" />
  10. <!-- #endif -->
  11. <!-- 表单组件 -->
  12. <view class="address-add">
  13. <u-form labelPosition="left" ref="form" labelWidth="90">
  14. <u-form-item label="收货人:" borderBottom required>
  15. <u-input v-model="form.receiver" border="none" placeholder="请输入收货人"></u-input>
  16. </u-form-item>
  17. <u-form-item label="手机号码:" borderBottom required>
  18. <u-input v-model="form.mobile" type="number" border="none" placeholder="请输入手机号码"></u-input>
  19. </u-form-item>
  20. <u-form-item label="所在地区:" borderBottom required @click="selectArea">
  21. <u-input v-model="form.cityShow" border="none" disabled disabledColor="#ffffff"
  22. placeholder="请选择地区">
  23. </u-input>
  24. <u-icon slot="right" name="arrow-right"></u-icon>
  25. </u-form-item>
  26. <u-form-item label="详细地址:" :borderBottom="false" required>
  27. <u-textarea v-model="form.addr" placeholder="请输入详细地址"></u-textarea>
  28. </u-form-item>
  29. </u-form>
  30. </view>
  31. <!-- 底部操作按钮 -->
  32. <view class="footer-fixed">
  33. <view class="btn-wrapper">
  34. <button type="default" @click="saveAddress">保存</button>
  35. </view>
  36. </view>
  37. <area-picker :area-show="areaShow" @cancel="cancel" @confirmArea="confirmArea" />
  38. </view>
  39. </template>
  40. <script>
  41. import $http from '@/utils/request.js'
  42. import AreaPicker from '../../components/area-picker/area-picker.vue'
  43. export default {
  44. components: {
  45. AreaPicker
  46. },
  47. data() {
  48. return {
  49. loading: false,
  50. addrId: '',
  51. form: {
  52. receiver: '',
  53. mobile: '',
  54. name: '',
  55. cityShow: '',
  56. addr: '',
  57. provinceId: '', // 省ID/编码
  58. province: '', // 省
  59. cityId: '', // 市ID/编码
  60. city: '', // 市
  61. areaId: '', // 区ID/编码
  62. area: '', // 区
  63. },
  64. areaShow: false,
  65. }
  66. },
  67. onLoad(options) {
  68. if (options.addrId) {
  69. this.addrId = options.addrId
  70. this.getAddrDetail()
  71. }
  72. },
  73. methods: {
  74. getAddrDetail() {
  75. uni.hideLoading();
  76. $http.post('/api/v1/mp/user/addr/query', {
  77. addrId: this.addrId
  78. }).then(res => {
  79. uni.hideLoading();
  80. if (res.code == 0) {
  81. let item = res.data
  82. this.form = {
  83. ...item,
  84. cityShow: `${ item.province }-${ item.city }-${ item.area }`
  85. }
  86. }
  87. }).catch(() => {
  88. uni.hideLoading();
  89. })
  90. },
  91. selectArea() {
  92. this.areaShow = true
  93. },
  94. cancel(){
  95. this.areaShow = false
  96. },
  97. confirmArea(obj) {
  98. this.form.province = obj.province
  99. this.form.provinceId = obj.provinceId
  100. this.form.city = obj.city
  101. this.form.cityId = obj.cityId
  102. this.form.area = obj.area
  103. this.form.areaId = obj.areaId
  104. this.form.cityShow = obj.cityShow
  105. this.areaShow = false
  106. },
  107. saveAddress() {
  108. let _this = this
  109. if (!_this.form.receiver) {
  110. uni.$u.toast('请输入收货人');
  111. return
  112. }
  113. if (!_this.form.mobile) {
  114. uni.$u.toast('请输入手机号码');
  115. return
  116. }
  117. const rule = /^[1][0-9][0-9]{9}$/
  118. if (!rule.test(_this.form.mobile)) {
  119. uni.$u.toast('请输入正确的手机号');
  120. return
  121. }
  122. if (!_this.form.cityShow) {
  123. uni.$u.toast('请选择所在地区');
  124. return
  125. }
  126. if (!_this.form.addr) {
  127. uni.$u.toast('请输入详细地址');
  128. return
  129. }
  130. let url = this.addrId ? '/api/v1/mp/user/addr/update' : '/api/v1/mp/user/addr/create'
  131. $http.post(url, this.form).then(res => {
  132. if (res.code == 0) {
  133. uni.$u.toast('保存成功');
  134. setTimeout(() => {
  135. uni.navigateBack({
  136. delta: 1
  137. })
  138. }, 1000)
  139. }
  140. })
  141. },
  142. },
  143. }
  144. </script>
  145. <style lang="scss" scoped>
  146. .address-add {
  147. box-shadow: 0 1rpx 5rpx 0px rgba(0, 0, 0, 0.05);
  148. border-radius: 10rpx;
  149. background-color: #FFFFFF;
  150. padding: 40rpx;
  151. margin: 20rpx;
  152. }
  153. // 底部操作栏
  154. .footer-fixed {
  155. position: fixed;
  156. bottom: var(--window-bottom);
  157. left: 0;
  158. right: 0;
  159. min-height: 120rpx;
  160. z-index: 11;
  161. box-shadow: 0 -4rpx 40rpx 0 rgba(151, 151, 151, 0.24);
  162. background: #fff;
  163. // 设置ios刘海屏底部横线安全区域
  164. padding-bottom: constant(safe-area-inset-bottom);
  165. padding-bottom: env(safe-area-inset-bottom);
  166. .btn-wrapper {
  167. display: flex;
  168. align-items: center;
  169. padding: 20rpx 0;
  170. }
  171. ::v-deep button {
  172. width: 80%;
  173. height: 76rpx;
  174. line-height: 76rpx;
  175. font-size: 28rpx;
  176. color: #fff;
  177. background-color: $uni-bg-color;
  178. border: none;
  179. border-radius: 44rpx;
  180. margin: 0 auto;
  181. }
  182. }
  183. </style>