Authored by 王水玲

有货分期

@@ -5,162 +5,65 @@ @@ -5,162 +5,65 @@
5 */ 5 */
6 6
7 var $ = require('yoho-jquery'), 7 var $ = require('yoho-jquery'),
  8 + Repayment = require('./repayment'),
8 tip = require('../plugin/tip'); 9 tip = require('../plugin/tip');
9 10
10 -var $currAmt = $('.repayment-bottom').find('.curr-amt'),  
11 - $currFee = $('.repayment-bottom').find('.curr-fee'),  
12 - $servePrice = $('.serve-price'); 11 +var CHECKBOX_SELECTOR = '.repay-list input';
  12 +var repayment = new Repayment({
  13 + onGetSelection: function() {
  14 + var ret = [];
13 15
14 -require('./overdue-notice');  
15 -  
16 -// 选择分期统计  
17 -function totalChecked(index) {  
18 - var total = 0;  
19 -  
20 - // 按时间顺序选择还款,不可以跨期  
21 - $('.repay-list input').each(function(key, item) {  
22 - if (key < index && $(item).is(':checked')) {  
23 - total++;  
24 - }  
25 - });  
26 -  
27 - return total;  
28 -}  
29 -  
30 -// 取消分期统计  
31 -function totalCancelChecked(index) {  
32 - var total = 0;  
33 -  
34 - $('.repay-list input').each(function(key, item) {  
35 - if (key > index && $(item).is(':checked')) {  
36 - total++;  
37 - }  
38 - });  
39 -  
40 - return total;  
41 -}  
42 -  
43 -$('.repay-list label').on('click', function() {  
44 - var li = $(this).parents('li'),  
45 - index = li.index(),  
46 - $input = $(this).prev(),  
47 - currAmt = +$currAmt.html(),  
48 - currFee = +$currFee.html(),  
49 - amt = +li.attr('data-curramt'),  
50 - fee = +li.attr('data-currfee'),  
51 - count = 0,  
52 - total = 0; 16 + $(`${CHECKBOX_SELECTOR}:checked`).each(function(key, item) {
  17 + var li = $(item).parents('li');
53 18
54 - if ($input.is(':checked')) {  
55 - total = totalCancelChecked(index); 19 + var data = {
  20 + index: li.index(),
  21 + orderCode: li.attr('data-billno'),
  22 + termNo: li.attr('data-termNo'),
  23 + amount: parseFloat(li.attr('data-currNoFeeAmt')),
  24 + fee: parseFloat(li.attr('data-currfee')) + 0
  25 + };
56 26
57 - if (total === 0) {  
58 - $currAmt.html((currAmt - amt).toFixed(2));  
59 - $currFee.html((currFee - fee).toFixed(2));  
60 - } else {  
61 - tip.show('请按时间顺序取消选择,不可以跨期');  
62 - return false;  
63 - }  
64 - } else {  
65 - total = totalChecked(index);  
66 -  
67 - if (total === index) {  
68 - $currAmt.html((amt + currAmt).toFixed(2));  
69 - $currFee.html((fee + currFee).toFixed(2));  
70 - } else {  
71 - tip.show('请按时间顺序添加还款,不可以跨期');  
72 - return false;  
73 - }  
74 - } 27 + ret.push(data);
  28 + });
75 29
76 - if (parseFloat($currFee.html()) > 0) {  
77 - $servePrice.show();  
78 - } else {  
79 - $servePrice.hide(); 30 + return ret;
  31 + },
  32 + onDeselectAll: function() {
  33 + $(`${CHECKBOX_SELECTOR}:checked`).prop('checked', false);
  34 + return [];
  35 + },
  36 + onSelectAll: function() {
  37 + $(`${CHECKBOX_SELECTOR}:not(:checked)`).prop('checked', true);
  38 + return this.getSelection();
80 } 39 }
81 -  
82 - setTimeout(function() {  
83 - count = $('.repay-list input:checked').length;  
84 - $('#repayment-total').prop('checked', count === $('.repay-list li').length);  
85 - }, 0);  
86 }); 40 });
87 41
88 -// 全选||全不选  
89 -$('.repayment-bottom label').on('click', function() {  
90 - var totalAmt = 0;  
91 - var totalFee = 0;  
92 -  
93 - if ($(this).prev().is(':checked')) {  
94 - $('.repay-list input').prop('checked', false);  
95 - $currAmt.html('0.00');  
96 - $currFee.html('0.00');  
97 - $servePrice.hide();  
98 - } else {  
99 - $('.repay-list input').prop('checked', true);  
100 - $('.repay-list li').each(function() {  
101 - totalAmt += (+$(this).attr('data-curramt'));  
102 - totalFee += (+$(this).attr('data-currfee'));  
103 - }); 42 +require('./overdue-notice');
104 43
105 - $currAmt.html(totalAmt.toFixed(2));  
106 - $currFee.html(totalFee.toFixed(2)); 44 +$(CHECKBOX_SELECTOR + ':checkbox').click(function() {
  45 + var selection = repayment.getSelection();
  46 + var isSkipped = false;
  47 + var lastIndex = 0;
  48 + var self = this;
107 49
108 - if (parseFloat($currFee.html()) > 0) {  
109 - $servePrice.show(); 50 + selection.forEach(function(sel) {
  51 + if ((sel.index + 1) - lastIndex > 1) {
  52 + isSkipped = true;
110 } else { 53 } else {
111 - $servePrice.hide();  
112 - }  
113 - }  
114 -});  
115 -  
116 -$('.repayment-btn').on('click', function() {  
117 - var path = location.pathname;  
118 - var list = [];  
119 -  
120 - $('.repay-list li').each(function(key, item) {  
121 - if ($(item).find('input').is(':checked')) {  
122 - list.push({  
123 - index: key, // 分期列表序号  
124 - orderCode: $(item).attr('data-billno'), // 订单号  
125 - termNo: $(item).attr('data-termNo') // 第几期还款  
126 - }); 54 + lastIndex = sel.index + 1;
127 } 55 }
128 }); 56 });
129 57
130 - $(this).attr('href',  
131 - path + '?openby:yohobuy={"action":"go.instalmentRepayment","params":{"list":' +  
132 - encodeURIComponent(JSON.stringify(list)) + ',"amount":' + (+$currAmt.html()) + '}}');  
133 -});  
134 -  
135 -// const repayment = new Repayment({  
136 -// onGetSelection: function() {  
137 -// const ret = [];  
138 -  
139 -// $(`${CHECKBOX_SELECTOR}:checked`).each(function(key, item) {  
140 -// const li = $(item).parents('li');  
141 -  
142 -// const data = {  
143 -// index: key,  
144 -// orderCode: li.attr('data-billno'),  
145 -// termNo: li.attr('data-termNo'),  
146 -// amount: parseFloat(li.attr('data-currNoFeeAmt')),  
147 -// fee: parseFloat(li.attr('data-currfee')) + 0  
148 -// };  
149 -  
150 -// ret.push(data);  
151 -// }); 58 + if (isSkipped) {
  59 + setTimeout(function() {
  60 + tip.show($(self).is(':checked') ? '请按时间顺序取消选择,不可以跨期' : '请按时间顺序添加还款,不可以跨期');
  61 + }, 0);
  62 + return false;
  63 + }
152 64
153 -// return ret;  
154 -// },  
155 -// onDeselectAll: function() {  
156 -// $(`${CHECKBOX_SELECTOR}:checked`).prop('checked', false);  
157 -// return [];  
158 -// },  
159 -// onSelectAll: function() {  
160 -// $(`${CHECKBOX_SELECTOR}:not(:checked)`).prop('checked', true);  
161 -// return this.getSelection();  
162 -// }  
163 -// }); 65 + repayment.update();
  66 +});
