This commit is contained in:
azw 2023-09-03 14:23:23 +08:00
parent 31dc304525
commit 3e9bc62466
3 changed files with 82 additions and 7 deletions

View File

@ -10,9 +10,12 @@ module.exports = class {
'desc': '用户登录',
'group': 'User',
'url': 'api/v1/user/login',
'is_json_params': true,
'params': [
new common.ReqHead(),
['account', '', '账号'],
['passwd', '', '密码']
['passwd', '', '密码'],
['!test', [0], '密码']
],
'response': [
new common.RspHead(),
@ -28,6 +31,7 @@ module.exports = class {
],
'response': [
new common.RspHead(),
['rsp', new common.RspHead(), 'ok rsp']
]
},
];

View File

@ -2,6 +2,8 @@ exports.ReqHead = class {
constructor() {
this.fields = [
['account_id', 0, '账号id'],
['session_id', '', '会话id'],
];
}

View File

@ -1,6 +1,7 @@
const parseArgs = require('minimist');
const fs = require('fs');
const assert = require('assert');
const util = require('util');
const { resolve } = require('path');
const IDENT = ' ';
@ -18,7 +19,8 @@ class MyDoc {
async convert() {
this.env = fs.readFileSync(`${this.workDir}env.json`, 'utf8');
this.data = `
<?php5
<?php
class Doc
{
@ -34,30 +36,97 @@ class Doc
this.convertClass(new c());
}
});
this.data += `\n}`;
console.log(this.data);
}
async convertClass(c) {
convertClass(c) {
c.apis.forEach(
(item) => {
const method = item.method ? 'GET' : item.method;
this.data += `
/**
* @api ${method} ${item.url}
* @api {${method}} ${item.url}
* @apiPermission
* @apiGroup ${item.group}
* @apiVersion 1.0
* @apiDescription ${item.desc}
`;
if (item.is_json_params) {
this.data += ` * @apiParamExample {json} 请求样例:\n`;
this.data += ` * {\n`;
this.asJsonOut(item.params, 1);
this.data += ` * }\n`;
} else {
this.asParamOut(item.params);
}
this.data += ` */
`;
}
);
console.log(this.data);
}
getFieldType(val) {
if (typeof val == 'string') {
return 'String';
} else if (typeof val == 'number') {
return 'Number';
}
return 'String';
}
asParamOut(itemList) {
itemList.forEach(
(item) => {
if (util.isArray(item)) {
const paramType = this.getFieldType(item[1]);
this.data += ` * @apiParam ${paramType} ${item[0]} ${item[2]}\n`;
} else {
this.asParamOut(item.fields);
}
}
);
}
getJsonValue(val) {
if (typeof val == 'number') {
return 0;
} else {
return '""';
}
}
asJsonOut(itemList, ident) {
itemList.forEach(
(item) => {
const curIdent = (new Array(ident * 4)).fill(' ').join('');
if (util.isArray(item[1])) {
if (item[0].slice(0,1) == '!') {
const realFieldName = item[0].slice(1);
this.data += ` * ${curIdent}'${realFieldName}': [ // struct ${item[2]}\n`;
if (item[1].length == 1) {
if (util.isArray(item[1][0])) {
this.data += ` * ${curIdent + IDENT}'${realFieldName}': [ // struct ${item[2]}\n`;
} else {
this.data += ` * ${curIdent + IDENT}${this.getJsonValue(item[1])}\n`;
}
} else {
}
this.data += ` * ${curIdent}]\n`;
} else {
this.data += ` * ${curIdent}'${item[0]}': { // struct ${item[2]}\n`;
}
} else {
if (['string', 'number'].indexOf(typeof item[1]) >= 0) {
const paramType = this.getFieldType(item[1]);
this.data += ` * ${curIdent}'${item[0]}': ${this.getJsonValue(item[1])}, //${item[2]}\n`;
} else {
console.log(11111111111);
this.asJsonOut(item.fields, ident);
}
}
}
);
}
}