miaopanel
1<script setup lang="ts">
2import { computed, inject, onMounted, reactive, ref } from 'vue'
3import { api } from '../api'
4import type { AuditRow, SessionPayload } from '../types'
5import { time } from '../format'
6import QrCode from '../components/QrCode.vue'
7
8const notify = inject<(text: string, kind?: 'ok' | 'error' | 'warn') => void>('notify')!
9const auditModule = ref('')
10const auditQuery = ref('')
11const codesOpen = ref(false)
12const codes = ref<string[]>([])
13const regen = reactive({
14 open: false,
15 password: '',
16 code: '',
17 recovery: '',
18 useRecovery: false,
19 busy: false,
20 error: '',
21})
22
23const me = ref<SessionPayload | null>(null)
24const audit = ref<AuditRow[]>([])
25
26const oldPassword = ref('')
27const newPassword = ref('')
28const confirmPassword = ref('')
29const passwordBusy = ref(false)
30
31const setupPassword = ref('')
32const secret = ref('')
33const otpURL = ref('')
34const enableCode = ref('')
35const recoveryCodes = ref<string[]>([])
36const disablePassword = ref('')
37const disableCode = ref('')
38const disableRecovery = ref('')
39const disableUseRecovery = ref(false)
40const tfBusy = ref(false)
41
42async function load() {
43 try {
44 me.value = await api.get<SessionPayload>('api/v1/auth/session')
45 audit.value = await api.get<AuditRow[]>('api/v1/system/audit?limit=200')
46 } catch (e) {
47 notify((e as Error).message, 'error')
48 }
49}
50
51async function changePassword() {
52 if (newPassword.value !== confirmPassword.value) {
53 notify('两次输入的新密码不一致', 'error')
54 return
55 }
56 passwordBusy.value = true
57 try {
58 await api.post('api/v1/auth/password', {
59 old_password: oldPassword.value,
60 new_password: newPassword.value,
61 })
62 oldPassword.value = ''
63 newPassword.value = ''
64 confirmPassword.value = ''
65 notify('密码已修改,其他设备已退出登录')
66 } catch (e) {
67 notify((e as Error).message, 'error')
68 } finally {
69 passwordBusy.value = false
70 }
71}
72
73async function startSetup() {
74 tfBusy.value = true
75 try {
76 const res = await api.post<{ secret: string; url: string }>('api/v1/auth/2fa/setup', {
77 password: setupPassword.value,
78 })
79 secret.value = res.secret
80 otpURL.value = res.url
81 setupPassword.value = ''
82 } catch (e) {
83 notify((e as Error).message, 'error')
84 } finally {
85 tfBusy.value = false
86 }
87}
88
89async function enable() {
90 tfBusy.value = true
91 try {
92 const res = await api.post<{ recovery_codes: string[] }>('api/v1/auth/2fa/enable', {
93 code: enableCode.value.trim(),
94 })
95 recoveryCodes.value = res.recovery_codes
96 codes.value = res.recovery_codes
97 codesOpen.value = true
98 secret.value = ''
99 otpURL.value = ''
100 enableCode.value = ''
101 notify('两步验证已启用,请妥善保存恢复码')
102 await load()
103 } catch (e) {
104 notify((e as Error).message, 'error')
105 } finally {
106 tfBusy.value = false
107 }
108}
109
110async function disable() {
111 tfBusy.value = true
112 try {
113 await api.post('api/v1/auth/2fa/disable', {
114 password: disablePassword.value,
115 code: disableUseRecovery.value ? '' : disableCode.value.trim(),
116 recovery: disableUseRecovery.value ? disableRecovery.value.trim() : '',
117 })
118 disablePassword.value = ''
119 disableCode.value = ''
120 disableRecovery.value = ''
121 notify('两步验证已关闭')
122 await load()
123 } catch (e) {
124 notify((e as Error).message, 'error')
125 } finally {
126 tfBusy.value = false
127 }
128}
129
130const auditModules = computed(() => {
131 const set = new Set(audit.value.map((r) => r.module).filter(Boolean))
132 return [...set].sort()
133})
134
135const auditRows = computed(() => {
136 const q = auditQuery.value.trim().toLowerCase()
137 return audit.value.filter((r) => {
138 if (auditModule.value && r.module !== auditModule.value) return false
139 if (!q) return true
140 return `${r.action} ${r.target} ${r.username} ${r.ip}`.toLowerCase().includes(q)
141 })
142})
143
144async function regenerateCodes() {
145 regen.busy = true
146 regen.error = ''
147 try {
148 const res = await api.post<{ recovery_codes: string[] }>('api/v1/auth/2fa/recovery', {
149 password: regen.password,
150 code: regen.useRecovery ? '' : regen.code.trim(),
151 recovery: regen.useRecovery ? regen.recovery.trim() : '',
152 })
153 codes.value = res.recovery_codes
154 regen.open = false
155 regen.password = ''
156 regen.code = ''
157 regen.recovery = ''
158 codesOpen.value = true
159 notify('恢复码已重新生成,旧恢复码立即失效')
160 } catch (e) {
161 regen.error = (e as Error).message
162 } finally {
163 regen.busy = false
164 }
165}
166
167async function copyCodes() {
168 try {
169 await navigator.clipboard.writeText(codes.value.join('\n'))
170 notify('恢复码已复制')
171 } catch {
172 notify('浏览器不允许访问剪贴板', 'error')
173 }
174}
175
176function downloadCodes() {
177 const blob = new Blob(
178 [`MiaoPanel 两步验证恢复码\n生成时间:${new Date().toLocaleString()}\n每个恢复码只能使用一次\n\n${codes.value.join('\n')}\n`],
179 { type: 'text/plain;charset=utf-8' },
180 )
181 const a = document.createElement('a')
182 a.href = URL.createObjectURL(blob)
183 a.download = 'miaopanel-recovery-codes.txt'
184 a.click()
185 URL.revokeObjectURL(a.href)
186}
187
188onMounted(load)
189</script>
190
191<template>
192 <div class="grid cols-2">
193 <div class="card">
194 <h2>修改密码</h2>
195 <p class="hint">使用 argon2id 存储;修改成功后其他设备会话立即失效。</p>
196 <form class="grid" @submit.prevent="changePassword">
197 <label class="field">
198 原密码
199 <input v-model="oldPassword" class="input" type="password" autocomplete="current-password" />
200 </label>
201 <label class="field">
202 新密码(至少 8 位)
203 <input v-model="newPassword" class="input" type="password" autocomplete="new-password" />
204 </label>
205 <label class="field">
206 确认新密码
207 <input v-model="confirmPassword" class="input" type="password" autocomplete="new-password" />
208 </label>
209 <button class="btn btn-primary" :disabled="passwordBusy" type="submit">
210 <span v-if="passwordBusy" class="spin"></span>
211 保存
212 </button>
213 </form>
214 </div>
215
216 <div class="card">
217 <h2>两步验证</h2>
218 <p class="hint">基于 TOTP(Google Authenticator / 1Password 等),时间窗口 ±30 秒。</p>
219
220 <template v-if="me?.user.totp_enabled">
221 <div class="alert ok" style="margin-bottom: 14px">已启用。关闭需要输入密码与当前动态口令。</div>
222 <div class="row" style="margin-bottom: 14px; gap: 8px; flex-wrap: wrap">
223 <button
224 v-if="recoveryCodes.length"
225 class="btn btn-ghost btn-sm"
226 @click="codes = recoveryCodes; codesOpen = true"
227 >
228 查看刚生成的恢复码
229 </button>
230 <button class="btn btn-ghost btn-sm" @click="regen.open = true">重新生成恢复码</button>
231 <span class="hint">恢复码只在生成时显示一次;丢失后可用「重新生成」替换。</span>
232 </div>
233 <div class="grid">
234 <label class="field">
235 密码
236 <input v-model="disablePassword" class="input" type="password" />
237 </label>
238 <label v-if="!disableUseRecovery" class="field">
239 动态口令
240 <input v-model="disableCode" class="input mono" maxlength="6" />
241 </label>
242 <label v-else class="field">
243 恢复码
244 <input v-model="disableRecovery" class="input mono" placeholder="XXXXX-XXXXX" />
245 </label>
246 <button
247 class="btn btn-ghost btn-sm"
248 type="button"
249 @click="disableUseRecovery = !disableUseRecovery"
250 >
251 {{ disableUseRecovery ? '使用动态口令' : '验证器丢失?使用恢复码' }}
252 </button>
253 <button class="btn btn-danger" :disabled="tfBusy" @click="disable">关闭两步验证</button>
254 </div>
255 </template>
256
257 <template v-else-if="secret">
258 <div class="otp-setup">
259 <QrCode :text="otpURL" :size="176" label="两步验证二维码" />
260 <dl class="kv" style="margin-bottom: 14px">
261 <dt>密钥</dt>
262 <dd class="mono">{{ secret }}</dd>
263 <dt>otpauth</dt>
264 <dd class="mono" style="font-size: 11.5px; word-break: break-all">{{ otpURL }}</dd>
265 </dl>
266 </div>
267 <div class="grid">
268 <label class="field">
269 输入验证器生成的 6 位口令
270 <input v-model="enableCode" class="input mono" maxlength="6" />
271 </label>
272 <button class="btn btn-primary" :disabled="tfBusy" @click="enable">启用</button>
273 </div>
274 </template>
275
276 <template v-else>
277 <div class="grid">
278 <label class="field">
279 当前密码
280 <input v-model="setupPassword" class="input" type="password" />
281 </label>
282 <button class="btn btn-primary" :disabled="tfBusy" @click="startSetup">生成密钥</button>
283 </div>
284 </template>
285 </div>
286 </div>
287
288 <div class="card">
289 <div class="row">
290 <div>
291 <h2>审计日志</h2>
292 <p class="hint">登录、站点变更、配置保存等操作均会记录</p>
293 </div>
294 <div class="spacer"></div>
295 <select v-model="auditModule" class="select" style="width: 140px">
296 <option value="">全部模块</option>
297 <option v-for="m in auditModules" :key="m" :value="m">{{ m }}</option>
298 </select>
299 <input v-model="auditQuery" class="input" style="width: 180px" placeholder="搜索动作 / 对象 / IP" />
300 <button class="btn btn-ghost btn-sm" @click="load">刷新</button>
301 </div>
302 <p v-if="audit.length" class="hint" style="margin: 0 0 8px">显示 {{ auditRows.length }} / {{ audit.length }} 条</p>
303 <table v-if="auditRows.length" class="table">
304 <thead>
305 <tr>
306 <th>时间</th>
307 <th>用户</th>
308 <th>模块</th>
309 <th>动作</th>
310 <th>对象</th>
311 <th>IP</th>
312 <th>结果</th>
313 <th>耗时</th>
314 </tr>
315 </thead>
316 <tbody>
317 <tr v-for="row in auditRows" :key="row.id">
318 <td class="mono">{{ time(row.created_at) }}</td>
319 <td>{{ row.username || '—' }}</td>
320 <td>{{ row.module }}</td>
321 <td>{{ row.action }}</td>
322 <td class="mono">{{ row.target || '—' }}</td>
323 <td class="mono">{{ row.ip || '—' }}</td>
324 <td>{{ row.result || '—' }}</td>
325 <td>{{ row.cost_ms }} ms</td>
326 </tr>
327 </tbody>
328 </table>
329 <div v-else class="empty">{{ audit.length ? '没有符合筛选条件的记录' : '暂无记录' }}</div>
330 </div>
331
332 <div v-if="codesOpen" class="overlay" @click.self="codesOpen = false">
333 <div class="modal" style="width: min(520px, 100%)">
334 <div class="modal-head">
335 <div>
336 <h3>恢复码</h3>
337 <p class="hint">每个恢复码只能使用一次,请离线保存。</p>
338 </div>
339 <button class="icon-btn" @click="codesOpen = false">×</button>
340 </div>
341 <pre class="code">{{ codes.length ? codes.join('\n') : '请点击「重新生成恢复码」获取新的恢复码。' }}</pre>
342 <div class="modal-foot">
343 <button class="btn btn-ghost" @click="downloadCodes">下载 .txt</button>
344 <button class="btn btn-ghost" @click="copyCodes">复制</button>
345 <div class="spacer"></div>
346 <button class="btn btn-primary" @click="codesOpen = false">我已保存</button>
347 </div>
348 </div>
349 </div>
350
351 <div v-if="regen.open" class="overlay" @click.self="regen.open = false">
352 <div class="modal" style="width: min(480px, 100%)">
353 <div class="modal-head">
354 <h3>重新生成恢复码</h3>
355 <button class="icon-btn" @click="regen.open = false">×</button>
356 </div>
357 <p class="hint">生成后旧的恢复码会立即失效。请输入密码与当前动态口令确认身份。</p>
358 <label class="field">
359 密码
360 <input v-model="regen.password" class="input" type="password" autocomplete="current-password" />
361 </label>
362 <label v-if="!regen.useRecovery" class="field">
363 动态口令
364 <input v-model="regen.code" class="input mono" inputmode="numeric" maxlength="6" />
365 </label>
366 <label v-else class="field">
367 恢复码
368 <input v-model="regen.recovery" class="input mono" placeholder="XXXXX-XXXXX" />
369 </label>
370 <button
371 class="btn btn-ghost btn-sm"
372 type="button"
373 @click="regen.useRecovery = !regen.useRecovery"
374 >
375 {{ regen.useRecovery ? '使用动态口令' : '验证器丢失?使用恢复码' }}
376 </button>
377 <div v-if="regen.error" class="alert">{{ regen.error }}</div>
378 <div class="row end" style="margin-top: 16px">
379 <button class="btn btn-ghost" @click="regen.open = false">取消</button>
380 <button class="btn btn-primary" :disabled="regen.busy" @click="regenerateCodes">
381 <span v-if="regen.busy" class="spin"></span>
382 生成
383 </button>
384 </div>
385 </div>
386 </div>
387</template>
388
389<style scoped>
390.otp-setup {
391 display: flex;
392 gap: 18px;
393 align-items: flex-start;
394 flex-wrap: wrap;
395 margin-bottom: 6px;
396}
397.otp-setup .kv {
398 flex: 1;
399 min-width: 220px;
400}
401</style>