addressAdd.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. deleteAddress
  48. } from '@/request/api/shop.js'
  49. import cityData from '../static/js/city.js';
  50. export default {
  51. data() {
  52. return {
  53. choose: true,
  54. id: '',
  55. address: {
  56. isDefault: false
  57. },
  58. defaultRegion: ['', '', ''],
  59. provinces: [], //省
  60. citys: [], //市
  61. areas: [], //区
  62. pickerValue: [0, 0, 0]
  63. };
  64. },
  65. computed: {
  66. addressShowValue() {
  67. if (this.address.province) {
  68. return this.address.province + ' ' + this.address.city + ' ' + this.address.district;
  69. }
  70. },
  71. addressList() {
  72. return [this.provinces, this.citys, this.areas];
  73. }
  74. },
  75. onLoad(option) {
  76. this.id = option.id
  77. if (option.choose) {
  78. this.choose = option.choose
  79. }
  80. if (this.id) {
  81. this.init()
  82. } else {
  83. this.id = ''
  84. //显示默认的地区
  85. this.defaultRegion = ["11", "1101", "110101"];
  86. this.getData();
  87. }
  88. },
  89. methods: {
  90. async init() {
  91. let addressData = await getAddressDetail(this.id);
  92. if (addressData.state) {
  93. let res = addressData.data;
  94. const code1 = res.areaCode.substr(0, 2);
  95. const code2 = res.areaCode.substr(0, 4);
  96. const code3 = res.areaCode;
  97. this.defaultRegion = [code1, code2, code3]
  98. this.address = res;
  99. this.getData();
  100. }
  101. },
  102. getData() {
  103. this.provinces = cityData;
  104. this.handlePickValueDefault();
  105. },
  106. handlePickValueDefault() {
  107. // 设置省
  108. this.pickerValue[0] = this.provinces.findIndex(item => item.value === this.defaultRegion[
  109. 0]);
  110. // 设置市
  111. this.citys = this.provinces[this.pickerValue[0]]?.children || [];
  112. this.pickerValue[1] = this.citys.findIndex(item => item.value === this.defaultRegion[1]);
  113. // 设置区
  114. this.areas = this.citys[this.pickerValue[1]]?.children || [];
  115. this.pickerValue[2] = this.areas.findIndex(item => item.value === this.defaultRegion[2]);
  116. // 重置下位置
  117. this.$refs.uPicker.setIndexs([this.pickerValue[0], this.pickerValue[1], this.pickerValue[2]], true);
  118. },
  119. change(e) {
  120. const {
  121. columnIndex,
  122. index,
  123. indexs
  124. } = e
  125. // 改变了省
  126. if (columnIndex === 0) {
  127. this.citys = this.provinces[index]?.children || []
  128. this.areas = this.citys[0]?.children || []
  129. this.$refs.uPicker.setIndexs([index, 0, 0], true)
  130. } else if (columnIndex === 1) {
  131. this.areas = this.citys[index]?.children || []
  132. this.$refs.uPicker.setIndexs(indexs, true)
  133. }
  134. },
  135. setDefault() {},
  136. showRegionPicker() {
  137. this.$refs.uPicker.open();
  138. this.handlePickValueDefault();
  139. },
  140. confirmArea(e) {
  141. this.address.areaCode = e.value[2].value;
  142. //设置选择器默认值
  143. this.defaultRegion = [e.value[0].value, e.value[1].value, e.value[2].value];
  144. //设置输入框值
  145. this.address = Object.assign({}, this.address)
  146. this.address.province = e.value[0].label;
  147. this.address.city = e.value[1].label;
  148. this.address.district = e.value[2].label;
  149. },
  150. async changeDefault(e) {
  151. if (this.address.id) {
  152. let setData = await setDefaultAddress(this.address.id, e);
  153. if (setData.state) this.$toast(e ? '设置默认地址' : '取消默认地址');
  154. }
  155. },
  156. async save() {
  157. let saveData = await saveAddress(this.address);
  158. if (saveData.state) {
  159. let res = saveData.data;
  160. if (this.choose) {
  161. let idCarts = uni.getStorageSync('idCarts')
  162. uni.setStorageSync('chooseAddrId', res.id);
  163. }
  164. this.$toast('操作成功');
  165. uni.navigateBack();
  166. }
  167. },
  168. del() {
  169. uni.showModal({
  170. title: 'WORKARK提示',
  171. content: '是否取消该地址',
  172. success: async (res) => {
  173. if (res.confirm) {
  174. let deleteData = await deleteAddress(this.address.id);
  175. if (deleteData.state) {
  176. this.$toast('操作成功');
  177. uni.navigateBack();
  178. }
  179. }
  180. }
  181. });
  182. }
  183. }
  184. };
  185. </script>
  186. <style lang="scss" scoped>
  187. .wrap {
  188. padding: 30rpx 0rpx;
  189. .top {
  190. padding: 0 30rpx;
  191. background: #ffffff;
  192. .item {
  193. display: flex;
  194. font-size: 32rpx;
  195. line-height: 100rpx;
  196. align-items: center;
  197. border-bottom: solid 2rpx $uv-border-color;
  198. .left {
  199. width: 180rpx;
  200. }
  201. input {
  202. text-align: left;
  203. }
  204. }
  205. .address {
  206. padding: 20rpx 0;
  207. textarea {
  208. // width: 100%;
  209. height: 150rpx;
  210. background-color: #f7f7f7;
  211. line-height: 60rpx;
  212. margin: 40rpx auto;
  213. padding: 20rpx;
  214. }
  215. }
  216. .site-clipboard {
  217. padding-right: 40rpx;
  218. textarea {
  219. // width: 100%;
  220. height: 150rpx;
  221. background-color: #f7f7f7;
  222. line-height: 60rpx;
  223. margin: 40rpx auto;
  224. padding: 20rpx;
  225. }
  226. .clipboard {
  227. display: flex;
  228. justify-content: center;
  229. align-items: center;
  230. font-size: 26rpx;
  231. color: $uv-tips-color;
  232. height: 80rpx;
  233. .icon {
  234. margin-top: 6rpx;
  235. margin-left: 10rpx;
  236. }
  237. }
  238. }
  239. }
  240. .bottom {
  241. margin-top: 30rpx;
  242. padding: 30rpx;
  243. background-color: #ffffff;
  244. font-size: 28rpx;
  245. .button {
  246. margin-top: 20rpx;
  247. }
  248. .tag {
  249. display: flex;
  250. .left {
  251. width: 160rpx;
  252. }
  253. .right {
  254. display: flex;
  255. flex-wrap: wrap;
  256. .tags {
  257. width: 140rpx;
  258. padding: 16rpx 8rpx;
  259. border: solid 2rpx $uv-border-color;
  260. text-align: center;
  261. border-radius: 50rpx;
  262. margin: 0 10rpx 20rpx;
  263. display: flex;
  264. font-size: 28rpx;
  265. align-items: center;
  266. justify-content: center;
  267. color: $uv-content-color;
  268. line-height: 1;
  269. }
  270. .plus {
  271. //padding: 10rpx 0;
  272. }
  273. }
  274. }
  275. .default {
  276. display: flex;
  277. justify-content: space-between;
  278. line-height: 64rpx;
  279. .tips {
  280. font-size: 24rpx;
  281. }
  282. .right {}
  283. }
  284. }
  285. }
  286. </style>