CropperAvatar.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <template>
  2. <div class="user-info-head" @click="open()">
  3. <img v-if="sourceValue" :src="sourceValue" alt="avatar" class="img-circle img-lg" />
  4. <el-button v-if="showBtn" :class="`${prefixCls}-upload-btn`" @click="open()">
  5. {{ btnText ? btnText : t('cropper.selectImage') }}
  6. </el-button>
  7. <CopperModal
  8. ref="cropperModelRef"
  9. :srcValue="sourceValue"
  10. @upload-success="handleUploadSuccess"
  11. />
  12. </div>
  13. </template>
  14. <script lang="ts" name="CropperAvatar" setup>
  15. import { useDesign } from '@/hooks/web/useDesign'
  16. import { propTypes } from '@/utils/propTypes'
  17. import { useI18n } from 'vue-i18n'
  18. import CopperModal from './CopperModal.vue'
  19. const props = defineProps({
  20. width: propTypes.string.def('200px'),
  21. value: propTypes.string.def(''),
  22. showBtn: propTypes.bool.def(true),
  23. btnText: propTypes.string.def('')
  24. })
  25. const emit = defineEmits(['update:value', 'change'])
  26. const sourceValue = ref(props.value)
  27. const { getPrefixCls } = useDesign()
  28. const prefixCls = getPrefixCls('cropper-avatar')
  29. const message = useMessage()
  30. const { t } = useI18n()
  31. const cropperModelRef = ref()
  32. watchEffect(() => {
  33. sourceValue.value = props.value
  34. })
  35. watch(
  36. () => sourceValue.value,
  37. (v: string) => {
  38. emit('update:value', v)
  39. }
  40. )
  41. function handleUploadSuccess({ source, data, filename }) {
  42. sourceValue.value = source
  43. emit('change', { source, data, filename })
  44. message.success(t('cropper.uploadSuccess'))
  45. }
  46. function open() {
  47. cropperModelRef.value.openModal()
  48. }
  49. function close() {
  50. cropperModelRef.value.closeModal()
  51. }
  52. defineExpose({
  53. open,
  54. close
  55. })
  56. </script>
  57. <style lang="scss" scoped>
  58. $prefix-cls: #{$namespace}--cropper-avatar;
  59. .#{$prefix-cls} {
  60. display: inline-block;
  61. text-align: center;
  62. &-image-wrapper {
  63. overflow: hidden;
  64. cursor: pointer;
  65. border: 1px solid;
  66. border-radius: 50%;
  67. img {
  68. width: 100%;
  69. }
  70. }
  71. &-image-mask {
  72. opacity: 0%;
  73. position: absolute;
  74. width: inherit;
  75. height: inherit;
  76. border-radius: inherit;
  77. border: inherit;
  78. background: rgb(0 0 0 / 40%);
  79. cursor: pointer;
  80. transition: opacity 0.4s;
  81. ::v-deep(svg) {
  82. margin: auto;
  83. }
  84. }
  85. &-image-mask:hover {
  86. opacity: 4000%;
  87. }
  88. &-upload-btn {
  89. margin: 10px auto;
  90. }
  91. }
  92. .user-info-head {
  93. position: relative;
  94. display: inline-block;
  95. }
  96. .img-circle {
  97. border-radius: 50%;
  98. }
  99. .img-lg {
  100. width: 120px;
  101. height: 120px;
  102. }
  103. .user-info-head:hover:after {
  104. content: '+';
  105. position: absolute;
  106. left: 0;
  107. right: 0;
  108. top: 0;
  109. bottom: 0;
  110. color: #eee;
  111. background: rgba(0, 0, 0, 0.5);
  112. font-size: 24px;
  113. font-style: normal;
  114. -webkit-font-smoothing: antialiased;
  115. -moz-osx-font-smoothing: grayscale;
  116. cursor: pointer;
  117. line-height: 110px;
  118. border-radius: 50%;
  119. }
  120. </style>