upload.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. :before-remove="handleRemove"
  12. :on-success="onSuccess"
  13. :before-upload="beforeUpload"
  14. :show-file-list="showList"
  15. :accept="accept"
  16. >
  17. <!-- :on-preview="handlePictureCardPreview" -->
  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. this.defalutProcess(val);
  74. },
  75. deep: true,
  76. },
  77. },
  78. computed: {},
  79. methods: {
  80. handlePictureCardPreview(file) {
  81. let uri = _.get(file, 'uri', _.get(file, 'url', _.get(file.response, 'uri', _.get(file.response, 'url'))));
  82. this.dialogImageUrl = uri;
  83. this.dialogVisible = true;
  84. },
  85. handleRemove(file, fileList) {
  86. let index = fileList.findIndex(f => _.isEqual(f, file));
  87. this.$emit('delete', index);
  88. return false;
  89. },
  90. outLimit() {
  91. this.$message.error(`只允许上传${this.limit}个文件`);
  92. },
  93. onSuccess(response, file, fileList) {
  94. //将文件整理好传回父组件
  95. this.$emit('upload', { type: this.type, data: { ...response, name: file.name } });
  96. },
  97. beforeUpload(file) {
  98. const sizeLimit = file.size / 1024 / 1024 < 10;
  99. if (sizeLimit) return true;
  100. else {
  101. this.$message.error('文件超出10M!');
  102. return false;
  103. }
  104. },
  105. toChangeName() {
  106. this.$emit('changeName', { uri: this.dialogImageUrl, name: this.form.name, type: this.type });
  107. this.dialogVisible = false;
  108. this.$forceUpdate();
  109. },
  110. defalutProcess(val) {
  111. if (_.isArray(val)) {
  112. let newArr = [];
  113. val.map(item => {
  114. let object = {};
  115. object.name = item.name;
  116. object.url = `${item.uri}`;
  117. newArr.push(object);
  118. });
  119. this.$set(this, `fileList`, newArr);
  120. } else if (_.isObject(val)) {
  121. let object = {};
  122. object.name = val.name;
  123. object.url = `${val.uri}`;
  124. this.$set(this, `fileList`, [object]);
  125. } else if (typeof val === 'string') {
  126. this.$set(this, `fileList`, [{ name: '附件', url: val }]);
  127. }
  128. },
  129. },
  130. };
  131. </script>
  132. <style lang="less" scoped></style>