like.js 1.63 KB
/**
 *  商品详情 --添加收藏
 * @author: Lynnic
 * @date: 2015/11/24
 */
var $ = require('jquery'),
     Hammer = require('yoho.hammer'),
     tip = require('../../plugin/tip');

var likeHammer = new Hammer(document.getElementById('likeBtn')),
    addToCartHammer = new Hammer(document.getElementById('addtoCart'));

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

likeHammer.on('tap', function(e) {
    var opt,
        favorite;

    var $this = $(this);

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

    $.ajax({
        type: 'POST',
        url: '/product/opt/favoriteProduct',
        data: {
            id: productId,
            opt: opt
        },
        success: function(data) {
            if (data.code === 200) {
                $this.toggleClass('liked');
            } else if (data.code === 400) {
                location.href = data.data;//未登录跳转登录页
            } else {
                tip.show(data.message);
            }

            // 统计代码:用于统计用户加入或取消商品收藏的动作
            if (window._yas) {
                window._yas.sendCustomInfo({
                    pd: productId,
                    fa: favorite
                });
            }
        },
        error: function() {
            tip.show('网络断开连接了~');
        }
    });

});

addToCartHammer.on('tap', function(e) {

    // 统计代码:用于统计用户加入购物车的动作
    if (window._yas) {
        window._yas.sendCustomInfo({
            pd: productId,
            by: 1
        });
    }
});