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

const _ = require('lodash');

module.exports = class extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);
    }
    getQuestionList() {
        return this.get({
            url: 'activity/question/questionList',
            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 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) {
                resData.errText = _.get(result, '[0].message', '哟,本次调研已经飞走了,请移步其他调研呗!');
            }

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

            if (data.questions) {
                _.forEach(data.questions, value => {
                    if (+value.questionType === 3) {
                        value.questionContents = _.fill(Array(value.fillBlankNum || 1), true);
                    }
                });
            }

            if (!_.isEmpty(data)) {
                resData.detail = data;
            }

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