question-list.page.js 3.62 KB
'use strict';
require('scss/3party/question-list.page.scss');

let $ = require('yoho-jquery'),
    yoho = require('js/yoho-app');

const DETAIL_URI = location.protocol + '//m.yohobuy.com/3party/questionnaire';

require('../common');

let qs = window.queryString;

function getQuestionStatus(reqData, cb) {
    if (qs.uid) {
        reqData.uid = qs.uid;
    }

    $.ajax({
        type: 'POST',
        url: '/3party/questionnaire/check',
        data: reqData
    }).then(function(data) {
        if (cb && typeof cb === 'function') {
            return cb(data);
        }
    });
}

function jumpQuestionDetail(data) {
    let href;

    if (qs && qs.uid && yoho.isApp) {
        href = DETAIL_URI + '/' + data.id + '?uid=' + qs.uid;
    } else {
        href = DETAIL_URI + '/' + data.id;
    }

    if (yoho && yoho.isApp) {
        let link = yoho.parseUrl(href);

        yoho.goH5(href, JSON.stringify({
            action: 'go.h5',
            params: {
                islogin: 'N',
                type: 14,
                updateflag: Date.now() + '',
                url: link.path,
                param: link.query
            }
        }));
    } else {
        window.location.href = href;
    }
}

let tipDialog = {
    $base: $('#tip-dialog'),
    init: function() {
        let that = this;

        this.$content = $('.dg-content', this.$base);
        this.$sureBtns = $('.sure-btns', this.$base);
        this.$shareBtns = $('.share-btns', this.$base);

        this.$base.on('click', '.close-btn', function() {
            that.hide();
        });
        this.$base.on('click', '.share-btn', function() {
            if (that.share && typeof that.share === 'function') {
                that.share();
            } else {
                that.hide();
            }
        });
    },
    show: function(info) {
        this.share = false;

        if (typeof info === 'object') {
            this.$content.html(info.content);
            this.$sureBtns.addClass('hide');
            this.$shareBtns.removeClass('hide');
        } else if (typeof info === 'string') {
            this.$content.html('<p>' + info + '</p>');
            this.$sureBtns.removeClass('hide');
            this.$shareBtns.addClass('hide');
        } else {
            return;
        }

        this.$base.removeClass('hide');
    },
    hide: function() {
        this.$base.addClass('hide');
    }
};

tipDialog.init();

let $list = $('#qs-list');

let canShare = $list.hasClass('can-share');

$list.on('click', 'li', function() {
    let data = $(this).data();

    if (!data.id) {
        return;
    }

    getQuestionStatus({uid: qs.uid, id: data.id}, function(resData) {
        if (resData.code === 200) {
            jumpQuestionDetail(data);
        } else if (resData.code === 206) {
            if (canShare && yoho && yoho.isApp) {
                yoho.invokeMethod('go.showShareAlert', {
                    title: data.title,
                    link: DETAIL_URI + '/' + data.id,
                    desc: data.desc,
                    imgUrl: data.img
                });
            } else {
                tipDialog.show('调查问卷已成功提交,<br>感谢您的帮助!');
            }
        } else if (resData.message) {
            if (yoho && yoho.isApp) {
                yoho.goH5(DETAIL_URI + '/0');
            } else {
                window.location.href = DETAIL_URI + '/0';
            }
        }
    });
});

if ($list.children().length === 1) {
    let data = $list.children().first().data();

    getQuestionStatus({uid: qs.uid, id: data.id}, function(resData) {
        if (resData.code === 200) {
            jumpQuestionDetail(data);
        }
    });
}