Jacob
1 天以前 e6fea61c05d96ed2e6aa1aff9e7026e48b046332
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
<template>
   <view class="new-address copy-data">
       <!-- #ifdef APP-->
       <view class='status'></view>
       <!-- #endif -->
       <form @submit="formSubmit" report-submit='true' style="overflow: hidden;background: #F5F5F5;height: 100vh;">
           <view class='addAddress pad30' v-if="loaded">
               <view class='list borRadius14'>
                   <view class='item acea-row row-between-wrapper' style="border: none;">
                       <view class='name'>姓名</view>
                       <input type='text' placeholder='请输入收货人姓名' placeholder-style="color:#ccc;" name='realName'
                           :value="userAddress.realName" placeholder-class='placeholder' maxlength="10"></input>
                   </view>
                   <view class='item acea-row row-between-wrapper'>
                       <view class='name'>手机号码</view>
                       <input type='number' placeholder='请输入手机号码' placeholder-style="color:#ccc;" name="phone"
                           :value='userAddress.phone' placeholder-class='placeholder' maxlength="11"></input>
                   </view>
                   <view class='item acea-row row-between-wrapper relative'>
                       <view class='name'>所在地区</view>
                       <view class="address">
                           <picker mode="multiSelector" @change="bindRegionChange"
                               @columnchange="bindMultiPickerColumnChange" :value="valueRegion" :range="multiArray">
                               <view class='acea-row'>
                                   <view class="picker line1" v-if="region.length>0">
                                       {{region[0]}},{{region[1]}},{{region[2]}}</view>
                                   <view class="picker line1 placeholder" v-else>省市区县、乡镇等</view>
                                   <view class='iconfont icon-xiangyou abs_right'></view>
                               </view>
                           </picker>
                       </view>
                   </view>
                   <view class='item acea-row row-between-wrapper relative'>
                       <view class='name'>详细地址</view>
                       <input type='text' placeholder='街道楼牌号等' placeholder-style="color:#ccc;" name='detail'
                           placeholder-class='placeholder' v-model='userAddress.detail' maxlength="50"></input>
                       <view class='iconfont icon-dizhi font-color abs_right' @tap="chooseLocation"></view>
                   </view>
                   <view class='item acea-row row-between-wrapper relative'>
                       <view class='name'>设为默认</view>
                       <switch @change='ChangeIsDefault' color="#04C763" :checked="userAddress.isDefault"
                           style="transform:scale(0.8)" />
                   </view>
               </view>
 
               <button class='keepBnt bg-color' form-type="submit">保存</button>
               <!-- #ifdef MP -->
               <view class="wechatAddress" v-if="!id" @click="getWxAddress">微信导入</view>
               <!-- #endif -->
               <!-- #ifdef H5 -->
               <view class="wechatAddress" v-if="this.$wechat.isWeixin() && !id" @click="getAddress">微信导入</view>
               <!-- #endif -->
           </view>
       </form>
       <!-- #ifdef MP -->
       <!-- <authorize @onLoadFun="onLoadFun" :isAuto="isAuto" :isShowAuth="isShowAuth" @authColse="authColse"></authorize> -->
       <!-- #endif -->
       <!-- <home></home> -->
   </view>
</template>
 
