index.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <template>
  2. <page title="添加收货地址" ref="pageRef">
  3. <view class="paddingT10 paddingL14">
  4. <view class="formBox">
  5. <view class="paddingY14 paddingR14">
  6. <input
  7. v-model="consignee"
  8. placeholder-class="color-2"
  9. placeholder="收货人"
  10. class="font4 color-1"
  11. />
  12. </view>
  13. <view class="paddingY14 paddingR14">
  14. <input
  15. v-model="mobile"
  16. placeholder-class="color-2"
  17. placeholder="手机号"
  18. type="number"
  19. class="color-1"
  20. />
  21. </view>
  22. <view class="paddingY14 paddingR14 ">
  23. <view class="font4" :class="[area ? 'color-1' : 'color-2']">
  24. <!-- <text v-if="!area">所在区域</text>
  25. <text v-else>{{ area.province }}{{ area.city }}{{ area.district }}</text> -->
  26. <picker
  27. style=""
  28. mode="multiSelector"
  29. @change="bindPickerChange"
  30. @columnchange="handleColumnchange"
  31. :value="index"
  32. range-key="name"
  33. :range="array"
  34. >
  35. {{ getArea }}
  36. </picker>
  37. </view>
  38. </view>
  39. <view class="paddingY14 paddingR14">
  40. <input
  41. v-model="address"
  42. placeholder-class="color-2"
  43. placeholder="详细地址"
  44. type="tel"
  45. class="color-1"
  46. />
  47. </view>
  48. </view>
  49. <view class="paddingY20 paddingR20">
  50. <view class="flex-align">
  51. <view
  52. class="checkBox"
  53. :class="{ check: isDefault }"
  54. @click="isDefault = !isDefault"
  55. >
  56. <view class="cuIcon-check"></view>
  57. </view>
  58. <view class="color-1 font4 bold marginL5 marginR10">设为默认</view>
  59. </view>
  60. </view>
  61. </view>
  62. <view class="marginX14">
  63. <cm-button @click="save">保存</cm-button>
  64. </view>
  65. </page>
  66. </template>
  67. <script>
  68. const chooseLocation = requirePlugin('chooseLocation')
  69. import { NAME } from '@/utils/config'
  70. export default {
  71. data() {
  72. return {
  73. consignee: '',
  74. mobile: '',
  75. area: null,
  76. address: '',
  77. isDefault: false,
  78. array: [],
  79. index: [0, 0, 0]
  80. }
  81. },
  82. onShow() {
  83. // const location = chooseLocation.getLocation() // 如果点击确认选点按钮,则返回选点结果对象,否则返回null
  84. // if (location) {
  85. // this.area = location
  86. // this.address = location.name
  87. // }
  88. this.initLocation()
  89. },
  90. computed: {
  91. getArea() {
  92. return this.area ? this.area.province + this.area.city + this.area.district : '地区选择'
  93. }
  94. },
  95. onUnload() {},
  96. methods: {
  97. async initLocation() {
  98. const res1 = await this.$service.address.getArea({ pid: 1 })
  99. this.array.push(res1.data)
  100. const res2 = await this.$service.address.getArea({ pid: res1.data[0].id })
  101. this.array.push(res2.data)
  102. const res3 = await this.$service.address.getArea({ pid: res2.data[0].id })
  103. this.array.push(res3.data)
  104. },
  105. async changeProvince(pid) {
  106. const res = await this.$service.address.getArea({ pid })
  107. this.array.splice(1, 1, res.data)
  108. this.$nextTick(()=>{
  109. this.changeCity(res.data[0].id)
  110. })
  111. },
  112. async changeCity(pid) {
  113. const res = await this.$service.address.getArea({ pid })
  114. this.array.splice(2, 1, res.data)
  115. },
  116. bindPickerChange(val) {
  117. console.log(val)
  118. let value = val.detail.value
  119. this.area = {
  120. province: this.array[0][value[0]].name,
  121. city: this.array[1][value[1]].name,
  122. district: this.array[2][value[2]].name
  123. }
  124. },
  125. handleColumnchange(val) {
  126. console.log(val)
  127. if (val.detail.column == 0) {
  128. this.changeProvince(this.array[0][val.detail.value].id)
  129. }
  130. if (val.detail.column == 1) {
  131. this.changeCity(this.array[1][val.detail.value].id)
  132. }
  133. },
  134. openLocation() {
  135. chooseLocation.setLocation(null)
  136. wx.navigateTo({
  137. url: `plugin://chooseLocation/index?key=VHGBZ-K77C4-IJTUG-KKCDR-FEGR3-EOFFJ&referer=supermart`
  138. })
  139. },
  140. async save() {
  141. if (!this.consignee) {
  142. this.$message.warn('请输入收货人')
  143. return
  144. }
  145. if (!this.mobile) {
  146. this.$message.warn('请输入手机号')
  147. return
  148. }
  149. if (!this.area) {
  150. this.$message.warn('请选择区域')
  151. return
  152. }
  153. if (!this.address) {
  154. this.$message.warn('请输入详细地址')
  155. return
  156. }
  157. let location = `${this.area.longitude},${this.area.latitude}`
  158. const res = await this.$service.address.add(
  159. this.consignee,
  160. this.mobile,
  161. location,
  162. this.area.province,
  163. this.area.city,
  164. this.area.district,
  165. this.address,
  166. this.isDefault ? 1 : 0
  167. )
  168. if (res) {
  169. this.$router.back()
  170. this.$event.emit(this.$event.key.ADDRESS)
  171. }
  172. }
  173. }
  174. }
  175. </script>
  176. <style lang="scss" scoped>
  177. page {
  178. background: #fff;
  179. }
  180. .checkBox {
  181. border: 4rpx solid #000000;
  182. background-color: #fff;
  183. color: #fff;
  184. width: 32rpx;
  185. border-radius: 50%;
  186. margin-right: 12rpx;
  187. height: 32rpx;
  188. text-align: center;
  189. line-height: 28rpx;
  190. font-size: 24rpx;
  191. &.check {
  192. background: #fec433;
  193. border-color: #fec433;
  194. color: #000;
  195. }
  196. }
  197. .formBox {
  198. background-color: #fff;
  199. border-radius: 12rpx;
  200. padding: 0 32rpx;
  201. }
  202. </style>