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


var $ = require('jquery'),
    tip = require('../../plugin/tip'),
    chosePanel = require('../../cart/chose-panel');

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

var skn = $('#productSkn').val(),
    productCode = $('#limitProductCode').val();

 //门票 限购数量
var ticketsLimit = $('#limitNum').val() || 0;

$('#likeBtn').on('touchstart', function() {
    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');

                if ('cancel' === opt) {
                    tip.show('取消收藏成功');
                } else if ('ok' === opt) {
                    tip.show('收藏成功');
                }
            } 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;
});

$('#addtoCart').on('touchstart', function() {

    //门票验证登录
    if (ticketsLimit) {
        if (!ticketCheckLogin()) {
            return false;
        }
    }

    $('.cart-bar').hide();

    productCode && chosePanel.setLimitGoodModeWithSknId(productCode, skn);
    chosePanel.show();

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

    return false;
});

ticketCheckOpen();

$('#buyLimit').on('touchstart', function() {
    tip.show('打开APP可抢购该商品哦~');
    return false;
});

//门票立即购买验证登录
 function ticketCheckLogin() {

     if (!window.getUid()) {
         var urlArray = window.location.href.split('?');
         window.location.href = '//m.yohobuy.com/signin.html?refer=' + urlArray[0] + '?product_type=ticket';
         return false;
     }
     return true;
 }

// 门票登录后自动弹出
 function ticketCheckOpen() {
     var showPannel = $('#showPannel').val();
     if (showPannel) {
         chosePanel.show();
     }
 }