feat: 登录即自动绑定云端并同步,免手动配置
This commit is contained in:
@@ -49,6 +49,24 @@ function clearSession(): void {
|
||||
localStorage.removeItem(SESSION_KEY)
|
||||
}
|
||||
|
||||
// 云端凭据:与本地登录同账号同密码,用于登录后自动云同步(保存在本机浏览器)
|
||||
const CREDS_KEY = 'dsh_cloud_creds'
|
||||
export function setCloudCreds(username: string, password: string): void {
|
||||
localStorage.setItem(CREDS_KEY, JSON.stringify({ username, password }))
|
||||
}
|
||||
export function getCloudCreds(): { username: string; password: string } | null {
|
||||
try {
|
||||
const raw = localStorage.getItem(CREDS_KEY)
|
||||
if (!raw) return null
|
||||
const c = JSON.parse(raw)
|
||||
if (c && c.username && c.password) return c as { username: string; password: string }
|
||||
} catch { /* 忽略损坏数据 */ }
|
||||
return null
|
||||
}
|
||||
export function clearCloudCreds(): void {
|
||||
localStorage.removeItem(CREDS_KEY)
|
||||
}
|
||||
|
||||
export function openUserDatabase(userId: string): void {
|
||||
bindDatabase(new WorklogDB(LEGACY_DB_NAME + '_' + userId))
|
||||
}
|
||||
@@ -71,6 +89,7 @@ export async function createUser(username: string, password: string, migrateLega
|
||||
}
|
||||
const session: Session = { userId: id, username: name }
|
||||
setSession(session)
|
||||
setCloudCreds(session.username, password)
|
||||
if (copied) showToastCopied(copied)
|
||||
return session
|
||||
}
|
||||
@@ -84,6 +103,7 @@ export async function login(username: string, password: string): Promise<Session
|
||||
openUserDatabase(rec.id)
|
||||
const session: Session = { userId: rec.id, username: rec.username }
|
||||
setSession(session)
|
||||
setCloudCreds(session.username, password)
|
||||
return session
|
||||
}
|
||||
|
||||
@@ -91,6 +111,7 @@ export async function logoutUser(): Promise<void> {
|
||||
lockVault()
|
||||
unbindDatabase()
|
||||
clearSession()
|
||||
clearCloudCreds()
|
||||
}
|
||||
|
||||
export async function changeLoginPassword(oldPwd: string, newPwd: string): Promise<void> {
|
||||
@@ -114,6 +135,7 @@ export async function deleteCurrentUser(): Promise<void> {
|
||||
await Dexie.delete(LEGACY_DB_NAME + '_' + s.userId)
|
||||
await registry.user.delete(s.userId)
|
||||
clearSession()
|
||||
clearCloudCreds()
|
||||
}
|
||||
|
||||
// 旧单机数据检测与迁移(迁移只复制,不删除原库,作为安全兜底)
|
||||
|
||||
Reference in New Issue
Block a user