question.js
3.48 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
'use strict';
const _ = require('lodash');
const semver = require('semver');
const questionModel = require('../models/question');
const headerModel = require('../../../doraemon/models/header'); // 头部model
const geetest = require('../../passport/controllers/geetest');
const cleanHtml = require('../../../utils/cleanHtml');
exports.list = (req, res, next) => {
let canShare = false;
if (req.yoho.isApp && req.query.app_version) {
let appVersion = req.query.app_version.split('.');
appVersion = `${appVersion[0]}.${appVersion[1]}.${appVersion[2]}`;
if (!semver.lt(appVersion, '5.9.0')) {
canShare = true;
}
}
req.ctx(questionModel).getQuestionList().then(result => {
res.render('question/list', {
title: '调研中心',
module: '3party',
page: 'question-list',
pageHeader: headerModel.setNav({
navTitle: '调研中心'
}),
list: result,
isApp: req.yoho.isApp,
canShare: canShare,
localCss: true
});
}).catch(next);
};
exports.check = (req, res, next) => {
let params = req.body;
params.uid = req.user.uid || params.uid;
if (!params.uid) {
return res.send({code: 400, message: '请先登录!'});
}
req.ctx(questionModel).getQuestionStatus(params).then(result => {
res.send(result);
}).catch(next);
};
exports.geetestLoad = (req, res, next) => {
if (req.user.uid) {
return next();
}
req.yoho.captchaShow = false;
res.locals.useGeetest = true;
if (_.has(res, 'locals.loadJsBefore')) {
res.locals.loadJsBefore.push({
src: global.yoho.config.geetestJs
});
} else {
res.locals.loadJsBefore = [
{
src: global.yoho.config.geetestJs
}
];
}
return next();
};
exports.geetestCheck = (req, res, next) => {
let testCode = req.body.yohobuy;
if (testCode === global.yoho.config.testCode || req.user.uid) {
return next();
}
return geetest.validate(req, res, next);
};
exports.submit = (req, res, next) => {
let params = req.body;
params.uid = req.user.uid || params.uid;
// 标识问卷来源
if (req.yoho.isApp) {
params.sourceType = 'APP';
} else if (req.yoho.isMobile) {
params.sourceType = 'H5';
} else {
params.sourceType = 'PC';
}
let frontAnswers = JSON.parse(params.frontAnswers || '[]') || [];
_.each(frontAnswers, item => {
if (item.addon) {
item.addon = cleanHtml.htmlEncode(item.addon);
}
});
params.frontAnswers = JSON.stringify(frontAnswers);
req.ctx(questionModel).submitQuestion(params).then(result => {
res.send(result);
}).catch(next);
};
exports.detail = (req, res, next) => {
let id = parseInt(`0${req.params.id}`, 10);
req.ctx(questionModel).getQuestionDetail(id, req.user.uid).then(result => {
if (result && result.detail && req.yoho.isApp) {
result.detail.uid = req.user.uid;
}
res.render('question/detail', Object.assign(result, {
title: 'YOHO!调研中心',
module: '3party',
page: 'question-detail',
pageHeader: headerModel.setNav({
navTitle: 'YOHO!调研中心'
}),
isApp: req.yoho.isApp,
localCss: true,
noLogin: !req.user.uid
}));
}).catch(next);
};