数据迁移支持新用户

This commit is contained in:
mtvpls
2025-12-24 00:42:28 +08:00
parent d63192ef7a
commit bc8b9515a8
6 ha cambiato i file con 127 aggiunte e 6 eliminazioni
@@ -94,13 +94,73 @@ export async function POST(req: NextRequest) {
// 不影响主流程,继续执行
}
// 导入V2用户信息
if (importData.data.usersV2 && Array.isArray(importData.data.usersV2)) {
for (const userV2 of importData.data.usersV2) {
try {
// 跳过站长(站长使用环境变量认证)
if (userV2.role === 'owner') {
continue;
}
// 获取用户的加密密码
const userData = importData.data.userData[userV2.username];
const passwordV2 = userData?.passwordV2;
if (passwordV2) {
// 直接使用加密后的密码创建用户
const storage = (db as any).storage;
if (storage && typeof storage.client?.hset === 'function') {
const userInfoKey = `user:${userV2.username}:info`;
const createdAt = userV2.created_at || Date.now();
const userInfo: any = {
role: userV2.role,
banned: userV2.banned,
password: passwordV2,
created_at: createdAt.toString(),
};
if (userV2.tags && userV2.tags.length > 0) {
userInfo.tags = JSON.stringify(userV2.tags);
}
if (userV2.oidcSub) {
userInfo.oidcSub = userV2.oidcSub;
// 创建OIDC映射
const oidcSubKey = `oidc:sub:${userV2.oidcSub}`;
await storage.client.set(oidcSubKey, userV2.username);
}
if (userV2.enabledApis && userV2.enabledApis.length > 0) {
userInfo.enabledApis = JSON.stringify(userV2.enabledApis);
}
await storage.client.hset(userInfoKey, userInfo);
// 添加到用户列表(Sorted Set
const userListKey = 'user:list';
await storage.client.zadd(userListKey, {
score: createdAt,
member: userV2.username,
});
console.log(`V2用户 ${userV2.username} 导入成功`);
}
}
} catch (error) {
console.error(`导入V2用户 ${userV2.username} 失败:`, error);
}
}
}
// 导入用户数据
const userData = importData.data.userData;
for (const username in userData) {
const user = userData[username];
// 重新注册用户(包含密码)
if (user.password) {
// 重新注册用户(包含密码)- 仅用于旧版用户
if (user.password && !importData.data.usersV2?.find((u: any) => u.username === username)) {
await db.registerUser(username, user.password);
}
@@ -139,6 +199,7 @@ export async function POST(req: NextRequest) {
return NextResponse.json({
message: '数据导入成功',
importedUsers: Object.keys(userData).length,
importedUsersV2: importData.data.usersV2?.length || 0,
timestamp: importData.timestamp,
serverVersion: typeof importData.serverVersion === 'string' ? importData.serverVersion : '未知版本'
});