');
var resp3 = $('');
respcontent.append(resp1).append(resp2).append(resp3);
resptab.append(respcontent);
respcleanDiv.append(resptab);
DApiUI.log(xhr);
DApiUI.log(xhr.getAllResponseHeaders());
var allheaders = xhr.getAllResponseHeaders();
if (allheaders !== null && typeof (allheaders) !== 'undefined' && allheaders !== "") {
var headers = allheaders.split("\r\n");
var headertable = $('
');
for (var i = 0; i < headers.length; i++) {
var header = headers[i];
if (header !== null && header !== "") {
var headerValu = header.split(":");
var headertr = $('
');
headertable.append(headertr);
}
}
//设置Headers内容
resp3.find(".panel-body").html("");
resp3.find(".panel-body").append(headertable);
}
var contentType = xhr.getResponseHeader("Content-Type");
DApiUI.log("Content-Type:" + contentType);
DApiUI.log(xhr.hasOwnProperty("responseJSON"));
if (xhr.hasOwnProperty("responseJSON")) {
//如果存在该对象,服务端返回为json格式
resp1.find(".panel-body").html("");
DApiUI.log(xhr["responseJSON"]);
var pre = $('
');
jsondiv.JSONView(xhr["responseJSON"]);
pre.html(JSON.stringify(xhr["responseJSON"], null, 2));
resp1.find(".panel-body").append(jsondiv);
} else {
//判断content-type
//如果是image资源
var regex = new RegExp('image/(jpeg|jpg|png|gif)', 'g');
if (regex.test(contentType)) {
var d = DApiUI.getDoc().data("data");
var imgUrl = "http://" + d.host + apiInfo.url;
var img = document.createElement("img");
img.onload = function (e) {
window.URL.revokeObjectURL(img.src); // 清除释放
};
img.src = imgUrl;
resp1.find(".panel-body").html("");
resp1.find(".panel-body")[0].appendChild(img);
} else {
//判断是否是text
var regex = new RegExp('.*?text.*', 'g');
if (regex.test(contentType)) {
resp1.find(".panel-body").html("");
resp1.find(".panel-body").html(xhr.responseText);
}
}
}
DApiUI.log("tab show...");
resptab.find("a:first").tab("show");
},
error: function (xhr, textStatus, errorThrown) {
DApiUI.log("error.....");
DApiUI.log(xhr);
DApiUI.log(textStatus);
DApiUI.log(errorThrown);
var resptab = $('
');
resptab.append(ulresp);
var respcontent = $('
暂无
');
var resp3 = $('');
respcontent.append(resp1).append(resp2).append(resp3);
resptab.append(respcontent);
respcleanDiv.append(resptab);
DApiUI.log(xhr);
DApiUI.log(xhr.getAllResponseHeaders());
var allheaders = xhr.getAllResponseHeaders();
if (allheaders !== null && typeof (allheaders) !== 'undefined' && allheaders !== "") {
var headers = allheaders.split("\r\n");
var headertable = $('
');
for (var i = 0; i < headers.length; i++) {
var header = headers[i];
if (header !== null && header !== "") {
var headerValu = header.split(":");
var headertr = $('
' + headerValu[0] + ' | ' + headerValu[1] + ' |
---|
');
headertable.append(headertr);
}
}
//设置Headers内容
resp3.find(".panel-body").html("");
resp3.find(".panel-body").append(headertable);
}
var contentType = xhr.getResponseHeader("Content-Type");
DApiUI.log("Content-Type:" + contentType);
var jsonRegex = "";
DApiUI.log(xhr.hasOwnProperty("responseJSON"));
if (xhr.hasOwnProperty("responseJSON")) {
//如果存在该对象,服务端返回为json格式
resp1.find(".panel-body").html("");
DApiUI.log(xhr["responseJSON"]);
var jsondiv = $('
');
jsondiv.JSONView(xhr["responseJSON"]);
resp1.find(".panel-body").append(jsondiv);
} else {
//判断是否是text
var regex = new RegExp('.*?text.*', 'g');
if (regex.test(contentType)) {
resp1.find(".panel-body").html("");
resp1.find(".panel-body").html(xhr.responseText);
}
}
DApiUI.log("tab show...");
resptab.find("a:first").tab("show");
}
})
}
})
};
DApiUI.createDebugResponseTab = function (parent, data) {
};
DApiUI.writeUTF8 = function (str, isGetBytes) {
var back = [],
byteSize = 0;
for (var i = 0; i < str.length; i++) {
var code = str.charCodeAt(i);
if (code >= 0 && code <= 127) {
byteSize += 1;
back.push(code);
} else if (code >= 128 && code <= 2047) {
byteSize += 2;
back.push((192 | (31 & (code >> 6))));
back.push((128 | (63 & code)))
} else if (code >= 2048 && code <= 65535) {
byteSize += 3;
back.push((224 | (15 & (code >> 12))));
back.push((128 | (63 & (code >> 6))));
back.push((128 | (63 & code)))
}
}
for (i = 0; i < back.length; i++) {
if (back[i] > 255) {
back[i] &= 255
}
}
if (isGetBytes) {
return back
}
if (byteSize <= 255) {
return [0, byteSize].concat(back);
} else {
return [byteSize >> 8, byteSize & 255].concat(back);
}
};
DApiUI.createApiInfoTable = function (apiInfo) {
var table = $('
');
var thead = $('
API接口文档 |
---|
');
table.append(thead);
var tbody = $('
');
var url = $('
接口url | ' + DApiUI.getStringValue(apiInfo.url) + ' |
---|
');
tbody.append(url);
var summary = $('
接口名称 | ' + DApiUI.getStringValue(apiInfo.summary) + ' |
---|
');
tbody.append(summary);
var description = $('
说明 | ' + DApiUI.getStringValue(apiInfo.description) + ' |
---|
');
tbody.append(description);
var methodType = $('
请求方式 | ' + DApiUI.getStringValue(apiInfo.methodType) + ' |
---|
');
tbody.append(methodType);
var consumesArr = DApiUI.getValue(apiInfo, "consumes", [], true);
var consumes = $('
consumes | ' + consumesArr + ' |
---|
');
tbody.append(consumes);
var producesArr = DApiUI.getValue(apiInfo, "produces", [], true);
var produces = $('
produces | ' + producesArr + ' |
---|
');
tbody.append(produces);
//请求参数
var args = $('
请求参数 |
---|
');
//判断是否有请求参数
if (typeof (apiInfo.parameters) !== 'undefined' && apiInfo.parameters !== null) {
var ptd = $("
| ");
var ptable = $('
');
var table2 = $("
");
table2.append($(""));
ptable.append(table2);
$.each(apiInfo.parameters, function (i, param) {
//判断是否是ref,如果是,列出他的属性说明
var refflag = false;
//判断是否有type属性,如果有,则后端为实体类形参
var ptype = "string";
if (param.hasOwnProperty("type")) {
ptype = param["type"];
} else {
///判断是有schma
if (param.hasOwnProperty("schema")) {
var schema = param["schema"];
//是否有type
if (schema.hasOwnProperty("type")) {
ptype = schema["type"];
} else if (schema.hasOwnProperty("$ref")) {
//是否是ref
var regex = new RegExp("#/definitions/(.*)$", "ig");
if (regex.test(schema["$ref"])) {
refflag = true;
ptype = RegExp.$1;
}
}
}
}
var ptr = null;
//列出属性
if (refflag) {
ptr = $('
' + param.name + '' + DApiUI.getStringValue(param['description']) + '' + ptype + '' + DApiUI.getStringValue(param['in']) + '' + param['required'] + '');
ptr.find("span:first-child").attr({
"data-parent": '#accordion',
'data-toggle': 'collapse',
'href': "#" + param.name
}).css({"color":"#428BCA","cursor":"pointer"})
.append($("
"));
table2.append(ptr);
var definitionsArray = DApiUI.getDoc().data("definitionsArray");
var mcs = DApiUI.getMenuConstructs();
for (var k in mcs.definitions) {
if (ptype === k) {
var tp = mcs.definitions[ptype];
var props = tp["properties"];
var $div = $("
")
for (var prop in props) {
var pvalue = props[prop];
var tr = $("
");
tr.append($("
" + prop + ""));
tr.append($("
" + DApiUI.toString(pvalue.description, "") + ""));
var type = DApiUI.toString(pvalue.type, "string");
tr.append($("
" + type + ""));
tr.append($("
" + DApiUI.getStringValue(param['in']) + ""));
tr.append($("
" + param['required'] + ""));
$div.append(tr);
}
table2.append($div);
}
}
} else {
ptr = $('
' + param.name + '' + DApiUI.getStringValue(param['description']) + '' + ptype + '' + DApiUI.getStringValue(param['in']) + '' + param['required'] + '');
// ptr = $('
' + param.name + ' | ' + DApiUI.getStringValue(param['description']) + ' | ' + ptype + ' | ' + DApiUI.getStringValue(param['in']) + ' | ' + param['required'] + ' |
');
table2.append(ptr);
}
});
table2.append($('
填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充'));
ptable.append(table2);
ptd.append(ptable);
args.append(ptd);
} else {
args.append($('
暂无 | '));
}
tbody.append(args);
//响应数据结构
var responseConstruct = $('
响应Model |
---|
');
var responseConstructtd = $('
| ');
responseConstructtd.append(DApiUI.createResponseDefinition(apiInfo));
responseConstruct.append(responseConstructtd);
tbody.append(responseConstruct);
//响应参数 add by xiaoymin 2017-8-20 16:17:18
var respParams = $('
响应参数说明 |
---|
');
var respPart = $('
| ');
respPart.append(DApiUI.createResponseDefinitionDetail(apiInfo));
respParams.append(respPart);
tbody.append(respParams);
//响应状态码
var response = $('
响应 |
---|
');
if (typeof (apiInfo.responses) !== 'undefined' && apiInfo.responses !== null) {
var resp = apiInfo.responses;
var ptd = $("
| ");
var ptable = $('
');
var phead = $('
状态码 | 说明 | schema | ');
ptable.append(phead);
var pbody = $('
');
if (resp.hasOwnProperty("200")) {
var ptr = $('
200 | http响应成功 | |
');
pbody.append(ptr);
}
//400
pbody.append($('
400 | Bad Request 请求出现语法错误,一般是请求参数不对 | |
'));
//404
pbody.append($('
404 | Not Found 无法找到指定位置的资源 | |
'));
//401
pbody.append($('
401 | Unauthorized 访问被拒绝 | |
'));
//403
pbody.append($('
403 | Forbidden 资源不可用 | |
'));
//500
pbody.append($('
500 | 服务器内部错误,请联系Java后台开发人员!!! | |
'));
ptable.append(pbody);
ptd.append(ptable);
response.append(ptd);
} else {
response.append($("
暂无 | "));
}
tbody.append(response);
table.append(tbody);
DApiUI.creatabTab();
//内容覆盖
//查找接口doc
//DApiUI.getDoc().find("#tab1").find(".panel-body").html("");
//DApiUI.getDoc().find("#tab1").find(".panel-body").append(table);
DApiUI.getDoc().find("#tab1").html("");
DApiUI.getDoc().find("#tab1").append(table);
};
/***
* 响应参数详情
* @param apiInfo
*/
DApiUI.createResponseDefinitionDetail = function (apiInfo) {
var resp = apiInfo.responses;
var div = $("
");
if (resp.hasOwnProperty("200")) {
var ok = resp["200"];
if (ok.hasOwnProperty("schema")) {
var schema = ok["schema"];
var ref = schema["$ref"];
var regex = new RegExp("#/definitions/(.*)$", "ig");
if (regex.test(ref)) {
var refType = RegExp.$1;
var definitionsArray = DApiUI.getDoc().data("definitionsArray");
var mcs = DApiUI.getMenuConstructs();
for (var k in mcs.definitions) {
if (refType === k) {
var table = $("
");
table.append("");
var tp = mcs.definitions[refType];
var props = tp["properties"];
for (var prop in props) {
var pvalue = props[prop];
var tr = $("
");
//只遍历一级属性
//判断是否是ref
if (pvalue.hasOwnProperty("items") || pvalue.hasOwnProperty("$ref")) {
var param_ref = (pvalue["items"] && pvalue["items"]["$ref"]) || pvalue["$ref"];
var regex1 = new RegExp("#/definitions/(.*)$", "ig");
if (regex1.test((param_ref))) {
var type = DApiUI.toString(pvalue.type, "string");
var ptype = param_ref.slice(param_ref.lastIndexOf("/") + 1);
tr.append($("
" + prop + ""));
tr.append($("
" + type + ""));
tr.append($("
" + pvalue.description + ""));
tr.find("span:first-child").attr({
"data-parent": '#accordion',
'data-toggle': 'collapse',
'href': "#" + prop
}).css({"color":"#428BCA","cursor":"pointer"})
.append("
");
table.append(tr);
for (var j in mcs.definitions) {
if (ptype === j) {
var tpp = mcs.definitions[ptype];
var pp_props = tpp["properties"];
var $div = $("
")
for (var prop1 in pp_props) {
var tr1 = $("
");
var pvalue1 = pp_props[prop1];
tr1.append($("
" + prop1 + ""));
tr1.append($("
" + DApiUI.getValue(pvalue1, "type", "string", true) + ""));
tr1.append($("
" + DApiUI.getValue(pvalue1, "description", "", true) + ""));
$div.append(tr1);
}
table.append($div);
}
}
}
} else {
tr.append($("
" + prop + ""));
var type = DApiUI.toString(pvalue.type, "string");
tr.append($("
" + type + ""));
tr.append($("
" + DApiUI.toString(pvalue.description, "") + ""));
table.append(tr);
}
}
table.append($('
填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充填充'));
div.append(table)
}
}
}
}
}
return div;
};
DApiUI.createResponseDefinition = function (apiInfo) {
var resp = apiInfo.responses;
var div = $("
暂无
");
if (resp.hasOwnProperty("200")) {
var ok = resp["200"];
if (ok.hasOwnProperty("schema")) {
var schema = ok["schema"];
var ref = schema["$ref"];
var regex = new RegExp("#/definitions/(.*)$", "ig");
if (regex.test(ref)) {
var refType = RegExp.$1;
//这里去definitionsArrar查找,如果未找到,直接展示refType
var flag = false;
var htmlValue = refType;
var definitionsArray = DApiUI.getDoc().data("definitionsArray");
for (var i = 0; i < definitionsArray.length; i++) {
var definition = definitionsArray[i];
if (definition.key === refType) {
flag = true;
htmlValue = definition.value;
break;
}
}
div.html("");
if (flag) {
div.JSONView(htmlValue);
} else {
div.html(refType);
}
} else {
//未发现ref属性
if (schema.hasOwnProperty("type")) {
div.html("");
div.html(schema["type"]);
}
}
}
}
return div;
};
DApiUI.definitions = function (menu) {
var definitionsArray = [];
DApiUI.log("definitionsArray....");
if (menu !== null && typeof (menu) !== "undefined" && menu.hasOwnProperty("definitions")) {
var definitions = menu["definitions"];
for (var definition in definitions) {
var defiType = new definitionType();
defiType.key = definition;
//获取value
var value = definitions[definition];
if (checkUndefined(value)) {
//是否有properties
if (value.hasOwnProperty("properties")) {
var properties = value["properties"];
var defiTypeValue = {};
for (var property in properties) {
var propobj = properties[property];
//默认string类型
var propValue = "";
//判断是否有类型
if (propobj.hasOwnProperty("type")) {
var type = propobj["type"];
//判断是否有example
if (propobj.hasOwnProperty("example")) {
propValue = propobj["example"];
} else if (checkIsBasicType(type)) {
propValue = getBasicTypeValue(type);
} else {
if (type === "array") {
propValue = [];
var items = propobj["items"];
var ref = items["$ref"];
var regex = new RegExp("#/definitions/(.*)$", "ig");
if (regex.test(ref)) {
var refType = RegExp.$1;
//这里需要递归判断是否是本身,如果是,则退出递归查找
if (refType !== definition) {
propValue.push(findRefDefinition(refType, definitions));
}
}
}
}
} else {
if (propobj.hasOwnProperty("$ref")) {
var ref = propobj["$ref"];
var regex = new RegExp("#/definitions/(.*)$", "ig");
if (regex.test(ref)) {
var refType = RegExp.$1;
//这里需要递归判断是否是本身,如果是,则退出递归查找
if (refType !== definition) {
propValue = findRefDefinition(refType, definitions);
} else {
propValue = {};
}
}
} else {
propValue = {};
}
}
defiTypeValue[property] = propValue;
}
defiType.value = defiTypeValue;
} else {
defiType.value = {};
}
}
definitionsArray.push(defiType);
}
}
DApiUI.getDoc().data("definitionsArray", definitionsArray);
};
DApiUI.getDefinitions = function () {
return DApiUI.getDoc().data("definitionsArray");
};
function checkIsBasicType(type) {
var basicTypes = ["string", "integer", "number", "object", "boolean"];
var flag = false;
if ($.inArray(type, basicTypes) > -1) {
flag = true;
}
return flag;
}
function getBasicTypeValue(type) {
var propValue = "";
//是否是基本类型
if (type === "integer") {
propValue = 0;
}
if (type === "boolean") {
propValue = true;
}
if (type === "object") {
propValue = {};
}
if (type === "number") {
propValue = parseFloat(0);
}
return propValue;
}
function findRefDefinition(definitionName, definitions) {
var defaultValue = "";
for (var definition in definitions) {
if (definitionName === definition) {
var value = definitions[definition];
//是否有properties
if (value.hasOwnProperty("properties")) {
var properties = value["properties"];
var defiTypeValue = {};
for (var property in properties) {
var propobj = properties[property];
//默认string类型
var propValue = "";
//判断是否有类型
if (propobj.hasOwnProperty("type")) {
var type = propobj["type"];
//判断是否有example
if (propobj.hasOwnProperty("example")) {
propValue = propobj["example"];
} else if (checkIsBasicType(type)) {
propValue = getBasicTypeValue(type);
} else {
if (type === "array") {
propValue = [];
var items = propobj["items"];
var ref = items["$ref"];
//var regex = new RegExp("#/definitions/(.*)$", "ig");
//if (regex.test(ref)) {
var refType = RegExp.$1;
if (refType !== definitionName) {
propValue.push(findRefDefinition(refType, definitions));
}
//}
}
}
} else {
}
defiTypeValue[property] = propValue;
}
defaultValue = defiTypeValue;
} else {
defaultValue = {};
}
}
}
return defaultValue;
}
function checkUndefined(obj) {
var flag = false;
if (obj !== null && typeof (obj) !== "undefined") {
flag = true;
}
return flag;
}
function definitionType() {
this.key = "";
this.value = {};
}
/***
* 获取默认请求参数类型
* @param obj
* @returns {string}
*/
DApiUI.getDefaultRequiredType = function (obj) {
var t = "string";
if (typeof (obj) !== 'undefined' && obj !== null) {
t = obj.toString();
}
return t;
};
/***
* 查找子类
* @param tagInfo
* @param menu
*/
DApiUI.initChildrens = function (tagInfo, menu) {
};
DApiUI.getDoc = function () {
return $("#content");
};
DApiUI.getMenu = function () {
return $("#menu");
};
DApiUI.log = function (msg) {
if (window.console) {
console.log(msg);
}
};
DApiUI.init();
/***
* 标签组信息
* @constructor
*/
function TagInfo(name, description) {
this.name = name;
this.description = description;
this.childrens = [];
}
/***
* api实体信息
* @param options
* @constructor
*/
function ApiInfo(options) {
//判断options
this.tag = "";
this.url = "";
this.description = "";
this.operationId = "";
this.parameters = [];
this.produces = [];
this.responses = {};
this.methodType = "post";
this.consumes = [];
this.summary = "";
if (options !== null && typeof (options) !== 'undefined') {
this.tag = options.tags[0];
this.description = options.description;
this.operationId = options.operationId;
this.summary = options.summary;
this.parameters = options.parameters;
this.produces = options.produces;
this.responses = options.responses;
this.consumes = options.consumes;
}
}
})(jQuery);