question.js 2.45 KB
'use strict';

const semver = require('semver');
const questionModel = require('../models/question');
const headerModel = require('../../../doraemon/models/header'); // 头部model

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.gt(appVersion, '5.8.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.submit = (req, res, next) => {
    let params = req.body;

    params.uid = req.user.uid || params.uid;

    if (!params.uid) {
        return res.send({code: 400, message: '请先登录!'});
    }

    // 标识问卷来源
    if (req.yoho.isApp) {
        params.sourceType = 'APP';
    } else if (req.yoho.isMobile) {
        params.sourceType = 'H5';
    } else {
        params.sourceType = 'PC';
    }

    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
        }));
    }).catch(next);
};