Sanakey
2020-09-03 0a07e233066560f587d746419d6e4801107f0b12
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
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
// pages/order/order.js
var listData = require('../../data/test-data.js');
var utils = require("../../utils/util.js");
var app = getApp();
var that;
let isUsingCoupon = false;
// var pointData = {
//   sum: 1000,
//   canUse: 100,
//   isUse: false
// };
// var userData = {
//   balance: 1000
// }
Page({
  data: {
    oldTotalMoney: 0,
    showAddr: false,
    showAddAddr: true,
    totalMoney: 0,   // 合计金额
    sumMoney:0,
    date: "",
    showHidelayer:false,
    voucherCost:-1,
    deliveryMethodIndex:0,
    // 发票信息
    invoice: [
      {
        id: 0,
        name: '不要发票'
      },
      {
        id: 1,
        name: '纸质发票--个人'
      },
      {
        id: 2,
        name: '纸质发票--企业'
      },
      {
        id: 3,
        name: '电子发票--个人'
      },
      {
        id: 4,
        name: '电子发票--企业'
      }
    ],
    // 积分信息
    // point: {
    //   sum: pointData.sum,
    //   canUse: pointData.canUse,
    //   offMoney: parseInt(pointData.canUse / 100),
    //   isUse: pointData.isUse
    // },
    // 个人余额
    Balance:0,
    clientHeight: "100%",
    overflow: "auto",
    isSelected: true, //优惠券默认选中不使用
    isSelected2: true, //积分默认选中不使用
    isChecked: false,  //默认不选择余额支付
    id: -1,  // 优惠券栏默认不使用
    pointId: -1,  // 积分栏默认显示不使用
    offLast: 0,  //上个折扣值
    newMsg: ''
  },
  onShow() {
    console.log(getCurrentPages())
    wx.showLoading({
      title: '加载中',
    });
 
 
    var that = this;
    // // 显示对方发来的最新消息
    // app.globalData.newMsgCallback = onMessage => {
    //   console.log('订单页监听WebSocket接受到服务器的消息事件。服务器返回的消息', JSON.parse(onMessage.data))
    //   utils.msgTimer(that);
    // }
    // // 页面显示时更新未读消息的值
    // utils.refreshUnreadMsgNum(this);
 
    // wx.request({
    //   url: utils.getUrl('/shopping/confirm.do?m=getConfirmInfo'),
    //   header: {
    //     "Cookie": "JSESSIONID=" + wx.getStorageSync('sessionID')
    //   },
    //   success: res => {
 
    //     // if (res.data.cltcode == "") {
    //     //   wx.navigateTo({
    //     //     url: '../login/login',
    //     //   })
    //     //   return false;
    //     // }
    //     wx.hideLoading();
    //     utils.requestError(res);
 
    //     this.setData({
    //       Address: res.data.Address
    //     })
    //   },
    //   fail: function (errmsg) {
    //     utils.requestFail(errmsg,'/shopping/confirm.do');
    //   }
    // })
 
    wx.request({
      url: utils.getUrl('/shopping/confirm.do?m=getConfirmInfo'),
      header: {
        "Cookie": "JSESSIONID=" + wx.getStorageSync('sessionID')
      },
      success: res => {
        wx.hideLoading();
        if(!utils.requestError(res)){
          return false;
        }
 
        this.setData({
          Address: res.data.Address,
        })
 
      },
      fail: function (errmsg) {
        utils.requestFail(errmsg, '/shopping/confirm.do');
      }
    })
  },
 
 
 
  /**
     * 生命周期函数--监听页面初次渲染完成
     */
  onReady: function () {
 
  },
 
  // 获取地址
  getAddress() {
    wx.navigateTo({
      url: '../addressList/addressList?way=select'
    })
  },
 
  getOrderData(){
    wx.request({
      url: utils.getUrl('/shopping/confirm.do?m=getConfirmInfo'),
      header: {
        "Cookie": "JSESSIONID=" + wx.getStorageSync('sessionID')
      },
      success: res => {
        wx.hideLoading();
        if(!utils.requestError(res)){
          return false;
        }
 
        this.setData({
          Address: res.data.Address,
          totalMoney: res.data.PayAmount,
          CartList: res.data.CartList,
          CouponList: res.data.CouponList,
          PointsTimesList: res.data.PointsTimesList,
          Points: res.data.UserPoints,
          Balance: res.data.Balance,
          CurrencySign: res.data.CurrencySign,
          PreSendDate: res.data.PreSendDate,
          balance2: res.data.Balance,
          isAllowPayable:res.data.isAllowPayable,
          VoucherAmount:res.data.VoucherList.VoucherAmount || 0
        })
 
        if (res.data.Balance>0){
          this.setData({
            isChecked:true
          })
        }
        if(res.data.CouponList.length>0){
          this.setData({
            id:0,
          })
        }
        isUsingCoupon=true;
 
      },
      fail: function (errmsg) {
        utils.requestFail(errmsg, '/shopping/confirm.do');
      }
    })
  },
  
  onLoad() {
    
    let deliveryMethod = app.globalData.deliveryMethod&&app.globalData.deliveryMethod.split(';');
    this.setData({
      RewardStatus:app.globalData.RewardStatus,
      isAllowSelectDeliveryMethod:app.globalData.isAllowSelectDeliveryMethod,
      deliveryMethod,
      CouponStatus:app.globalData.CouponStatus,
      VoucherStatus:app.globalData.VoucherStatus,
    })
    console.log('进入order页',deliveryMethod,this.data.isAllowSelectDeliveryMethod)
    this.getOrderData();
 
    
  },
  // 日期选择器
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value,
      PreSendDate: e.detail.value
    })
  },
  // 发票选择
  // bindPickerChange: function (e) {
  //   this.setData({
  //     index: e.detail.value
  //   })
  // },
 
 
  // 积分按钮
  pointUseTap: function () {
 
    if (this.data.isShow) {
      this.hideLayer2();
    }
    else {
      this.showLayer2();
    }
  },
 
  // 余额按钮
  balanceTap: function () {
    var check = this.data.isChecked;
    check = !check;
    this.setData({
      isChecked: check
    })
 
    if(this.data.isChecked){
      this.setData({
        Balance: this.data.balance2
      })
    } else{
      this.setData({
        Balance: 0
      })
    }
 
    wx.showLoading();
    wx.request({
      url: utils.getUrl('/shopping/confirm.do?m=getConfirmInfo'),
      header: {
        "Cookie": "JSESSIONID=" + wx.getStorageSync('sessionID')
      },
      data: {
        couponCode: this.data.id == -1 ? "" : this.data.CouponList[this.data.id].CouponCode,
        points: this.data.pointId == -1 ? "0" : this.data.PointsTimesList[this.data.pointId].StartPointsAmount,
        balance: this.data.Balance,
        isUsingCoupon
      },
      success: res => {
        wx.hideLoading();
        if(!utils.requestError(res)){
          return false;
        }
 
        this.setData({
          totalMoney: res.data.PayAmount,
          CartList: res.data.CartList,
          CouponList: res.data.CouponList,
          PointsTimesList: res.data.PointsTimesList,
          Points: res.data.UserPoints,
          Balance: res.data.Balance,
          PreSendDate: res.data.PreSendDate
        })
 
      },
      fail: function (errmsg) {
        utils.requestFail(errmsg,'/shopping/confirm.do');
      }
    })
  },
 
 
 
  // 优惠券栏点击
  ticketTap: function () {
    if (this.data.isShow) {
      this.hideLayer();
    }
    else {
      this.showLayer();
    }
  },
 
