custom-upload.vue 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <template>
  2. <div id="c-upload">
  3. <el-upload v-if="url" ref="upload" :action="url" :limit="limit" :accept="accept" :file-list="list" :list-type="listType" :on-exceed="outLimit" :on-preview="filePreview" :on-success="onSuccess" :before-remove="onRemove" :show-file-list="showList">
  4. <el-button type="primary">{{ $t('common.upload_btn') }}</el-button>
  5. <template #tip v-if="tip">
  6. <p style="color: #ff0000">{{ tip }}</p>
  7. </template>
  8. </el-upload>
  9. <el-dialog v-model="dialog.show" append-to-body>
  10. <img width="100%" :src="dialog.url" alt="" />
  11. </el-dialog>
  12. </div>
  13. </template>
  14. <script setup>
  15. import { ElMessage } from 'element-plus'
  16. import { omit, cloneDeep, isArray } from 'lodash-es'
  17. let dialog = ref({ show: false, url: '' })
  18. const props = defineProps({
  19. url: { type: String, default: () => '' },
  20. limit: { type: Number, default: () => 6 },
  21. accept: { type: String, default: () => 'image/png, image/jpeg' },
  22. listType: { type: String, default: () => 'text' }, //'text' | 'picture' | 'picture-card'
  23. tip: { type: String, default: () => undefined },
  24. list: { type: Array, default: () => [] },
  25. model: { type: String, default: () => '' },
  26. showList: { type: Boolean, default: true }
  27. })
  28. // 图片上传地址
  29. const { url } = toRefs(props)
  30. // 可上传文件数目
  31. const { limit } = toRefs(props)
  32. // 接收上传的文件类型
  33. const { accept } = toRefs(props)
  34. // 文件列表的类型--picture-card---picture
  35. const { listType } = toRefs(props)
  36. // 文件提醒
  37. const { tip } = toRefs(props)
  38. // 已有数据,赋值,预览
  39. const { list } = toRefs(props)
  40. const { model } = toRefs(props)
  41. // const list = ref<UploadUserFile[]>([]);
  42. const emit = defineEmits(['change'])
  43. // 图片预览
  44. const filePreview = (file) => {
  45. // this.dialog = { show: true, url: file.url };
  46. window.open(file.url)
  47. }
  48. // 只允许上传多少个文件
  49. const outLimit = () => {
  50. ElMessage.error(`只允许上传${limit.value}个文件`)
  51. }
  52. // 上传成功,response:成功信息,file:图片信息,fileList:图片列表
  53. const onSuccess = (response, file) => {
  54. console.log(response)
  55. if (response.errcode !== 0) {
  56. ElMessage({ type: 'error', message: '删除成功' })
  57. return
  58. }
  59. let ponse = omit(response, ['errcode', 'errmsg'])
  60. let arr = cloneDeep(list)
  61. if (isArray(list.value)) {
  62. arr.value.push({ ...ponse, name: file.name, url: `${response.uri}` })
  63. } else {
  64. arr.value = [{ ...ponse, name: file.name, url: `${response.uri}` }]
  65. }
  66. emit('change', { model: model.value, value: arr.value })
  67. }
  68. // 删除图片
  69. // file: { id: any; uri: string }, fileList: any
  70. const onRemove = () => {
  71. // let arr: Ref<ListItem[]> = _.cloneDeep(list);
  72. // let info = arr.value.filter((f) => f.id != file.id);
  73. // emit('change', info);
  74. return true
  75. }
  76. // #endregion
  77. </script>
  78. <style lang="scss" scoped>
  79. #c-upload {
  80. width: 100%;
  81. }
  82. </style>