Files
flutter_learning/scripts/import-sets.js
T
2026-08-04 18:07:40 +08:00

39 lines
1.1 KiB
JavaScript

const fs = require('fs');
const path = require('path');
const BASE = 'http://localhost:3030';
const FILES = ['set-dart-dev.json', 'set-flutter-dev.json'];
async function main() {
const loginRes = await fetch(BASE + '/api/admin/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username: 'admin', password: 'admin123' })
});
const login = await loginRes.json();
if (!loginRes.ok) throw new Error('登录失败: ' + login.error);
for (const file of FILES) {
const data = JSON.parse(fs.readFileSync(path.join(__dirname, 'data', file), 'utf8'));
const res = await fetch(BASE + '/api/admin/import', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: 'Bearer ' + login.token
},
body: JSON.stringify(data)
});
const result = await res.json();
if (!res.ok) {
console.log(`${data.name} 导入失败: ${result.error}`);
} else {
console.log(`${data.name}: 导入 ${result.imported} 题, 套题 id=${result.set.id}`);
}
}
}
main().catch(e => {
console.error(e.message);
process.exit(1);
});