You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
SOP/sop-website/website-front/pages/doc/doc.js

194 lines
6.8 KiB

layui.config({
base: '../../assets/lib/layuiext/module/'
}).extend({
treetable: 'treetable-lay/treetable'
}).use(['element', 'form', 'treetable'], function(){ //加载code模块
6 years ago
var form = layui.form;
var treetable = layui.treetable;
var $ = layui.jquery;
6 years ago
// key:module
var docItemStore = {};
6 years ago
function initDocModules() {
$.getJSON(SopConfig.url + '/doc/getDocBaseInfo', function (baseInfo) {
var html = [];
var docInfoList = baseInfo.docInfoList;
for (var i = 0; i < docInfoList.length; i++) {
var docInfo = docInfoList[i];
6 years ago
var selected = i === 0 ? 'selected="selected"' : '';
var title = docInfo.title;
html.push('<option value="' + title + '" ' + selected + '>' + title + '</option>');
6 years ago
}
$('#moduleList').html(html.join(''));
$('.url-prod').text(baseInfo.urlProd);
6 years ago
form.render('select');
if (docInfoList && docInfoList.length > 0) {
selectDocInfo(docInfoList[0].title);
6 years ago
}
})
}
function selectDocInfo(title) {
$.getJSON(SopConfig.url + '/doc/docinfo/' + title, function (docInfo) {
var moduleList = docInfo.docModuleList;
var html = [];
var firstItem;
for (var j = 0; j < moduleList.length; j++) {
var module = moduleList[j];
var docItems = module.docItems;
html.push('<li><h2>' + module.module + '</h2></li>');
for (var i = 0; i < docItems.length; i++) {
var docItem = docItems[i];
var first = j == 0 && j == 0;
if (first) {
firstItem = docItem;
}
docItemStore[docItem.nameVersion] = docItem;
/*
<li class="site-tree-noicon layui-this">
<a href="/">
<cite>统一收单交易退款查询</cite>
</a>
</li>
*/
html.push('<li class="site-tree-noicon" nameversion="'+docItem.nameVersion+'">');
html.push('<a href="#"><cite>'+docItem.summary+'</cite></a>')
}
6 years ago
}
6 years ago
$('#docItemTree').html(html.join(''));
if (firstItem) {
6 years ago
selectDocItem(firstItem.nameVersion);
6 years ago
}
})
}
function initEvent() {
form.on('select(moduleListFilter)', function (data) {
selectDocInfo(data.value);
6 years ago
})
$('#docItemTree').on('click', 'li', function () {
var $li = $(this);
selectDocItem($li.attr('nameversion'));
6 years ago
})
}
6 years ago
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);
6 years ago
6 years ago
createRequestParameter(docItem);
createResponseParameter(docItem);
createResponseCode(docItem);
var $li = $('#docItemTree').find('li[nameversion="'+nameVersion+'"]');
$li.addClass('layui-this').siblings().removeClass('layui-this');
6 years ago
}
function createRequestParameter(docItem) {
var data = buildTreeData(docItem.requestParameters);
createTreeTable('treeTableReq', data);
6 years ago
}
function createResponseParameter(docItem) {
var data = buildTreeData(docItem.responseParameters);
createTreeTable('treeTableResp', data);
6 years ago
}
function buildTreeData(parameters, parentId) {
var data = [];
parentId = parentId || 0;
6 years ago
for (var i = 0; i < parameters.length; i++) {
var parameter = parameters[i];
parameter.id = parentId * 100 + (i + 1);
parameter.parentId = parentId;
data.push(parameter);
var refs = parameter.refs;
if (refs && refs.length > 0) {
var childData = buildTreeData(refs, parameter.id);
data = data.concat(childData);
}
6 years ago
}
return data;
}
function createTreeTable(id, data) {
var el = '#' + id;
$(el).text('');
treetable.render({
elem: el,
treeColIndex: 0,
treeSpid: 0,
treeIdName: 'id',
treePidName: 'parentId',
treeDefaultClose: false,
treeLinkage: false,
data: data,
page: false,
cols: [[
{field: 'name', title: '参数'}
,{field: 'type', title: '类型', width: 80}
,{field: 'required', title: '是否必填', width: 100, templet:function (row) {
return row.required ? '<span style="color: red;">是</span>' : '否';
}}
,{field: 'maxLength', title: '最大长度', width: 100}
,{field: 'description', title: '描述', width: 200}
,{field: 'paramExample', title: '示例值', width: 200}
]]
});
6 years ago
}
function createResponseCode(docItem) {
var method = docItem.name.replace(/\./g, '_');
var responseParameters = docItem.responseParameters;
var bizResult = buildResult(responseParameters);
6 years ago
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);
}
function buildResult(parameters) {
var result = [];
for (var i = 0; i < parameters.length; i++) {
var parameter = parameters[i];
result.push('\"'+parameter.name+'\": ' + buildExample(parameter))
}
return result.join(",");
}
function buildExample(parameter) {
var refs = parameter.refs;
if (refs) {
return '{' + buildResult(refs) + '}';
} else {
return '\"' + parameter.example + '\"';
}
}
6 years ago
initDocModules();
initEvent();
});