data-yas.js 3.05 KB
/**
 * Created by DELL on 2016.11.9.
 */
var $ = require('yoho-jquery');

function getCookie(name) {
    var arr, reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)');

    if (arr === document.cookie.match(reg)) {
        return unescape(arr[2]);
    } else {
        return null;
    }
}

// 频道、性别、品类、折扣、人群、颜色、尺码、分类
function _getCommonFilterValue() {
    var $section = $('.people, .sort, .discount, .misort, .color');
    var $attrchecked = $section.find('.attr-content .attr.checked');
    var filterValue = '';

    $attrchecked.each(function() {
        var $this = $(this);

        var key = $this.parents('.attr-content').prev().html(),
            name = $this.attr('name');

        filterValue += key.replace(new RegExp(/()/g), ':') + name + ';';
    });

    return filterValue;
}

// 价格、品牌、高级筛选
function _getOtherFilterValue() {
    var filterValue = '';

    $('.checked-conditions .attr-content .tag').each(function() {
        var type = $(this).attr('itemType'),
            name = $(this).attr('name'),
            totalName = $(this).attr('totalName');

        if (type !== '') {
            switch (type) {
                case 'brand':
                    type = '品牌';
                    break;
                case 'price':
                    type = '价格';
                    break;
                case 'size':
                    type = '尺码';
                    break;
                case 'gender':
                    type = '性别';
                    break;
                default:
                    break;
            }
            if (totalName) {
                filterValue += type + ':' + totalName + ';';
            } else {
                filterValue += type + ':' + name + ';';
            }
        }
    });

    return filterValue;
}

/** *埋点方法***/
function yasEvent(op, parameter) {
    var CID = 1;

    if (!window._yas || !window._yas.sendCustomInfo) {
        return false;
    }

    if (!getCookie('_Channel')) {
        // 男:1,女:2,潮童:3,创意生活:4
        switch (getCookie('_Channel')) {
            case 'boys':
                CID = 1;
                break;
            case 'girls':
                CID = 2;
                break;
            case 'kids':
                CID = 3;
                break;
            case 'lifestyle':
                CID = 4;
                break;
            default:
                CID = 1;
        }
    }

    parameter = $.extend({
        C_ID: CID
    }, parameter);
    console.info(op);
    console.info(parameter);
    window._yas.sendCustomInfo({
        op: op,
        param: JSON.stringify(parameter)
    }, true);
}

function getFilterValue() {
    return _getCommonFilterValue() + _getOtherFilterValue();
}

function givePoint(op, parameter) {
    var sto = setInterval(function() {
        if (window._yas && window._yas.sendCustomInfo) {
            yasEvent(op, parameter);
            clearInterval(sto);
        }
    }, 100);
}
exports.givePoint = givePoint;
exports.getFilterValue = getFilterValue;