complaints.page.js
3.91 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
/**
* 我的投诉
* @author: yyqing<yanqing.yang@yoho.cn>
* @date: 2016/3/18
*/
var $ = require('yoho-jquery');
var $cancelBtn = $('.cancel-btn'),
$title = $('#title'),
$customer = $('#customer'),
$complaintsType = $('#complaintsType'),
$orderCode = $('#orderCode'),
$content = $('#content');
var ajaxing = false;
var rule = {
title: {
dom: $title,
min: 1,
max: 10,
onshow: '请输入您的投诉主题',
onfocus: '请输入主题',
onmin: '投诉主题不能为空',
onmax: '输入投诉主题不能超过10个字'
},
customer: {
dom: $customer,
min: 1,
max: 10,
onshow: '请填写您要投诉的对象,比如:客服00*号、发货员...',
onfocus: '请填写您要投诉的对象',
onmin: '投诉对象不能为空',
onmax: '输入投诉对象不能超过10个字'
},
orderCode: {
dom: $orderCode,
regexp: /^([0-9,]{9,})$/, // 一个和多个订单
onshow: '如果多个是多个订单号,请用英文逗号隔开,没有则不填',
onfocus: '请输入订单号',
onerror: '您输入的订单格式不对,如果多个是多个订单号,请用英文逗号隔开'
},
content: {
dom: $content,
min: 1,
max: 130,
onshow: '输入您投诉的内容',
onfocus: '请输入您投诉的内容',
onmin: '投诉内容不能为空',
onmax: '输入的投诉内容不能超过130个字'
}
};
function preventResubmit() {
if (!ajaxing) {
ajaxing = true;
setTimeout(function() {
ajaxing = false;
}, 1000);
return false;
}
return true;
}
function validate(info) {
var $tips = info.dom.siblings('.option-tip'),
val = $.trim(info.dom.val()),
len = val.length;
if (info.min && len < info.min) {
$tips.text(info.onmin);
return false;
} else if (info.max && len > info.max) {
$tips.text(info.onmax);
return false;
}
if (info.regexp && !info.regexp.test(val)) {
$tips.text(info.onerror);
return false;
}
$tips.text('输入正确');
return true;
}
function validateForm() {
var res = {},
focus;
$.each(rule, function(key, info) {
if (validate(info)) {
res[key] = $.trim(info.dom.val());
} else {
if (!focus) {
focus = key;
info.dom.focus();
}
}
});
res.complaintsType = $complaintsType.val();
if (focus) {
return false;
}
return res;
}
function addComplaint() {
var sendParm = validateForm();
if (preventResubmit() || !sendParm) {
return;
}
$.ajax({
type: 'POST',
url: '/home/complaints/submit',
data: sendParm
}).then(function(jsonData) {
ajaxing = false;
if (jsonData.code === 200) {
window.location.reload();
} else {
alert(jsonData.message);
}
});
}
function cancelComplaint(id, dom) {
var $dom = dom;
if (preventResubmit()) {
return;
}
$.ajax({
type: 'POST',
url: '/home/complaints/cancel',
data: {
id: id
}
}).then(function(jsonData) {
ajaxing = false;
if (jsonData.code === 200) {
$dom.prev().remove();
$dom.text('问题已撤销');
} else {
alert(jsonData.message);
}
});
}
$.each(rule, function(key, info) {
info.dom.focus(function() {
info.dom.siblings('.option-tip').text(info.onfocus);
}).blur(function() {
validate(info);
});
});
$('#complaint-submit').click(function() {
addComplaint();
});
$cancelBtn.click(function() {
var data = $(this).data();
if (data && data.id) {
cancelComplaint(data.id, $(this).parent());
}
});