2025-12-05 18:15:23 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 通过API更新课件时长
|
|
|
|
|
|
* 使用方法:node update_courseware_via_api.js
|
|
|
|
|
|
* 需要先安装:npm install axios
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
const axios = require('axios');
|
|
|
|
|
|
|
|
|
|
|
|
// 配置
|
2025-12-06 14:53:35 +08:00
|
|
|
|
const API_BASE_URL = 'http://192.168.137.1:30091';
|
2025-12-05 18:15:23 +08:00
|
|
|
|
const TOKEN = 'YOUR_ADMIN_TOKEN_HERE'; // 需要替换为管理员token
|
|
|
|
|
|
|
|
|
|
|
|
// 需要更新的课件列表
|
|
|
|
|
|
const coursewareUpdates = [
|
|
|
|
|
|
{ id: 882, duration: 3 }, // 课件882设置为3秒
|
|
|
|
|
|
{ id: 873, duration: 17 }, // 课件873设置为17秒
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
// 更新单个课件
|
|
|
|
|
|
async function updateCourseware(coursewareId, duration) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
console.log(`正在更新课件 ${coursewareId},设置时长为 ${duration} 秒...`);
|
|
|
|
|
|
|
|
|
|
|
|
// 1. 先查询课件详情
|
|
|
|
|
|
const detailResponse = await axios.get(
|
|
|
|
|
|
`${API_BASE_URL}/study/courseware/${coursewareId}`,
|
|
|
|
|
|
{
|
|
|
|
|
|
headers: {
|
|
|
|
|
|
'Authorization': `Bearer ${TOKEN}`
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
if (detailResponse.data.code !== 200) {
|
|
|
|
|
|
console.error(`❌ 查询课件 ${coursewareId} 失败:`, detailResponse.data.msg);
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const courseware = detailResponse.data.data;
|
|
|
|
|
|
|
|
|
|
|
|
// 2. 更新duration字段
|
|
|
|
|
|
courseware.duration = duration;
|
|
|
|
|
|
|
|
|
|
|
|
// 3. 提交更新
|
|
|
|
|
|
const updateResponse = await axios.put(
|
|
|
|
|
|
`${API_BASE_URL}/study/courseware`,
|
|
|
|
|
|
courseware,
|
|
|
|
|
|
{
|
|
|
|
|
|
headers: {
|
|
|
|
|
|
'Authorization': `Bearer ${TOKEN}`,
|
|
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
if (updateResponse.data.code === 200) {
|
|
|
|
|
|
console.log(`✅ 课件 ${coursewareId} 更新成功`);
|
|
|
|
|
|
return true;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
console.error(`❌ 课件 ${coursewareId} 更新失败:`, updateResponse.data.msg);
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error(`❌ 课件 ${coursewareId} 更新出错:`, error.message);
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 主函数
|
|
|
|
|
|
async function main() {
|
|
|
|
|
|
console.log('='.repeat(50));
|
|
|
|
|
|
console.log('通过API更新课件时长');
|
|
|
|
|
|
console.log('='.repeat(50));
|
|
|
|
|
|
|
|
|
|
|
|
if (TOKEN === 'YOUR_ADMIN_TOKEN_HERE') {
|
|
|
|
|
|
console.error('❌ 错误:请先在脚本中设置管理员TOKEN');
|
|
|
|
|
|
console.log('获取TOKEN的方法:');
|
|
|
|
|
|
console.log('1. 登录后台管理系统');
|
|
|
|
|
|
console.log('2. 打开浏览器开发者工具(F12)');
|
|
|
|
|
|
console.log('3. 查看Network标签中的请求头,找到Authorization字段');
|
|
|
|
|
|
console.log('4. 复制Bearer后面的token,替换脚本中的YOUR_ADMIN_TOKEN_HERE');
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let successCount = 0;
|
|
|
|
|
|
let failCount = 0;
|
|
|
|
|
|
|
|
|
|
|
|
for (const update of coursewareUpdates) {
|
|
|
|
|
|
const success = await updateCourseware(update.id, update.duration);
|
|
|
|
|
|
if (success) {
|
|
|
|
|
|
successCount++;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
failCount++;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 延迟1秒,避免请求过快
|
|
|
|
|
|
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
console.log('='.repeat(50));
|
|
|
|
|
|
console.log(`✅ 成功: ${successCount} 个`);
|
|
|
|
|
|
console.log(`❌ 失败: ${failCount} 个`);
|
|
|
|
|
|
console.log('='.repeat(50));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 运行
|
|
|
|
|
|
main().catch(console.error);
|