54 lines
1.9 KiB
JavaScript
54 lines
1.9 KiB
JavaScript
|
|
import fs from 'fs';
|
||
|
|
import path from 'path';
|
||
|
|
import { fileURLToPath } from 'url';
|
||
|
|
|
||
|
|
const __filename = fileURLToPath(import.meta.url);
|
||
|
|
const __dirname = path.dirname(__filename);
|
||
|
|
|
||
|
|
// 读取头像列表
|
||
|
|
const avatarList = JSON.parse(
|
||
|
|
fs.readFileSync(path.join(__dirname, '网页未导入数据/头像列表.json'), 'utf-8')
|
||
|
|
);
|
||
|
|
|
||
|
|
// 为前三名选择特定的头像(选择比较有特色的)
|
||
|
|
const topThreeAvatars = {
|
||
|
|
"胡雨泽": avatarList[18].url, // 第1名 - 选择一个专业形象的头像
|
||
|
|
"林俊豪": avatarList[23].url, // 第2名 - 选择另一个专业形象
|
||
|
|
"郭宇航": avatarList[31].url // 第3名 - 选择第三个专业形象
|
||
|
|
};
|
||
|
|
|
||
|
|
// 读取mockData.js文件
|
||
|
|
const mockDataPath = path.join(__dirname, 'src/data/mockData.js');
|
||
|
|
let mockDataContent = fs.readFileSync(mockDataPath, 'utf-8');
|
||
|
|
|
||
|
|
// 备份原文件
|
||
|
|
const backupPath = path.join(__dirname, 'src/data', `mockData.js.backup_${new Date().toISOString().replace(/[:.]/g, '-')}`);
|
||
|
|
fs.copyFileSync(mockDataPath, backupPath);
|
||
|
|
console.log(`备份已创建: ${backupPath}`);
|
||
|
|
|
||
|
|
// 更新前三名的头像
|
||
|
|
Object.entries(topThreeAvatars).forEach(([name, newAvatar]) => {
|
||
|
|
// 使用正则表达式查找并替换该学生的头像
|
||
|
|
const pattern = new RegExp(
|
||
|
|
`("name":\\s*"${name}"[^}]*?"avatar":\\s*)"[^"]*"`,
|
||
|
|
'g'
|
||
|
|
);
|
||
|
|
|
||
|
|
mockDataContent = mockDataContent.replace(pattern, `$1"${newAvatar}"`);
|
||
|
|
|
||
|
|
// 同时处理studentName的情况
|
||
|
|
const pattern2 = new RegExp(
|
||
|
|
`("studentName":\\s*"${name}"[^}]*?"avatar":\\s*)"[^"]*"`,
|
||
|
|
'g'
|
||
|
|
);
|
||
|
|
|
||
|
|
mockDataContent = mockDataContent.replace(pattern2, `$1"${newAvatar}"`);
|
||
|
|
});
|
||
|
|
|
||
|
|
// 写入更新后的文件
|
||
|
|
fs.writeFileSync(mockDataPath, mockDataContent, 'utf-8');
|
||
|
|
|
||
|
|
console.log('\n✅ 成功更新前三名学生头像:');
|
||
|
|
console.log('1. 胡雨泽 - 新头像:', topThreeAvatars["胡雨泽"]);
|
||
|
|
console.log('2. 林俊豪 - 新头像:', topThreeAvatars["林俊豪"]);
|
||
|
|
console.log('3. 郭宇航 - 新头像:', topThreeAvatars["郭宇航"]);
|