194 lines
4.6 KiB
JavaScript
194 lines
4.6 KiB
JavaScript
const fs = require('fs');
|
||
const path = require('path');
|
||
|
||
const root = process.cwd();
|
||
const methodsDir = path.join(root, 'app', 'service', 'qimenMethods');
|
||
const outputPath = path.join(root, 'methods', 'qimen.json');
|
||
|
||
function walk(dir) {
|
||
const out = [];
|
||
for (const name of fs.readdirSync(dir)) {
|
||
const full = path.join(dir, name);
|
||
const stat = fs.statSync(full);
|
||
if (stat.isDirectory()) {
|
||
out.push(...walk(full));
|
||
} else if (name.endsWith('.js')) {
|
||
out.push(full);
|
||
}
|
||
}
|
||
return out;
|
||
}
|
||
|
||
function extractAsyncMethods(content) {
|
||
const methods = [];
|
||
const classMatch = content.match(/class\s+[A-Za-z_$][\w$]*\s+extends\s+[A-Za-z_$][\w$]*\s*\{([\s\S]*)\}\s*module\.exports\s*=\s*/);
|
||
if (!classMatch) {
|
||
return methods;
|
||
}
|
||
|
||
const body = classMatch[1];
|
||
const regex = /async\s+([A-Za-z_$][\w$]*)\s*\(/g;
|
||
let m = regex.exec(body);
|
||
while (m) {
|
||
if (m[1] !== 'constructor') {
|
||
methods.push(m[1]);
|
||
}
|
||
m = regex.exec(body);
|
||
}
|
||
|
||
return Array.from(new Set(methods));
|
||
}
|
||
|
||
function extractRequestKeys(content) {
|
||
const keys = new Set();
|
||
const patterns = [
|
||
/data\.request(?:\?|)\.(\w+)/g,
|
||
/data\?\.request\?\.(\w+)/g,
|
||
];
|
||
|
||
for (const re of patterns) {
|
||
let m = re.exec(content);
|
||
while (m) {
|
||
const key = m[1];
|
||
if (key) keys.add(key);
|
||
m = re.exec(content);
|
||
}
|
||
}
|
||
|
||
return Array.from(keys).sort((a, b) => a.localeCompare(b));
|
||
}
|
||
|
||
function buildMethodItem(fullMethod, requestKeys) {
|
||
const params = [
|
||
{
|
||
name: 'app_key',
|
||
type: 'string',
|
||
required: true,
|
||
source: 'query',
|
||
description: 'app_key 参数',
|
||
},
|
||
{
|
||
name: 'method',
|
||
type: 'string',
|
||
required: true,
|
||
source: 'query',
|
||
description: 'method 参数',
|
||
},
|
||
{
|
||
name: 'v',
|
||
type: 'string',
|
||
required: true,
|
||
source: 'query',
|
||
description: 'v 参数',
|
||
},
|
||
{
|
||
name: 'format',
|
||
type: 'string',
|
||
required: true,
|
||
source: 'query',
|
||
description: 'format 参数',
|
||
},
|
||
{
|
||
name: 'sign_method',
|
||
type: 'string',
|
||
required: true,
|
||
source: 'query',
|
||
description: 'sign_method 参数',
|
||
},
|
||
{
|
||
name: 'sign',
|
||
type: 'string',
|
||
required: true,
|
||
source: 'query',
|
||
description: 'sign 参数',
|
||
},
|
||
{
|
||
name: 'timestamp',
|
||
type: 'string',
|
||
required: true,
|
||
source: 'query',
|
||
description: 'timestamp 参数',
|
||
},
|
||
{
|
||
name: 'request',
|
||
type: 'object',
|
||
required: true,
|
||
source: 'body',
|
||
description: 'request XML 节点',
|
||
},
|
||
];
|
||
|
||
for (const key of requestKeys) {
|
||
params.push({
|
||
name: `request.${key}`,
|
||
type: 'string',
|
||
required: false,
|
||
source: 'body',
|
||
description: `request.${key} 参数`,
|
||
});
|
||
}
|
||
|
||
return {
|
||
method: fullMethod,
|
||
description: `${fullMethod} 接口`,
|
||
isNeedAuth: false,
|
||
isNeedLogin: false,
|
||
headers: [],
|
||
params,
|
||
response: [],
|
||
errors: [],
|
||
};
|
||
}
|
||
|
||
function main() {
|
||
if (!fs.existsSync(methodsDir)) {
|
||
throw new Error(`目录不存在: ${methodsDir}`);
|
||
}
|
||
|
||
const files = walk(methodsDir).sort((a, b) => a.localeCompare(b));
|
||
const groupMap = new Map();
|
||
|
||
for (const file of files) {
|
||
const content = fs.readFileSync(file, 'utf8');
|
||
const rel = path.relative(methodsDir, file).replace(/\\/g, '/').replace(/\.js$/, '');
|
||
const moduleName = rel.replace(/\//g, '.');
|
||
const methodNames = extractAsyncMethods(content);
|
||
const requestKeys = extractRequestKeys(content);
|
||
|
||
if (!groupMap.has(moduleName)) {
|
||
groupMap.set(moduleName, []);
|
||
}
|
||
|
||
for (const methodName of methodNames) {
|
||
const fullMethod = `${moduleName}.${methodName}`;
|
||
groupMap.get(moduleName).push(buildMethodItem(fullMethod, requestKeys));
|
||
}
|
||
}
|
||
|
||
const groups = Array.from(groupMap.entries())
|
||
.map(([ moduleName, list ]) => ({
|
||
groupName: `${moduleName} 模块`,
|
||
module: moduleName,
|
||
list: list.sort((a, b) => a.method.localeCompare(b.method)),
|
||
}))
|
||
.sort((a, b) => a.module.localeCompare(b.module));
|
||
|
||
const now = new Date().toISOString().slice(0, 10);
|
||
const doc = {
|
||
module: 'qimen',
|
||
description: '奇门接口标准入口,method 对应 qimenMethods 异步方法,全部使用 POST 请求,参数包含 query 与 XML body(request 节点)',
|
||
version: '0.3.0',
|
||
updatedAt: now,
|
||
headers: [],
|
||
methods: {
|
||
groups,
|
||
},
|
||
};
|
||
|
||
fs.writeFileSync(outputPath, JSON.stringify(doc, null, 2) + '\n', 'utf8');
|
||
|
||
const methodCount = groups.reduce((sum, group) => sum + group.list.length, 0);
|
||
console.log(`WROTE methods/qimen.json, groups=${groups.length}, methods=${methodCount}`);
|
||
}
|
||
|
||
main(); |