like.js 1.07 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'));

likeHammer.on('tap', function(e) {
    var productId = $('#productId').val(),
        opt;

    var $this = $(this);

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

    $.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);
            }
        },
        error: function() {
            tip.show('网络断开连接了~');
        }
    });

});

// $('#likeBtn').on('click', function(e) {
//     return false;
// });