164 67
165 // 跳转到还款详情 68 // 跳转到还款详情
166 window.jumpDetail = function(id) { 69 window.jumpDetail = function(id) {
@@ -28,7 +28,7 @@ var Repayment = function(options) { @@ -28,7 +28,7 @@ var Repayment = function(options) {
28 28
29 var params = { 29 var params = {
30 action: 'go.instalmentRepayment', 30 action: 'go.instalmentRepayment',
31 - list: self.getSelection(), 31 + list: self.setParams(),
32 amount: self.total 32 amount: self.total
33 }; 33 };
34 34
@@ -92,6 +92,23 @@ Repayment.prototype.setFee = function(fee) { @@ -92,6 +92,23 @@ Repayment.prototype.setFee = function(fee) {
92 this.currFee.text(this._formatCurrency(this.fee)); 92 this.currFee.text(this._formatCurrency(this.fee));
93 }; 93 };
94 94
  95 +// 获取立即付款的传参
  96 +Repayment.prototype.setParams = function() {
  97 + var values = this.getSelection();
  98 + var formatValues = [];
  99 +
  100 + if (values && values.length) {
  101 + values.forEach(function(value) {
  102 + formatValues.push({
  103 + index: value.index,
  104 + orderCode: value.orderCode,
  105 + termNo: value.termNo
  106 + });
  107 + });
  108 + }
  109 +
  110 + return formatValues;
  111 +};
95 112
96 Repayment.prototype.getSelection = function() { 113 Repayment.prototype.getSelection = function() {
97 return this.settings.onGetSelection(); 114 return this.settings.onGetSelection();
@@ -99,6 +116,7 @@ Repayment.prototype.getSelection = function() { @@ -99,6 +116,7 @@ Repayment.prototype.getSelection = function() {
99 116
100 Repayment.prototype.update = function() { 117 Repayment.prototype.update = function() {
101 var values = this.getSelection(); 118 var values = this.getSelection();
  119 + var $repaymentBottom = $('.repayment-bottom');
102 var self = this; 120 var self = this;
103 121
104 this.total = 0; 122 this.total = 0;
@@ -112,6 +130,12 @@ Repayment.prototype.update = function() { @@ -112,6 +130,12 @@ Repayment.prototype.update = function() {
112 }); 130 });
113 } 131 }
114 132
  133 + if (values.length === 0) {
  134 + $repaymentBottom.slideUp();
  135 + } else {
  136 + $repaymentBottom.slideDown();
  137 + }
  138 +
115 this.setTotal(this.total); 139 this.setTotal(this.total);
116 this.setFee(this.fee); 140 this.setFee(this.fee);
117 141