Header.vue
3.33 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
<template>
<div class="header">
<div class="header-left">
<img src="../assets/logo.png" alt="Logo" class="header-logo" />
<span class="header-title">和家智检管理系统</span>
</div>
<div class="header-right">
<el-dropdown @command="handleCommand">
<div class="user-info">
<span class="username">Hi,{{ username }}</span>
<el-icon class="arrow-icon">
<ArrowDown />
</el-icon>
</div>
<template #dropdown>
<el-dropdown-menu>
<el-dropdown-item command="logout">
<el-icon><SwitchButton /></el-icon>
退出登录
</el-dropdown-item>
</el-dropdown-menu>
</template>
</el-dropdown>
<el-dropdown @command="handleCommand">
<div class="user-info">
<span class="username">Hi,{{ username }}</span>
<el-icon class="arrow-icon">
<ArrowDown />
</el-icon>
</div>
<template #dropdown>
<el-dropdown-menu>
<el-dropdown-item command="logout">
<el-icon><SwitchButton /></el-icon>
退出登录
</el-dropdown-item>
</el-dropdown-menu>
</template>
</el-dropdown>
</div>
</div>
</template>
<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { ArrowDown, SwitchButton } from '@element-plus/icons-vue'
const router = useRouter()
const username = ref('')
onMounted(() => {
// 从localStorage获取用户名,如果没有则使用默认值
username.value = localStorage.getItem('adminNum') || '用户'
})
const handleCommand = async (command) => {
if (command === 'logout') {
try {
await ElMessageBox.confirm(
'确定要退出登录吗?',
'提示',
{
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}
)
// 清除本地存储
localStorage.removeItem('token')
localStorage.removeItem('adminNum')
ElMessage.success('退出登录成功')
router.push('/login')
} catch (error) {
// 用户取消操作
}
}
}
</script>
<style scoped>
.header {
position: fixed;
top: 0;
left: 0;
right: 0;
height: 60px;
background: #ffffff;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
display: flex;
align-items: center;
justify-content: space-between;
padding: 0 20px;
z-index: 1000;
}
.header-left {
display: flex;
align-items: center;
}
.header-logo {
width: 40px;
height: 40px;
object-fit: contain;
margin-right: 8px;
}
.header-title {
font-size: 18px;
font-weight: 600;
color: #333333;
}
.header-right {
display: flex;
align-items: center;
}
.user-info {
display: flex;
align-items: center;
gap: 8px;
cursor: pointer;
padding: 8px 12px;
border-radius: 6px;
transition: background-color 0.3s ease;
border: none !important;
outline: none !important;
}
.user-info:hover {
background-color: #f5f5f5;
border: none !important;
outline: none !important;
}
.username {
font-size: 14px;
color: #666666;
}
.arrow-icon {
font-size: 12px;
color: #666666;
transition: transform 0.3s ease;
}
.user-info:hover .arrow-icon {
transform: rotate(180deg);
}
</style>