UserAvatar.vue 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. defineOptions({ name: 'UserAvatar' })
  21. defineProps({
  22. img: propTypes.string.def('')
  23. })
  24. const userStore = useUserStore()
  25. const cropperRef = ref()
  26. const handelUpload = async ({ data }) => {
  27. const { httpRequest } = useUpload()
  28. const avatar = (
  29. (await httpRequest({
  30. file: data,
  31. filename: 'avatar.png'
  32. } as UploadRequestOptions)) as unknown as { data: string }
  33. ).data
  34. await updateUserProfile({ avatar })
  35. // 关闭弹窗,并更新 userStore
  36. cropperRef.value.close()
  37. await userStore.setUserAvatarAction(avatar)
  38. }
  39. </script>
  40. <style lang="scss" scoped>
  41. .change-avatar {
  42. img {
  43. display: block;
  44. margin-bottom: 15px;
  45. border-radius: 50%;
  46. }
  47. }
  48. </style>