2018-03-06 15:31:46 +08:00
|
|
|
|
const schema = require('./shema-transformTo-table.js');
|
2018-03-06 11:35:09 +08:00
|
|
|
|
const _ = require('underscore');
|
|
|
|
|
|
2018-07-31 19:45:01 +08:00
|
|
|
|
const json_parse = function(json) {
|
|
|
|
|
try {
|
2018-04-09 16:14:55 +08:00
|
|
|
|
return JSON.parse(json);
|
2018-07-31 19:45:01 +08:00
|
|
|
|
} catch (err) {
|
2018-04-09 16:14:55 +08:00
|
|
|
|
return {};
|
|
|
|
|
}
|
2018-07-31 19:45:01 +08:00
|
|
|
|
};
|
2018-07-12 14:41:51 +08:00
|
|
|
|
// 处理字符串换行
|
2018-07-31 19:45:01 +08:00
|
|
|
|
const handleWrap = str => {
|
|
|
|
|
return _.isString(str) ? str.replace(/\n/gi, '<br/>') : str;
|
|
|
|
|
};
|
2018-03-06 11:35:09 +08:00
|
|
|
|
const messageMap = {
|
|
|
|
|
desc: '备注',
|
|
|
|
|
default: '实例',
|
|
|
|
|
maximum: '最大值',
|
|
|
|
|
minimum: '最小值',
|
|
|
|
|
maxItems: '最大数量',
|
|
|
|
|
minItems: '最小数量',
|
|
|
|
|
maxLength: '最大长度',
|
|
|
|
|
minLength: '最小长度',
|
|
|
|
|
uniqueItems: '元素是否都不同',
|
|
|
|
|
itemType: 'item 类型',
|
2018-03-06 17:37:40 +08:00
|
|
|
|
format: 'format',
|
2018-07-12 14:41:51 +08:00
|
|
|
|
enum: '枚举',
|
2019-04-26 16:00:25 +08:00
|
|
|
|
enumDesc: '枚举备注',
|
|
|
|
|
mock: 'mock'
|
2018-03-06 15:31:46 +08:00
|
|
|
|
};
|
2018-03-06 11:35:09 +08:00
|
|
|
|
|
|
|
|
|
const columns = [
|
|
|
|
|
{
|
|
|
|
|
title: '名称',
|
|
|
|
|
dataIndex: 'name',
|
|
|
|
|
key: 'name'
|
2018-03-06 15:31:46 +08:00
|
|
|
|
},
|
|
|
|
|
{
|
2018-03-06 11:35:09 +08:00
|
|
|
|
title: '类型',
|
|
|
|
|
dataIndex: 'type',
|
|
|
|
|
key: 'type'
|
2018-03-06 15:31:46 +08:00
|
|
|
|
},
|
|
|
|
|
{
|
2018-03-06 11:35:09 +08:00
|
|
|
|
title: '是否必须',
|
|
|
|
|
dataIndex: 'required',
|
|
|
|
|
key: 'required'
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
title: '默认值',
|
|
|
|
|
dataIndex: 'default',
|
|
|
|
|
key: 'default'
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
title: '备注',
|
|
|
|
|
dataIndex: 'desc',
|
|
|
|
|
key: 'desc'
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
title: '其他信息',
|
|
|
|
|
dataIndex: 'sub',
|
|
|
|
|
key: 'sub'
|
2018-03-06 15:31:46 +08:00
|
|
|
|
}
|
|
|
|
|
];
|
2018-02-08 17:52:58 +08:00
|
|
|
|
|
|
|
|
|
function escapeStr(str, isToc) {
|
|
|
|
|
return isToc ? escape(str) : str;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function createBaseMessage(basepath, inter) {
|
|
|
|
|
// 基本信息
|
2018-03-06 15:31:46 +08:00
|
|
|
|
let baseMessage = `### 基本信息\n\n**Path:** ${basepath + inter.path}\n\n**Method:** ${
|
|
|
|
|
inter.method
|
2019-04-26 16:00:25 +08:00
|
|
|
|
}\n\n**接口描述:**\n${_.isUndefined(inter.desc) ? '' : inter.desc}\n`;
|
2018-02-08 17:52:58 +08:00
|
|
|
|
return baseMessage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function createReqHeaders(req_headers) {
|
|
|
|
|
// Request-headers
|
|
|
|
|
if (req_headers && req_headers.length) {
|
|
|
|
|
let headersTable = `**Headers**\n\n`;
|
|
|
|
|
headersTable += `| 参数名称 | 参数值 | 是否必须 | 示例 | 备注 |\n| ------------ | ------------ | ------------ | ------------ | ------------ |\n`;
|
|
|
|
|
for (let j = 0; j < req_headers.length; j++) {
|
2018-03-06 15:31:46 +08:00
|
|
|
|
headersTable += `| ${req_headers[j].name || ''} | ${req_headers[j].value || ''} | ${
|
|
|
|
|
req_headers[j].required == 1 ? '是' : '否'
|
2018-07-31 19:45:01 +08:00
|
|
|
|
} | ${handleWrap(req_headers[j].example) || ''} | ${handleWrap(req_headers[j].desc) ||
|
|
|
|
|
''} |\n`;
|
2018-02-08 17:52:58 +08:00
|
|
|
|
}
|
|
|
|
|
return headersTable;
|
|
|
|
|
}
|
2018-03-06 15:31:46 +08:00
|
|
|
|
return '';
|
2018-02-08 17:52:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function createPathParams(req_params) {
|
|
|
|
|
if (req_params && req_params.length) {
|
2018-07-11 19:27:17 +08:00
|
|
|
|
let paramsTable = `**路径参数**\n\n`;
|
2019-04-12 20:19:04 +08:00
|
|
|
|
paramsTable += `| 参数名称 | 示例 | 备注 |\n| ------------ | ------------ | ------------ |\n`;
|
2018-02-08 17:52:58 +08:00
|
|
|
|
for (let j = 0; j < req_params.length; j++) {
|
2018-07-12 14:41:51 +08:00
|
|
|
|
paramsTable += `| ${req_params[j].name || ''} | ${handleWrap(req_params[j].example) ||
|
|
|
|
|
''} | ${handleWrap(req_params[j].desc) || ''} |\n`;
|
2018-02-08 17:52:58 +08:00
|
|
|
|
}
|
|
|
|
|
return paramsTable;
|
|
|
|
|
}
|
2018-03-06 15:31:46 +08:00
|
|
|
|
return '';
|
2018-02-08 17:52:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function createReqQuery(req_query) {
|
|
|
|
|
if (req_query && req_query.length) {
|
|
|
|
|
let headersTable = `**Query**\n\n`;
|
|
|
|
|
headersTable += `| 参数名称 | 是否必须 | 示例 | 备注 |\n| ------------ | ------------ | ------------ | ------------ |\n`;
|
|
|
|
|
for (let j = 0; j < req_query.length; j++) {
|
2018-03-06 15:31:46 +08:00
|
|
|
|
headersTable += `| ${req_query[j].name || ''} | ${
|
|
|
|
|
req_query[j].required == 1 ? '是' : '否'
|
2018-07-31 19:45:01 +08:00
|
|
|
|
} | ${handleWrap(req_query[j].example) || ''} | ${handleWrap(req_query[j].desc) ||
|
|
|
|
|
''} |\n`;
|
2018-02-08 17:52:58 +08:00
|
|
|
|
}
|
|
|
|
|
return headersTable;
|
|
|
|
|
}
|
2018-03-06 15:31:46 +08:00
|
|
|
|
return '';
|
2018-02-08 17:52:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-06 15:31:46 +08:00
|
|
|
|
function createReqBody(req_body_type, req_body_form, req_body_other, req_body_is_json_schema) {
|
|
|
|
|
if (req_body_type === 'form' && req_body_form.length) {
|
|
|
|
|
let bodyTable = `**Body**\n\n`;
|
|
|
|
|
bodyTable += `| 参数名称 | 参数类型 | 是否必须 | 示例 | 备注 |\n| ------------ | ------------ | ------------ | ------------ | ------------ |\n`;
|
|
|
|
|
let req_body = req_body_form;
|
2018-02-08 17:52:58 +08:00
|
|
|
|
for (let j = 0; j < req_body.length; j++) {
|
2018-03-06 15:31:46 +08:00
|
|
|
|
bodyTable += `| ${req_body[j].name || ''} | ${req_body[j].type || ''} | ${
|
|
|
|
|
req_body[j].required == 1 ? '是' : '否'
|
|
|
|
|
} | ${req_body[j].example || ''} | ${req_body[j].desc || ''} |\n`;
|
2018-02-08 17:52:58 +08:00
|
|
|
|
}
|
|
|
|
|
return `${bodyTable}\n\n`;
|
2018-03-06 15:31:46 +08:00
|
|
|
|
} else if (req_body_other) {
|
|
|
|
|
if (req_body_is_json_schema) {
|
|
|
|
|
let reqBody = createSchemaTable(req_body_other);
|
|
|
|
|
return `**Body**\n\n` + reqBody;
|
|
|
|
|
} else {
|
|
|
|
|
//other
|
|
|
|
|
return `**Body**\n\n` + '```javascript' + `\n${req_body_other || ''}` + '\n```';
|
|
|
|
|
}
|
2018-02-08 17:52:58 +08:00
|
|
|
|
}
|
2018-03-06 15:31:46 +08:00
|
|
|
|
return '';
|
2018-02-08 17:52:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-06 11:35:09 +08:00
|
|
|
|
function tableHeader(columns) {
|
|
|
|
|
let header = ``;
|
|
|
|
|
columns.map(item => {
|
2018-03-06 15:31:46 +08:00
|
|
|
|
header += `<th key=${item.key}>${item.title}</th>`;
|
|
|
|
|
});
|
2018-03-06 11:35:09 +08:00
|
|
|
|
|
2018-03-06 15:31:46 +08:00
|
|
|
|
return header;
|
2018-03-06 11:35:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-06 15:31:46 +08:00
|
|
|
|
function handleObject(text) {
|
|
|
|
|
if (!_.isObject(text)) {
|
|
|
|
|
return text;
|
2018-03-06 11:35:09 +08:00
|
|
|
|
}
|
2018-03-06 15:31:46 +08:00
|
|
|
|
let tpl = ``;
|
|
|
|
|
Object.keys(text || {}).map((item, index) => {
|
|
|
|
|
let name = messageMap[item];
|
|
|
|
|
let value = text[item];
|
|
|
|
|
tpl += _.isUndefined(text[item])
|
|
|
|
|
? ''
|
|
|
|
|
: `<p key=${index}><span style="font-weight: '700'">${name}: </span><span>${value.toString()}</span></p>`;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return tpl;
|
2018-03-06 11:35:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-06 15:31:46 +08:00
|
|
|
|
function tableCol(col, columns, level) {
|
2018-03-06 11:35:09 +08:00
|
|
|
|
let tpl = ``;
|
2018-03-06 15:31:46 +08:00
|
|
|
|
columns.map((item, index) => {
|
2018-03-06 11:35:09 +08:00
|
|
|
|
let dataIndex = item.dataIndex;
|
2018-03-06 15:31:46 +08:00
|
|
|
|
let value = col[dataIndex];
|
|
|
|
|
value = _.isUndefined(value) ? '' : value;
|
2018-03-06 11:35:09 +08:00
|
|
|
|
let text = ``;
|
2018-03-06 15:31:46 +08:00
|
|
|
|
|
|
|
|
|
switch (dataIndex) {
|
|
|
|
|
case 'sub':
|
|
|
|
|
text = handleObject(value);
|
|
|
|
|
break;
|
2018-03-06 11:35:09 +08:00
|
|
|
|
case 'type':
|
2018-03-06 15:31:46 +08:00
|
|
|
|
text =
|
2018-07-31 19:45:01 +08:00
|
|
|
|
value === 'array'
|
|
|
|
|
? `<span>${col.sub ? col.sub.itemType || '' : 'array'} []</span>`
|
|
|
|
|
: `<span>${value}</span>`;
|
2018-03-06 15:31:46 +08:00
|
|
|
|
break;
|
2018-03-06 11:35:09 +08:00
|
|
|
|
case 'required':
|
2018-03-06 15:31:46 +08:00
|
|
|
|
text = value ? '必须' : '非必须';
|
|
|
|
|
break;
|
2018-03-06 11:35:09 +08:00
|
|
|
|
case 'desc':
|
2018-03-06 15:31:46 +08:00
|
|
|
|
text = _.isUndefined(col.childrenDesc)
|
2018-07-12 14:41:51 +08:00
|
|
|
|
? `<span style="white-space: pre-wrap">${value}</span>`
|
|
|
|
|
: `<span style="white-space: pre-wrap">${col.childrenDesc}</span>`;
|
2018-03-06 15:31:46 +08:00
|
|
|
|
break;
|
|
|
|
|
case 'name':
|
2018-07-31 19:45:01 +08:00
|
|
|
|
text = `<span style="padding-left: ${20 * level}px"><span style="color: #8c8a8a">${
|
|
|
|
|
level > 0 ? '├─' : ''
|
|
|
|
|
}</span> ${value}</span>`;
|
2018-03-06 15:31:46 +08:00
|
|
|
|
break;
|
2018-03-06 11:35:09 +08:00
|
|
|
|
default:
|
2018-03-06 15:31:46 +08:00
|
|
|
|
text = value;
|
2018-03-06 11:35:09 +08:00
|
|
|
|
}
|
2018-03-06 15:31:46 +08:00
|
|
|
|
tpl += `<td key=${index}>${text}</td>`;
|
|
|
|
|
});
|
2018-03-06 11:35:09 +08:00
|
|
|
|
|
|
|
|
|
return tpl;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-06 15:31:46 +08:00
|
|
|
|
function tableBody(dataSource, columns, level) {
|
|
|
|
|
// 按照columns的顺序排列数据
|
|
|
|
|
let tpl = ``;
|
|
|
|
|
dataSource.map(col => {
|
|
|
|
|
let child = null;
|
|
|
|
|
tpl += `<tr key=${col.key}>${tableCol(col, columns, level)}</tr>`;
|
|
|
|
|
if (!_.isUndefined(col.children) && _.isArray(col.children)) {
|
2018-07-31 19:45:01 +08:00
|
|
|
|
let index = level + 1;
|
2018-03-06 15:31:46 +08:00
|
|
|
|
child = tableBody(col.children, columns, index);
|
|
|
|
|
}
|
|
|
|
|
tpl += child ? `${child}` : ``;
|
|
|
|
|
});
|
2018-03-06 11:35:09 +08:00
|
|
|
|
|
2018-03-06 15:31:46 +08:00
|
|
|
|
return tpl;
|
2018-03-06 11:35:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-06 15:31:46 +08:00
|
|
|
|
function createSchemaTable(body) {
|
2018-03-06 11:35:09 +08:00
|
|
|
|
let template = ``;
|
2018-04-09 16:14:55 +08:00
|
|
|
|
let dataSource = schema.schemaTransformToTable(json_parse(body));
|
2018-03-06 11:35:09 +08:00
|
|
|
|
template += `<table>
|
|
|
|
|
<thead class="ant-table-thead">
|
|
|
|
|
<tr>
|
2018-03-06 15:31:46 +08:00
|
|
|
|
${tableHeader(columns)}
|
2018-03-06 11:35:09 +08:00
|
|
|
|
</tr>
|
2018-03-06 15:31:46 +08:00
|
|
|
|
</thead>`;
|
2018-03-06 11:35:09 +08:00
|
|
|
|
|
2018-03-06 15:31:46 +08:00
|
|
|
|
template += `<tbody className="ant-table-tbody">${tableBody(dataSource, columns, 0)}
|
2018-03-06 11:35:09 +08:00
|
|
|
|
</tbody>
|
|
|
|
|
</table>
|
2018-03-06 15:31:46 +08:00
|
|
|
|
`;
|
2018-03-06 11:35:09 +08:00
|
|
|
|
|
2018-03-06 15:31:46 +08:00
|
|
|
|
return template;
|
2018-03-06 11:35:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-17 20:10:00 +08:00
|
|
|
|
function createResponse(res_body, res_body_is_json_schema, res_body_type) {
|
2018-03-06 15:31:46 +08:00
|
|
|
|
let resTitle = `\n### 返回数据\n\n`;
|
2018-02-08 17:52:58 +08:00
|
|
|
|
if (res_body) {
|
2018-05-17 20:10:00 +08:00
|
|
|
|
if (res_body_is_json_schema && res_body_type === 'json') {
|
2018-03-06 15:31:46 +08:00
|
|
|
|
let resBody = createSchemaTable(res_body);
|
2018-03-06 11:35:09 +08:00
|
|
|
|
return resTitle + resBody;
|
|
|
|
|
} else {
|
2018-03-06 15:31:46 +08:00
|
|
|
|
let resBody = '```javascript' + `\n${res_body || ''}\n` + '```';
|
2018-03-06 11:35:09 +08:00
|
|
|
|
return resTitle + resBody;
|
|
|
|
|
}
|
2018-02-08 17:52:58 +08:00
|
|
|
|
}
|
2018-03-06 15:31:46 +08:00
|
|
|
|
return '';
|
2018-02-08 17:52:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-06 15:31:46 +08:00
|
|
|
|
function createInterMarkdown(basepath, listItem, isToc) {
|
2018-02-08 17:52:58 +08:00
|
|
|
|
let mdTemplate = ``;
|
|
|
|
|
const toc = `[TOC]\n\n`;
|
|
|
|
|
// 接口名称
|
|
|
|
|
mdTemplate += `\n## ${escapeStr(`${listItem.title}\n<a id=${listItem.title}> </a>`, isToc)}\n`;
|
2018-03-06 15:31:46 +08:00
|
|
|
|
isToc && (mdTemplate += toc);
|
2018-02-08 17:52:58 +08:00
|
|
|
|
// 基本信息
|
|
|
|
|
mdTemplate += createBaseMessage(basepath, listItem);
|
|
|
|
|
// Request
|
2018-03-06 15:31:46 +08:00
|
|
|
|
mdTemplate += `\n### 请求参数\n`;
|
2018-02-08 17:52:58 +08:00
|
|
|
|
// Request-headers
|
|
|
|
|
mdTemplate += createReqHeaders(listItem.req_headers);
|
|
|
|
|
// Request-params
|
|
|
|
|
mdTemplate += createPathParams(listItem.req_params);
|
|
|
|
|
// Request-query
|
|
|
|
|
mdTemplate += createReqQuery(listItem.req_query);
|
|
|
|
|
// Request-body
|
2018-03-06 15:31:46 +08:00
|
|
|
|
mdTemplate += createReqBody(
|
|
|
|
|
listItem.req_body_type,
|
|
|
|
|
listItem.req_body_form,
|
|
|
|
|
listItem.req_body_other,
|
|
|
|
|
listItem.req_body_is_json_schema
|
|
|
|
|
);
|
2018-02-08 17:52:58 +08:00
|
|
|
|
// Response
|
|
|
|
|
// Response-body
|
2018-07-31 19:45:01 +08:00
|
|
|
|
mdTemplate += createResponse(
|
|
|
|
|
listItem.res_body,
|
|
|
|
|
listItem.res_body_is_json_schema,
|
|
|
|
|
listItem.res_body_type
|
|
|
|
|
);
|
2018-02-08 17:52:58 +08:00
|
|
|
|
|
|
|
|
|
return mdTemplate;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-19 20:02:16 +08:00
|
|
|
|
function createProjectMarkdown(curProject, wikiData) {
|
2018-02-08 17:52:58 +08:00
|
|
|
|
let mdTemplate = ``;
|
2018-03-06 15:31:46 +08:00
|
|
|
|
// 项目名、项目描述
|
|
|
|
|
let title = `<h1 class="curproject-name"> ${curProject.name} </h1>`;
|
2018-07-31 19:45:01 +08:00
|
|
|
|
|
2018-03-06 15:31:46 +08:00
|
|
|
|
mdTemplate += `\n ${title} \n ${curProject.desc || ''}\n\n`;
|
2018-02-08 17:52:58 +08:00
|
|
|
|
|
2018-07-19 20:02:16 +08:00
|
|
|
|
// 增加公共wiki信息展示
|
2018-08-01 11:17:22 +08:00
|
|
|
|
mdTemplate += wikiData ? `\n### 公共信息\n${wikiData.desc || ''}\n` : '';
|
2018-03-06 15:31:46 +08:00
|
|
|
|
return mdTemplate;
|
2018-02-08 17:52:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function createClassMarkdown(curProject, list, isToc) {
|
|
|
|
|
let mdTemplate = ``;
|
|
|
|
|
const toc = `[TOC]\n\n`;
|
|
|
|
|
list.map(item => {
|
|
|
|
|
// 分类名称
|
|
|
|
|
mdTemplate += `\n# ${escapeStr(item.name, isToc)}\n`;
|
2018-03-06 15:31:46 +08:00
|
|
|
|
isToc && (mdTemplate += toc);
|
|
|
|
|
for (let i = 0; i < item.list.length; i++) {
|
|
|
|
|
//循环拼接 接口
|
2018-02-08 17:52:58 +08:00
|
|
|
|
// 接口内容
|
|
|
|
|
mdTemplate += createInterMarkdown(curProject.basepath, item.list[i], isToc);
|
|
|
|
|
}
|
2018-03-06 15:31:46 +08:00
|
|
|
|
});
|
2018-02-08 17:52:58 +08:00
|
|
|
|
return mdTemplate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let r = {
|
|
|
|
|
createInterMarkdown,
|
|
|
|
|
createProjectMarkdown,
|
|
|
|
|
createClassMarkdown
|
2018-03-06 15:31:46 +08:00
|
|
|
|
};
|
2018-02-08 17:52:58 +08:00
|
|
|
|
|
|
|
|
|
module.exports = r;
|