list-common.js 1.72 KB
/**
 * 星潮教室-列表公共部分
 * @author: wsl<shuiling.wang@yoho.cn>
 * @date: 2016/6/7
*/

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

require('js/common');

window.setCookie('guangStarUid', window.queryString.uid);

// 星搭配收藏请求
$('.collocation-list,.detail-list').on('click', '.collection', function() {
    let $that = $(this),
        $icon = $that.find('.collected-ico');

    let type, pageType, tag;

    if ($icon.hasClass('collected')) {
        type = 'del';
    } else {
        type = 'fav';
    }

    if ($(this).parents('.collocation-list').length > 0) {
        pageType = '1';
        tag = '';
    } else {
        pageType = '2';
        tag = $('.detail-list').data('name');
    }

    $.ajax({
        type: 'POST',
        url: '/guang/star/setFavorite',
        data: {
            articleId: $that.parents('li').attr('articleId'),
            type: type,
            pageType: pageType,
            tag: tag
        },
        success: function(data) {
            let code = data.code;

            if (code === 200) {
                if ($icon.hasClass('collected')) {
                    $icon.removeClass('collected');
                } else {
                    $icon.addClass('collected');
                }

            }

            if (code === 400) {
                if ($('#collocation-link').length <= 0) {
                    $('body').append('<a href=\'' + data.data + '\' style="display:none;" id="collocation-link">' +
                    '<span class="collocation-link"></span></a>');
                }

                $('.collocation-link').click();
            }
        },
        error: function() {
            tip.show('网络断开连接了~');
        }
    });
});