nginx_switch.js 4.37 KB
$(function () {
});

/**
 * "切向*"按钮点击事件,打开确认操作对话框
 * @param cloudName 源中心名称
 * @param target 目标中心名称
 * @param onlineOrGray 切换:线上/灰度
 */
function initSwitch(cloudName, target, onlineOrGray) {
    var arr = getNoChangeIpArr(cloudName, onlineOrGray);
    if (undefined === arr || null === arr || 0 === arr.length) {
        prompt("提示", "无可切换的gateway!");
        return;
    }

    var dialog = $("<div>").appendTo($("body"));
    dialog.dialog({
        title: "你确定切换吗",
        backdrop: "static",
        content: "你确定要将" + cloudName + "上的" + onlineOrGray + "流量切向" + target + "吗?",
        buttons: [{
            text: "否",
            className: "btn-danger",
            onclick: function () {
                dialog.dialog("hide");
            }
        }, {
            text: "是",
            className: "btn-success",
            onclick: function () {
                var param = {
                    cloudName: cloudName,
                    target: target,
                    onlineOrGray: onlineOrGray,
                    noChangeIps: JSON.stringify(arr)
                };
                sendAjax("post", "viewToChangeNginxConf", param, "text", viewToChangeSuccess, errorFunc);
                dialog.dialog("hide");
            }
        }]
    });
}

/**
 * 打开对话框,展示切换后的配置
 * @param resp 切换后的响应数据
 */
function viewToChangeSuccess(resp) {
    var data = JSON.parse(resp);
    var dialog = $("<div>").appendTo($("body"));
    dialog.dialog({
        title: "切换结果",
        backdrop: "static",
        content: "<pre>" + data.data.result + "</pre>",
        buttons: [{
            text: "否",
            className: "btn-danger",
            onclick: function () {
                dialog.dialog("hide");
            }
        }, {
            text: "确定",
            className: "btn-success",
            onclick: function () {
                var param = {
                    cloudName: data.data.cloudName
                };
                sendAjax("post", "switchNginxConf", param, "text", switchSuccess, errorFunc);

                dialog.dialog("hide");
                toWait();
            }
        }]
    }).find(".modal-body").css({
        height: "650px"
    });
}

/**
 * 切换配置成功,跳转回首页
 */
function switchSuccess() {
    window.location.href = getUrlBasePath() + "/nginxswitch/toNginxSwitch";
}

/**
 * 获取不切换的服务器ip(由于线上和灰度同在nginx.conf配置中,模板须设计两个占位符,所以切换某一处时,须将不切换的ip提交到后台)
 * @param cloudName 切换的中心aws/qcloud
 * @param onlineOrGray  切换线上/灰度
 * @returns {Array} 不切换服务器的ip列表
 */
function getNoChangeIpArr(cloudName, onlineOrGray) {
    var list = $("." + cloudName + ("online" === onlineOrGray ? "grayapigateway" : "apigateway") + "Ips");
    if (undefined === list || null === list || 0 === list.length) {
        return [];
    }
    var result = [];
    list.each(function () {
        result.push($(this).text().split(":")[0]);
    });
    return result;
}

/**
 * 提示函数
 * @param title 标题
 * @param content 内容
 */
function prompt(title, content) {
    var dialog = $("<div>").appendTo($("body"));
    dialog.dialog({
        title: title,
        backdrop: "static",
        content: content,
        buttons: [{
            text: "确定",
            className: "btn-success",
            onclick: function () {
                dialog.dialog("hide");
            }
        }]
    });
}

/**
 * 发送ajax请求
 * @param type 请求方法post/get
 * @param url 请求url
 * @param data 请求参数数据
 * @param dataType 请求参数类型
 * @param success 请求成功回调函数
 * @param error 请求异常回调函数
 */
function sendAjax(type, url, data, dataType, success, error) {
    $.ajax({
        type: type,
        url: url,
        data: data,
        dataType: dataType,
        success: success,
        error: error
    });
}

/**
 * ajax请求异常回调函数
 */
function errorFunc() {
    layer.msg("Token异常", {icon: 2});
}

/**
 * 打开等待对话框
 */
function toWait() {
    $("<div>").appendTo($("body")).dialog({
        title: "提示",
        backdrop: "static",
        content: "正在切换,请稍后..."
    });
}