question.js 3.44 KB
/**
 * question model
 * @author: yyq<yanqing.yang@yoho.cn>
 * @date: 2017/05/23
 */

const _ = require('lodash');

const _fillQuestionContents = data => {
    if (data && +data.questionType === 3) {
        data.questionContents = _.fill(Array(data.fillBlankNum || 1), true);
    }

    return data;
};

const _handelSubQuestion = (qs, jid) => {
    if (qs) {
        qs = _.concat([], qs);

        _.forEach(qs, value => {
            Object.assign(value, {
                subQs: true,
                jid: jid
            });
            _fillQuestionContents(value);
        });

    }

    return qs;

};

module.exports = class extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);
    }
    getQuestionList() {
        return this.get({
            url: 'activity/question/questionList',
            data: {uid: _.get(this.ctx, 'req.user.uid', 0)},
            api: global.yoho.ServiceAPI
        }).then(result => {
            let list = _.get(result, 'data.rows', []);

            _.forEach(list, (value, key) => {
                value.index = key + 1;

                if (!_.get(value, 'share.imgUrl', '')) {
                    _.set(value, 'share.imgUrl', 'http://img11.static.yhbimg.com/sns/2017/05/25/14/0177e28a98f73417ae1a2a146aa2670dd2.png'); // eslint-disable-line
                }
            });

            return _.isEmpty(list) ? false : list;
        });
    }
    getQuestionStatus(params) {
        return this.get({
            url: '/activity/question/questionValidate',
            data: params,
            api: global.yoho.ServiceAPI
        });
    }
    getQuestionDetail(id, uid) {
        return Promise.all([
            this.get({
                url: '/activity/question/questionValidate',
                data: {id: id, uid: uid},
                api: global.yoho.ServiceAPI
            }),
            this.get({
                url: 'activity/question/questionDetail',
                data: {id: id},
                api: global.yoho.ServiceAPI
            })
        ]).then(result => {
            let resData = {};

            if (_.get(result, '[0].code', '') === 200) {
                let data = _.get(result, '[1].data', {});

                if (data.questions) {
                    _.forEach(data.questions, (value, key) => {
                        if (!_.isEmpty(value.questionContents)) {
                            _.forEach(value.questionContents, (subval, subkey) => {
                                if (!subval.jumpQuestion) {
                                    return;
                                }

                                value.hasSub = true;
                                subval.jid = `${key}-${subkey}`;
                                subval.jumpQuestion = _handelSubQuestion(subval.jumpQuestion,
                                    subval.jid);
                            });
                        }

                        _fillQuestionContents(value);
                    });
                }

                if (!_.isEmpty(data)) {
                    resData.detail = data;
                }
            } else {
                resData.errText = _.get(result, '[0].message', '哟,本次调研已经飞走了,请移步其他调研呗!');
            }

            return resData;
        });
    }
    submitQuestion(info) {
        return this.post({
            url: '/activity/question/submitQuestions',
            data: info,
            api: global.yoho.ServiceAPI
        });
    }
};