addressManage.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <template>
  2. <view class="content">
  3. <view class="row b-b">
  4. <text class="tit">联系人</text>
  5. <input class="input" type="text" v-model="addressData.name" placeholder="收货人姓名" placeholder-class="placeholder" />
  6. </view>
  7. <view class="row b-b">
  8. <text class="tit">手机号</text>
  9. <input class="input" type="number" v-model="addressData.mobile" placeholder="收货人手机号码" placeholder-class="placeholder" />
  10. </view>
  11. <view class="row b-b" v-on:click="showCityPicker()">
  12. <text class="tit">地区</text>
  13. <view class="input">
  14. {{cityLebel}}
  15. </view>
  16. </view>
  17. <view class="row b-b">
  18. <text class="tit">详细</text>
  19. <input class="input" type="text" v-model="addressData.address" placeholder="详细地址,楼号" placeholder-class="placeholder" />
  20. </view>
  21. <view class="row default-row">
  22. <text class="tit">设为默认</text>
  23. <switch :checked="addressData.is_default" color="#fa436a" @change="switchChange" />
  24. </view>
  25. <button class="add-btn" @click="confirm">提交</button>
  26. <mpvue-city-picker @onChange="onChange" @onCancel="onCancel"
  27. @onConfirm="onConfirm" ref="mpvueCityPicker" ></mpvue-city-picker>
  28. </view>
  29. </template>
  30. <script>
  31. import mpvueCityPicker from '@/components/mpvueCityPicker.vue';
  32. export default {
  33. components: {
  34. mpvueCityPicker
  35. },
  36. data() {
  37. return {
  38. addressData: {
  39. name: '',
  40. mobile: '',
  41. address: '',
  42. province_id: 0,
  43. city_id: 0,
  44. area_id: 0,
  45. is_default: false
  46. },
  47. pickerValueDefault: [0, 0, 0] ,//城市选择器默认值 省市区id
  48. cityLebel:'请选择地区',
  49. }
  50. },
  51. onLoad(option) {
  52. let title = '新增收货地址';
  53. if (option.type === 'edit') {
  54. this.getInfo(option.id);
  55. title = '编辑收货地址'
  56. } else {
  57. this.$refs.mpvueCityPicker.creat(this.pickerValueDefault);
  58. }
  59. this.manageType = option.type;
  60. uni.setNavigationBarTitle({
  61. title
  62. })
  63. },
  64. methods: {
  65. // 获取地址详情
  66. async getInfo(id){
  67. let addressData = await this.$api.request(`/address/info?id=${id}`);
  68. if (addressData) {
  69. console.log(addressData);
  70. this.addressData = addressData;
  71. let pickerValueDefault = [];
  72. pickerValueDefault.push(addressData.province_id);
  73. pickerValueDefault.push(addressData.city_id);
  74. pickerValueDefault.push(addressData.area_id);
  75. this.pickerValueDefault = pickerValueDefault;
  76. this.$refs.mpvueCityPicker.creat(pickerValueDefault);
  77. }
  78. },
  79. // 城市选择器
  80. showCityPicker() {
  81. this.$refs.mpvueCityPicker.show();
  82. },
  83. // 城市选择器改变值
  84. onChange(e) {
  85. // console.log('选择的值')
  86. // console.log(e);
  87. },
  88. // 城市选择器关闭
  89. onCancel(e) {
  90. //console.log(e);
  91. },
  92. // 城市选择器确定
  93. onConfirm(e) {
  94. //console.log(e);
  95. this.cityLebel = e.label;
  96. this.pickerValueDefault = e.value;
  97. this.addressData.province_id = this.pickerValueDefault[0];
  98. this.addressData.city_id = this.pickerValueDefault[1];
  99. this.addressData.area_id = this.pickerValueDefault[2];
  100. },
  101. //默认地址
  102. switchChange(e) {
  103. this.addressData.is_default = e.detail.value;
  104. },
  105. //提交
  106. async confirm() {
  107. //Deep Clone
  108. let data = JSON.parse(JSON.stringify(this.addressData));
  109. if (!data.name) {
  110. this.$api.msg('请填写收货人姓名');
  111. return;
  112. }
  113. if (!/(^1[3|4|5|7|8][0-9]{9}$)/.test(data.mobile)) {
  114. this.$api.msg('请输入正确的手机号码');
  115. return;
  116. }
  117. if (!data.address) {
  118. this.$api.msg('请填详细地址信息');
  119. return;
  120. }
  121. console.log(data.is_default);
  122. data.is_default = data.is_default == true ? 1 : 0;
  123. let action = this.manageType == 'edit' ? 'edit' : 'add';
  124. let result = await this.$api.request('/address/' + action, 'POST', data);
  125. if (result) {
  126. this.$api.prePage().refreshList(data, this.manageType);
  127. setTimeout(() => {
  128. uni.navigateBack()
  129. }, 800)
  130. }
  131. },
  132. }
  133. }
  134. </script>
  135. <style lang="scss">
  136. page {
  137. background: $page-color-base;
  138. padding-top: 16upx;
  139. }
  140. .row {
  141. display: flex;
  142. align-items: center;
  143. position: relative;
  144. padding: 0 30upx;
  145. height: 110upx;
  146. background: #fff;
  147. .tit {
  148. flex-shrink: 0;
  149. width: 120upx;
  150. font-size: 30upx;
  151. color: $font-color-dark;
  152. }
  153. .input {
  154. flex: 1;
  155. font-size: 30upx;
  156. color: $font-color-dark;
  157. }
  158. .icon-shouhuodizhi {
  159. font-size: 36upx;
  160. color: $font-color-light;
  161. }
  162. }
  163. .default-row {
  164. margin-top: 16upx;
  165. .tit {
  166. flex: 1;
  167. }
  168. switch {
  169. transform: translateX(16upx) scale(.9);
  170. }
  171. }
  172. .add-btn {
  173. display: flex;
  174. align-items: center;
  175. justify-content: center;
  176. width: 690upx;
  177. height: 80upx;
  178. margin: 60upx auto;
  179. font-size: $font-lg;
  180. color: #fff;
  181. background-color: $base-color;
  182. border-radius: 10upx;
  183. box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
  184. }
  185. </style>