update-avatar.vue 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <script setup lang="ts">
  2. import type { UploadChangeParam } from 'ant-design-vue';
  3. import { computed } from 'vue';
  4. import { useAccessStore, useUserStore } from '@vben/stores';
  5. import { Avatar, message, Upload } from 'ant-design-vue';
  6. import { Icon } from '#/components/icon';
  7. defineProps({
  8. avatar: {
  9. type: String,
  10. default: '',
  11. },
  12. });
  13. const accessStore = useAccessStore();
  14. const userStore = useUserStore();
  15. const getAction = computed(() => {
  16. return `${import.meta.env.VITE_GLOB_API_URL}/user/avatar`;
  17. });
  18. const headers = {
  19. Authorization: `Bearer ${accessStore.accessToken}`,
  20. };
  21. const handleChange = (info: UploadChangeParam) => {
  22. if (info.file.status === 'done') {
  23. userStore.setUserInfo({
  24. ...userStore.userInfo,
  25. avatar: info.file.response.data.url,
  26. } as any);
  27. } else if (info.file.status === 'error') {
  28. message.error(`${info.file.name} file upload failed.`);
  29. }
  30. };
  31. </script>
  32. <template>
  33. <Upload
  34. :action="getAction"
  35. :headers="headers"
  36. :max-count="1"
  37. :show-upload-list="false"
  38. accept="image/*"
  39. class="relative"
  40. @change="handleChange"
  41. >
  42. <Avatar :size="120" :src="avatar" />
  43. <div
  44. class="absolute left-[50%-120px] top-[0px] flex h-[120px] w-[120px] cursor-pointer items-center justify-center rounded-full bg-black opacity-0 hover:opacity-30"
  45. >
  46. <Icon :size="48" icon="ant-design:cloud-upload-outlined" />
  47. </div>
  48. </Upload>
  49. </template>
  50. <style lang="less" scoped></style>