upload.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. <el-form :model="form" size="mini" label-width="80px" :inline="true">
  31. <el-form-item label="文件名">
  32. <el-input v-model="form.name" placeholder="若需要修改名称,请在此处输入后点击保存" style="width:300px"></el-input>
  33. </el-form-item>
  34. <el-form-item label="">
  35. <el-button type="primary" @click="toChangeName">保存</el-button>
  36. </el-form-item>
  37. </el-form>
  38. <img width="100%" :src="dialogImageUrl" alt="" />
  39. </el-dialog>
  40. </div>
  41. </template>
  42. <script>
  43. import _ from 'lodash';
  44. export default {
  45. name: 'upload',
  46. props: {
  47. url: { type: null },
  48. limit: { type: Number },
  49. data: { type: null },
  50. type: { type: String },
  51. isBtn: { type: Boolean, default: false },
  52. uploadBtn: { type: Boolean, default: false },
  53. showList: { type: Boolean, default: true },
  54. accept: { type: String, default: '' },
  55. tip: { type: String, default: undefined },
  56. listType: { type: String, default: 'picture-card' },
  57. },
  58. components: {},
  59. data: () => ({
  60. dialogVisible: false,
  61. dialogImageUrl: '',
  62. fileList: [],
  63. form: {},
  64. }),
  65. created() {
  66. if (this.data) {
  67. this.defalutProcess(this.data);
  68. }
  69. },
  70. watch: {
  71. data: {
  72. handler(val) {
  73. console.log('in function:');
  74. this.defalutProcess(val);
  75. },
  76. deep: true,
  77. },
  78. },
  79. computed: {},
  80. methods: {
  81. handlePictureCardPreview(file) {
  82. let uri = _.get(file, 'uri', _.get(file, 'url', _.get(file.response, 'uri', _.get(file.response, 'url'))));
  83. this.dialogImageUrl = uri;
  84. this.dialogVisible = true;
  85. },
  86. handleRemove(file, fileList) {
  87. let index = fileList.findIndex(f => _.isEqual(f, file));
  88. this.$emit('delete', index);
  89. return false;
  90. },
  91. outLimit() {
  92. this.$message.error(`只允许上传${this.limit}个文件`);
  93. },
  94. onSuccess(response, file, fileList) {
  95. //将文件整理好传回父组件
  96. this.$emit('upload', { type: this.type, data: { ...response, name: file.name } });
  97. },
  98. beforeUpload(file) {
  99. const sizeLimit = file.size / 1024 / 1024 < 10;
  100. if (sizeLimit) return true;
  101. else {
  102. this.$message.error('文件超出10M!');
  103. return false;
  104. }
  105. },
  106. toChangeName() {
  107. this.$emit('changeName', { uri: this.dialogImageUrl, name: this.form.name, type: this.type });
  108. this.dialogVisible = false;
  109. this.$forceUpdate();
  110. },
  111. defalutProcess(val) {
  112. if (_.isArray(val)) {
  113. let newArr = [];
  114. val.map(item => {
  115. let object = {};
  116. object.name = item.name;
  117. object.url = `${item.uri}`;
  118. newArr.push(object);
  119. });
  120. this.$set(this, `fileList`, newArr);
  121. } else if (_.isObject(val)) {
  122. let object = {};
  123. object.name = val.name;
  124. object.url = `${val.uri}`;
  125. this.$set(this, `fileList`, [object]);
  126. } else if (typeof val === 'string') {
  127. this.$set(this, `fileList`, [{ name: '附件', url: val }]);
  128. }
  129. },
  130. },
  131. };
  132. </script>
  133. <style lang="less" scoped></style>