Blame view

public/js/product/detail/like.js 2.17 KB
1 2 3 4 5 6 7 8
/**
 *  商品详情 --添加收藏
 * @author: Lynnic
 * @date: 2015/11/24
 */


var $ = require('yoho-jquery'),
陈峰 authored
9
    tip = require('plugin/tip');
10 11 12

var productId = $('#productId').val();
zhangxiaoru authored
13
var C_ID = window._ChannelVary[window.cookie('_Channel')],
zhangxiaoru authored
14 15
    PRD_ID;
16 17 18 19 20 21
$('#likeBtn').on('touchstart', function() {
    var opt,
        favorite;

    var $this = $(this);
zhangxiaoru authored
22 23
    PRD_ID = productId;
24 25 26
    if ($this.hasClass('liked')) {
        opt = 'cancel';
        favorite = 0;
zhangxiaoru authored
27 28 29 30 31 32 33 34 35 36

        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);
zhangxiaoru authored
37
        }
38 39 40
    } else {
        opt = 'ok';
        favorite = 1;
zhangxiaoru authored
41 42 43 44 45 46 47 48 49 50

        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);
zhangxiaoru authored
51
        }
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
    }

    $.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('取消收藏成功');
                } else if (opt === 'ok') {
zhangxiaoru authored
68
                    tip.show('收藏成功');
69 70 71 72 73 74 75 76 77 78 79 80 81
                }
            } else if (data.code === 400) {
                location.href = data.data;// 未登录跳转登录页
            } else {
                tip.show(data.message);
            }
        },
        error: function() {
            tip.show('网络断开连接了~');
        }
    });

    // 统计代码:用于统计用户加入或取消商品收藏的动作
82
    if (window._yas && window._yas.sendCustomInfo) {
83 84 85 86 87 88 89 90 91 92
        window._yas.sendCustomInfo({
            pd: productId,
            fa: favorite
        }, true);
    }

    return false;
});