custom-upload.vue 2.9 KB

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