From 3e9bc624663bbc48c55624a0e22849a2917611c9 Mon Sep 17 00:00:00 2001 From: azw Date: Sun, 3 Sep 2023 14:23:23 +0800 Subject: [PATCH] q2 --- doc/admin/User.js | 6 +++- doc/admin/common.js | 2 ++ tools/mydoc/app.js | 81 +++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 82 insertions(+), 7 deletions(-) diff --git a/doc/admin/User.js b/doc/admin/User.js index c7e37329..feeefcbc 100644 --- a/doc/admin/User.js +++ b/doc/admin/User.js @@ -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'] ] }, ]; diff --git a/doc/admin/common.js b/doc/admin/common.js index beff7597..d9ae44d7 100644 --- a/doc/admin/common.js +++ b/doc/admin/common.js @@ -2,6 +2,8 @@ exports.ReqHead = class { constructor() { this.fields = [ + ['account_id', 0, '账号id'], + ['session_id', '', '会话id'], ]; } diff --git a/tools/mydoc/app.js b/tools/mydoc/app.js index ea9f78de..ce626b0d 100644 --- a/tools/mydoc/app.js +++ b/tools/mydoc/app.js @@ -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 = ` - { 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); + } + } + } + ); } }