feat(dcauth): 新增后台手机号与设备认证登录

登录链路改为账号密码 + 短信验证码 + DCAuth 设备签名,并新增后台手机号绑定、
登录日志、设备绑定与重置接口。
This commit is contained in:
yml2213
2026-10-21 20:46:24 +08:00
parent 7542d547bf
commit 040100f290
36 changed files with 3128 additions and 41 deletions
@@ -7,9 +7,17 @@ type AdminUserRow = {
password_hash: string
role: string
status: string
phone: string
phone_bound_at: string | null
phone_bound_by: number | null
test_account: boolean
session_version: number
created_at: string
updated_at: string
device_id?: string | null
machine_code_mask?: string | null
device_bound_at?: string | null
device_last_used_at?: string | null
}
type AdminUserCreateInput = {
@@ -18,6 +26,10 @@ type AdminUserCreateInput = {
passwordHash: string
role: string
status: string
phone?: string
phoneBoundAt?: string | null
phoneBoundBy?: number | null
testAccount?: boolean
createdAt: string
updatedAt: string
}
@@ -30,6 +42,10 @@ type AdminUserPatch = Partial<
| 'password_hash'
| 'role'
| 'status'
| 'phone'
| 'phone_bound_at'
| 'phone_bound_by'
| 'test_account'
| 'session_version'
| 'updated_at'
>
@@ -49,8 +65,15 @@ type AdminUserListResult = {
}
const ADMIN_USER_SELECT = `
SELECT au.*
SELECT
au.*,
d.device_id AS device_id,
d.machine_code_mask AS machine_code_mask,
d.bound_at AS device_bound_at,
d.last_used_at AS device_last_used_at
FROM admin_users au
LEFT JOIN admin_user_devices d
ON d.user_id = au.id AND d.status = 'active'
`
export async function getAdminUserById(userId: number | string): Promise<AdminUserRow | null> {
@@ -69,6 +92,13 @@ export async function getAdminUserByUsername(username: string): Promise<AdminUse
return result.rows[0] || null
}
export async function getAdminUserByPhone(phone: string): Promise<AdminUserRow | null> {
const result = await query<AdminUserRow>(`${ADMIN_USER_SELECT} WHERE au.phone = $1 LIMIT 1`, [
String(phone || '').trim(),
])
return result.rows[0] || null
}
export async function createAdminUser(input: AdminUserCreateInput): Promise<AdminUserRow | null> {
const result = await query<{ [column: string]: unknown; id: number }>(
`
@@ -78,10 +108,14 @@ export async function createAdminUser(input: AdminUserCreateInput): Promise<Admi
password_hash,
role,
status,
phone,
phone_bound_at,
phone_bound_by,
test_account,
session_version,
created_at,
updated_at
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
RETURNING id
`,
[
@@ -90,6 +124,10 @@ export async function createAdminUser(input: AdminUserCreateInput): Promise<Admi
input.passwordHash,
input.role,
input.status,
String(input.phone || '').trim(),
input.phoneBoundAt || null,
input.phoneBoundBy ?? null,
input.testAccount === true,
1,
input.createdAt,
input.updatedAt,
@@ -118,9 +156,13 @@ export async function updateAdminUser(
password_hash = $3,
role = $4,
status = $5,
session_version = $6,
updated_at = $7
WHERE id = $8
phone = $6,
phone_bound_at = $7,
phone_bound_by = $8,
test_account = $9,
session_version = $10,
updated_at = $11
WHERE id = $12
RETURNING id
`,
[
@@ -129,6 +171,10 @@ export async function updateAdminUser(
next.password_hash,
next.role,
next.status,
String(next.phone || '').trim(),
next.phone_bound_at || null,
next.phone_bound_by ?? null,
next.test_account === true,
Number(next.session_version || 1),
next.updated_at,
Number(userId),
@@ -151,7 +197,9 @@ export async function listAdminUsers({
if (keyword) {
params.push(`%${keyword}%`)
filters.push(`(au.username ILIKE $${params.length} OR au.display_name ILIKE $${params.length})`)
filters.push(
`(au.username ILIKE $${params.length} OR au.display_name ILIKE $${params.length} OR au.phone ILIKE $${params.length})`,
)
}
if (role) {