limitSwitch.jsp 13.8 KB
<%@page language="java" contentType="text/html;charset=utf-8" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link rel="stylesheet" href="<%=basePath %>css/bootstrap.min.css"/>
    <link rel="stylesheet" href="<%=basePath %>css/unicorn.main.css"/>
    <link rel="stylesheet" href="<%=basePath %>css/unicorn.grey.css"/>
    <link rel="stylesheet" href="<%=basePath %>css/jquery-ui.css"/>
    <link rel="stylesheet" href="<%=basePath %>css/bootstrap-responsive.min.css"/>
    <link rel="stylesheet" href="<%=basePath %>css/uniform.css"/>
    <link rel="stylesheet" href="<%=basePath %>css/select2.css"/>
    <link rel="stylesheet" href="<%=basePath %>css/font-awesome.min.css"/>
    <link rel="stylesheet" href="<%=basePath %>css/dnspod_switch.css"/>
    <link rel="stylesheet" href="<%=basePath %>css/ace.min.css"/>
    <link rel="stylesheet" href="<%=basePath %>css/ace-rtl.min.css"/>
    <link rel="stylesheet" href="<%=basePath %>css/ace-skins.min.css"/>
    <script src="<%=basePath %>js/jquery-2.1.4.min.js" type="text/javascript"></script>
    <script src="<%=basePath %>js/bootstrap.min.js" type="text/javascript"></script>
    <script src="<%=basePath %>js/typeahead-bs2.min.js" type="text/javascript"></script>
    <script src="<%=basePath %>js/ace-elements.min.js" type="text/javascript"></script>
    <script src="<%=basePath %>js/ace.min.js" type="text/javascript"></script>
    <script src="<%=basePath %>js/layer/layer.js" type="text/javascript"></script>
    <script src="<%=basePath %>/js/unicorn.js" type="text/javascript"></script>
    <script src="<%=basePath %>js/bootstrap-plugin/datetimepicker/moment-with-locales.js" charset="UTF-8"
            type="text/javascript"></script>
    <script src="<%=basePath %>js/bootstrap-plugin/datetimepicker/bootstrap-datetimepicker.js" charset="UTF-8"
            type="text/javascript"></script>
    <script src="<%=basePath %>js/global.js" charset="UTF-8" type="text/javascript"></script>
    <script src="<%=basePath %>js/bootstrap-plugin/bootstrap.pagination.js" charset="UTF-8"
            type="text/javascript"></script>
    <script src="<%=basePath %>js/bootstrap-plugin/bootstrap.table.js" charset="UTF-8" type="text/javascript"></script>
    <script src="<%=basePath %>js/bootstrap-plugin/bootstrap.dialog.js" charset="UTF-8" type="text/javascript"></script>
    <script src="<%=basePath %>js/bootstrap-plugin/bootstrap.form.js" charset="UTF-8" type="text/javascript"></script>
    <script src="<%=basePath %>js/bootstrap-plugin/bootstrap.panel.js" charset="UTF-8" type="text/javascript"></script>
    <script src="<%=basePath %>js/bootstrap-plugin/bootstrap.alerts.js" charset="UTF-8" type="text/javascript"></script>
    <script src="<%=basePath %>js/bootstrap-plugin/bootstrap.accordion.js" charset="UTF-8"
            type="text/javascript"></script>
    <script src="<%=basePath %>js/bootstrap-plugin/bootstrap.breadcrumb.js" charset="UTF-8"
            type="text/javascript"></script>
    <script src="<%=basePath %>js/bootstrap-plugin/bootstrap.validate.js" charset="UTF-8"
            type="text/javascript"></script>
    <script src="<%=basePath %>js/bootstrap-plugin/bootstrap.form.js" charset="UTF-8" type="text/javascript"></script>
    <script src="<%=basePath %>js/bootstrap-plugin/bootstrap.select.js" charset="UTF-8" type="text/javascript"></script>
    <script src="<%=basePath %>js/jquery.toaster.js" type="text/javascript"></script>
    <script type="text/javascript">
        var contextPath = '<%=basePath %>';
    </script>
    <title>YOHO!运维</title>
</head>
<body style="background-color: #444444;font-size: 14px">

<!-- 头部 -->
<div id="head">
</div>
<!-- 右侧具体内容 -->
<div id="content">
    <div id="breadcrumb">
        <a href="#" title="Go to Home" class="tip-bottom"><em class="icon-home"></em> Home</a> <a href="#"
                                                                                                  class="current">限流调整</a>
    </div>

    <div class="row">
        <div class="col-xs-12">
            <!-- PAGE CONTENT BEGINS -->

            <div class="row">
                <div class="col-xs-12 col-sm-12 widget-container-span">
                    <div class="tabbable">

                        <div class="tab-content">
                            <div id="home">
                                <dl id="dt-list-1" class="dl-horizontal">
                                    <dt>open_limit_flow</dt>
                                    <dd id="open_limit_flow">${open_limit_flow}</dd>
                                    <br/>
                                    <dt>limit_config</dt>
                                    <dd id="limit_config">
                                        <c:forEach items="${limit_config}" var="map">
                                            <ul>
                                                <li>${map}</li>
                                            </ul>
                                        </c:forEach>
                                    </dd>
                                    <br/>
                                    <dt>limit_service_config</dt>
                                    <dd id="limit_service_config">
                                        <c:forEach items="${limit_service_config}" var="map">
                                            <ul>
                                                <li>${map}</li>
                                            </ul>
                                        </c:forEach>
                                    </dd>
                                </dl>

                                <h3 class="header smaller red"></h3>
                                <div class="btn-group">
                                    <button class="btn btn-sm btn-success" onclick="initSwitch()">
                                        <em class="icon-cogs bigger-110"></em>调整
                                    </button>
                                </div>
                            </div>

                        </div>
                    </div>
                </div><!-- /span -->

                <div class="vspace-xs-6"></div>
            </div><!-- /row -->

            <script type="text/javascript">
                var $path_assets = "assets";//this will be used in gritter alerts containing images
            </script>

            <!-- PAGE CONTENT ENDS -->
        </div><!-- /.col -->
    </div><!-- /.row -->
