Profile.vue 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <script lang="ts" setup>
  2. import defaultAvatar from '@/assets/avatar.jpg';
  3. import { computed } from 'vue';
  4. import { useDialog } from 'naive-ui';
  5. import { useUserStore } from '@/store';
  6. import { useRouter } from 'vue-router';
  7. import { SvgIcon } from '@/components/common';
  8. import { useBasicLayout } from '@/hooks/useBasicLayout';
  9. const { isMobile } = useBasicLayout();
  10. const router = useRouter();
  11. const dialog = useDialog();
  12. const userStore = useUserStore();
  13. const user = computed(() => userStore.user);
  14. async function onLogout() {
  15. dialog.warning({
  16. title: '提示',
  17. content: '你确定注销当前账户吗',
  18. positiveText: '注销',
  19. negativeText: '取消',
  20. onPositiveClick: async () => {
  21. await userStore.logout();
  22. userStore.changeIsLogin();
  23. },
  24. });
  25. }
  26. async function onLogin() {
  27. userStore.changeIsLogin();
  28. }
  29. function onProfile() {
  30. router.push({ name: 'Profile' });
  31. }
  32. </script>
  33. <template>
  34. <template v-if="user == null">
  35. <n-avatar class="cursor-pointer !text-black" round>
  36. <SvgIcon class="text-2xl" icon="solar:user-broken" />
  37. </n-avatar>
  38. <n-button
  39. :class="isMobile ? '!py-4' : '!py-4'"
  40. :size="isMobile ? 'tiny' : 'small'"
  41. block
  42. class="!rounded-lg"
  43. secondary
  44. type="success"
  45. @click="onLogin()"
  46. >
  47. <span class="text-center w-full">登录系统</span>
  48. </n-button>
  49. </template>
  50. <div v-else class="flex w-full flex-col gap-3 mb-2 px-2">
  51. <div class="flex gap-2 items-center px-2">
  52. <n-avatar
  53. :fallback-src="defaultAvatar"
  54. :src="user.avatar ?? '/avatar.jpg'"
  55. class="cursor-pointer w-[30px]"
  56. round
  57. />
  58. <n-ellipsis style="max-width: 85px">
  59. {{ user.username }}
  60. </n-ellipsis>
  61. </div>
  62. <n-button class="!rounded-lg" size="small" tertiary type="info" @click="onProfile()">
  63. <div class="w-full text-center flex justify-center items-center gap-2">
  64. <SvgIcon class="text-lg" icon="iconamoon:profile" />
  65. <span>个人中心</span>
  66. </div>
  67. </n-button>
  68. <n-button class="!rounded-lg" secondary size="small" type="warning" @click="onLogout()">
  69. <div class="w-full text-center flex justify-center items-center gap-2">
  70. <SvgIcon class="text-lg" icon="material-symbols:logout" />
  71. <span>注销账户</span>
  72. </div>
  73. </n-button>
  74. </div>
  75. </template>
  76. <style lang="less" scoped></style>