// 选择优惠券
  ticketItemTap: function (e) {
    
    // 页面显示优惠券选择状态
    var id = parseInt(e.detail.value);
    this.setData({
      id: id
    })
 
    if (id == -1) {
      this.setData({
        isSelected: true
      })
      isUsingCoupon=false;
    } else {
      this.setData({
        isSelected: false
      })
      isUsingCoupon=true;
    }
 
 
    this.hideLayer();
 
    wx.showLoading();
    wx.request({
      url: utils.getUrl('/shopping/confirm.do?m=getConfirmInfo'),
      header: {
        "Cookie": "JSESSIONID=" + wx.getStorageSync('sessionID')
      },
      data: {
        couponCode: this.data.id == -1 ? "" : this.data.CouponList[this.data.id].CouponCode,
        points: this.data.pointId == -1 ? "0" : this.data.PointsTimesList[this.data.pointId].StartPointsAmount,
        balance: this.data.Balance,
        isUsingCoupon
      },
      success: res => {
        wx.hideLoading();
        if(!utils.requestError(res)){
          return false;
        }
 
        this.setData({
          totalMoney: res.data.PayAmount,
          CartList: res.data.CartList,
          CouponList: res.data.CouponList,
          PointsTimesList: res.data.PointsTimesList,
          Points: res.data.UserPoints,
          Balance: res.data.Balance,
          PreSendDate: res.data.PreSendDate
        })
 
      },
      fail: function (errmsg) {
        utils.requestFail(errmsg,'/shopping/confirm.do');
      }
    })
  },
 
