UserAvatar.vue 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <template>
  2. <div class="change-avatar">
  3. <CropperAvatar
  4. ref="cropperRef"
  5. :btnProps="{ preIcon: 'ant-design:cloud-upload-outlined' }"
  6. :showBtn="false"
  7. :value="img"
  8. width="120px"
  9. @change="handelUpload"
  10. />
  11. </div>
  12. </template>
  13. <script lang="ts" setup>
  14. import { propTypes } from '@/utils/propTypes'
  15. import { updateUserProfile } from '@/api/system/user/profile'
  16. import { CropperAvatar } from '@/components/Cropper'
  17. import { useUserStore } from '@/store/modules/user'
  18. import { useUpload } from '@/components/UploadFile/src/useUpload'
  19. import { UploadRequestOptions } from 'element-plus/es/components/upload/src/upload'
  20. // TODO @芋艿:合并到 ProfileUser 组件中,更简洁一点
  21. defineOptions({ name: 'UserAvatar' })
  22. defineProps({
  23. img: propTypes.string.def('')
  24. })
  25. const userStore = useUserStore()
  26. const cropperRef = ref()
  27. const handelUpload = async ({ data }) => {
  28. const { httpRequest } = useUpload()
  29. const avatar = ((await httpRequest({
  30. file: data,
  31. filename: 'avatar.png',
  32. } as UploadRequestOptions)) as unknown as { data: string }).data
  33. await updateUserProfile({ avatar })
  34. // 关闭弹窗,并更新 userStore
  35. cropperRef.value.close()
  36. await userStore.setUserAvatarAction(avatar)
  37. }
  38. </script>
  39. <style lang="scss" scoped>
  40. .change-avatar {
  41. img {
  42. display: block;
  43. margin-bottom: 15px;
  44. border-radius: 50%;
  45. }
  46. }
  47. </style>