</div>

<script src="<%=basePath %>script/common/genarate_left_panel.js" type="text/javascript"></script>

<script type="text/javascript">
    $("#li_switch").addClass("active open");
    $("#li_limitswitch").addClass("active");
</script>

<script type="text/javascript">

    /**
     * 按钮“调整”点击事件,打开设置对话框并初始化数据
     */
    function initSwitch() {
        var html = "<div><dl class='dl-horizontal'>";
        html += "<dt>open_limit_flow</dt>";
        html += "<dd id='open_limit_flow_a'><input type='text' class='form-control' value='" + $("#open_limit_flow").text() + "'></dd>";
        html += "<br />";
        html += "<dt>limit_config</dt>";
        html += "<dd id='limit_config_a'><ul>";
        html += "<li style='padding: 5px;'><button class='btn btn-sm btn-success' onclick='addLi(this)'><em class='icon-cogs bigger-110'></em>新增</button></li>";
        $("#limit_config").find("li").each(function () {
            html += "<li style='padding: 5px;'><input type='text' class='form-control' style='width: 80%;' value='" + $(this).text() + "'>&nbsp;&nbsp;";
            html += "<button class='btn btn-sm btn-success' onclick='delLi(this)'><em class='icon-cogs bigger-110'></em>删除</button>";
            html += "</li>";
        });
        html += "</ul></dd>";
        html += "<br />";
        html += "<dt>limit_service_config</dt>";
        html += "<dd id='limit_service_config_a'><ul>";
        html += "<li style='padding: 5px;'><button class='btn btn-sm btn-success' onclick='addLi(this)'><em class='icon-cogs bigger-110'></em>新增</button></li>";
        $("#limit_service_config").find("li").each(function () {
            html += "<li style='padding: 5px;'><input type='text' class='form-control' style='width: 80%;' value='" + $(this).text() + "'>&nbsp;&nbsp;";
            html += "<button class='btn btn-sm btn-success' onclick='delLi(this)'><em class='icon-cogs bigger-110'></em>删除</button>";
            html += "</li>";
        });
        html += "</ul></dd>";
        html += "</dl></div>";

        var dialog = $("<div>").appendTo($("body"));
        dialog.dialog({
            title: "限流调整",
            backdrop: "static",
            content: html,
            buttons: [{
                text: "取消",
                className: "btn-danger",
                onclick: function () {
                    dialog.dialog("hide");
                }
            }, {
                text: "提交",
                className: "btn-success",
                onclick: function () {
                    toSubmit(dialog);
                }
            }]
        }).find(".modal-body").css({
            height: "650px"
        });
    }

    /**
     * ajax提交编辑后的数据
     */
    function toSubmit(dialog) {
        var toSendAjaxFlag = false;

        var openLimitFlow = $("#open_limit_flow_a").find("input").val();
        var limitConfig = [];
        $("#limit_config_a").find("input").each(function () {
            var config = $(this).val();
            if (undefined !== config && null !== config && "" !== config) {
                limitConfig.push(config);
            }
        });
        var limitServiceConfig = [];
        $("#limit_service_config_a").find("input").each(function () {
            var config = $(this).val();
            if (undefined !== config && null !== config && "" !== config) {
                limitServiceConfig.push(config);
            }
        });
        if (undefined === openLimitFlow || null === openLimitFlow || "" === openLimitFlow) {
            prompt("提示", "限流开关不能为空!")
        } else {
            toSendAjaxFlag = true;
        }

        if (toSendAjaxFlag) {
            var param = {
                openLimitFlow: openLimitFlow,
                limitConfig: JSON.stringify(limitConfig),
                limitServiceConfig: JSON.stringify(limitServiceConfig)
            };
            sendAjax("post", "viewToChangeLimitConf", param, "text", viewToChangeSuccess, errorFunc);
            dialog.dialog("hide");
        }
    }

    /**
     * 弹出调整后数据对话框
     */
    function viewToChangeSuccess(resp) {
        var data = JSON.parse(resp);
        var dialog = $("<div>").appendTo($("body"));
        dialog.dialog({
            title: "切换结果",
            backdrop: "static",
            content: "<pre>" + data.data + "</pre>",
            buttons: [{
                text: "否",
                className: "btn-danger",
                onclick: function () {
                    dialog.dialog("hide");
                }
            }, {
                text: "确定",
                className: "btn-success",
                onclick: function () {
                    sendAjax("post", "switchConf", {}, "text", switchSuccess, errorFunc);

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

    /**
     * 调整成功后,跳转首页
     */
    function switchSuccess() {
        window.location.href = getUrlBasePath() + "/limitSwitch/toLimitSwitch";
    }

    /**
     * 增加配置项
     */
    function addLi(btn) {
        var html = "<li style='padding: 5px;'><input type='text' class='form-control' style='width: 80%;'>&nbsp;&nbsp;";
        html += "<button class='btn btn-sm btn-success' onclick='delLi(this)'><em class='icon-cogs bigger-110'></em>删除</button>";
        html += "</li>";
        $(btn).parent().parent().append(html);
    }

    /**
     * 删除配置项
     */
    function delLi(btn) {
        $(btn).parent().remove();
    }

    /**
     * 发送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: "正在切换,请稍后..."
        });
    }

    /**
     * 打开提示对话框
     * @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");
                }
            }]
        });
    }
</script>
</body>
</html>