cancel-order.js
4.43 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
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
var dialog = require('../../plugins/dialog');
var _dialog = dialog.Dialog;
var _alert = dialog.Alert;
var validate = require('../../me/order/validation');
require('../../common/foreach-polyfill');
// 添加.check方法
require('../../plugins/check');
// 将一维数组转化成子数组长度为l的二维数组
function getDimensions(arr, l) {
var a = arr.slice(0, arr.length);
var newArr = [];
var subArr = [];
do {
subArr = a.splice(0, l);
newArr.push(subArr);
} while (subArr.length >= l);
return newArr;
}
function getTpl(data) {
var cancelOrderTpl = require('../../../tpl/me/cancel-order.hbs');
return cancelOrderTpl(data);
}
// 取消订单
function cancelOrder(code, onCancel, w) {
var $checked = $('.reason .row .checked');
var reason = $checked.next('.reason-text').text();
var reasonId = $checked.parent().data('value');
var pass = true;
if (reason === '其它' || reason === '其他') {
reason = $checked.siblings('.input').val();
pass = validate.start('#other-reason', {
maxLength: 50,
errMsg: '原因最多50个字符'
});
}
if (!pass) {
return false;
}
w.close();
$.ajax({
url: '/me/cancelOrder',
data: {
orderCode: code,
reasonId: reasonId,
reason: reason
}
}).done(function(result) {
var tip;
if (result && result.code === 200) {
tip = new _dialog({
className: 'order-dialog alert',
content: '<h1>订单修改</h1>' +
'<span>您已成功取消了该订单</span>',
closeCb: onCancel,
btns: [
{
id: 'cancel-complete',
btnClass: ['confirm'],
name: '确定',
cb: function() {
tip.close(onCancel);
}
}
]
}).show();
} else {
new _alert('<h1>' + result.message + '</h1>').show();
}
}).fail(function(err) {
console.log(err);
new _alert('<h1>出错了!修改失败!</h1>').show();
});
}
function showDiaglog(tpl, code, onCancel) {
var tip = new _dialog({
className: 'order-dialog cancel-dialog',
content: tpl,
keep: true,
btns: [
{
id: 'cancel-confirm',
btnClass: ['confirm'],
name: '确定并取消订单',
cb: function() {
cancelOrder(code, onCancel, tip);
}
},
{
id: 'cancel-stop',
btnClass: ['cancel-btn'],
name: '暂不取消',
cb: function() {
tip.close();
}
}
]
}).show();
$('.cancel-dialog .reason p').check({
type: 'radio',
group: 'reason',
onChange: function(ele, checked) {
var $ele = $(ele);
var $input = $('.cancel-dialog .reason input');
if (checked && $ele.hasClass('other')) {
$input.prop('disabled', false);
$input.focus();
} else {
$input.val('');
$input.prop('disabled', true);
}
}
});
}
// 获取取消订单的理由
function getCancelReason(code, onCancel) {
$.ajax({
url: '/me/getCancelOrderReason'
}).done(function(result) {
var tpl;
var data;
var reasons = [];
if (result.code) {
data = getDimensions(result.data, 2);
data.forEach(function(d, idx) {
if (idx === 0) {
d[0].checked = true;
}
d.forEach(function(r) {
if (r.reason === '其他' || r.reason === '其它') {
r.isOther = true;
}
})
reasons.push({
subReasons: d
});
});
tpl = getTpl({
reasons: reasons
});
showDiaglog(tpl, code, onCancel);
}
}).fail(function(err) {
console.log(err);
});
}
function start(code, onCancel) {
getCancelReason(code, onCancel);
}
module.exports = {
start: start
};