电话切换功能[续]

最近闲来无事,对之前的电话切换功能的脚本代码进行了重构,废话不多说


小二儿,上代码~

"use strict";

// 电话列表
var telList = ["400-663-1986", "400-6969-256", "400-0123-267"],
    xmDomainReg = new RegExp(".*.\.xiaoma\.com"),
    xsatDominReg = new RegExp("www\.xuesat\.com"),
    cgDomainReg = new RegExp("www\.xiaomachuguo\.com"),
    seoKfCookie = new RegExp("103608619|103609219|103609522"),
    bbsKfCookie = new RegExp("103608719|103609019|103609520|103609521|103609622|103609623"),
    semKfCookie = new RegExp("103608819|103609119|103609319|103609419|103609519|103609588|103609589");

// 判断客服 Cookie 对应的电话 type
function changeTelByKFtype(kftype) {
    var telType = 1;
    if (seoKfCookie.test(kftype)) {
        telType = 1;
    } else if (bbsKfCookie.test(kftype)) {
        telType = 2;
    } else if (semKfCookie.test(kftype)) {
        telType = 3;
    } else {
        telType = 1;
    }
    return telType;
}

// 判断电话类型并切换电话
var replaceImgSrc = {
    getElement: function(className, parentElement) {
        parentElement || (parentElement = document);
        var descendants = parentElement.getElementsByTagName("*"),
            i =- 1,
            e,
            result = [className];

        while (e = descendants[++i]) {
            ((" " + (e["class"] || e.className) + " ").indexOf(" " + className + " ") > -1) && result.push(e);
        }
        return result;
    },
    nImgUrl: function(imgUrl, type) {
        var fileName = imgUrl.substring(imgUrl.lastIndexOf("\/"), imgUrl.lastIndexOf(".")),
            tmpName = fileName.substring(0, fileName.length-1);
        tmpName = tmpName + type;
        return imgUrl.replace(fileName, tmpName);
    },
    replaces: function(tagList, type) {
        var i = 1,
            className = tagList[0];
        for (; i < tagList.length; i++) {
            var ele = tagList[i],
                eleTag = ele.tagName,
                eleStyle = ele.currentStyle || window.getComputedStyle(ele, null),
                imgUrl;

            if (className === "telnumbcls") {
                if (eleTag == "img" || eleTag == "IMG") {
                    imgUrl = ele.src;
                    imgUrl = this.nImgUrl(imgUrl, type);
                    ele.src = imgUrl + "?" + Math.random();
                } else if (eleTag == "span" || eleTag == "SPAN" || eleTag == "em" || eleTag == "EM" || eleTag == "i" || eleTag == "I") {
                    ele.innerHTML = telList[type];
                }
            } else if (className === "telnumbclsbg") {
                imgUrl = eleStyle.backgroundImage;
                imgUrl = this.nImgUrl(imgUrl, type);
                ele.style.backgroundImage = imgUrl;
            }
        }
    }
};

// 按对应关系切换电话: 1 对应 sem, 2 对应 seo, 3 对应 bbs
function changeTelBytype(teltype) {
    replaceImgSrc.replaces(replaceImgSrc.getElement("telnumbcls"), teltype);
    replaceImgSrc.replaces(replaceImgSrc.getElement("telnumbclsbg"), teltype);
}

// 封装 Cookie 对象
var CookieUtil = {
    get: function(name) {
        var cookie = document.cookie;
        var cookieName = encodeURIComponent(name) + "=";
        var start = cookie.indexOf(cookieName);
        var value = null;
        if (start > -1)
        {
            var end = cookie.indexOf(";", start);
            if (end == -1)
                end = cookie.length;
            value = decodeURIComponent(cookie.substring(start + cookieName.length, end));
        }
        return value;
    },
    set: function(name, value, expires, path, domain, secure) {
        var cookieText = encodeURIComponent(name) + "=" + encodeURIComponent(value);
        if (expires instanceof Date)
            cookieText += "; expires=" + expires.toGMTString();
        if (path)
            cookieText += "; path=" + path;
        if (domain)
            cookieText += "; domain=" + domain;
        if (secure)
            cookieText += "; secure";
        document.cookie = cookieText;
    },
    unset: function(name, path, domain, secure) {
        this.set(name, "", new Date(0), path, domain, secure);
    }
};

// 用户首次进站
(function changeTel() {

        var telType = CookieUtil.get("xiaoma_tel_cookie"); // 获取电话对应的 type

        // 判断电话 Cookie 是否存在
        if (telType !== null && telType !== undefined && telType !== "" && telType !== "0") {

            // Cookie 存在,切换电话
            changeTelBytype(telType);
        } else {

            // Cookie 不存在,判断当前域名主机名,切换电话
            var hostName = window.location.host;

            if (xmDomainReg.test(hostName)) {

                // 如果同域,获取客服 Cookie 及过期时间
                var kfType = CookieUtil.get("xiaoma_style_id"),
                    kfOverdue = parseInt(CookieUtil.get("xiaoma_style_id_overdue") + "000"),
                    telOverdue = new Date(kfOverdue);
                telType = changeTelByKFtype(kfType);
                changeTelBytype(telType);
                CookieUtil.set("xiaoma_tel_cookie", telType, telOverdue, "/", "xiaoma");
            } else {

                // 如果不同域,请求服务器获取主域下客服 Cookie 及过期时间
                $.getJSON("http://www.xiaoma.com/didiadmin/test_telchange.php?callback=?", function(data) {
                    var kfType = data.xiaoma_style_id,
                        kfOverdue = parseInt(data.xiaoma_style_id_overdue + "000"),
                        telOverdue = new Date(kfOverdue),
                        otherHostName;
                    telType = changeTelByKFtype(kfType);
                    changeTelBytype(telType);
                    if (xsatDominReg.test(hostName)) {
                        otherHostName = "xuesat.com";
                    } else if (cgDomainReg.test(hostName)) {
                        otherHostName = "xiaomachuguo.com";
                    } else {
                        otherHostName = "xiaoma.com";
                    }
                    CookieUtil.set("xiaoma_tel_cookie", telType, telOverdue, "/", otherHostName);
                });
            }
        }
}());

[服务端脚本和前台页面结构] http://varyu.com/notes/82.html

发表评论
* 昵称
* Email
* 网址
* 评论