upload.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <template>
  2. <div id="upload">
  3. <el-upload
  4. v-if="url"
  5. ref="upload"
  6. :action="url"
  7. :list-type="listType"
  8. :file-list="fileList"
  9. :limit="limit"
  10. :on-exceed="outLimit"
  11. :on-preview="handlePictureCardPreview"
  12. :before-remove="handleRemove"
  13. :on-success="onSuccess"
  14. :before-upload="beforeUpload"
  15. :show-file-list="showList"
  16. :accept="accept"
  17. >
  18. <el-button size="small" type="primary" v-if="isBtn">点击上传</el-button>
  19. <template v-else-if="uploadBtn">
  20. <el-button type="danger">选择文件</el-button>
  21. </template>
  22. <template v-else>
  23. <i class="el-icon-plus"></i>
  24. </template>
  25. <template #tip v-if="tip">
  26. {{ tip }}
  27. </template>
  28. </el-upload>
  29. <el-dialog :visible.sync="dialogVisible">
  30. <img width="100%" :src="dialogImageUrl" alt="" />
  31. </el-dialog>
  32. </div>
  33. </template>
  34. <script>
  35. import _ from 'lodash';
  36. export default {
  37. name: 'upload',
  38. props: {
  39. url: { type: null },
  40. limit: { type: Number },
  41. data: { type: null },
  42. type: { type: String },
  43. isBtn: { type: Boolean, default: false },
  44. uploadBtn: { type: Boolean, default: false },
  45. showList: { type: Boolean, default: true },
  46. accept: { type: String, default: '' },
  47. tip: { type: String, default: undefined },
  48. listType: { type: String, default: 'picture-card' },
  49. },
  50. components: {},
  51. data: () => ({
  52. dialogVisible: false,
  53. dialogImageUrl: '',
  54. fileList: [],
  55. }),
  56. created() {
  57. if (this.data) {
  58. this.defalutProcess(this.data);
  59. }
  60. },
  61. watch: {
  62. data: {
  63. handler(val) {
  64. this.defalutProcess(val);
  65. },
  66. },
  67. },
  68. computed: {},
  69. methods: {
  70. handlePictureCardPreview(file) {
  71. this.dialogImageUrl = file.url;
  72. this.dialogVisible = false;
  73. },
  74. handleRemove(file, fileList) {
  75. let index = fileList.findIndex(f => _.isEqual(f, file));
  76. this.$emit('delete', index);
  77. return false;
  78. },
  79. outLimit() {
  80. this.$message.error(`只允许上传${this.limit}个文件`);
  81. },
  82. onSuccess(response, file, fileList) {
  83. //将文件整理好传回父组件
  84. this.$emit('upload', { type: this.type, data: { ...response, name: file.name } });
  85. },
  86. beforeUpload(file) {
  87. const sizeLimit = file.size / 1024 / 1024 < 10;
  88. if (sizeLimit) return true;
  89. else {
  90. this.$message.error('文件超出10M!');
  91. return false;
  92. }
  93. },
  94. defalutProcess(val) {
  95. if (_.isArray(val)) {
  96. let newArr = val.map(item => {
  97. let object = {};
  98. object.name = item.name;
  99. object.url = item.url;
  100. return object;
  101. });
  102. this.$set(this, `fileList`, newArr);
  103. } else if (_.isObject(val)) {
  104. let object = {};
  105. if (_.get(val, `url`)) {
  106. object.name = val.name;
  107. object.url = val.url;
  108. this.$set(this, `fileList`, [object]);
  109. }
  110. } else if (typeof val === 'string') {
  111. this.$set(this, `fileList`, [{ name: '附件', url: val }]);
  112. }
  113. },
  114. },
  115. };
  116. </script>
  117. <style lang="less" scoped></style>