fix: fetchUser on mount, password validation, remove dead code
- Call authStore.fetchUser() in onMounted so avatar shows username after refresh - Simplify navigator.onLine checks (remove redundant !== false) - Remove unused navItems array (dead code from earlier iteration) - Add password form validation with confirm password field and rules Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -124,13 +124,16 @@
|
||||
|
||||
<!-- Change password dialog -->
|
||||
<el-dialog v-model="showPwd" title="修改密码" width="420px" :close-on-click-modal="false">
|
||||
<el-form :model="pwdForm" label-width="70px">
|
||||
<el-form-item label="旧密码">
|
||||
<el-form ref="pwdFormRef" :model="pwdForm" :rules="pwdRules" label-width="70px">
|
||||
<el-form-item label="旧密码" prop="old_password">
|
||||
<el-input v-model="pwdForm.old_password" type="password" show-password />
|
||||
</el-form-item>
|
||||
<el-form-item label="新密码">
|
||||
<el-form-item label="新密码" prop="new_password">
|
||||
<el-input v-model="pwdForm.new_password" type="password" show-password />
|
||||
</el-form-item>
|
||||
<el-form-item label="确认密码" prop="confirm_password">
|
||||
<el-input v-model="pwdForm.confirm_password" type="password" show-password />
|
||||
</el-form-item>
|
||||
</el-form>
|
||||
<template #footer>
|
||||
<el-button @click="showPwd = false">取消</el-button>
|
||||
@@ -142,7 +145,7 @@
|
||||
<script setup lang="ts">
|
||||
import { ref, computed, reactive, onMounted, onUnmounted } from 'vue'
|
||||
import { useRoute, useRouter } from 'vue-router'
|
||||
import { ElMessage } from 'element-plus'
|
||||
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
|
||||
import {
|
||||
HomeFilled, Memo, Connection, Setting, Cloudy, Timer, Notebook, FolderOpened,
|
||||
ArrowDown, Lock, SwitchButton, DArrowLeft, DArrowRight
|
||||
@@ -156,32 +159,44 @@ const authStore = useAuthStore()
|
||||
|
||||
const isCollapse = ref(false)
|
||||
const showPwd = ref(false)
|
||||
const pwdForm = reactive({ old_password: '', new_password: '' })
|
||||
const isOnline = ref(navigator.onLine !== false)
|
||||
const pwdForm = reactive({ old_password: '', new_password: '', confirm_password: '' })
|
||||
const pwdFormRef = ref<FormInstance>()
|
||||
const pwdRules: FormRules = {
|
||||
old_password: [{ required: true, message: '请输入旧密码', trigger: 'blur' }],
|
||||
new_password: [
|
||||
{ required: true, message: '请输入新密码', trigger: 'blur' },
|
||||
{ min: 6, message: '密码至少6位', trigger: 'blur' }
|
||||
],
|
||||
confirm_password: [
|
||||
{ required: true, message: '请确认新密码', trigger: 'blur' },
|
||||
{
|
||||
validator: (_rule: any, value: string, callback: any) => {
|
||||
if (value !== pwdForm.new_password) {
|
||||
callback(new Error('两次输入的密码不一致'))
|
||||
} else {
|
||||
callback()
|
||||
}
|
||||
},
|
||||
trigger: 'blur'
|
||||
}
|
||||
]
|
||||
}
|
||||
const isOnline = ref(navigator.onLine)
|
||||
|
||||
// Track online/offline status
|
||||
function updateOnlineStatus() {
|
||||
isOnline.value = navigator.onLine !== false
|
||||
isOnline.value = navigator.onLine
|
||||
}
|
||||
onMounted(() => {
|
||||
onMounted(async () => {
|
||||
window.addEventListener('online', updateOnlineStatus)
|
||||
window.addEventListener('offline', updateOnlineStatus)
|
||||
await authStore.fetchUser()
|
||||
})
|
||||
onUnmounted(() => {
|
||||
window.removeEventListener('online', updateOnlineStatus)
|
||||
window.removeEventListener('offline', updateOnlineStatus)
|
||||
})
|
||||
|
||||
const navItems: { path: string; label: string; icon: any; badge?: string }[] = [
|
||||
{ path: '/', label: '处理中心', icon: HomeFilled },
|
||||
{ path: '/tasks', label: '任务历史', icon: Timer },
|
||||
{ path: '/logs', label: '日志中心', icon: Notebook },
|
||||
{ path: '/memory', label: '记忆库', icon: Memo },
|
||||
{ path: '/barcodes', label: '条码映射', icon: Connection },
|
||||
{ path: '/config', label: '系统配置', icon: Setting },
|
||||
{ path: '/sync', label: '云端同步', icon: Cloudy },
|
||||
]
|
||||
|
||||
const filesMenuOpen = ['/files']
|
||||
|
||||
const pageTitles: Record<string, string> = {
|
||||
@@ -208,19 +223,26 @@ function handleCommand(cmd: string) {
|
||||
} else if (cmd === 'password') {
|
||||
pwdForm.old_password = ''
|
||||
pwdForm.new_password = ''
|
||||
pwdForm.confirm_password = ''
|
||||
showPwd.value = true
|
||||
}
|
||||
}
|
||||
|
||||
async function changePassword() {
|
||||
if (!pwdForm.new_password) { ElMessage.warning('请输入新密码'); return }
|
||||
try {
|
||||
await api.post('/auth/change-password', pwdForm)
|
||||
ElMessage.success('密码修改成功')
|
||||
showPwd.value = false
|
||||
} catch (err: any) {
|
||||
ElMessage.error(err.response?.data?.detail || '修改失败')
|
||||
}
|
||||
if (!pwdFormRef.value) return
|
||||
await pwdFormRef.value.validate(async (valid) => {
|
||||
if (!valid) return
|
||||
try {
|
||||
await api.post('/auth/change-password', {
|
||||
old_password: pwdForm.old_password,
|
||||
new_password: pwdForm.new_password
|
||||
})
|
||||
ElMessage.success('密码修改成功')
|
||||
showPwd.value = false
|
||||
} catch (err: any) {
|
||||
ElMessage.error(err.response?.data?.detail || '修改失败')
|
||||
}
|
||||
})
|
||||
}
|
||||
</script>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user