<script>
   import {
       editAddress,
       getAddressDetail
   } from '@/api/user.js';
   import {
       getCity
   } from '@/api/api.js';
   import {
       toLogin
   } from '@/libs/login.js';
   import {
       mapGetters
   } from "vuex";
   import home from '@/components/home';
   // import city from '@/utils/cityData';
   let app = getApp();
   export default {
       components: {
           home
       },
       data() {
           return {
               regionDval: ['浙江省', '杭州市', '滨江区'],
               cartId: '', //购物车id
               pinkId: 0, //拼团id
               couponId: 0, //优惠券id
               id: 0, //地址id
               userAddress: {
                   isDefault: false
               }, //地址详情
               region: [],
               valueRegion: [0, 0, 0],
               isAuto: false, //没有授权的不会自动授权
               isShowAuth: false, //是否隐藏授权
               district: [],
               multiArray: [],
               multiIndex: [0, 0, 0],
               cityId: 0,
               defaultRegion: ['广东省', '广州市', '番禺区'],
               defaultRegionCode: '440113',
               bargain: false, //是否是砍价
               combination: false, //是否是拼团
               secKill: false, //是否是秒杀
               isPageshow:false,
               loginBackFn:null,
               loaded:false
           };
       },
       computed: mapGetters(['isLogin']),
       watch: {
           isLogin: {
               handler: function(newV, oldV) {
                   if (newV) {
                       this.getUserAddress();
                   }
               },
               deep: true
           }
       },
       onShow() {
           let that = this;
           if (that.$store.getters.isSystemHide) {
               console.log('系统事件触发了onshow事件');
               return false;
           }
           
           let pages = getCurrentPages(),
               currPage = pages[pages.length - 1];
           console.log('------------------page onShow start-----------------');
           console.log('route', currPage.route);
           console.log('options', currPage.options);
           console.log('------------------page onShow end-----------------');
           
           if (that.$store.getters.isBelong) {
               that.belongInfoReadyFn({
                   isRegister: false
               })
           }else{
               uni.$once('BELONG_READY', that.belongInfoReadyFn);
           }
       },
       onHide(){
           let that=this;
           if (that.$store.getters.isSystemHide) {
               console.log('系统事件触发了onhide事件');
               return false;
           }
           that.loaded=false;
           uni.$off('loginFlSuccess', that.registerFn);
           uni.$off('BELONG_READY', that.belongInfoReadyFn);
       },
       methods: {
           belongInfoReadyFn(data) {
               let that = this;
               if (!data.isRegister) {
                   that.checkLoginFn()
               }else{
                   that.loadFn()
               }
           },
           checkLoginFn() {
               let that=this;
               let pages = getCurrentPages(),
               currPage = pages[pages.length - 1];
               let options=currPage.options;
               that.addTrack('yy_pageView', {
                   pageTitle_var: options.id ? '修改地址' : '添加地址',
                   pagePath_var: currPage.$page.fullPath,
                   pageType_var: '地址管理'
               });
               if(that.$store.getters.isLogin){
                   that.loadFn();
               }else{
                   toLogin(that);
               }
               that.loginBackFn=function(){
                   that.loadFn()
               };
               uni.$once('loginFlSuccess', that.registerFn);
           },
           registerFn(){
               let that=this;
               if(that.isPageShow){
                   if(that.loginBackFn){
                       that.loginBackFn();
                   }
                   that.loginBackFn=null;
               }
           },
           loadFn(){
               let that=this;
               let pages = getCurrentPages(),
               currPage = pages[pages.length - 1];
               let options=currPage.options;
               this.preOrderNo = options.preOrderNo || 0;
               this.id = options.id || 0;
               uni.setNavigationBarTitle({
                   title: options.id ? '修改地址' : '添加地址'
               })
               this.getUserAddress();
           },
           // #ifdef APP-PLUS
           // 获取选择的地区
           handleGetRegion(region) {
               this.region = region
           },
           // #endif
           // 获取地址数据
           getCityList: function() {
               let that = this;
               if (this.$Cache.has('cityList')&&this.$Cache.getItem('cityList')) {
                   console.log('缓存拿到的城市列表',this.$Cache.getItem('cityList'));
                   //检测城市数据是否存在缓存,有的话从缓存取,没有的话请求接口
                   this.district = this.$Cache.getItem('cityList')
                   this.initialize();
               } else {
                   uni.showLoading();
                   getCity().then(res => {
                       uni.hideLoading();
                       console.log('地址接口的返回值',res.data);
                       if(res.data.length){
                           this.district = res.data;
                           let oneDay = 24 * 3600 * 1000;
                           // this.$Cache.set('cityList', JSON.stringify(res.data)); //设置不过期时间的方法 
                           this.$Cache.setItem({
                               name: 'cityList',
                               value: res.data,
                               expires: oneDay * 7
                           }); //设置七天过期时间
                           that.initialize();
                       }else{
                           console.log('地址接口未获取到值');
                           that.$util.Tips({
                               title: "地址接口未获取到值"
                           });
                       }
                   })
               }
           },
           initialize: function() {
               let that = this,
                   province = [],
                   city = [],
                   area = [];
               if (that.district.length) {
                   let cityChildren = that.district[0].child || [];
                   let areaChildren = cityChildren.length ? (cityChildren[0].child || []) : [];
                   that.district.forEach(function(item) {
                       province.push(item.name);
                   });
                   cityChildren.forEach(function(item) {
                       city.push(item.name);
                   });
                   areaChildren.forEach(function(item) {
                       area.push(item.name);
                   });
                   this.multiArray = [province, city, area];
                   this.loaded=true;
               }else{
                   console.log('选择地址列表不存在');
                   that.$util.Tips({
                       title: "选择地址列表不存在"
                   });
               }
           },
           bindRegionChange: function(e) {
               console.log('bindRegionChange');
               let multiIndex = this.multiIndex,
                   province = this.district[multiIndex[0]] || {
                       child: []
                   },
                   city = province.child[multiIndex[1]] || {
                       cityId: 0
                   },
                   multiArray = this.multiArray,
                   value = e.detail.value;
 
               this.region = [multiArray[0][value[0]], multiArray[1][value[1]], multiArray[2][value[2]]]
               this.cityId = city.cityId
               this.valueRegion = [0, 0, 0]
               this.initialize();
           },
           bindMultiPickerColumnChange: function(e) {
               let that = this,
                   column = e.detail.column,
                   value = e.detail.value,
                   currentCity = this.district[value] || {
                       child: []
                   },
                   multiArray = that.multiArray,
                   multiIndex = that.multiIndex;
               multiIndex[column] = value;
               switch (column) {
                   case 0:
                       let areaList = currentCity.child[0] || {
                           child: []
                       };
                       multiArray[1] = currentCity.child.map((item) => {
                           return item.name;
                       });
                       multiArray[2] = areaList.child.map((item) => {
                           return item.name;
                       });
                       break;
                   case 1:
                       let cityList = that.district[multiIndex[0]].child[multiIndex[1]].child || [];
                       multiArray[2] = cityList.map((item) => {
                           return item.name;
                       });
                       break;
                   case 2:
 
                       break;
               }
               // #ifdef MP || APP-PLUS
               this.$set(this.multiArray, 0, multiArray[0]);
               this.$set(this.multiArray, 1, multiArray[1]);
               this.$set(this.multiArray, 2, multiArray[2]);
               // #endif
               // #ifdef H5
               this.multiArray = multiArray;
               // #endif
               this.multiIndex = multiIndex
               // this.setData({ multiArray: multiArray, multiIndex: multiIndex});
           },
           // 授权关闭
           authColse: function(e) {
               this.isShowAuth = e
           },
           toggleTab(str) {
               this.$refs[str].show();
           },
           onConfirm(val) {
               this.region = val.checkArr[0] + '-' + val.checkArr[1] + '-' + val.checkArr[2];
           },
           getUserAddress: function() {
               if (!this.id){
                   this.getCityList();
                   return false;
               } 
               let that = this;
               uni.showLoading();
               getAddressDetail(this.id).then(res => {
                   uni.hideLoading();
                   let region = [res.data.province, res.data.city, res.data.district];
                   that.$set(that, 'userAddress', res.data);
                   that.$set(that, 'region', region);
                   that.city_id = res.data.cityId;
                   this.getCityList();
               });
           },
           chooseLocation: function() {
               let that = this;
               that.$store.commit('UPDATE_isSystemHide', {
                   isSystemHide: (new Date()).getTime()
               });
               uni.chooseLocation({
                   success: (res) => {
                       this.$set(this.userAddress, 'detail', res.address.replace(/.+?(省|市|自治区|自治州|县|区)/g,
                           ''));
                   }
               })
           },
           // 导入共享地址(小程序)
           getWxAddress: function() {
               let that = this;
               that.$store.commit('UPDATE_isSystemHide', {
                   isSystemHide: (new Date()).getTime()
               });
               uni.authorize({
                   scope: 'scope.address',
                   success: function(res) {
                       console.log('isSystemHide-------',that.$store.getters.isSystemHide)
                       uni.chooseAddress({
                           success: function(res) {
                               let addressP = {};
                               addressP.province = res.provinceName;
                               addressP.city = res.cityName;
                               addressP.district = res.countyName;
                               addressP.cityId = 0;
                               editAddress({
                                   address: addressP,
                                   isDefault: true,
                                   realName: res.userName,
                                   postCode: res.postalCode,
                                   phone: res.telNumber,
                                   detail: res.detailInfo,
                                   id: 0
                               }).then(res => {
                                   setTimeout(function() {
                                       if (that.cartId) {
                                           let cartId = that.cartId;
                                           let pinkId = that.pinkId;
                                           let couponId = that.couponId;
                                           that.cartId = '';
                                           that.pinkId = '';
                                           that.couponId = '';
                                           uni.navigateTo({
                                               url: '/pages/users/order_confirm/index?cartId=' +
                                                   cartId +
                                                   '&addressId=' + (
                                                       that.id ? that
                                                       .id :
                                                       res.data
                                                       .id) +
                                                   '&pinkId=' +
                                                   pinkId +
                                                   '&couponId=' +
                                                   couponId +
                                                   '&secKill=' + that
                                                   .secKill +
                                                   '&combination=' +
                                                   that.combination +
                                                   '&bargain=' + that
                                                   .bargain
                                           });
                                       } else {
                                           uni.navigateBack({
                                               delta: 1
                                           });
                                       }
                                   }, 1000);
                                   return that.$util.Tips({
                                       title: "添加成功",
                                       icon: 'success'
                                   });
                               }).catch(err => {
                                   return that.$util.Tips({
                                       title: (err&&err.message)?err.message:err
                                   });
                               });
                           },
                           fail: function(res) {
                               if (res.errMsg == 'chooseAddress:cancel') return that.$util
                                   .Tips({
                                       title: '取消选择'
                                   });
                           },
                       })
                   },
                   fail: function(res) {
                       uni.showModal({
                           title: '您已拒绝导入微信地址权限',
                           content: '是否进入权限管理,调整授权?',
                           success(res) {
                               if (res.confirm) {
                                   uni.openSetting({
                                       success: function(res) {}
                                   });
                               } else if (res.cancel) {
                                   return that.$util.Tips({
                                       title: '已取消!'
                                   });
                               }
                           }
                       })
                   },
               })
           },
           // 导入共享地址(微信);
           getAddress() {
               let that = this;
               
               that.$store.commit('UPDATE_isSystemHide', {
                   isSystemHide: (new Date()).getTime()
               });
               that.$wechat.openAddress().then(userInfo => {
                   // open();
                   editAddress({
                           id: this.id,
                           realName: userInfo.userName,
                           phone: userInfo.telNumber,
                           address: {
                               province: userInfo.provinceName,
                               city: userInfo.cityName,
                               district: userInfo.countryName,
                               cityId: 0
                           },
                           detail: userInfo.detailInfo,
                           isDefault: 1,
                           postCode: userInfo.postalCode
                       })
                       .then(() => {
                           setTimeout(function() {
                               if (that.cartId) {
                                   let cartId = that.cartId;
                                   let pinkId = that.pinkId;
                                   let couponId = that.couponId;
                                   that.cartId = '';
                                   that.pinkId = '';
                                   that.couponId = '';
                                   uni.navigateTo({
                                       url: '/pages/users/order_confirm/index?cartId=' +
                                           cartId + '&addressId=' + (that.id ? that.id :
                                               res.data
                                               .id) + '&pinkId=' + pinkId + '&couponId=' +
                                           couponId + '&secKill=' + that.secKill +
                                           '&combination=' + that.combination + '&bargain=' +
                                           that.bargain
                                   });
                               } else {
                                   uni.navigateTo({
                                       url: '/pages/users/user_address_list/index'
                                   })
                                   // history.back();
                               }
                           }, 1000);
                           // close();
                           that.$util.Tips({
                               title: "添加成功",
                               icon: 'success'
                           });
                       })
                       .catch(err => {
                           // close();
                           return that.$util.Tips({
                               title: ((err&&err.message)?err.message:err) || "添加失败"
                           });
                       });
               }).catch(err => {
                   console.log(err);
               });
           },
           /**
            * 提交用户添加地址
            * 
            */
           formSubmit: function(e) {
               let that = this,
                   value = e.detail.value;
               if (!value.realName) return that.$util.Tips({
                   title: '请填写收货人姓名'
               });
               if (!value.phone) return that.$util.Tips({
                   title: '请填写联系电话'
               });
               if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(value.phone)) return that.$util.Tips({
                   title: '请输入正确的手机号码'
               });
               if (that.region.length == 0) return that.$util.Tips({
                   title: '请选择所在地区'
               });
               if (!value.detail) return that.$util.Tips({
                   title: '请填写详细地址'
               });
               value.id = that.id;
               let regionArray = that.region;
               value.address = {
                   province: regionArray[0],
                   city: regionArray[1],
                   district: regionArray[2],
                   cityId: that.cityId,
               };
               value.isDefault = that.userAddress.isDefault;
 
               uni.showLoading({
                   title: '保存中',
                   mask: true
               })
               editAddress(value).then(res => {
                   if (that.id)
                       that.$util.Tips({
                           title: '修改成功',
                           icon: 'success'
                       });
                   else
                       that.$util.Tips({
                           title: '添加成功',
                           icon: 'success'
                       });
                   setTimeout(function() {
                       if (that.preOrderNo > 0) {
                           uni.redirectTo({
                               url: '/pages/users/order_confirm/index?preOrderNo=' + that
                                   .preOrderNo + '&addressId=' + (that.id ? that.id :
                                       res.data.id)
                           })
                       } else {
                           // #ifdef H5
                           return history.back();
                           // #endif
                           // #ifndef H5
                           return uni.navigateBack({
                               delta: 1,
                           })
                           // #endif
                       }
                   }, 1000);
               }).catch(err => {
                   return that.$util.Tips({
                       title: (err&&err.message)?err.message:err
                   });
               })
           },
           ChangeIsDefault: function(e) {
               this.$set(this.userAddress, 'isDefault', !this.userAddress.isDefault);
           }
       }
   }
