|
|
@@ -0,0 +1,100 @@
|
|
|
+#!/usr/bin/env node
|
|
|
+
|
|
|
+/**
|
|
|
+ * 批量添加测试机器数据
|
|
|
+ *
|
|
|
+ * 使用方法:
|
|
|
+ * 1. 在浏览器中登录系统
|
|
|
+ * 2. 打开开发者工具 -> Application -> Cookies
|
|
|
+ * 3. 复制 token 值
|
|
|
+ * 4. 运行: TOKEN=your_token_here node scripts/add-test-machines.mjs
|
|
|
+ */
|
|
|
+
|
|
|
+const API_BASE = 'https://tg-live-game.pwtk.cc/api'
|
|
|
+const TOKEN = process.env.TOKEN
|
|
|
+
|
|
|
+if (!TOKEN) {
|
|
|
+ console.error('❌ 请设置 TOKEN 环境变量')
|
|
|
+ console.log('\n使用方法:')
|
|
|
+ console.log(' TOKEN=your_token_here node scripts/add-test-machines.mjs')
|
|
|
+ console.log('\n获取 token:')
|
|
|
+ console.log(' 1. 登录系统')
|
|
|
+ console.log(' 2. 打开开发者工具 -> Application -> Cookies')
|
|
|
+ console.log(' 3. 复制 token 值')
|
|
|
+ process.exit(1)
|
|
|
+}
|
|
|
+
|
|
|
+// 位置列表
|
|
|
+const locations = [
|
|
|
+ '北京机房A区', '北京机房B区', '上海数据中心', '深圳机房',
|
|
|
+ '广州机房', '杭州数据中心', '成都机房', '武汉机房',
|
|
|
+ '南京数据中心', '西安机房'
|
|
|
+]
|
|
|
+
|
|
|
+// 生成测试机器数据
|
|
|
+function generateMachines(count) {
|
|
|
+ const machines = []
|
|
|
+ for (let i = 1; i <= count; i++) {
|
|
|
+ const paddedNum = String(i).padStart(3, '0')
|
|
|
+ machines.push({
|
|
|
+ machineId: `TEST_MACHINE_${paddedNum}`,
|
|
|
+ name: `测试机器 ${paddedNum}`,
|
|
|
+ location: locations[Math.floor(Math.random() * locations.length)],
|
|
|
+ description: `这是第 ${i} 台测试机器,用于系统功能验证`
|
|
|
+ })
|
|
|
+ }
|
|
|
+ return machines
|
|
|
+}
|
|
|
+
|
|
|
+// 添加单个机器
|
|
|
+async function addMachine(machine) {
|
|
|
+ const response = await fetch(`${API_BASE}/admin/machines/add`, {
|
|
|
+ method: 'POST',
|
|
|
+ headers: {
|
|
|
+ 'Content-Type': 'application/json',
|
|
|
+ 'Authorization': `Bearer ${TOKEN}`
|
|
|
+ },
|
|
|
+ body: JSON.stringify(machine)
|
|
|
+ })
|
|
|
+
|
|
|
+ if (!response.ok) {
|
|
|
+ throw new Error(`HTTP ${response.status}: ${response.statusText}`)
|
|
|
+ }
|
|
|
+
|
|
|
+ return response.json()
|
|
|
+}
|
|
|
+
|
|
|
+// 主函数
|
|
|
+async function main() {
|
|
|
+ console.log('🚀 开始添加测试机器数据...\n')
|
|
|
+
|
|
|
+ const machines = generateMachines(30)
|
|
|
+ let successCount = 0
|
|
|
+ let failCount = 0
|
|
|
+
|
|
|
+ for (const machine of machines) {
|
|
|
+ try {
|
|
|
+ const result = await addMachine(machine)
|
|
|
+ if (result.code === 0) {
|
|
|
+ console.log(`✅ ${machine.machineId} - ${machine.name}`)
|
|
|
+ successCount++
|
|
|
+ } else {
|
|
|
+ console.log(`⚠️ ${machine.machineId} - ${result.message || '添加失败'}`)
|
|
|
+ failCount++
|
|
|
+ }
|
|
|
+ } catch (error) {
|
|
|
+ console.log(`❌ ${machine.machineId} - ${error.message}`)
|
|
|
+ failCount++
|
|
|
+ }
|
|
|
+
|
|
|
+ // 添加延迟,避免请求过快
|
|
|
+ await new Promise(resolve => setTimeout(resolve, 100))
|
|
|
+ }
|
|
|
+
|
|
|
+ console.log('\n📊 统计结果:')
|
|
|
+ console.log(` 成功: ${successCount}`)
|
|
|
+ console.log(` 失败: ${failCount}`)
|
|
|
+ console.log(` 总计: ${machines.length}`)
|
|
|
+}
|
|
|
+
|
|
|
+main().catch(console.error)
|