mirror of
https://github.com/YMFE/yapi.git
synced 2024-12-09 05:00:30 +08:00
fix: 完善导出数据功能
This commit is contained in:
parent
5306ce36e9
commit
b8ff45e9c0
@ -9,5 +9,7 @@ module.exports = {
|
||||
name: 'import-swagger'
|
||||
},{
|
||||
name: 'statistics'
|
||||
},{
|
||||
name: 'export-data'
|
||||
}]
|
||||
}
|
27
exts/yapi-plugin-export-data/client.js
Normal file
27
exts/yapi-plugin-export-data/client.js
Normal file
@ -0,0 +1,27 @@
|
||||
|
||||
// import {message} from 'antd'
|
||||
|
||||
|
||||
function exportData(exportDataModule,pid){
|
||||
exportDataModule.markdown = {
|
||||
name: 'markdown',
|
||||
route: `/api/plugin/export?type=markdown&pid=${pid}`,
|
||||
desc: '导出项目接口文档为 markdown 文件'
|
||||
}
|
||||
exportDataModule.pdf = {
|
||||
name: 'pdf',
|
||||
route: `/api/plugin/export?type=pdf&pid=${pid}`,
|
||||
desc: '导出项目接口文档为 pdf 文件'
|
||||
}
|
||||
exportDataModule.html = {
|
||||
name: 'html',
|
||||
route: `/api/plugin/export?type=html&pid=${pid}`,
|
||||
desc: '导出项目接口文档为 html 文件'
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
module.exports = function(){
|
||||
this.bindHook('export_data', exportData)
|
||||
}
|
167
exts/yapi-plugin-export-data/controller.js
Normal file
167
exts/yapi-plugin-export-data/controller.js
Normal file
@ -0,0 +1,167 @@
|
||||
const baseController = require('controllers/base.js');
|
||||
const interfaceModel = require('models/interface.js');
|
||||
const yapi = require('yapi.js');
|
||||
const markdownIt = require("markdown-it");
|
||||
const markdownItAnchor = require("markdown-it-anchor");
|
||||
const markdownItTableOfContents = require("markdown-it-table-of-contents");
|
||||
const defaultTheme = require("./defaultTheme.js");
|
||||
class exportController extends baseController{
|
||||
constructor(ctx){
|
||||
super(ctx);
|
||||
this.interModel = yapi.getInst(interfaceModel);
|
||||
}
|
||||
|
||||
async exportData(ctx){
|
||||
let pid = ctx.request.query.pid;
|
||||
let type = ctx.request.query.type;
|
||||
if(!pid){
|
||||
ctx.body = yapi.commons.resReturn(null, 200, "pid 不为空");
|
||||
}
|
||||
let tp = "";
|
||||
switch(type){
|
||||
case "markdown": {
|
||||
tp = await createMarkdown.bind(this)(pid);
|
||||
break;
|
||||
}
|
||||
case "pdf": {
|
||||
|
||||
break;
|
||||
}
|
||||
default: {//默认为html
|
||||
let md = await createMarkdown.bind(this)(pid);
|
||||
let markdown = new markdownIt();
|
||||
markdown.use(markdownItAnchor); // Optional, but makes sense as you really want to link to something
|
||||
markdown.use(markdownItTableOfContents,{
|
||||
markerPattern: /^\[toc\]/im
|
||||
});
|
||||
tp = defaultTheme + markdown.render(md);
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
async function createMarkdown(pid){//拼接markdown
|
||||
//模板
|
||||
let mdTemplate = ``;
|
||||
const toc = `**目录 (Table of Contents)**
|
||||
|
||||
[TOC]
|
||||
|
||||
`;
|
||||
mdTemplate += toc;
|
||||
try{
|
||||
const interList = await this.interModel.listByPid(pid);
|
||||
for(let i = 0;i<interList.length;i++){
|
||||
// 接口名称
|
||||
mdTemplate += `
|
||||
## ${interList[i].title}
|
||||
`;
|
||||
// 基本信息
|
||||
mdTemplate += `### 基本信息
|
||||
|
||||
**接口名称:**${interList[i].title}
|
||||
|
||||
**接口路径:**${interList[i].path}
|
||||
|
||||
**接口描述:**
|
||||
|
||||
${interList[i].desc?interList[i].desc:""}
|
||||
|
||||
`;
|
||||
// Request-headers
|
||||
mdTemplate += `### Request
|
||||
`;
|
||||
if(interList[i].req_headers&&interList[i].req_headers.length){
|
||||
const req_headers = interList[i].req_headers;
|
||||
let headersTable = `**Headers:**
|
||||
|
||||
`;
|
||||
headersTable += `| 参数名称 | 参数值 | 是否必须 | 示例 | 备注 |
|
||||
| ------------ | ------------ | ------------ | ------------ | ------------ |
|
||||
`;
|
||||
for(let j = 0;j<req_headers.length;j++){
|
||||
headersTable += `| ${req_headers[j].name||""} | ${req_headers[j].value||""} | ${req_headers[j].required?"是":"否"} | ${req_headers[j].example||""} | ${req_headers[j].desc||""} |
|
||||
`;
|
||||
}
|
||||
mdTemplate += headersTable;
|
||||
}
|
||||
// Request-params
|
||||
if(interList[i].req_params&&interList[i].req_params.length){
|
||||
let req_params = interList[i].req_params;
|
||||
let paramsTable = `**路径参数**
|
||||
|
||||
`;
|
||||
paramsTable += `| 参数名称 | 示例 | 备注 |
|
||||
| ------------ | ------------ | ------------ | ------------ | ------------ |
|
||||
`;
|
||||
for(let j = 0;j<req_params.length;j++){
|
||||
paramsTable += `| ${req_params[j].name || ""} | ${req_params[j].example || ""} | ${req_params[j].desc||""} |
|
||||
`;
|
||||
}
|
||||
mdTemplate += paramsTable;
|
||||
}
|
||||
|
||||
// Request-query
|
||||
if(interList[i].req_query&&interList[i].req_query.length){
|
||||
const req_query = interList[i].req_query;
|
||||
let headersTable = `**Query**
|
||||
|
||||
`;
|
||||
headersTable += `| 参数名称 | 是否必须 | 示例 | 备注 |
|
||||
| ------------ | ------------ | ------------ | ------------ |
|
||||
`;
|
||||
for(let j = 0;j<req_query.length;j++){
|
||||
headersTable += `| ${req_query[j].name || ""} | ${req_query[j].required?"是":"否"} | ${req_query[j].example || ""} | ${req_query[j].desc||""} |
|
||||
`;
|
||||
}
|
||||
mdTemplate += headersTable;
|
||||
}
|
||||
// Request-body
|
||||
if(interList[i].req_body_type === "form" && interList[i].req_body_form.length){
|
||||
let bodyTable = `**Body:**
|
||||
|
||||
`
|
||||
bodyTable += `| 参数名称 | 参数类型 | 是否必须 | 示例 | 备注 |
|
||||
| ------------ | ------------ | ------------ | ------------ | ------------ |
|
||||
`; let req_body = interList[i].req_body_form;
|
||||
for(let j = 0;j<req_body.length;j++){
|
||||
bodyTable += `| ${req_body[j].name || ""} | ${req_body[j].required?"是":"否"} | ${req_body[j].example || ""} | ${req_body[j].desc||""} |
|
||||
|
||||
`;
|
||||
}
|
||||
mdTemplate += bodyTable;
|
||||
}else if(interList[i].res_body_other){//other
|
||||
mdTemplate += "```javascript"+
|
||||
`
|
||||
${interList[i].res_body_other || ""}
|
||||
`+
|
||||
"```";
|
||||
}
|
||||
// Response
|
||||
mdTemplate += `
|
||||
### Reponse
|
||||
|
||||
`;
|
||||
if(interList[i].res_body){
|
||||
mdTemplate += "```javascript"+
|
||||
`
|
||||
${interList[i].res_body || ""}
|
||||
`+
|
||||
"```";
|
||||
}
|
||||
}
|
||||
return mdTemplate;
|
||||
}catch(e){
|
||||
console.log(e);
|
||||
}
|
||||
}
|
||||
|
||||
ctx.set("Content-Type", "application/octet-stream");
|
||||
ctx.set("Content-Disposition",`attachment; filename=test.md`);
|
||||
|
||||
return ctx.body = tp;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = exportController;
|
148
exts/yapi-plugin-export-data/defaultTheme.js
Normal file
148
exts/yapi-plugin-export-data/defaultTheme.js
Normal file
@ -0,0 +1,148 @@
|
||||
module.exports = `<style>
|
||||
h1,
|
||||
h2,
|
||||
h3,
|
||||
h4,
|
||||
h5,
|
||||
h6,
|
||||
p,
|
||||
blockquote {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
body {
|
||||
font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", Arial, sans-serif;
|
||||
font-size: 13px;
|
||||
line-height: 18px;
|
||||
color: #737373;
|
||||
background-color: white;
|
||||
margin: 10px 13px 10px 13px;
|
||||
}
|
||||
table {
|
||||
margin: 10px 0 15px 0;
|
||||
border-collapse: collapse;
|
||||
}
|
||||
td,th {
|
||||
border: 1px solid #ddd;
|
||||
padding: 3px 10px;
|
||||
}
|
||||
th {
|
||||
padding: 5px 10px;
|
||||
}
|
||||
|
||||
a {
|
||||
color: #0069d6;
|
||||
}
|
||||
a:hover {
|
||||
color: #0050a3;
|
||||
text-decoration: none;
|
||||
}
|
||||
a img {
|
||||
border: none;
|
||||
}
|
||||
p {
|
||||
margin-bottom: 9px;
|
||||
}
|
||||
h1,
|
||||
h2,
|
||||
h3,
|
||||
h4,
|
||||
h5,
|
||||
h6 {
|
||||
color: #404040;
|
||||
line-height: 36px;
|
||||
}
|
||||
h1 {
|
||||
margin-bottom: 18px;
|
||||
font-size: 30px;
|
||||
}
|
||||
h2 {
|
||||
font-size: 24px;
|
||||
}
|
||||
h3 {
|
||||
font-size: 18px;
|
||||
}
|
||||
h4 {
|
||||
font-size: 16px;
|
||||
}
|
||||
h5 {
|
||||
font-size: 14px;
|
||||
}
|
||||
h6 {
|
||||
font-size: 13px;
|
||||
}
|
||||
hr {
|
||||
margin: 0 0 19px;
|
||||
border: 0;
|
||||
border-bottom: 1px solid #ccc;
|
||||
}
|
||||
blockquote {
|
||||
padding: 13px 13px 21px 15px;
|
||||
margin-bottom: 18px;
|
||||
font-family:georgia,serif;
|
||||
font-style: italic;
|
||||
}
|
||||
blockquote:before {
|
||||
font-size:40px;
|
||||
margin-left:-10px;
|
||||
font-family:georgia,serif;
|
||||
color:#eee;
|
||||
}
|
||||
blockquote p {
|
||||
font-size: 14px;
|
||||
font-weight: 300;
|
||||
line-height: 18px;
|
||||
margin-bottom: 0;
|
||||
font-style: italic;
|
||||
}
|
||||
code, pre {
|
||||
font-family: Monaco, Andale Mono, Courier New, monospace;
|
||||
}
|
||||
code {
|
||||
background-color: #fee9cc;
|
||||
color: rgba(0, 0, 0, 0.75);
|
||||
padding: 1px 3px;
|
||||
font-size: 12px;
|
||||
-webkit-border-radius: 3px;
|
||||
-moz-border-radius: 3px;
|
||||
border-radius: 3px;
|
||||
}
|
||||
pre {
|
||||
display: block;
|
||||
padding: 14px;
|
||||
margin: 0 0 18px;
|
||||
line-height: 16px;
|
||||
font-size: 11px;
|
||||
border: 1px solid #d9d9d9;
|
||||
white-space: pre-wrap;
|
||||
word-wrap: break-word;
|
||||
}
|
||||
pre code {
|
||||
background-color: #fff;
|
||||
color:#737373;
|
||||
font-size: 11px;
|
||||
padding: 0;
|
||||
}
|
||||
sup {
|
||||
font-size: 0.83em;
|
||||
vertical-align: super;
|
||||
line-height: 0;
|
||||
}
|
||||
* {
|
||||
-webkit-print-color-adjust: exact;
|
||||
}
|
||||
@media screen and (min-width: 914px) {
|
||||
body {
|
||||
width: 854px;
|
||||
margin:10px auto;
|
||||
}
|
||||
}
|
||||
@media print {
|
||||
body,code,pre code,h1,h2,h3,h4,h5,h6 {
|
||||
color: black;
|
||||
}
|
||||
table, pre {
|
||||
page-break-inside: avoid;
|
||||
}
|
||||
}
|
||||
</style>`
|
@ -8,8 +8,8 @@ module.exports = function(){
|
||||
addRouter({
|
||||
controller: controller,
|
||||
method: 'get',
|
||||
path: 'export/markdown',
|
||||
action: 'exportMarkdown'
|
||||
path: 'export',
|
||||
action: 'exportData'
|
||||
})
|
||||
})
|
||||
|
@ -8,7 +8,7 @@ module.exports = function(){
|
||||
addRouter({
|
||||
controller: controller,
|
||||
method: 'get',
|
||||
path: 'export/html',
|
||||
path: 'export/htdasml',
|
||||
action: 'exportHtml'
|
||||
})
|
||||
})
|
||||
|
@ -1,18 +0,0 @@
|
||||
|
||||
// import {message} from 'antd'
|
||||
|
||||
|
||||
function exportMarkdown(exportDataModule,...arg){
|
||||
let pid = arg[0];
|
||||
exportDataModule.markdown = {
|
||||
name: 'markdown',
|
||||
route: `/api/plugin/export/markdown?pid=${pid}`,
|
||||
desc: '导出项目接口文档为 markdown 文件'
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
module.exports = function(){
|
||||
this.bindHook('export_data', exportMarkdown)
|
||||
}
|
@ -1,96 +0,0 @@
|
||||
const baseController = require('controllers/base.js');
|
||||
const interfaceModel = require('models/interface.js');
|
||||
const yapi = require('yapi.js');
|
||||
|
||||
class exportMarkdownController extends baseController{
|
||||
constructor(ctx){
|
||||
super(ctx);
|
||||
this.interModel = yapi.getInst(interfaceModel);
|
||||
}
|
||||
|
||||
async exportMarkdown(ctx){
|
||||
var pid = ctx.request.query.pid;
|
||||
if(!pid){
|
||||
ctx.body = yapi.commons.resReturn(null, 200, "pid 不为空");
|
||||
}
|
||||
//模板
|
||||
let mdTemplate = ``;
|
||||
const toc = `**目录 (Table of Contents)**
|
||||
|
||||
[TOC]
|
||||
|
||||
`;
|
||||
mdTemplate += toc;
|
||||
try{
|
||||
const interList = await this.interModel.listByPid(pid);
|
||||
for(let i = 0;i<interList.length;i++){
|
||||
// 接口名称
|
||||
mdTemplate += `
|
||||
##${interList[i].title}
|
||||
`;
|
||||
// 基本信息
|
||||
mdTemplate += `### 基本信息
|
||||
** 接口名称:**${interList[i].title}
|
||||
**接口路径:**${interList[i].path}
|
||||
**接口描述:**
|
||||
|
||||
${interList[i].desc?interList[i].desc:""}
|
||||
|
||||
`;
|
||||
// Request-headers
|
||||
mdTemplate += `###Request
|
||||
`;
|
||||
if(interList[i].req_headers&&interList[i].req_headers.length){
|
||||
const req_headers = interList[i].req_headers;
|
||||
let headersTable = `**Headers:**
|
||||
|
||||
`;
|
||||
headersTable += `| 参数名称 | 参数值 | 是否必须 | 示例 | 备注 |
|
||||
| ------------ | ------------ | ------------ | ------------ | ------------ |
|
||||
`;
|
||||
for(let j = 0;j<req_headers.length;j++){
|
||||
headersTable += `| ${req_headers[j].name||""} | ${req_headers[j].value||""} | ${req_headers[j].required?"是":"否"} | ${req_headers[j].example||""} | ${req_headers[j].desc||""} |
|
||||
`;
|
||||
}
|
||||
mdTemplate += headersTable;
|
||||
}
|
||||
// Request-query
|
||||
if(interList[i].req_query&&interList[i].req_query.length){
|
||||
const req_query = interList[i].req_query;
|
||||
let headersTable = `**Query**
|
||||
|
||||
`;
|
||||
headersTable += `| 参数名称 | 是否必须 | 示例 | 备注 |
|
||||
| ------------ | ------------ | ------------ | ------------ |
|
||||
`;
|
||||
for(let j = 0;j<req_query.length;j++){
|
||||
headersTable += `| ${req_query[j].name || ""} | ${req_query[j].required?"是":"否"} | ${req_query[j].example || ""} | ${req_query[j].desc||""} |
|
||||
`;
|
||||
}
|
||||
mdTemplate += headersTable;
|
||||
}
|
||||
// Response
|
||||
mdTemplate += `###Reponse
|
||||
|
||||
`;
|
||||
if(interList[i].res_body){
|
||||
mdTemplate += "```javascript"+
|
||||
`
|
||||
${interList[i].res_body || ""}
|
||||
`+
|
||||
"```";
|
||||
}
|
||||
}
|
||||
}catch(e){
|
||||
console.log(e);
|
||||
}
|
||||
|
||||
ctx.set("Content-Type", "application/octet-stream");
|
||||
ctx.set("Content-Disposition",`attachment; filename=test.md`);
|
||||
|
||||
|
||||
return ctx.body = mdTemplate;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = exportMarkdownController;
|
@ -8,7 +8,7 @@ module.exports = function(){
|
||||
addRouter({
|
||||
controller: controller,
|
||||
method: 'get',
|
||||
path: 'export/pdf',
|
||||
path: 'export/psaddf',
|
||||
action: 'exportPdf'
|
||||
})
|
||||
})
|
||||
|
@ -36,6 +36,9 @@
|
||||
"koa-static": "^3.0.0",
|
||||
"koa-views": "^5.2.0",
|
||||
"koa-websocket": "^4.0.0",
|
||||
"markdown-it": "^8.4.0",
|
||||
"markdown-it-anchor": "^4.0.0",
|
||||
"markdown-it-table-of-contents": "^0.3.2",
|
||||
"mockjs": "^1.0.1-beta3",
|
||||
"moment": "^2.18.1",
|
||||
"mongoose": "4.7.0",
|
||||
|
Loading…
Reference in New Issue
Block a user