</script>
 
<style scoped lang="scss">
   .new-address {
       display: flex;
       flex-direction: column;
       height: 100%;
   }
 
   .addAddress {
       padding-top: 20rpx;
   }
 
   .addAddress .list {
       background-color: #fff;
       padding: 0 24rpx;
   }
 
   .addAddress .list .item {
       border-top: 1rpx solid #eee;
       height: 100rpx;
       line-height: 100rpx;
   }
 
   .addAddress .list .item .name {
       width: 135rpx;
       font-size: 30rpx;
       color: #333;
       font-weight: bold;
       text-align-last: justify;
   }
 
   .addAddress .list .item .address {
       flex: 1;
       margin-left: 50rpx;
   }
 
   .addAddress .list .item input {
       flex: 1;
       // width: 425rpx;
       font-size: 28rpx;
       font-weight: 400;
       padding-right: 50rpx;
       margin-left: 48rpx;
   }
 
   .addAddress .list .item .placeholder {
       color: #ccc;
   }
 
   .addAddress .list .item picker .picker {
       width: 410rpx;
       font-size: 28rpx;
 
       &.placeholder {
           color: #CCCCCC;
       }
   }
 
   .addAddress .default {
       padding: 0 30rpx;
       height: 90rpx;
       background-color: #fff;
       margin-top: 23rpx;
   }
 
   .addAddress .default checkbox {
       margin-right: 15rpx;
   }
 
   .addAddress .keepBnt {
       width: 330rpx;
       height: 76rpx;
       border-radius: 16rpx;
       text-align: center;
       line-height: 76rpx;
       margin: 80rpx auto 24rpx auto;
       font-size: 32rpx;
       color: #fff;
       background-color: #016DCC !important;
   }
 
   .addAddress .wechatAddress {
       width: 330rpx;
       height: 76rpx;
       border-radius: 16rpx;
       text-align: center;
       line-height: 76rpx;
       margin: 0 auto;
       font-size: 32rpx;
       color: #fff;
       background-color: #04C763 !important;
   }
 
   .relative {
       position: relative;
   }
 
   .icon-dizhi {
       font-size: 44rpx;
       z-index: 100;
       color: #016DCC !important;
   }
 
   .abs_right {
       position: absolute;
       right: 0;
   }
 
   .status {
       display: flex;
       width: 750rpx;
       // background-color: #E93323;
       height: var(--status-bar-height);
   }
</style>