/**
|
* web调用接口方法
|
* @author weidy
|
*/
|
layui.define(['layer','element','form','table','util','element'],function(exports){
|
var Class = function(){
|
this.MODELNAME = "platform/objectService/OsWebMethods";
|
this.moduleKey = "OsWebMethods";
|
this.id='OsWebMethods';
|
this.sourceData={};
|
this.columns = [];
|
this.backPath = configData.compatibility?path:configData.objectServicePath;
|
this.url={
|
controller:'webApiController/',
|
listMethods:'gridWebApiMethodByApiOid'
|
};
|
this.buttonIconMap = {
|
SEARCH:'layui-icon-refresh-2',
|
SENIORSEARCH:'layui-icon-query',
|
ADD:'layui-icon-add-1',
|
EDIT:'layui-icon-edit',
|
DELETE:'layui-icon-delete'
|
};
|
this.prototalColor = {
|
'POST':'GREEN',
|
'GET':'BLUE',
|
'PUT':'GRAY',
|
'DELETE':'RED'
|
};
|
this.javaTypeMap = {
|
'java.lang.String':'字符串',
|
'java.lang.Integer' :'数字',
|
'int':'数字',
|
'java.lang.Long':'长数字',
|
'long':'长数字',
|
'java.lang.Double':'高精度数字',
|
'double':'高精度数字',
|
'short':'数字',
|
'java.lang.Short':'数字',
|
'float':'浮点数',
|
'java.lang.Float':'浮点数',
|
'java.util.Date':'日期/时间'
|
};
|
this.getContent=function(){//返回这个组件的基础html
|
var that = this;
|
var html = "";
|
html = [
|
'<div class="layui-layout-border">',
|
'<div class="layui-center">',
|
'<span>需要验证的接口,需要在header里添加AuthorizationToken</span>',
|
'<div class="layui-collapse" id="collapse_',that.id,'"></div>',
|
'</div>',
|
'</div>'
|
].join("");
|
return html;
|
};
|
this.init=function(){//基础的html被添加后,再执行初始化
|
var that = this;
|
$webUtil.copyConfig(that,that.moduleKey);
|
var table = layui.table;
|
if(!that.sourceData || !("apiOid" in that.sourceData) || $webUtil.isNull(that.sourceData['apiOid'])){
|
$webUtil.showErrorMsg("没有传递api的对象");
|
}
|
var apiOid = that.sourceData['apiOid'];
|
$webUtil.post(that.url.controller + that.url.listMethods,{apiOid:apiOid},function(result){
|
if(result.success){
|
that.showMethods(result.obj) ;
|
}else{
|
$webUtil.showErrorMsg(result.msg);
|
}
|
},function(xhr,error){
|
$webUtil.showErrorMsg("获取接口方法出现了错误,可能是服务没有启动");
|
},that.backPath,false,false);
|
};
|
this.showMethods = function(data){
|
var that = this;
|
$("#collapse_" + that.id).html('');
|
if(data && data.length > 0){
|
var itemHtmls = [];
|
layui.each(data,function(_index,_item){
|
itemHtmls.push('<div class="layui-colla-item">');
|
itemHtmls.push('<h2 class="layui-colla-title">');
|
itemHtmls.push('<span style="font-size: 10px;color:#fff;font-weight: 700;min-width:80px; padding: 6px 15px;text-align: center;border-radius: 3px;background: #000;background-color:'
|
+ that.getPrototalColor(_item) + ';" >' + _item.supportProtocal + '</span>');
|
itemHtmls.push('<span style="margin-left:20px">' + _item.text + "</span>");
|
itemHtmls.push('<span style="margin-left:100px;">' + _item.requestMap + '</span>');
|
if(_item.unControllRightFlag){
|
itemHtmls.push('<span style="margin-left:50px;">无需认证</span>');
|
}
|
itemHtmls.push('</h2>');
|
itemHtmls.push('<div class="layui-colla-content ' + (_index == 0?'layui-show':'') + '">');
|
//处理具体的内容
|
itemHtmls.push('<span>参数</span><hr class="layui-bg-red"></hr>');
|
if(_item.params){
|
itemHtmls.push('<div class="layui-container" style=" margin-left: 5px;">');
|
//有参数
|
var paramHtml = [];
|
layui.each(_item.params,function(__index,param){
|
paramHtml.push('<div class="layui-row">');
|
var requestName = param.requestName;
|
if($webUtil.isNull(requestName)){
|
requestName = param.name;
|
}
|
if(param.requestBody){
|
paramHtml.push('<div class="layui-col-md12">');
|
paramHtml.push('contentType需要设置为"application/json",并且提交的数据必须为json格式的字符串<br/>');
|
paramHtml.push('<pre class="layui-code">');
|
if('java.lang.List' == param.classType){
|
paramHtml.push("[" + JSON.stringify(that.fieldToShowText(param.paramClassFields)) + "]");
|
}else{
|
paramHtml.push(JSON.stringify(that.fieldToShowText(param.paramClassFields)) );
|
}
|
paramHtml.push('</pre>');
|
paramHtml.push('</div>');
|
}else if(param.sessionValue || param.cookieValue ){
|
paramHtml.push('<div class="layui-col-md12">');
|
paramHtml.push('本参数无需传递,是由后台自行处理');
|
paramHtml.push('</div>');
|
}else{
|
if(param.paramBasicType){
|
//说明是基础类型
|
paramHtml.push('<div class="layui-col-md3">');
|
paramHtml.push(requestName);
|
paramHtml.push('</div>');
|
paramHtml.push('<div class="layui-col-md4">');
|
paramHtml.push(that.javaTypeMap[param.classType]);
|
paramHtml.push('</div>');
|
paramHtml.push('<div class="layui-col-md5">');
|
paramHtml.push(param.text);
|
paramHtml.push('</div>');
|
}else{
|
paramHtml.push('<div class="layui-col-md3">');
|
paramHtml.push(requestName);
|
paramHtml.push('</div>');
|
paramHtml.push('<div class="layui-col-md3">');
|
paramHtml.push(param.text);
|
paramHtml.push('</div>');
|
paramHtml.push('<div class="layui-col-md6">');
|
paramHtml.push('<pre class="layui-code">');
|
if('java.lang.List' == param.classType){
|
paramHtml.push("[" + JSON.stringify(that.fieldToShowText(param.paramClassFields,null,4)) + "]");
|
}else{
|
paramHtml.push(JSON.stringify(that.fieldToShowText(param.paramClassFields),null,4) );
|
}
|
paramHtml.push('</pre>');
|
paramHtml.push('</div>');
|
}
|
}
|
paramHtml.push('</div>');
|
});
|
itemHtmls.push(paramHtml.join(''));
|
itemHtmls.push('</div>');
|
}
|
if(_item.returns){
|
itemHtmls.push('<span>返回值:' + _item.returns.text + '</span><hr class="layui-bg-blue"></hr>');
|
itemHtmls.push('<div class="layui-container" style=" margin-left: 5px;">');
|
itemHtmls.push('<div class="layui-row">');
|
itemHtmls.push('<div class="layui-col-md12">');
|
//无论是基础类型,还是自定义类型,返回值都是BaseResult
|
itemHtmls.push('<pre class="layui-code">');
|
var baseResult = _item.returns.returnResult;
|
if(baseResult.obj){
|
baseResult.obj = that.fieldToShowText(baseResult.obj);
|
}else if(baseResult.data){
|
baseResult.data = that.fieldToShowText(baseResult.data);
|
}else if(baseResult.treeData){
|
baseResult.treeData = that.getTreeField();
|
}
|
that.switchBaseResult(baseResult);
|
itemHtmls.push(JSON.stringify(baseResult,null, 4));
|
itemHtmls.push('</pre>');
|
itemHtmls.push('</div>');
|
itemHtmls.push('</div>');
|
itemHtmls.push('</div>');
|
}
|
itemHtmls.push('</div>');
|
itemHtmls.push('</div>');
|
});
|
$("#collapse_" + that.id).html(itemHtmls.join(''));
|
layui.element.init();
|
}
|
};
|
this.switchBaseResult = function(result){
|
result.code = "状态码,200表示成功,401表示未权限,403表示数据格式不对,404表示路径不对,500是后台出现了未捕获的异常";
|
result.exceptionClassName = "出现异常的类的名称,只有后台调用其他服务的时候才使用";
|
result.exceptionCode="异常的编号,只有后台在返回值的上说明要使用这个属性判断业务异常的情况时,前端才读取";
|
result.exceptionObjs="异常的语句使用的对象,只有后台调用其他服务的是才使用";
|
result.finishTime ="性能测试使用,后台处理完成的时间";
|
result.msg="提示信息,在业务错误和业务成功时都可能使用到,某些接口业务成功不会返回此属性";
|
result.msgObjs = "转换提示消息的多语使用的源数据";
|
result.requestTraceId = "防止接口幂等性时的请求唯一ID";
|
result.success = "true表示执行成功,否则表示执行失败";
|
result.total = "总数,列表时使用";
|
result.traceId = "后台执行的唯一ID";
|
result.description = "这个属性后台不返回,是描述返回结果中的data,obj和treeData的用途的。如果是列表数据则一般取data,如果是树形数据则取treeData,其他操作取obj";
|
|
};
|
this.getTreeField = function(){
|
return {
|
oid:'字符串 树节点主键',
|
text:'字符串 树节点显示文本',
|
leaf:'布尔 是否叶子节点,默认为false',
|
showCheckbox:'布尔 是否显示复选框,默认为false',
|
checked:'布尔 是否默认选中,在showCheckbox为true时才生效,默认为false',
|
children:'列表对象 下级树节点',
|
icon:'字符串 图标路径,目前未启用',
|
iconCls:'字符串 图标样式',
|
parentId:'字符串 上级节点主键',
|
expanded:'布尔 是否默认展开当前节点,默认为false',
|
href:'字符串 点击树节点的链接地址',
|
index:'字符串 排序索引',
|
attributes:{
|
xxx:'attributes是这个树节点关联业务数据,map的形式,xxx是属性名'
|
}
|
};
|
};
|
this.getPrototalColor = function(_item){
|
var that = this;
|
if(!_item.supportProtocal){
|
_item.supportProtocal = '';
|
}
|
var color = that.prototalColor[_item.supportProtocal.toUpperCase()];
|
if(!color){
|
color = 'orange ';
|
}
|
return color;
|
};
|
this.fieldToShowText = function(fieldVOs,isList){
|
var that = this;
|
var fieldShow = {};
|
layui.each(fieldVOs,function(_index,field){
|
var showType = that.javaTypeMap[field.classType];
|
if(field.customClassType){
|
//说明不是基础的类型
|
var fieldShowForThis = that.fieldToShowText(field.customFields,field.fieldIsList);
|
fieldShow[field.name] = fieldShowForThis;
|
}else{
|
if(field.fieldIsList){
|
fieldShow[field.name] = (showType + " " + field.text + ",这个是列表提交");
|
}else if(field.fieldIsMap){
|
fieldShow[field.name] = (field.text + " 这个是map格式");
|
}else{
|
fieldShow[field.name] = (showType + " " + field.text);
|
}
|
}
|
});
|
if(isList){
|
return [fieldShow];
|
}else{
|
return fieldShow;
|
}
|
};
|
};
|
var cs = new Class();
|
exports(cs.MODELNAME,cs);
|
});
|