From c6865b4607cb7a08554e14c144d92deb3df81c22 Mon Sep 17 00:00:00 2001 From: mtvpls Date: Sun, 8 Feb 2026 02:10:55 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=8A=A8=E5=88=9B=E5=BB=BA=E7=AB=99?= =?UTF-8?q?=E9=95=BF=E8=B4=A6=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib/d1.db.ts | 32 ++++++++++++++++++++++++++++---- src/lib/postgres.db.ts | 31 ++++++++++++++++++++++++++++--- src/lib/redis-base.db.ts | 34 ++++++++++++++++++++++++++++++---- 3 files changed, 86 insertions(+), 11 deletions(-) diff --git a/src/lib/d1.db.ts b/src/lib/d1.db.ts index 8cacfa3..2ea644d 100644 --- a/src/lib/d1.db.ts +++ b/src/lib/d1.db.ts @@ -886,11 +886,35 @@ export class D1Storage implements IStorage { const ownerInfo = { role: 'owner' as const, banned: false, - created_at: 0, - playrecord_migrated: false, - favorite_migrated: false, - skip_migrated: false, + created_at: Date.now(), + playrecord_migrated: true, + favorite_migrated: true, + skip_migrated: true, }; + + // 为站长创建数据库记录 + try { + await this.db + .prepare(` + INSERT INTO users ( + username, password_hash, role, banned, created_at, + playrecord_migrated, favorite_migrated, skip_migrated + ) + VALUES (?, ?, ?, 0, ?, 1, 1, 1) + `) + .bind( + userName, + '', // 站长不需要密码哈希 + 'owner', + ownerInfo.created_at + ) + .run(); + console.log(`Created database record for site owner: ${userName}`); + } catch (insertErr) { + console.error('Failed to create owner record:', insertErr); + // 即使插入失败,仍然返回默认信息 + } + // 缓存站长信息 userInfoCache?.set(userName, ownerInfo); return ownerInfo; diff --git a/src/lib/postgres.db.ts b/src/lib/postgres.db.ts index 6c9e696..a14efbb 100644 --- a/src/lib/postgres.db.ts +++ b/src/lib/postgres.db.ts @@ -439,14 +439,39 @@ export class PostgresStorage implements IStorage { // 如果数据库中没有,检查是否是环境变量中的站长 if (userName === process.env.USERNAME) { - return { - role: 'owner', + const ownerInfo = { + role: 'owner' as const, banned: false, - created_at: 0, + created_at: Date.now(), playrecord_migrated: true, favorite_migrated: true, skip_migrated: true, }; + + // 为站长创建数据库记录 + try { + await this.db + .prepare(` + INSERT INTO users ( + username, password_hash, role, banned, created_at, + playrecord_migrated, favorite_migrated, skip_migrated + ) + VALUES ($1, $2, $3, 0, $4, 1, 1, 1) + `) + .bind( + userName, + '', // 站长不需要密码哈希 + 'owner', + ownerInfo.created_at + ) + .run(); + console.log(`Created database record for site owner: ${userName}`); + } catch (insertErr) { + console.error('Failed to create owner record:', insertErr); + // 即使插入失败,仍然返回默认信息 + } + + return ownerInfo; } return null; diff --git a/src/lib/redis-base.db.ts b/src/lib/redis-base.db.ts index 94aa905..7c60f05 100644 --- a/src/lib/redis-base.db.ts +++ b/src/lib/redis-base.db.ts @@ -1006,11 +1006,37 @@ export abstract class BaseRedisStorage implements IStorage { const ownerInfo = { role: 'owner' as const, banned: false, - created_at: 0, - playrecord_migrated: false, - favorite_migrated: false, - skip_migrated: false, + created_at: Date.now(), + playrecord_migrated: true, + favorite_migrated: true, + skip_migrated: true, }; + + // 为站长创建数据库记录 + try { + const userInfo: Record = { + role: 'owner', + banned: 'false', + created_at: ownerInfo.created_at.toString(), + playrecord_migrated: 'true', + favorite_migrated: 'true', + skip_migrated: 'true', + }; + + await this.withRetry(() => this.adapter.hSet(this.userInfoKey(userName), userInfo)); + + // 添加到用户列表(Sorted Set,按注册时间排序) + await this.withRetry(() => this.adapter.zAdd(this.userListKey(), { + score: ownerInfo.created_at, + value: userName, + })); + + console.log(`Created database record for site owner: ${userName}`); + } catch (insertErr) { + console.error('Failed to create owner record:', insertErr); + // 即使插入失败,仍然返回默认信息 + } + // 缓存站长信息 userInfoCache?.set(userName, ownerInfo); return ownerInfo;