common.js 5.63 KB
/**
 * 页面公共逻辑和接口
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2015/11/23
 */
var $ = require('yoho.jquery');


var $body = $('body');
//为您优选-40位随机数指纹请求id
var RECID = (new Date().getTime() + '_PC_YOHOBUY_' + Math.floor(Math.random() * 1000000 + 1000000) + '_' + Math.floor(Math.random() * 1000000 + 1000000));

require('./footer');

function cookie(name) {
    var re = new RegExp(name + '=([^;$]*)', 'i'),
        matchPattern = '$1';

    return re.test(decodeURIComponent(document.cookie)) ? RegExp[matchPattern] : '';
}

function setCookie(name, value, options) {
    var expires = '',
        path,
        domain,
        secure,
        date;

    if (typeof value !== 'undefined') {
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }

        if (options.expires &&
            (typeof options.expires === 'number' || options.expires.toUTCString)) {
            if (typeof options.expires === 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString();
        }
        path = options.path ? '; path=' + options.path : '';
        domain = options.domain ? '; domain=' + options.domain : '';
        secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    }
}

function getUser() {
    var c = cookie('_UID'),
        user;

    if (typeof c === 'undefined') {
        return 0;
    }

    user = c.split('::');

    if (typeof user === 'undefined' || user.length < 4) {
        return 0;
    }

    return user;
}

function getUid() {
    var user = getUser();

    if (user === 0) {
        return 0;
    }

    return user[1];
}

function getProfileName() {
    var user = getUser();

    if (user === 0) {
        return 0;
    }

    return user[0];
}

function getShoppingKey() {
    var c = cookie('_g');

    if (typeof c === 'undefined') {
        return '';
    }

    return JSON.parse(c).k;
}

//YAS统计代码
(function(w, d, s, j, f) {
    var a = d.createElement(s);
    var m = d.getElementsByTagName(s)[0];

    w.YohoAcquisitionObject = f;

    w[f] = function() {
        w[f].p = arguments;
    };

    a.async = 1;
    a.src = j;
    m.parentNode.insertBefore(a, m);
})(window, document, 'script', ('https:' == document.location.protocol ? 'https' : 'http') + '://' + 'cdn.yoho.cn/yas-jssdk/1.0.17/yas.js', '_yas');

(function() {
    var uid = getUid();

    uid = uid === 0 ? '' : uid;

    window._ozuid = uid; //暴露ozuid

    if (window._yas) {
        window._yas(1 * new Date(), '1.0.17', 'yohobuy_web', uid, '', '');
    }
}());

//window.resize在width<1180时适配成990
$(window).on('resize', function() {
    var w = $(this).width();

    //return top
    if (w < 1360) {
        $('.return-top').addClass('min');
    } else {
        $('.return-top').removeClass('min');
    }

    //body
    if (w < 1180) {
        $body.addClass('min-screen');
    } else {
        $body.removeClass('min-screen');
    }
}).trigger('resize');

function queryString() {
    var vars = {},
        hash,
        i,
        search = window.location.search,
        hashes = search ? decodeURIComponent(search).slice(1).split('&') : [];

    for (i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
        vars[hash[0]] = hash[1];
    }
    return vars;
}

// 给jQuery 扩展 queryString函数
$.extend({
    queryString: function() {
        var vars = [],
            hash,
            i;
        var hashes = window.location.search.slice(1).split('&');

        for (i = 0; i < hashes.length; i++) {
            hash = hashes[i].split('=');
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];
        }
        return vars;
    }
});

//个人中心左侧栏头像加载失败后,显示默认头像
(function() {
    $(window).load(function() {
        var thumb = document.getElementById('user-thumb');
        var img;

        if (!thumb) {
            return;
        }

        img = new Image();

        img.src = thumb.src;

        img.onerror = function() {
            $(thumb).parent().append('<div class="default-user-thumb"></div>').end().remove();
        };
    });
}());

//为您优选埋点 http://redmine.yoho.cn/issues/10117
function givePoint(parameter) {
    var CID = 1;

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

    //男:1,女:2,潮童:3,创意生活:4
    switch(cookie('_Channel')) {
        case 'boys':
            CID = 1;
            break;
        case 'girls':
            CID = 2;
            break;
        case 'kids':
            CID = 3;
            break;
        case 'lifestyle':
            CID = 4;
            break;
    }
         
    parameter = $.extend({
            'REC_POSE': '',
            'REC_ID': RECID,
            'PRD_ID': '',
            'PRD_NUM': 0,
            'C_ID': CID,
            'ACTION_ID': 0,
            'page_num': 1
        }, parameter);

    if (parameter.REC_POSE === '' || parameter.PRD_ID === '') {
        return true;
    }

    window._yas.sendCustomInfo({
        'op': 'YB_CHOOSE_FOR_YOU_Y',
        'uid': getUid(),
        'param': JSON.stringify(parameter)
    }, true);
}

window.cookie = cookie;

window.setCookie = setCookie;

window.getUser = getUser;

window.getUid = getUid;

window.getProfileName = getProfileName;

window.getShoppingKey = getShoppingKey;

window.queryString = queryString;

window.givePoint = givePoint;