对接后端

This commit is contained in:
ovo 2024-12-07 22:26:09 +08:00
parent 26712f144d
commit e1ae060551
2 changed files with 108 additions and 90 deletions

View File

@ -1,58 +1,72 @@
<template> <template>
<div class="top-bar"> <div class="topbar">
<div class="left"> <div class="left">
<el-icon class="menu-icon" @click="toggleSidebar"> <router-link to="/" class="logo">智慧养老平台</router-link>
<Expand v-if="isCollapsed" />
<Fold v-else />
</el-icon>
</div> </div>
<div class="right"> <div class="right">
<el-dropdown trigger="click" @command="handleCommand"> <template v-if="userStore.userInfo">
<div class="user-info"> <!-- 已登录状态 -->
<el-avatar :src="userInfo.avatar" /> <el-dropdown @command="handleCommand">
<span class="username">{{ userInfo.nickname }}</span> <div class="user-info">
</div> <el-avatar
<template #dropdown> :size="32"
<el-dropdown-menu> :src="userStore.userInfo.avatar || defaultAvatar"
<el-dropdown-item command="profile"> />
<el-icon><User /></el-icon> <span class="nickname">{{ userStore.userInfo.nickname || '用户' }}</span>
</el-dropdown-item> </div>
<el-dropdown-item command="settings"> <template #dropdown>
<el-icon><Setting /></el-icon> <el-dropdown-menu>
</el-dropdown-item> <el-dropdown-item command="profile">
<el-dropdown-item divided command="logout"> <el-icon><User /></el-icon>
<el-icon><SwitchButton /></el-icon>退 个人中心
</el-dropdown-item> </el-dropdown-item>
</el-dropdown-menu> <el-dropdown-item command="settings">
</template> <el-icon><Setting /></el-icon>
</el-dropdown> 设置
</el-dropdown-item>
<el-dropdown-item divided command="logout">
<el-icon><SwitchButton /></el-icon>
退出登录
</el-dropdown-item>
</el-dropdown-menu>
</template>
</el-dropdown>
</template>
<template v-else>
<!-- 未登录状态 -->
<router-link to="/login" class="login-btn">
<el-button type="primary">登录</el-button>
</router-link>
<router-link to="/register" class="register-btn">
<el-button>注册</el-button>
</router-link>
</template>
</div> </div>
</div> </div>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { ref, inject, onMounted } from 'vue' import { onMounted } from 'vue'
import { useRouter } from 'vue-router' import { useRouter } from 'vue-router'
import { ElMessageBox } from 'element-plus' import { useUserStore } from '@/stores/user'
import { User, Setting, SwitchButton, Expand, Fold } from '@element-plus/icons-vue' import { ElMessage } from 'element-plus'
import { User, Setting, SwitchButton } from '@element-plus/icons-vue'
import defaultAvatar from '@/assets/default-avatar.png'
const router = useRouter() const router = useRouter()
const isCollapsed = inject('isCollapsed', ref(false)) const userStore = useUserStore()
// //
const userInfo = ref({ onMounted(async () => {
nickname: '张三', const token = localStorage.getItem('token')
avatar: 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png' if (token && !userStore.userInfo) {
userStore.setToken(token)
await userStore.getUserInfo()
}
}) })
//
const toggleSidebar = () => {
isCollapsed.value = !isCollapsed.value
}
const handleCommand = async (command: string) => { const handleCommand = async (command: string) => {
switch (command) { switch (command) {
case 'profile': case 'profile':
@ -62,68 +76,69 @@ const handleCommand = async (command: string) => {
router.push('/settings') router.push('/settings')
break break
case 'logout': case 'logout':
await handleLogout() userStore.clearUserInfo()
ElMessage.success('退出登录成功')
router.push('/login')
break break
} }
} }
const handleLogout = async () => {
try {
await ElMessageBox.confirm('确定要退出登录吗?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
})
// token
localStorage.removeItem('token')
localStorage.removeItem('userInfo')
//
router.push('/login')
} catch {
//
}
}
</script> </script>
<style scoped> <style scoped>
.top-bar { .topbar {
position: fixed;
top: 0;
right: 0;
left: 0;
height: 60px;
background: #fff;
box-shadow: 0 1px 4px rgba(0,21,41,.08);
display: flex; display: flex;
justify-content: space-between; justify-content: space-between;
align-items: center; align-items: center;
padding: 0 20px; padding: 0 20px;
z-index: 1000; height: 60px;
background-color: #fff;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
} }
.menu-icon { .logo {
font-size: 20px; font-size: 20px;
cursor: pointer; font-weight: bold;
color: #666; color: #333;
} text-decoration: none;
.user-info {
display: flex;
align-items: center;
cursor: pointer;
}
.username {
margin-left: 8px;
font-size: 14px;
color: #666;
} }
.right { .right {
display: flex; display: flex;
align-items: center; align-items: center;
gap: 20px; gap: 16px;
}
.user-info {
display: flex;
align-items: center;
gap: 8px;
cursor: pointer;
padding: 2px 8px;
border-radius: 4px;
transition: background-color 0.3s;
}
.user-info:hover {
background-color: #f5f7fa;
}
.nickname {
font-size: 14px;
color: #333;
}
.login-btn,
.register-btn {
text-decoration: none;
}
.el-dropdown-menu__item {
display: flex;
align-items: center;
gap: 8px;
}
.el-icon {
margin-right: 4px;
} }
</style> </style>

View File

@ -2,6 +2,7 @@ import { defineStore } from 'pinia'
import { ref } from 'vue' import { ref } from 'vue'
import type { UserInfo } from '@/api/user' import type { UserInfo } from '@/api/user'
import { userApi } from '@/api/user' import { userApi } from '@/api/user'
import { ElMessage } from 'element-plus'
export const useUserStore = defineStore('user', () => { export const useUserStore = defineStore('user', () => {
const userInfo = ref<UserInfo | null>(null) const userInfo = ref<UserInfo | null>(null)
@ -10,8 +11,9 @@ export const useUserStore = defineStore('user', () => {
// 获取用户信息 // 获取用户信息
const getUserInfo = async () => { const getUserInfo = async () => {
try { try {
console.log(1111) const data = await userApi.getCurrentUser()
await userApi.getCurrentUser() userInfo.value = data
return data
} catch (error) { } catch (error) {
console.error('获取用户信息失败:', error) console.error('获取用户信息失败:', error)
return null return null
@ -19,8 +21,9 @@ export const useUserStore = defineStore('user', () => {
} }
// 设置token // 设置token
const setToken = (token: string) => { const setToken = (newToken: string) => {
localStorage.setItem('token', token) token.value = newToken
localStorage.setItem('token', newToken)
} }
// 清除用户信息 // 清除用户信息
@ -35,6 +38,6 @@ export const useUserStore = defineStore('user', () => {
token, token,
getUserInfo, getUserInfo,
setToken, setToken,
clearUserInfo, clearUserInfo
} }
}) })