/** * 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 = [ '
', '
', '需要验证的接口,需要在header里添加AuthorizationToken', '
', '
', '
' ].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('
'); itemHtmls.push('

'); itemHtmls.push('' + _item.supportProtocal + ''); itemHtmls.push('' + _item.text + ""); itemHtmls.push('' + _item.requestMap + ''); if(_item.unControllRightFlag){ itemHtmls.push('无需认证'); } itemHtmls.push('

'); itemHtmls.push('
'); //处理具体的内容 itemHtmls.push('参数
'); if(_item.params){ itemHtmls.push('
'); //有参数 var paramHtml = []; layui.each(_item.params,function(__index,param){ paramHtml.push('
'); var requestName = param.requestName; if($webUtil.isNull(requestName)){ requestName = param.name; } if(param.requestBody){ paramHtml.push('
'); paramHtml.push('contentType需要设置为"application/json",并且提交的数据必须为json格式的字符串
'); paramHtml.push('
');
                                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('
'); paramHtml.push('
'); }else if(param.sessionValue || param.cookieValue ){ paramHtml.push('
'); paramHtml.push('本参数无需传递,是由后台自行处理'); paramHtml.push('
'); }else{ if(param.paramBasicType){ //说明是基础类型 paramHtml.push('
'); paramHtml.push(requestName); paramHtml.push('
'); paramHtml.push('
'); paramHtml.push(that.javaTypeMap[param.classType]); paramHtml.push('
'); paramHtml.push('
'); paramHtml.push(param.text); paramHtml.push('
'); }else{ paramHtml.push('
'); paramHtml.push(requestName); paramHtml.push('
'); paramHtml.push('
'); paramHtml.push(param.text); paramHtml.push('
'); paramHtml.push('
'); paramHtml.push('
');
                                    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('
'); paramHtml.push('
'); } } paramHtml.push('
'); }); itemHtmls.push(paramHtml.join('')); itemHtmls.push('
'); } if(_item.returns){ itemHtmls.push('返回值:' + _item.returns.text + '
'); itemHtmls.push('
'); itemHtmls.push('
'); itemHtmls.push('
'); //无论是基础类型,还是自定义类型,返回值都是BaseResult itemHtmls.push('
');
                        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('
'); itemHtmls.push('
'); itemHtmls.push('
'); itemHtmls.push('
'); } itemHtmls.push('
'); itemHtmls.push('
'); }); $("#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); });