layui.use(['element', 'form'], function(){ //加载code模块 var form = layui.form; // key:module var docItemStore = {}; function initDocModules() { $.getJSON(SopConfig.url + '/doc/getDocBaseInfo', function (baseInfo) { var html = []; var modules = baseInfo.docModuleVOList; for (var i = 0; i < modules.length; i++) { var docDefinition = modules[i]; var module = docDefinition.module; var selected = i === 0 ? 'selected="selected"' : ''; html.push(''); } $('#moduleList').html(html.join('')); form.render('select'); if (modules && modules.length > 0) { selectModule(modules[0].module); } $('.url-prod').text(baseInfo.urlProd); }) } function selectModule(docModule) { $.getJSON(SopConfig.url + '/doc/module/' + docModule, function (module) { var docItems = module.docItems; var html = ['
  • ' + docModule + '

  • ']; for (var i = 0; i < docItems.length; i++) { var docItem = docItems[i]; docItemStore[docItem.nameVersion] = docItem; /*
  • 统一收单交易退款查询
  • */ var selectedClass = i === 0 ? 'layui-this' : ''; html.push('
  • '); html.push(''+docItem.summary+'') } $('#docItemTree').html(html.join('')); if (docItems && docItems.length > 0) { var firstItem = docItems[0]; selectDocItem(firstItem.nameVersion); } }) } function initEvent() { form.on('select(moduleListFilter)', function (data) { selectModule(data.value); }) $('#docItemTree').on('click', 'a', function () { var $tagA = $(this); selectDocItem($tagA.attr('nameversion')); $tagA.parent().addClass('layui-this').siblings().removeClass('layui-this'); }) } function selectDocItem(nameVersion) { var docItem = docItemStore[nameVersion]; $('.sop-name').text(docItem.name); $('.sop-version').text(docItem.version); $('.sop-summary').text(docItem.summary); $('.sop-description').text(docItem.description || docItem.summary); createRequestParameter(docItem); createResponseParameter(docItem); createResponseCode(docItem); } function createRequestParameter(docItem) { var html = createParameterBody(docItem.requestParameters); $('#requestTbody').html(html); } function createResponseParameter(docItem) { var html = createParameterBody(docItem.responseParameters); $('#responseTbody').html(html); } function createParameterBody(parameters) { /* 参数 类型 是否必填 最大长度 描述 示例值 */ var html = []; for (var i = 0; i < parameters.length; i++) { var parameter = parameters[i]; html.push('\n' + ' '+parameter.name+'\n' + ' '+parameter.type+'\n' + ' '+(parameter.required ? '' : '否')+'\n' + ' -\n' + ' '+parameter.description+'\n' + ' ' + (parameter.example || parameter['x-example']) +'\n' + '') } return html.join(''); } function createResponseCode(docItem) { var responseParameters = docItem.responseParameters; var method = docItem.name.replace(/\./g, '_'); var result = []; for (var i = 0; i < responseParameters.length; i++) { var responseParameter = responseParameters[i]; result.push('\"'+responseParameter.name+'\": \"' + responseParameter.example + '\"') } var bizResult = result.join(","); var json = '{\n' + ' "'+method+'_response": {\n' + ' "code": "10000",\n' + ' "msg": "Success",\n' + bizResult + ' },\n' + ' "sign": "xxxxxx"\n' + '}'; json = formatJson(json); $('#responseExampleJson').text(json); var errorJson = '{\n' + ' "'+method+'_response": {\n' + ' "code": "20000",\n' + ' "msg": "Service is temporarily unavailable",\n' + ' "sub_code": "isp.unknow-error",\n' + ' "sub_msg": "服务暂不可用"\n' + ' },\n' + ' "sign": "xxxxxxx"\n' + '}'; errorJson = formatJson(errorJson); $('#responseErrorJson').text(errorJson); } initDocModules(); initEvent(); });