fav.js 1.73 KB
/**
 *  商品详情咨询页  --点赞和帮助功能
 * @author: Lynnic
 * @date: 2015/12/09
 */

 var $ = require('jquery'),
     Hammer = require('yoho.hammer'),
     tip = require('../../plugin/tip');

var goodsConsultsEle = $('#goods-consults')[0],
    goodsConsultsHammer = goodsConsultsEle && new Hammer(goodsConsultsEle);

function showCountPlus($el) {
    var $count = $el.find('.animate-count');

    $count.css('display', 'inline');
    $count.animate({
        opacity: 0.25,
        fontSize: '0.7rem',
        right: '-=5'
    }, 300, function() {
        $count.css('display', 'none');
    });
}

if (goodsConsultsHammer) {

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

        var $this = $(e.target).closest('li'),
            id = $this.closest('.consult-item').data('id'),
            count = $this.find('.count').html() - 0,
            url;

        if (!$this.hasClass('highlight')) {

            if ($this.hasClass('fav')) {
                url = '/product/detail/consultupvote';
            } else if ($this.hasClass('useful')) {
                url = '/product/detail/consultuseful';
            }

            $.ajax({
                method: 'post',
                url: url,
                data: {
                    id: id
                }
            }).done(function(data) {
                if (data.code === 200) {
                    showCountPlus($this);
                    $this.addClass('highlight');
                    $this.find('.count').html(count + 1);
                } else if (data.code === 401) {
                    location.href = data.data;//未登录跳转登录页
                }
            }).fail(function(data) {
                tip.show('网络断开连接了~');
            });

        }
    });
}