like.js 2.41 KB
/**
 *  商品详情 --添加收藏
 * @author: Lynnic
 * @date: 2015/11/24
 */


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

let productId = $('#productId').val();

let C_ID = window._ChannelVary[window.cookie('_Channel')],
    PRD_ID;

$('#likeBtn, .like-btn-c').on('touchstart', function() {
    let opt,
        favorite;

    let $this = $(this);

    PRD_ID = productId;

    if ($this.hasClass('like-btn-c')) {
        $this = $this.find('#likeBtn');
    }

    if ($this.hasClass('liked')) {
        opt = 'cancel';
        favorite = 0;

        if (window._yas && window._yas.sendCustomInfo) {
            window._yas.sendCustomInfo({
                op: 'YB_COLLECTION_C',
                param: JSON.stringify({
                    C_ID: C_ID,
                    PRD_ID: PRD_ID,
                    COL_TYPE: 2
                })
            }, true);
        }
    } else {
        opt = 'ok';
        favorite = 1;

        if (window._yas && window._yas.sendCustomInfo) {
            window._yas.sendCustomInfo({
                op: 'YB_COLLECTION_C',
                param: JSON.stringify({
                    C_ID: C_ID,
                    PRD_ID: PRD_ID,
                    COL_TYPE: 1
                })
            }, true);
        }
    }

    $.ajax({
        type: 'POST',
        url: '/product/opt/favoriteProduct',
        data: {
            id: productId,
            opt: opt
        },
        success: function(data) {
            if (data.code === 200) {
                $this.toggleClass('liked');

                if (opt === 'cancel') {
                    tip.show('取消收藏成功');
                    $this.next('.tip').text('收藏').addClass('opa');
                } else if (opt === 'ok') {
                    tip.show('收藏成功');
                    $this.next('.tip').text('已收藏').removeClass('opa');
                }
            } else if (data.code === 400) {
                location.href = data.data;// 未登录跳转登录页
            } else {
                tip.show(data.message);
            }
        },
        error: function() {
            tip.show('网络断开连接了~');
        }
    });

    // 统计代码:用于统计用户加入或取消商品收藏的动作
    if (window._yas && window._yas.sendCustomInfo) {
        window._yas.sendCustomInfo({
            pd: productId,
            fa: favorite
        }, true);
    }

    return false;
});