addressAdd.vue 7.6 KB

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