78 lines
2.8 KiB
JavaScript
78 lines
2.8 KiB
JavaScript
const XLSX = require('xlsx');
|
|
const pool = require('./db');
|
|
const path = require('path');
|
|
|
|
async function importTemplates() {
|
|
try {
|
|
const filePath = path.join(__dirname, '../Moka_全量API接口.xlsx');
|
|
console.log('读取文件:', filePath);
|
|
|
|
const workbook = XLSX.readFile(filePath);
|
|
console.log(`共 ${workbook.SheetNames.length} 个 Sheet\n`);
|
|
|
|
let totalSuccess = 0;
|
|
let totalError = 0;
|
|
|
|
for (const sheetName of workbook.SheetNames) {
|
|
console.log(`\n=== 处理 Sheet: ${sheetName} ===`);
|
|
const sheet = workbook.Sheets[sheetName];
|
|
const data = XLSX.utils.sheet_to_json(sheet);
|
|
console.log(`共 ${data.length} 条记录`);
|
|
|
|
let successCount = 0;
|
|
let errorCount = 0;
|
|
|
|
for (const row of data) {
|
|
try {
|
|
// Excel 列名映射
|
|
const module = sheetName; // 使用 Sheet 名称作为业务模块
|
|
const name = row['接口名称'] || '';
|
|
const description = row['接口说明'] || null;
|
|
const url = row['请求地址'] || '';
|
|
const body = row['请求体 Request Body'] || null;
|
|
|
|
// 默认值
|
|
const category = '旗舰版PP'; // 默认分类
|
|
const method = 'POST'; // 默认 POST
|
|
const apiCode = null;
|
|
const userName = null;
|
|
|
|
if (!name || !url) {
|
|
console.log(' 跳过无效记录 (缺少名称或URL):', name || '未命名');
|
|
errorCount++;
|
|
continue;
|
|
}
|
|
|
|
await pool.query(
|
|
`INSERT INTO endpoint_templates
|
|
(name, category, module, api_code, user_name, url, method, body, description)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
[name, category, module, apiCode, userName, url, method, body, description]
|
|
);
|
|
|
|
successCount++;
|
|
console.log(` ✓ ${name}`);
|
|
} catch (err) {
|
|
errorCount++;
|
|
console.error(` ✗ 导入失败:`, err.message);
|
|
}
|
|
}
|
|
|
|
console.log(`${sheetName}: 成功 ${successCount} 条, 失败 ${errorCount} 条`);
|
|
totalSuccess += successCount;
|
|
totalError += errorCount;
|
|
}
|
|
|
|
console.log('\n========== 导入完成 ==========');
|
|
console.log(`总成功: ${totalSuccess} 条`);
|
|
console.log(`总失败: ${totalError} 条`);
|
|
|
|
process.exit(0);
|
|
} catch (error) {
|
|
console.error('导入失败:', error);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
importTemplates();
|