forked from mxx1111/mdlook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccountDialog.vue
More file actions
559 lines (511 loc) · 19.3 KB
/
Copy pathAccountDialog.vue
File metadata and controls
559 lines (511 loc) · 19.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
<script setup lang="ts">
import { CheckCircle2, Cloud, CloudAlert, Eye, EyeOff, KeyRound, Loader2, LogIn, LogOut, RefreshCw, ShieldCheck, User, UserPlus } from '@lucide/vue'
import { storeToRefs } from 'pinia'
import { computed, reactive, ref, watch } from 'vue'
import AIAccountSecurity from '@/components/ai/config/AIAccountSecurity.vue'
import { isProPlan, SYNC_PRO_ENABLED } from '@/services/account/features'
import { useAccountSettingsStore } from '@/stores/accountSettings'
import { useAuthStore } from '@/stores/auth'
import { useSyncStore } from '@/stores/sync'
const props = defineProps({
visible: {
type: Boolean,
default: false,
},
})
const emit = defineEmits([`close`])
const authStore = useAuthStore()
const accountSettingsStore = useAccountSettingsStore()
const syncStore = useSyncStore()
const { user, isConfigured, isLoggedIn } = storeToRefs(authStore)
const {
status: settingsStatus,
lastError: settingsError,
lastSavedAt,
} = storeToRefs(accountSettingsStore)
const activeTab = ref<`login` | `register`>(`login`)
const pendingAction = ref<`login` | `register` | `logout` | `password` | `settings` | null>(null)
const formError = ref(``)
const passwordError = ref(``)
const showLoginPassword = ref(false)
const showRegisterPassword = ref(false)
const showCurrentPassword = ref(false)
const showNewPassword = ref(false)
const loginForm = reactive({
username: ``,
password: ``,
})
const registerForm = reactive({
username: ``,
password: ``,
confirmPassword: ``,
})
const passwordForm = reactive({
currentPassword: ``,
newPassword: ``,
confirmPassword: ``,
})
const isPro = computed(() => isProPlan(user.value?.plan))
const settingsStatusText = computed(() => {
switch (settingsStatus.value) {
case `loading`:
return `正在读取你的配置`
case `saving`:
return `正在保存你的配置`
case `saved`:
return lastSavedAt.value
? `配置已同步`
: `配置同步已开启`
case `error`:
return `配置同步失败`
default:
return `登录后自动同步个人配置`
}
})
function onUpdate(value: boolean) {
if (!value)
emit(`close`)
}
function clearPasswords() {
loginForm.password = ``
registerForm.password = ``
registerForm.confirmPassword = ``
passwordForm.currentPassword = ``
passwordForm.newPassword = ``
passwordForm.confirmPassword = ``
}
function validateUsername(username: string): string | null {
if (!/^\w{4,32}$/.test(username))
return `用户名需为 4–32 位字母、数字或下划线`
return null
}
function validatePassword(password: string): string | null {
if (password.length < 8 || password.length > 72)
return `密码长度需为 8–72 位`
if (new TextEncoder().encode(password).length > 72)
return `密码使用 UTF-8 编码后不能超过 72 字节`
return null
}
function messageOf(error: unknown): string {
return error instanceof Error ? error.message : `操作失败,请稍后再试`
}
async function handleLogin() {
formError.value = validateUsername(loginForm.username.trim())
?? validatePassword(loginForm.password)
?? ``
if (formError.value)
return
pendingAction.value = `login`
try {
await authStore.login({
username: loginForm.username.trim(),
password: loginForm.password,
})
await accountSettingsStore.bootstrap()
loginForm.password = ``
toast.success(`登录成功`)
}
catch (error) {
formError.value = messageOf(error)
}
finally {
pendingAction.value = null
}
}
async function handleRegister() {
formError.value = validateUsername(registerForm.username.trim())
?? validatePassword(registerForm.password)
?? (registerForm.password !== registerForm.confirmPassword ? `两次输入的密码不一致` : ``)
if (formError.value)
return
pendingAction.value = `register`
try {
const outcome = await authStore.register({
username: registerForm.username.trim(),
password: registerForm.password,
confirmPassword: registerForm.confirmPassword,
})
if (!outcome.autoLoggedIn) {
loginForm.username = registerForm.username.trim()
activeTab.value = `login`
clearPasswords()
await nextTick()
formError.value = `注册已成功,但自动登录未完成:${outcome.loginError ?? '自动登录失败'}。请直接登录。`
toast.success(`注册成功,请登录`)
return
}
await accountSettingsStore.bootstrap()
clearPasswords()
toast.success(`注册成功,已自动登录`)
}
catch (error) {
formError.value = messageOf(error)
}
finally {
pendingAction.value = null
}
}
async function handleLogout() {
const previousLogin = user.value?.login ?? loginForm.username
pendingAction.value = `logout`
try {
await authStore.logout()
toast.success(`已退出登录`)
}
catch (error) {
toast.error(messageOf(error))
}
finally {
accountSettingsStore.reset()
syncStore.reset()
clearPasswords()
loginForm.username = previousLogin
registerForm.username = ``
activeTab.value = `login`
pendingAction.value = null
}
}
async function handleChangePassword() {
passwordError.value = validatePassword(passwordForm.newPassword)
?? (passwordForm.newPassword !== passwordForm.confirmPassword ? `两次输入的新密码不一致` : ``)
if (!passwordForm.currentPassword)
passwordError.value = `请输入当前密码`
if (passwordError.value)
return
pendingAction.value = `password`
try {
await authStore.changePassword({
currentPassword: passwordForm.currentPassword,
newPassword: passwordForm.newPassword,
})
clearPasswords()
toast.success(`密码已更新,请重新登录`)
}
catch (error) {
passwordError.value = messageOf(error)
}
finally {
pendingAction.value = null
}
}
async function handleSettingsSync() {
pendingAction.value = `settings`
try {
await accountSettingsStore.saveNow(true)
toast.success(`个人配置已同步`)
}
catch (error) {
toast.error(messageOf(error))
}
finally {
pendingAction.value = null
}
}
watch(activeTab, () => {
formError.value = ``
})
watch(() => props.visible, (visible) => {
if (visible) {
formError.value = ``
passwordError.value = ``
return
}
clearPasswords()
})
</script>
<template>
<Dialog :open="props.visible" @update:open="onUpdate">
<DialogContent
class="max-h-[calc(100dvh-1rem)] w-[calc(100vw-1rem)] max-w-2xl gap-0 overflow-hidden rounded-xl p-0 sm:max-w-2xl"
>
<DialogHeader class="space-y-1.5 border-b px-5 py-4 pr-12 sm:px-6">
<DialogTitle class="flex items-center gap-2">
<User class="size-5" />
{{ isLoggedIn ? `我的账户` : `登录 mdlook` }}
</DialogTitle>
<DialogDescription>
{{ isLoggedIn ? `个人排版、AI 凭据与私有 Skill 由你的账户独立管理。` : `登录后可同步个人配置,并使用加密 AI 凭据与私有 Skill。` }}
</DialogDescription>
</DialogHeader>
<div v-if="!isConfigured" class="text-muted-foreground px-5 py-8 text-center text-sm sm:px-6">
账户服务暂未配置,请检查 <code>VITE_ACCOUNT_API_URL</code>。
</div>
<div v-else-if="!isLoggedIn" class="max-h-[calc(100dvh-8rem)] overflow-y-auto px-5 py-5 sm:px-6">
<Tabs v-model="activeTab" class="w-full">
<TabsList class="grid h-11 w-full grid-cols-2">
<TabsTrigger value="login" class="h-9">
登录
</TabsTrigger>
<TabsTrigger value="register" class="h-9">
注册
</TabsTrigger>
</TabsList>
<div
v-if="formError"
role="alert"
class="mt-4 rounded-md border border-destructive/30 bg-destructive/5 px-3 py-2.5 text-sm text-destructive"
>
{{ formError }}
</div>
<TabsContent value="login" class="mt-4">
<form class="space-y-4" @submit.prevent="handleLogin">
<div class="space-y-1.5">
<Label for="account-login-username">用户名</Label>
<Input
id="account-login-username"
v-model="loginForm.username"
class="h-11 text-base sm:text-sm"
autocomplete="username"
autocapitalize="none"
spellcheck="false"
maxlength="32"
placeholder="请输入用户名"
/>
</div>
<div class="space-y-1.5">
<Label for="account-login-password">密码</Label>
<div class="relative">
<Input
id="account-login-password"
v-model="loginForm.password"
:type="showLoginPassword ? 'text' : 'password'"
class="h-11 pr-11 text-base sm:text-sm"
autocomplete="current-password"
maxlength="72"
placeholder="请输入密码"
/>
<button
type="button"
class="text-muted-foreground absolute right-0 top-0 grid size-11 place-items-center rounded-md"
:aria-label="showLoginPassword ? '隐藏密码' : '显示密码'"
@click="showLoginPassword = !showLoginPassword"
>
<EyeOff v-if="showLoginPassword" class="size-4" />
<Eye v-else class="size-4" />
</button>
</div>
</div>
<Button type="submit" class="h-11 w-full gap-2" :disabled="pendingAction !== null">
<Loader2 v-if="pendingAction === 'login'" class="size-4 animate-spin" />
<LogIn v-else class="size-4" />
登录
</Button>
</form>
</TabsContent>
<TabsContent value="register" class="mt-4">
<form class="space-y-4" @submit.prevent="handleRegister">
<div class="space-y-1.5">
<Label for="account-register-username">用户名</Label>
<Input
id="account-register-username"
v-model="registerForm.username"
class="h-11 text-base sm:text-sm"
autocomplete="username"
autocapitalize="none"
spellcheck="false"
maxlength="32"
placeholder="4–32 位字母、数字或下划线"
/>
</div>
<div class="space-y-1.5">
<Label for="account-register-password">密码</Label>
<div class="relative">
<Input
id="account-register-password"
v-model="registerForm.password"
:type="showRegisterPassword ? 'text' : 'password'"
class="h-11 pr-11 text-base sm:text-sm"
autocomplete="new-password"
maxlength="72"
placeholder="8–72 位字符"
/>
<button
type="button"
class="text-muted-foreground absolute right-0 top-0 grid size-11 place-items-center rounded-md"
:aria-label="showRegisterPassword ? '隐藏密码' : '显示密码'"
@click="showRegisterPassword = !showRegisterPassword"
>
<EyeOff v-if="showRegisterPassword" class="size-4" />
<Eye v-else class="size-4" />
</button>
</div>
</div>
<div class="space-y-1.5">
<Label for="account-register-confirm">确认密码</Label>
<Input
id="account-register-confirm"
v-model="registerForm.confirmPassword"
type="password"
class="h-11 text-base sm:text-sm"
autocomplete="new-password"
maxlength="72"
placeholder="请再次输入密码"
/>
</div>
<Button type="submit" class="h-11 w-full gap-2" :disabled="pendingAction !== null">
<Loader2 v-if="pendingAction === 'register'" class="size-4 animate-spin" />
<UserPlus v-else class="size-4" />
注册并登录
</Button>
</form>
</TabsContent>
</Tabs>
<p class="text-muted-foreground mt-4 text-center text-xs leading-5">
未登录时 AI Key 只保存在当前页面内存,不会默认写入本机;登录后可由你主动保存到加密云端。
</p>
</div>
<div v-else class="max-h-[calc(100dvh-8rem)] space-y-4 overflow-y-auto px-5 py-4 sm:px-6">
<div class="flex items-center gap-3 rounded-lg border p-3">
<img
v-if="user?.avatar"
:src="user.avatar"
:alt="user?.login"
class="size-11 shrink-0 rounded-full"
>
<div v-else class="bg-muted grid size-11 shrink-0 place-items-center rounded-full">
<User class="size-5 text-muted-foreground" />
</div>
<div class="min-w-0 flex-1">
<div class="flex items-center gap-2 text-sm font-medium">
<span class="truncate">{{ user?.name || user?.login }}</span>
<template v-if="SYNC_PRO_ENABLED">
<span
v-if="isPro"
class="bg-primary text-primary-foreground inline-flex shrink-0 items-center rounded px-1.5 py-0.5 text-xs"
>
Pro
</span>
<span v-else class="bg-muted text-muted-foreground shrink-0 rounded px-1.5 py-0.5 text-xs uppercase">
Free
</span>
</template>
</div>
<div class="text-muted-foreground truncate text-xs">
@{{ user?.login }}
</div>
</div>
</div>
<div
class="rounded-lg border p-3"
:class="{ 'border-destructive/30 bg-destructive/5': settingsStatus === 'error' }"
>
<div class="flex items-start gap-3">
<Loader2 v-if="settingsStatus === 'loading' || settingsStatus === 'saving'" class="mt-0.5 size-4 shrink-0 animate-spin" />
<CloudAlert v-else-if="settingsStatus === 'error'" class="mt-0.5 size-4 shrink-0 text-destructive" />
<CheckCircle2 v-else-if="settingsStatus === 'saved'" class="mt-0.5 size-4 shrink-0 text-emerald-600" />
<Cloud v-else class="mt-0.5 size-4 shrink-0 text-muted-foreground" />
<div class="min-w-0 flex-1">
<p class="text-sm font-medium">
{{ settingsStatusText }}
</p>
<p class="text-muted-foreground mt-1 text-xs leading-5">
主题、字号、标题样式和排版偏好;编辑模式与预览设备保留在本机。
</p>
<p v-if="settingsError" class="mt-1 break-words text-xs text-destructive">
{{ settingsError }}
</p>
</div>
<Button
variant="ghost"
size="icon"
class="size-10 shrink-0"
title="立即同步配置"
:disabled="pendingAction !== null"
@click="handleSettingsSync"
>
<RefreshCw class="size-4" :class="{ 'animate-spin': pendingAction === 'settings' }" />
</Button>
</div>
</div>
<details class="group rounded-lg border">
<summary class="flex min-h-11 cursor-pointer list-none items-center gap-2 px-3 py-2.5 text-sm font-medium">
<ShieldCheck class="size-4 text-primary" />
AI 密钥与私有文风 Skill
<span class="ml-auto text-xs font-normal text-muted-foreground group-open:hidden">展开</span>
<span class="ml-auto hidden text-xs font-normal text-muted-foreground group-open:inline">收起</span>
</summary>
<div class="border-t p-3">
<AIAccountSecurity provider="deepseek" />
</div>
</details>
<form class="space-y-3 rounded-lg border p-3" @submit.prevent="handleChangePassword">
<div class="flex items-center gap-2 text-sm font-medium">
<KeyRound class="size-4" />
修改密码
</div>
<div
v-if="passwordError"
role="alert"
class="rounded-md border border-destructive/30 bg-destructive/5 px-3 py-2 text-sm text-destructive"
>
{{ passwordError }}
</div>
<div class="relative">
<Input
v-model="passwordForm.currentPassword"
:type="showCurrentPassword ? 'text' : 'password'"
class="h-11 pr-11 text-base sm:text-sm"
autocomplete="current-password"
maxlength="72"
aria-label="当前密码"
placeholder="当前密码"
/>
<button
type="button"
class="text-muted-foreground absolute right-0 top-0 grid size-11 place-items-center rounded-md"
:aria-label="showCurrentPassword ? '隐藏密码' : '显示密码'"
@click="showCurrentPassword = !showCurrentPassword"
>
<EyeOff v-if="showCurrentPassword" class="size-4" />
<Eye v-else class="size-4" />
</button>
</div>
<div class="relative">
<Input
v-model="passwordForm.newPassword"
:type="showNewPassword ? 'text' : 'password'"
class="h-11 pr-11 text-base sm:text-sm"
autocomplete="new-password"
maxlength="72"
aria-label="新密码"
placeholder="新密码(8–72 位)"
/>
<button
type="button"
class="text-muted-foreground absolute right-0 top-0 grid size-11 place-items-center rounded-md"
:aria-label="showNewPassword ? '隐藏密码' : '显示密码'"
@click="showNewPassword = !showNewPassword"
>
<EyeOff v-if="showNewPassword" class="size-4" />
<Eye v-else class="size-4" />
</button>
</div>
<Input
v-model="passwordForm.confirmPassword"
type="password"
class="h-11 text-base sm:text-sm"
autocomplete="new-password"
maxlength="72"
aria-label="确认新密码"
placeholder="再次输入新密码"
/>
<Button type="submit" variant="outline" class="h-11 w-full gap-2" :disabled="pendingAction !== null">
<Loader2 v-if="pendingAction === 'password'" class="size-4 animate-spin" />
<KeyRound v-else class="size-4" />
更新密码
</Button>
</form>
<Button
variant="ghost"
class="h-11 w-full gap-2 text-muted-foreground hover:text-destructive"
:disabled="pendingAction !== null"
@click="handleLogout"
>
<Loader2 v-if="pendingAction === 'logout'" class="size-4 animate-spin" />
<LogOut v-else class="size-4" />
退出登录
</Button>
</div>
</DialogContent>
</Dialog>
</template>