iTest.html 8.59 KB
<style>

    #interface_test_page .result_wrap {
        height: calc(100% - 70px);
        margin-top: 25px;
    }

    #interface_test_page .col-md-2 {
        padding: 0;
    }

    #interface_test_page .col-md-2:first-child {
        padding-left: 15px;
    }
</style>
<div class="container-fluid page" id="interface_test_page">
    <div class="card">

        <div class="card-body" style="height: 100%">
            <div class="top_form_card" style="width: 100%;">
                <form>
                    <div class="row">
                        <div class="col-md-2">
                            <div class="form-group">
                                <label class="bmd-label-floating">接口名称</label>
                                <select class="form-control" name="interfaceName">
                                    <option>getLineInfoByName</option>
                                    <option>getLine</option>
                                    <option selected>carMonitor</option>
                                    <option>getdispatchScreen</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-2 field" style="margin-top: 5px;">
                            <div class="form-group">
                                <label class="bmd-label-floating">线路</label>
                                <div class="ct_auto_wrap line_autocompleter">
                                    <input type="text" name="lineName" required class="form-control" autocomplete="off">
                                </div>
                            </div>
                        </div>

                        <div class="col-md-2 field">
                            <div class="form-group">
                                <label class="bmd-label-floating">走向</label>
                                <select class="form-control" name="direction">
                                    <option value=0>上行</option>
                                    <option value=1>下行</option>
                                </select>
                            </div>
                        </div>

                        <div class="col-md-2 field">
                            <div class="form-group">
                                <label class="bmd-label-floating">站点</label>
                                <select class="form-control" name="stopid"></select>
                            </div>
                        </div>

                        <div class="col-md-2 field" style="margin-top: 5px;">
                            <div class="form-group">
                                <label class="bmd-label-floating">时间(t)</label>
                                <input type="text" name="t" class="form-control" value="" placeholder="暂时没校验时间">
                            </div>
                        </div>
                        <div class="col-md-1">
                            <div class="form-group">
                                <button type="submit" class="btn btn-primary btn-raised" style="margin-top: 22px;">
                                    <i class="material-icons">search</i> 请求接口
                                </button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>

            <div class="result_wrap">
                <div class="form-group">
                    <label for="get_url_text" class="bmd-label-floating">GET</label>
                    <textarea class="form-control" id="get_url_text" rows="1" spellcheck="false"></textarea>
                </div>
                <br>
                <div class="form-group" style="height: calc(100% - 120px)">
                    <label for="result_text" class="bmd-label-floating">RESULT</label>
                    <textarea class="form-control" id="result_text" style="height: 100% !important;" spellcheck="false"></textarea>
                </div>
            </div>
        </div>
    </div>
</div>


<script>
    (function () {
        var wrap = '#interface_test_page';

        //切换接口
        $('[name=interfaceName]', wrap).on('change', show_fields);

        //线路自动补全
        $.get('/basic/lines', function (rs) {
            var data = [], item;
            for (var i = 0, len = rs.length; i < len; i++) {
                item = rs[i];
                data.push({
                    code: item.lineCode,
                    text: item.name,
                    others: [item.fullChars, item.camelChars]
                });
            }
            gb_ct_autocompleter.build($('.line_autocompleter', wrap), data);
        });

        //线路选择事件
        $('.line_autocompleter input', wrap).on('auto_change', fullStations);
        $('select[name=direction]', wrap).on('change', fullStations);

        function fullStations() {
            var $stopSelect = $('select[name=stopid]', wrap);
            /*if(!$stopSelect.is(':visible'))
                return;*/

            //填充站点下拉框
            var lineCode = $('input[name=lineName]', wrap).data('val')
                , upDown = $('select[name=direction]', wrap).val();

            $.get('/geo_data/stations/' + lineCode + '/' + upDown, function (rs) {
                console.log('stations', rs);
                var array = rs.stations;

                var opts = '<option value="">请选择...</option>';
                for (var i = 0, len = array.length; i < len; i++) {
                    opts += '<option value="' + array[i].stationCode + '">' + array[i].name + '</option>';
                }

                $stopSelect.html(opts);
            });
        }

        function show_fields() {
            var v = $(this).val()
                , ns;

            if (v == 'getLineInfoByName' || v == 'getLine')
                ns = ['lineName', 't'];
            else if (v == 'carMonitor')
                ns = ['lineName', 'stopid', 'direction', 't'];
            else if (v == 'getdispatchScreen')
                ns = ['lineName', 'direction', 't'];

            $('form .row>div.field', wrap).hide();

            $('form .form-group [name]', wrap).each(function () {
                if (ns.indexOf($(this).attr('name')) != -1)
                    $(this).parents('.field').show();
            });
        }

        $('form', wrap).on('submit', function () {
            try {
                var params = '';
                var data = $(this).serializeJSON();
                var lineCode = $('input[name=lineName]', wrap).data('val');
                var v = data.interfaceName;

                if (v == 'getLineInfoByName')
                    params = '?linename=' + data.lineName;
                else if (v == 'getLine')
                    params = '?lineid=' + lineCode;

                else if (v == 'carMonitor')
                    params = '?lineid=' + lineCode + '&stopid=' + data.stopid + '&direction=' + data.direction;
                else if (v == 'getdispatchScreen')
                    params = '?lineid=' + lineCode + '&direction=' + data.direction;

                params += '&t=' + data.t;

                var url = '/xxfb/' + v + params
                    , fullUrl = window.location.href + 'xxfb/' + v + params;

                $('#get_url_text').text(fullUrl);

                $.ajax({
                    url: url,
                    dataType: 'text',
                    success: function (data) {
                        $('#result_text').text(formatXml(data));
                    }
                });
            } catch (e) {
                console.log(e);
            }

            return false;
        });

        function formatXml(xml) {
            var formatted = '';
            var reg = /(>)(<)(\/*)/g;
            xml = xml.replace(reg, '$1\r\n$2$3');
            var pad = 0;
            jQuery.each(xml.split('\r\n'), function(index, node) {
                var indent = 0;
                if (node.match( /.+<\/\w[^>]*>$/ )) {
                    indent = 0;
                } else if (node.match( /^<\/\w/ )) {
                    if (pad != 0) {
                        pad -= 1;
                    }
                } else if (node.match( /^<\w[^>]*[^\/]>.*$/ )) {
                    indent = 1;
                } else {
                    indent = 0;
                }

                var padding = '';
                for (var i = 0; i < pad; i++) {
                    padding += '    ';
                }

                formatted += padding + node + '\r\n';
                pad += indent;
            });

            return formatted;
        }
    })();
</script>