question.js 3.48 KB
'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);
};