// 选择积分
  pointItemTap: function (e) {
    console.log(e)
    // 页面显示积分选择状态
    var pointId = parseInt(e.detail.value);
    this.setData({
      pointId: pointId
    })
    this.hideLayer2();
 
    if (pointId==-1){
      this.setData({
        isSelected2: true
      })
 
    } else {
      this.setData({
        isSelected2: false
      })
    }
 
    wx.showLoading();
    wx.request({
      url: utils.getUrl('/shopping/confirm.do?m=getConfirmInfo'),
      header: {
        "Cookie": "JSESSIONID=" + wx.getStorageSync('sessionID')
      },
      data: {
        couponCode: this.data.id == -1 ? "" : this.data.CouponList[this.data.id].CouponCode,
        points: this.data.pointId == -1 ? "0" : this.data.PointsTimesList[this.data.pointId].StartPointsAmount,
        balance: this.data.Balance,
        isUsingCoupon
      },
      success: res => {
        wx.hideLoading();
        if(!utils.requestError(res)){
          return false;
        }
 
        this.setData({
          totalMoney: res.data.PayAmount,
          CartList: res.data.CartList,
          CouponList: res.data.CouponList,
          PointsTimesList: res.data.PointsTimesList,
          Points: res.data.UserPoints,
          Balance: res.data.Balance,
          PreSendDate: res.data.PreSendDate
        })
 
      },
      fail: function (errmsg) {
        utils.requestFail(errmsg,'/shopping/confirm.do');
      }
    })
 
  },
  openHidelayer() {
    this.setData({
      showHidelayer: true
    });
    // setTimeout(()=>{
      
    // },200)
 
  },
  maskTap(event) {
    this.setData({
      showHidelayer: event.detail.isShow
    });
  },
 
  getPresentCode(e){
    let presentCode = e.detail.value.presentCode;
    wx.request({
      url: utils.getUrl('/shopping/voucher.do?m=get&voucherCode='+presentCode),
      header: {
        "Cookie": "JSESSIONID=" + wx.getStorageSync('sessionID')
      },
      success: res => {
        wx.hideLoading();
        if(!utils.requestError(res)){
          return false;
        }
 
        // wx.showModal({
        //   title:'提示',
        //   content: '礼品券使用成功! '
        // })
        if(res.data.state=='success'){
          this.getOrderData();
          wx.showToast({
            title: '礼品券使用成功!',
            icon: 'success',
            duration: 2000
          })
          this.setData({
            showHidelayer: false,
            // voucherCost:res.data.voucherCost
          });
        }
 
      },
      fail: function (errmsg) {
        utils.requestFail(errmsg, '/shopping/voucher.do?m=get&voucherCode=');
      }
    })
  },
 
  showLayer: function () {
    // 优惠券隐藏层滑入
    var that = this;
    utils.showLayer(that, "translateY", 300, 0, "animationData","isShow");
  },
 
  hideLayer: function () {
    // 优惠券隐藏层滑出
    var that = this;
    utils.hideLayer(that, "translateY", 300, "animationData", "isShow");
  },
 
  showLayer2: function () {
    // 积分隐藏层滑入
    var that = this;
    utils.showLayer(that, "translateY", 300, 0, "animationData2", "isShow2");
  },
 
  hideLayer2: function () {
    // 积分隐藏层滑出
    var that = this;
    utils.hideLayer(that, "translateY", 300, "animationData2", "isShow2");
  },
 
  // preventMove: function () {
  //   // 利用catchtouchmove阻止背景滚动 不用添加任何代码
  // },
  // getAuthorize(e){
  //   console.log(e)
  // },
  
  deliveryMethodIndexChange(e) {
    console.log('picker发送选择改变,携带值为', e.detail.value)
    this.setData({
      deliveryMethodIndex: e.detail.value
    })
  },
  postOrder(event){
    console.log(event)
    let deliveryMethod = event.detail.value.deliveryMethod||0;
    let delivery = this.data.deliveryMethod[deliveryMethod];
    console.log(delivery);
    if (delivery!=='到店自提'&&!this.data.Address.FullAddress || this.data.Address.FullAddress.trim() == '') {
      wx.showToast({
        title: '请选择地址',
        icon: 'none',
        duration: 1500
      })
      return false
    }
    
    // return
    // if (this.data.isChecked && this.data.Balance < this.data.totalMoney) {
    //   wx.showToast({
    //     title: '余额不足',
    //     icon: 'none',
    //     duration: 1500
    //   })
    //   return false
    // }
    wx.showLoading()
    wx.request({
      url: utils.getUrl('/shopping/confirm.do?m=save'),
      header: {
        "Cookie": "JSESSIONID=" + wx.getStorageSync('sessionID')
      },
      // method:'POST',
      data:{
        paymentaddress: this.data.Address.paymentaddress,
        couponCode: this.data.id == -1 ? "" : this.data.CouponList[this.data.id].CouponCode,
        presenddate: event.detail.value.date,
        paymentcomment: event.detail.value.remark,
        points: this.data.pointId == -1 ? "0" : this.data.PointsTimesList[this.data.pointId].StartPointsAmount,
        balance: this.data.isChecked ? this.data.Balance : 0,
        isUsingCoupon,
        deliverymethod:delivery||''
      },
      success: res => {
        wx.hideLoading();
        if(!utils.requestError(res)){
          return false;
        }
        // let weiXinPayAmount = 0
        if (res.data.weiXinPayAmount == 0){
 
          // docstatus = 0 and  isAllowPayable = 0
          wx.redirectTo({
            url: '../orderSuccess/orderSuccess?doccode='+ res.data.doccode+'&linkman='+this.data.Address.LinkName + '&isCustomer=1',
          })
          
          
        } else {
          
          if(res.data.DocStatus == 0 && res.data.isAllowPayable == 0){
            // docstatus = 0 and  isAllowPayable = 0
            wx.redirectTo({
              url: '../orderSuccess/orderSuccess?doccode='+ res.data.doccode+'&linkman='+this.data.Address.LinkName + '&isCustomer=1',
            })
          } else {
            wx.redirectTo({
              url: '../orderPay/orderPay?doccode=' + res.data.doccode,
            })
          }
        }
 
 
      },
      fail: function (errmsg) {
        utils.requestFail(errmsg,'/shopping/confirm.do');
      }
    })
  },
  onDialogClose(){
    this.setData({
      isShowDialog:false
    })
    // this.postOrder(this.data.dataEvent);
  },
  getUserInfo(e){
    // if (!utils.authorize(e, this)){
    //   e.detail.isAuthorize = false;
    //   contactComp.getAuthorize(e);
    //   return;
    // }
    utils.authorize(e, this);
    this.postOrder(this.data.dataEvent);
  },
 
  placeOrder: function (event) {
    console.log('提交订单事件',event)
    // return;
    let orderTemplateId = app.globalData.orderNoticeTemplateIdList;
    let ids = [];
    if(this.data.isAllowPayable==0){
      ids = [orderTemplateId.payableTemplateId,orderTemplateId.templateId,orderTemplateId.shippedTemplateId];
    } else {
      ids = [orderTemplateId.shippedTemplateId,orderTemplateId.templateId];
    }
    
    utils.subscribeMessage({
      ids:ids,
      callback:()=>{
        this.postOrder(event);
        // utils.isAuthorize(this,()=>{
        //   if (this.data.isAuthorize) {
        //     this.postOrder(event);
        //   } else {
        //     this.setData({
        //       isShowDialog: true,
        //       dataEvent:event
        //     })
        //   }
        // });
        
      }
    })
    
 
 
    // if (this.data.showAddAddr) {
    //   utils.showTip("请填写收货地址", "none");
    //   return false;
    // }
    // else if (event.detail.value.date == "" && !this.data.PreSendDate) {
    //   utils.showTip("请填写交货日期","none");
    //   return false;
    // }
 
  //   // 发起支付
  //   var orderDetail = this.data.detail;
  //   var userInfo = { name: this.data.name, tel: this.data.tel, addrdetail: this.data.addrdetail };
  //   var totalPrice = this.data.totalMoney;
  //   var remarks = event.detail.value.remark;
  //   wx.getStorage({
  //     key: 'openid',
  //     success: function (res) {
  //       var openId = res.data;
  //       if (!openId) {
  //         console.log('未获取到openId请刷新重试');
  //         return false;
  //       }
 
 
 
  //       //传参数金额,名称,描述,openid
  //       Bmob.Pay.wechatPay(totalPrice, '小程序商城', '描述', openId).then(function (resp) {
 
  //         //服务端返回成功
  //         var timeStamp = resp.timestamp,
  //           nonceStr = resp.noncestr,
  //           packages = resp.package,
  //           orderId = resp.out_trade_no,//订单号,如需保存请建表保存。
  //           sign = resp.sign;
  //         //发起支付
  //         wx.requestPayment({
  //           'timeStamp': timeStamp,
  //           'nonceStr': nonceStr,
  //           'package': packages,
  //           'signType': 'MD5',
  //           'paySign': sign,F
  //           'success': function (res) {
  //             //付款成功,这里可以写你的业务代码
  //             var User = Bmob.Object.extend("_User");
  //             var currentUser = Bmob.User.current();
  //             var objectid = currentUser.id;
  //             var Order = Bmob.Object.extend("Order");
  //             var Order = new Order();
  //             var me = new Bmob.User();
  //             me.id = objectid;
  //             Order.set("remarks", remarks);
  //             Order.set("orderUser", me);
  //             Order.set("totalprice", parseFloat(totalPrice));
  //             Order.set("orderDetail", orderDetail);
  //             Order.set("orderId", orderId);
  //             Order.set("status", 1);
  //             Order.set("userInfo", userInfo);
  //             Order.save(null, {
  //               success: function (result) {
  //                 wx.redirectTo({
  //                   url: '../order/index'
  //                 })
  //               },
  //               error: function (result, error) {
 
  //               }
  //             });
  //           },
  //           'fail': function (res) {
  //             console.log(res)
  //             var User = Bmob.Object.extend("_User");
  //             var currentUser = Bmob.User.current();
  //             var objectid = currentUser.id;
  //             var Order = Bmob.Object.extend("Order");
  //             var Order = new Order();
  //             var me = new Bmob.User();
  //             me.id = objectid;
  //             Order.set("remarks", remarks);
  //             Order.set("orderUser", me);
  //             Order.set("totalprice", parseInt(totalPrice));
  //             Order.set("orderDetail", orderDetail);
  //             Order.set("status", 0);
  //             Order.set("userInfo", userInfo);
  //             Order.set("orderId", orderId);
  //             Order.save(null, {
  //               success: function (result) {
  //                 console.log(result.id)
  //               },
  //               error: function (result, error) {
 
  //               }
  //             });
  //           }
  //         })
 
  //       }, function (err) {
  //         console.log('服务端返回失败');
  //         console.log(err);
  //       });
 
  //     }
  //   })
 
  // console.log(event);
 
 
 
  }
 
});