s-upload.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. <el-button type="primary" size="mini">选择文件</el-button>
  24. </template>
  25. <template #tip v-if="tip">
  26. {{ tip }}
  27. </template>
  28. </el-upload>
  29. <el-dialog :visible.sync="dialogVisible" append-to-body>
  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. file_type: { type: String, default: 'jpeg' },
  50. },
  51. components: {},
  52. data: () => ({
  53. dialogVisible: false,
  54. dialogImageUrl: '',
  55. fileList: [],
  56. }),
  57. created() {
  58. if (this.data) {
  59. this.defalutProcess(this.data);
  60. }
  61. },
  62. watch: {
  63. data: {
  64. handler(val) {
  65. this.defalutProcess(val);
  66. },
  67. },
  68. },
  69. computed: {},
  70. methods: {
  71. handlePictureCardPreview(file) {
  72. this.dialogImageUrl = file.url;
  73. this.dialogVisible = true;
  74. },
  75. handleRemove(file, fileList) {
  76. this.$set(this, `fileList`, []);
  77. // let index = fileList.findIndex(f => _.isEqual(f, file));
  78. this.$emit('delete', { file, type: this.type });
  79. return false;
  80. },
  81. outLimit() {
  82. this.$message.error(`只允许上传${this.limit}个文件`);
  83. },
  84. onSuccess(response, file, fileList) {
  85. //将文件整理好传回父组件
  86. this.$emit('upload', { type: this.type, data: { ...response, name: file.name } });
  87. },
  88. beforeUpload(file) {
  89. let setting_type = this.file_type.split('|');
  90. let file_type = file.type.split('/')[1];
  91. let isType = false;
  92. for (const val of setting_type) {
  93. if (val == file_type) isType = true;
  94. }
  95. // 文件大小改为10Mb
  96. const isLt20M = file.size / 1024 / 1024 < 10;
  97. // if (!isType) {
  98. // this.$message.error('图片格式不正确');
  99. // }
  100. if (!isLt20M) {
  101. this.$message.error('文件超出10M');
  102. }
  103. // return isType && isLt20M;
  104. return isLt20M;
  105. },
  106. defalutProcess(val) {
  107. if (_.isArray(val)) {
  108. let newArr = val.map(item => {
  109. let object = {};
  110. object.name = item.name;
  111. object.url = item.url;
  112. return object;
  113. });
  114. this.$set(this, `fileList`, newArr);
  115. } else if (_.isObject(val)) {
  116. let object = {};
  117. if (_.get(val, `url`)) {
  118. object.name = val.name;
  119. object.url = val.url;
  120. this.$set(this, `fileList`, [object]);
  121. }
  122. } else if (typeof val === 'string') {
  123. this.$set(this, `fileList`, [{ name: '附件', url: val }]);
  124. }
  125. },
  126. },
  127. };
  128. </script>
  129. <style lang="less" scoped></style>