Blame view

apps/3party/models/question.js 3.47 KB
yyq authored
1 2 3 4 5 6 7 8
/**
 * question model
 * @author: yyq<yanqing.yang@yoho.cn>
 * @date: 2017/05/23
 */

const _ = require('lodash');
yyq authored
9 10 11 12 13 14 15 16
const _fillQuestionContents = data => {
    if (data && +data.questionType === 3) {
        data.questionContents = _.fill(Array(data.fillBlankNum || 1), true);
    }

    return data;
};
yyq authored
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
const _handelSubQuestion = (qs, jid) => {
    if (qs) {
        qs = _.concat([], qs);

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

    }

    return qs;

};
yyq authored
35 36 37 38 39 40 41
module.exports = class extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);
    }
    getQuestionList() {
        return this.get({
            url: 'activity/question/questionList',
yyq authored
42
            data: {uid: _.get(this.ctx, 'req.user.uid', 0)},
yyq authored
43 44 45 46 47
            api: global.yoho.ServiceAPI
        }).then(result => {
            let list = _.get(result, 'data.rows', []);

            _.forEach(list, (value, key) => {
yyq authored
48 49 50 51 52
                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
                }
yyq authored
53 54
            });
yyq authored
55
            return _.isEmpty(list) ? false : list;
yyq authored
56 57 58 59 60 61 62 63 64 65 66
        });
    }
    getQuestionStatus(params) {
        return this.get({
            url: '/activity/question/questionValidate',
            data: params,
            api: global.yoho.ServiceAPI
        });
    }
    getQuestionDetail(id, uid) {
        return Promise.all([
yyq authored
67
            uid ? this.get({
yyq authored
68 69 70
                url: '/activity/question/questionValidate',
                data: {id: id, uid: uid},
                api: global.yoho.ServiceAPI
yyq authored
71
            }) : Promise.resolve({code: 200}),
yyq authored
72 73 74 75 76 77
            this.get({
                url: 'activity/question/questionDetail',
                data: {id: id},
                api: global.yoho.ServiceAPI
            })
        ]).then(result => {
yyq authored
78 79
            let resData = {};
yyq authored
80 81
            if (_.get(result, '[0].code', '') === 200) {
                let data = _.get(result, '[1].data', {});
yyq authored
82
yyq authored
83
                if (data.questions) {
yyq authored
84
                    _.forEach(data.questions, (value, key) => {
yyq authored
85
                        if (!_.isEmpty(value.questionContents)) {
yyq authored
86 87 88
                            _.forEach(value.questionContents, (subval, subkey) => {
                                if (!subval.jumpQuestion) {
                                    return;
yyq authored
89
                                }
yyq authored
90 91 92 93 94

                                value.hasSub = true;
                                subval.jid = `${key}-${subkey}`;
                                subval.jumpQuestion = _handelSubQuestion(subval.jumpQuestion,
                                    subval.jid);
yyq authored
95
                            });
yyq authored
96
                        }
yyq authored
97 98

                        _fillQuestionContents(value);
yyq authored
99 100
                    });
                }
yyq authored
101
yyq authored
102 103 104 105 106
                if (!_.isEmpty(data)) {
                    resData.detail = data;
                }
            } else {
                resData.errText = _.get(result, '[0].message', '哟,本次调研已经飞走了,请移步其他调研呗!');
yyq authored
107 108 109
            }

            return resData;
yyq authored
110 111 112 113 114
        });
    }
    submitQuestion(info) {
        return this.post({
            url: '/activity/question/submitQuestions',
yyq authored
115 116
            data: info,
            api: global.yoho.ServiceAPI
yyq authored
117 118 119
        });
    }
};