index.vue 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <template>
  2. <el-dropdown @command="handleCommand" @visible-change="handleVisibleChange">
  3. <span class="avatar-dropdown">
  4. <el-avatar class="user-avatar" :src="avatar" />
  5. <div class="username">
  6. <span class="hidden-xs-only">{{ username }}</span>
  7. <vab-icon class="vab-dropdown" :class="{ 'vab-dropdown-active': active }" icon="arrow-down-s-line" />
  8. </div>
  9. </span>
  10. <template #dropdown>
  11. <el-dropdown-menu>
  12. <el-dropdown-item command="logout">
  13. <vab-icon icon="logout-circle-r-line" />
  14. <span>{{ translate('退出登录') }}</span>
  15. </el-dropdown-item>
  16. </el-dropdown-menu>
  17. </template>
  18. </el-dropdown>
  19. </template>
  20. <script lang="ts" setup>
  21. import { translate } from '/@/i18n'
  22. import { useUserStore } from '/@/store/modules/user'
  23. import { toLoginRoute } from '/@/utils/routes'
  24. defineOptions({
  25. name: 'VabAvatar',
  26. })
  27. const route = useRoute()
  28. const router = useRouter()
  29. const userStore = useUserStore()
  30. const { avatar, username } = storeToRefs(userStore)
  31. const { logout } = userStore
  32. const active = ref<boolean>(false)
  33. const handleVisibleChange = (value: boolean) => {
  34. active.value = value
  35. }
  36. const handleCommand = async (command: any) => {
  37. switch (command) {
  38. case 'logout': {
  39. await logout()
  40. await router.push(toLoginRoute(route.fullPath))
  41. break
  42. }
  43. }
  44. }
  45. </script>
  46. <style lang="scss" scoped>
  47. .avatar-dropdown {
  48. display: flex;
  49. align-content: center;
  50. align-items: center;
  51. justify-content: center;
  52. justify-items: center;
  53. .user-avatar {
  54. box-sizing: border-box;
  55. width: 40px;
  56. height: 40px;
  57. padding: 8px;
  58. margin-left: 15px;
  59. cursor: pointer;
  60. border-radius: 50%;
  61. }
  62. .username {
  63. position: relative;
  64. display: flex;
  65. align-content: center;
  66. align-items: center;
  67. width: max-content;
  68. height: 40px;
  69. margin-left: 6px;
  70. line-height: 40px;
  71. cursor: pointer;
  72. [class*='ri-'] {
  73. margin-left: 0 !important;
  74. }
  75. }
  76. }
  77. </style>