addressAdd.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <template>
  2. <view class="wrap">
  3. <view class="top">
  4. <view class="item">
  5. <view class="left">收货人</view>
  6. <input type="text" placeholder-class="line" placeholder="请填写收货人姓名" v-model="address.name" />
  7. </view>
  8. <view class="item">
  9. <view class="left">手机号码</view>
  10. <input type="text" placeholder-class="line" placeholder="请填写收货人手机号" v-model="address.tel" />
  11. </view>
  12. <view class="item" @tap="showRegionPicker">
  13. <view class="left">所在地区</view>
  14. <input disabled type="text" placeholder-class="line" placeholder="省市区县、乡镇等"
  15. v-model="addressShowValue" />
  16. </view>
  17. <view class="item address">
  18. <view class="left">详细地址</view>
  19. <textarea type="text" placeholder-class="line" placeholder="街道、楼牌等" v-model="address.addressDetail" />
  20. </view>
  21. </view>
  22. <view class="bottom">
  23. <view class="default">
  24. <view class="left">
  25. <view class="set">设置默认地址</view>
  26. <view class="tips">提醒:每次下单会默认推荐该地址</view>
  27. </view>
  28. <view class="right"><uv-switch v-model="address.isDefault" @change="changeDefault"></uv-switch></view>
  29. </view>
  30. <view class="button">
  31. <uv-button type="error" shape="circle" @click="save">保存</uv-button>
  32. </view>
  33. <view class="button">
  34. <uv-button type="info" shape="circle" v-if="id!=''" @click="del">删除</uv-button>
  35. </view>
  36. </view>
  37. <uv-picker ref="uPicker" :columns="addressList" keyName="label" :defaultIndex="defaultRegion"
  38. @confirm="confirmArea" @change="change">
  39. </uv-picker>
  40. </view>
  41. </template>
  42. <script>
  43. import {
  44. getAddressDetail,
  45. saveAddress,
  46. setDefaultAddress
  47. } from '@/request/api/shop.js'
  48. import cityData from '../static/js/city.js';
  49. export default {
  50. data() {
  51. return {
  52. choose: true,
  53. id: '',
  54. address: {
  55. isDefault: false
  56. },
  57. defaultRegion: ['', '', ''],
  58. provinces: [], //省
  59. citys: [], //市
  60. areas: [], //区
  61. pickerValue: [0, 0, 0]
  62. };
  63. },
  64. computed: {
  65. addressShowValue() {
  66. if (this.address.province) {
  67. return this.address.province + ' ' + this.address.city + ' ' + this.address.district;
  68. }
  69. },
  70. addressList() {
  71. return [this.provinces, this.citys, this.areas];
  72. }
  73. },
  74. onLoad(option) {
  75. this.id = option.id
  76. if (option.choose) {
  77. this.choose = option.choose
  78. }
  79. if (this.id) {
  80. this.init()
  81. } else {
  82. this.id = ''
  83. //显示默认的地区
  84. this.defaultRegion = ["11", "1101", "110101"]
  85. }
  86. },
  87. methods: {
  88. async init() {
  89. let addressData = await getAddressDetail(this.id);
  90. if (addressData.state) {
  91. let res = addressData.data;
  92. const code1 = res.areaCode.substr(0, 2);
  93. const code2 = res.areaCode.substr(0, 4);
  94. const code3 = res.areaCode;
  95. this.defaultRegion = [code1, code2, code3]
  96. this.address = res;
  97. this.getData();
  98. }
  99. },
  100. getData() {
  101. this.provinces = cityData;
  102. this.handlePickValueDefault();
  103. },
  104. handlePickValueDefault() {
  105. // 设置省
  106. this.pickerValue[0] = this.provinces.findIndex(item => item.value === this.defaultRegion[
  107. 0]);
  108. // 设置市
  109. this.citys = this.provinces[this.pickerValue[0]]?.children || [];
  110. this.pickerValue[1] = this.citys.findIndex(item => item.value === this.defaultRegion[1]);
  111. // 设置区
  112. this.areas = this.citys[this.pickerValue[1]]?.children || [];
  113. this.pickerValue[2] = this.areas.findIndex(item => item.value === this.defaultRegion[2]);
  114. // 重置下位置
  115. this.$refs.uPicker.setIndexs([this.pickerValue[0], this.pickerValue[1], this.pickerValue[2]], true);
  116. },
  117. change(e) {
  118. const {
  119. columnIndex,
  120. index,
  121. indexs
  122. } = e
  123. // 改变了省
  124. if (columnIndex === 0) {
  125. this.citys = this.provinces[index]?.children || []
  126. this.areas = this.citys[0]?.children || []
  127. this.$refs.uPicker.setIndexs([index, 0, 0], true)
  128. } else if (columnIndex === 1) {
  129. this.areas = this.citys[index]?.children || []
  130. this.$refs.uPicker.setIndexs(indexs, true)
  131. }
  132. },
  133. setDefault() {},
  134. showRegionPicker() {
  135. this.$refs.uPicker.open();
  136. this.handlePickValueDefault();
  137. },
  138. confirmArea(e) {
  139. this.address.areaCode = e.value[2].value;
  140. //设置选择器默认值
  141. this.defaultRegion = [e.value[0].value, e.value[1].value, e.value[2].value];
  142. //设置输入框值
  143. this.address = Object.assign({}, this.address)
  144. this.address.province = e.value[0].label;
  145. this.address.city = e.value[1].label;
  146. this.address.district = e.value[2].label;
  147. },
  148. async changeDefault(e) {
  149. if (this.address.id) {
  150. let setData = await setDefaultAddress(this.address.id, e);
  151. if (setData.state) this.$toast(e ? '设置默认地址' : '取消默认地址');
  152. }
  153. },
  154. async save() {
  155. let saveData = await saveAddress(this.address);
  156. if (saveData.state) {
  157. let res = saveData.data;
  158. if (this.choose) {
  159. let idCarts = uni.getStorageSync('idCarts')
  160. uni.setStorageSync('chooseAddrId', res.id);
  161. }
  162. this.$toast('操作成功');
  163. uni.navigateBack();
  164. }
  165. },
  166. async del() {
  167. let deleteData = await deleteAddress(this.address.id);
  168. if (deleteData.state) {
  169. this.$toast('操作成功');
  170. uni.navigateBack();
  171. }
  172. }
  173. }
  174. };
  175. </script>
  176. <style lang="scss" scoped>
  177. .wrap {
  178. padding: 30rpx 0rpx;
  179. .top {
  180. padding: 0 30rpx;
  181. background: #ffffff;
  182. .item {
  183. display: flex;
  184. font-size: 32rpx;
  185. line-height: 100rpx;
  186. align-items: center;
  187. border-bottom: solid 2rpx $uv-border-color;
  188. .left {
  189. width: 180rpx;
  190. }
  191. input {
  192. text-align: left;
  193. }
  194. }
  195. .address {
  196. padding: 20rpx 0;
  197. textarea {
  198. // width: 100%;
  199. height: 150rpx;
  200. background-color: #f7f7f7;
  201. line-height: 60rpx;
  202. margin: 40rpx auto;
  203. padding: 20rpx;
  204. }
  205. }
  206. .site-clipboard {
  207. padding-right: 40rpx;
  208. textarea {
  209. // width: 100%;
  210. height: 150rpx;
  211. background-color: #f7f7f7;
  212. line-height: 60rpx;
  213. margin: 40rpx auto;
  214. padding: 20rpx;
  215. }
  216. .clipboard {
  217. display: flex;
  218. justify-content: center;
  219. align-items: center;
  220. font-size: 26rpx;
  221. color: $uv-tips-color;
  222. height: 80rpx;
  223. .icon {
  224. margin-top: 6rpx;
  225. margin-left: 10rpx;
  226. }
  227. }
  228. }
  229. }
  230. .bottom {
  231. margin-top: 30rpx;
  232. padding: 30rpx;
  233. background-color: #ffffff;
  234. font-size: 28rpx;
  235. .button {
  236. margin-top: 20rpx;
  237. }
  238. .tag {
  239. display: flex;
  240. .left {
  241. width: 160rpx;
  242. }
  243. .right {
  244. display: flex;
  245. flex-wrap: wrap;
  246. .tags {
  247. width: 140rpx;
  248. padding: 16rpx 8rpx;
  249. border: solid 2rpx $uv-border-color;
  250. text-align: center;
  251. border-radius: 50rpx;
  252. margin: 0 10rpx 20rpx;
  253. display: flex;
  254. font-size: 28rpx;
  255. align-items: center;
  256. justify-content: center;
  257. color: $uv-content-color;
  258. line-height: 1;
  259. }
  260. .plus {
  261. //padding: 10rpx 0;
  262. }
  263. }
  264. }
  265. .default {
  266. display: flex;
  267. justify-content: space-between;
  268. line-height: 64rpx;
  269. .tips {
  270. font-size: 24rpx;
  271. }
  272. .right {}
  273. }
  274. }
  275. }
  276. </style>