repayment.js
3.1 KB
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
var Repayment = function(options) {
var self = this;
this.settings = $.extend({}, {
onSelectAll: $.noop,
onDeselectAll: $.noop,
/**
* 获取选择
*
* @returns {Array}
*/
onGetSelection: function() {
return [];
}
}, options);
this.currAmt = $('.repayment-bottom').find('.curr-amt');
this.currFee = $('.repayment-bottom').find('.curr-fee');
this.total = 0;
this.fee = 0;
$('.repayment-btn').on('click', function() {
var path = location.pathname;
var params = {
action: 'go.instalmentRepayment',
list: self.setParams(),
amount: self.total
};
$(this).attr('href', path + '?openby:yohobuy=' + encodeURIComponent(JSON.stringify(params)));
});
$('#repayment-total').click(function() {
var isChecked = $(this).data('checked');
if (!isChecked) {
self.settings.onSelectAll.bind(self)();
} else {
self.settings.onDeselectAll.bind(self)();
}
self.update();
$(this).data('checked', !$(this).data('checked'));
});
};
/**
* 格式化金额
*
* @param amount
* @returns {string}
* @private
*/
Repayment.prototype._formatCurrency = function(amount) {
var m = ('' + amount).match(/(\d+)\.?(\d+)?/);
var fixedPart = m[1], floatPart = m[2];
var ret = '0.00';
if (m && m.length === 3) {
ret = fixedPart + '.' + (floatPart || '00');
}
return ret;
};
/**
* 设置总金额
*
* @param total
*/
Repayment.prototype.setTotal = function(total) {
this.total = total;
this.currAmt.text(this._formatCurrency(this.total));
};
/**
* 设置费用
*
* @param fee
*/
Repayment.prototype.setFee = function(fee) {
this.fee = fee;
this.currFee.text(this._formatCurrency(this.fee));
};
// 获取立即付款的传参
Repayment.prototype.setParams = function() {
var values = this.getSelection();
var formatValues = [];
if (values && values.length) {
values.forEach(function(value) {
formatValues.push({
index: value.index,
orderCode: value.orderCode,
termNo: value.termNo
});
});
}
return formatValues;
};
Repayment.prototype.getSelection = function() {
return this.settings.onGetSelection();
};
Repayment.prototype.update = function() {
var values = this.getSelection();
var $repaymentBottom = $('.repayment-bottom');
var self = this;
this.total = 0;
this.fee = 0;
if (values && values.length) {
values.forEach(function(value) {
// 计算还款总额和费用
self.total += value.amount + value.fee;
self.fee += value.fee;
});
}
if (values.length === 0) {
$repaymentBottom.slideUp();
} else {
$repaymentBottom.slideDown();
}
this.setTotal(this.total);
this.setFee(this.fee);
if (this.fee > 0) {
$('.serve-price').show();
} else {
$('.serve-price').hide();
}
};
module.exports = Repayment;