hc-form-upload.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <template>
  2. <el-upload
  3. v-loading="isLoading"
  4. drag
  5. :accept="accept" :action="action" :class="isFocus ? 'is-focus' : ''"
  6. :disabled="isLoading" :headers="getHeader()" :keyname="isKeyName"
  7. :on-error="formUploadError"
  8. :on-progress="uploadprogress" :placeholder="placeholder" :show-file-list="false"
  9. class="hc-upload-table-form"
  10. element-loading-text="上传中..." @exceed="formUploadExceed" @success="formUploadSuccess"
  11. >
  12. <img v-if="isSrc" :src="isSrc" alt="" class="hc-table-form-img">
  13. <div v-else class="hc-table-form-icon">
  14. 点此选择或者拖拽文件并上传
  15. </div>
  16. <div v-if="isSrc" class="hc-table-form-del">
  17. <el-button plain type="danger" @click.stop="delTableFormFile">
  18. 删除当前文件
  19. </el-button>
  20. </div>
  21. <input :id="isKeyName" v-model="isSrc" class="hc-upload-input-src" @blur="handleBlur" @focus="handleFocus">
  22. </el-upload>
  23. </template>
  24. <script setup>
  25. import { ref, watch } from 'vue'
  26. import { getHeader } from 'hc-vue3-ui'
  27. const props = defineProps({
  28. src: {
  29. type: [Number, String],
  30. default: '',
  31. },
  32. keyname: {
  33. type: [Number, String],
  34. default: '',
  35. },
  36. placeholder: {
  37. type: [Number, String],
  38. default: '相片',
  39. },
  40. })
  41. //事件
  42. const emit = defineEmits(['success', 'del'])
  43. //变量
  44. const isLoading = ref(false)
  45. const isSrc = ref(props.src)
  46. const isKeyName = ref(props.keyname)
  47. const action = '/api/blade-manager/exceltab/add-buss-imginfo'
  48. const accept = 'image/png,image/jpg,image/jpeg'
  49. //监听
  50. watch(() => [
  51. props.src,
  52. props.keyname,
  53. ], ([src, keyname]) => {
  54. isSrc.value = src
  55. isKeyName.value = keyname
  56. })
  57. //上传进度
  58. const uploadprogress = () => {
  59. isLoading.value = true
  60. }
  61. //上传完成
  62. const formUploadSuccess = (res) => {
  63. isLoading.value = false
  64. if (res.code === 200) {
  65. const link = res.data?.link || ''
  66. emit('success', {
  67. res,
  68. src: link,
  69. key: isKeyName.value,
  70. })
  71. }
  72. }
  73. //上传失败
  74. const formUploadError = () => {
  75. isLoading.value = false
  76. }
  77. //格式错误
  78. const formUploadExceed = () => {
  79. isLoading.value = false
  80. }
  81. //删除上传的文件
  82. const delTableFormFile = () => {
  83. emit('del', isKeyName.value)
  84. }
  85. const isFocus = ref(false)
  86. //获得焦点
  87. const handleFocus = () => {
  88. isFocus.value = true
  89. }
  90. //失去焦点
  91. const handleBlur = () => {
  92. isFocus.value = false
  93. }
  94. </script>
  95. <style lang="scss" scoped>
  96. .hc-upload-table-form {
  97. display: flex;
  98. flex-direction: column;
  99. justify-content: center; /* 纵向居中 */
  100. // align-items: center; /* 横向居中,如果需要的话 */
  101. height: 100%; /* 确保容器有高度 */
  102. border-radius: 3px;
  103. &.is-focus, &:hover {
  104. background-color: #eddac4;
  105. box-shadow: 0 0 0 1.5px var(--el-color-primary) inset;
  106. }
  107. .hc-upload-input-src {
  108. position: absolute;
  109. z-index: -1;
  110. right: 10px;
  111. width: 10px;
  112. }
  113. }
  114. .hc-table-form-icon {
  115. display: flex;
  116. align-items: center;
  117. justify-content: center;
  118. width: 100%; /* 如果需要的话,确保宽度填满容器 */
  119. height: 100%; /* 如果需要的话,确保高度填满容器 */
  120. }
  121. </style>
  122. <style lang="scss">
  123. .hc-upload-table-form{
  124. border-radius: 3px;
  125. transition: box-shadow 0.3s, background-color 0.3s;
  126. &.is-focus, &:hover {
  127. background-color: #eddac4;
  128. box-shadow: 0 0 0 1.5px var(--el-color-primary) inset;
  129. }
  130. .el-upload-dragger{
  131. height:100%;
  132. width: 100%;
  133. background-color: transparent;
  134. padding: 10px;
  135. text-align: left;
  136. border:none;
  137. }
  138. }
  139. </style>