question-detail.page.js
8.19 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
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
require('3party/question-detail.page.css');
let $ = require('yoho-jquery'),
yoho = require('yoho-app'),
tipDg = require('plugin/tip'),
share = require('common/share'),
Validate = require('plugin/validata');
let validate = new Validate('#js-img-check', {
useREM: {
rootFontSize: 40,
picWidth: 150
}
});
let question = {
$base: $('#qs-wrap'),
init: function() {
let that = this;
this.$errTip = $('.error-tip');
this.$mobile = $('.user-mobile > input');
this.$item = $('.qs-item, .sub-qs-item', this.$base);
this.startTime = Date.parse(new Date()) / 1000;
if (this.$base.length) {
let data = this.$base.data();
this.shareInfo = {
title: data.title,
link: 'http://m.yohobuy.com/3party/questionnaire/' + data.id,
desc: data.desc,
imgUrl: data.img
};
// 设置分享信息
share(this.shareInfo);
// 设置app页面信息及分享信息
if (yoho.isApp) {
yoho.ready(function() {
yoho.invokeMethod('get.pageType', {
pageType: 'questionnaire'
});
yoho.invokeMethod('set.shareInfo', that.shareInfo);
});
}
}
this.bindEvent();
},
bindEvent: function() {
let that = this;
this.$base.on('click', '.radio-option', function() {
let $this = $(this),
$par = $this.parent();
let jid = $this.data('jid');
if (!$this.hasClass('on')) {
$this.siblings('.on').removeClass('on');
$this.addClass('on');
if ($par.hasClass('qs-item')) {
that.setSubQuestion($par.next('.sub-qs-wrap'), jid);
}
}
}).on('click', '.check-option', function() {
let $this = $(this);
if ($this.hasClass('on')) {
$this.removeClass('on');
} else {
$this.addClass('on');
}
}).on('click', 'input', function(e) {
if (e && e.stopPropagation) {
e.stopPropagation();
} else {
window.event.cancelBubble = true;
}
});
$('.submit-btn').click(function() {
that.saveAnswers(that.packAnswersInfo());
});
},
setSubQuestion: function($wrap, ids) {
if (!$wrap.length) {
return;
}
$wrap.slideUp();
$wrap.children().addClass('hide');
if (typeof ids === 'string' || typeof ids === 'number') {
ids = [ids];
}
if (!Array.isArray(ids)) {
return;
}
let i;
for (i = 0; i < ids.length; i++) {
if (ids[i]) {
$wrap.find('.sub-' + ids[i]).removeClass('hide');
}
}
$wrap.slideDown();
},
_validationPartten: function(val) {
let validationPartten = /['"<>&\|]|--/g,
matchChars,
errText;
if (validationPartten.test(val)) {
matchChars = val.match(validationPartten).join(' ');
errText = '不可以输入 ' + matchChars + ' 哦!';
}
return errText;
},
packAnswersInfo: function() {
let that = this;
let answer = [];
let $errDom;
this.$item.each(function() {
let $this = $(this);
if ($errDom || $this.hasClass('hide')) {
return;
}
let data = $this.data();
let ans = [];
let errText = '';
if (+data.type === 3) {
$this.find('.text-input').each(function() {
let val = $.trim($(this).val());
if (val) {
ans.push({
questionIndex: data.index,
answerIndex: ans.length,
addon: val
});
errText = that._validationPartten(val);
}
if (val.length > 400) {
errText = '输入内容过长';
}
});
} else {
$this.find('.on').each(function() {
let $that = $(this),
$input = $that.find('input'),
a = {
questionIndex: data.index,
answerIndex: $that.data('index')
};
if ($input && $input.length) {
a.addon = $input.val();
errText = that._validationPartten(a.addon);
}
ans.push(a);
});
if (data.type === '1') {
ans.length = 1;
}
}
if (errText || !ans.length) {
$errDom = $this;
if (!errText) {
errText = +data.type === 3 ? '请填写一条回答' : '请选择一个选项';
}
that.showError(errText, $errDom);
} else {
answer = $.merge(answer, ans);
}
});
if ($errDom) {
return [];
} else {
return answer;
}
},
showError: function(tip, $errDom) {
let that = this;
this.$errTip.html(tip);
if (this.timer) {
clearTimeout(this.timer);
}
this.timer = setTimeout(function() {
that.$errTip.empty();
}, 5000);
if ($errDom) {
let offTop = $errDom.offset().top,
errHeight = this.$errTip.outerHeight();
$('html,body').animate({scrollTop: offTop - errHeight}, 500);
}
},
saveAnswers: function(info) {
if (!info || !info.length) {
return;
}
return validate.getResults().then(result => {
if (typeof result === 'undefined') {
return;
}
let params = {
id: this.$base.data('id'),
uid: this.$base.data('cid'),
startTime: this.startTime,
endTime: Date.parse(new Date()) / 1000,
frontAnswers: JSON.stringify(info),
mobile: this.$mobile.val()
};
$.extend(params, result);
$.ajax({
type: 'POST',
url: '/3party/questionnaire/submit',
data: params
}).then(function(data) {
validate.type === 2 && validate.refresh();
if (data.code === 200 || data.code === 206) {
let tip = '调查问卷已成功提交,感谢您的帮助!';
if (data.code === 206 && data.message) {
tip = data.message;
}
tipDg.show(tip);
setTimeout(function() {
if (yoho.isApp) {
yoho.invokeMethod('go.back');
} else {
window.history.go(-1);
}
}, 2000);
} else {
tipDg.show(data.message || '网络出了点问题~');
}
});
});
}
};
let tipDialog = {
$base: $('#tip-dialog'),
init: function() {
var that = this;
this.$base.on('click', '.close-btn', function() {
that.hide();
});
this.$base.on('click', '.back-btn', function() {
window.history.go(-1);
});
},
show: function() {
this.$base.removeClass('hide');
},
hide: function() {
this.$base.addClass('hide');
}
};
tipDialog.init();
question.init();
if (question.$base.length) {
$('.nav-back').removeAttr('href').click(function() {
tipDialog.show();
});
} else if (yoho.isApp) {
$('.qs-err a').removeAttr('href').click(function() {
yoho.invokeMethod('go.back');
});
}