upload.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. :show-file-list="showList"
  15. :accept="accept"
  16. >
  17. <el-button size="small" class="isBtn" v-if="isBtn">点击上传文件</el-button>
  18. <template v-else>
  19. <i class="el-icon-plus"></i>
  20. </template>
  21. <template #tip v-if="tip">
  22. <span class="tip">{{ tip }}</span>
  23. </template>
  24. <template v-if="downBtn">
  25. <el-link :underline="false" @click="clickDown" class="links">下载模板</el-link>
  26. </template>
  27. </el-upload>
  28. <el-dialog :visible.sync="dialogVisible">
  29. <img width="100%" :src="dialogImageUrl" alt="" />
  30. </el-dialog>
  31. </div>
  32. </template>
  33. <script>
  34. import _ from 'lodash';
  35. export default {
  36. name: 'upload',
  37. props: {
  38. url: { type: null },
  39. limit: { type: Number },
  40. data: { type: null },
  41. type: { type: String },
  42. isBtn: { type: Boolean, default: false },
  43. downBtn: { type: Boolean, default: false },
  44. showList: { type: Boolean, default: true },
  45. accept: { type: String },
  46. tip: { type: String, default: undefined },
  47. listType: { type: String, default: 'picture-card' },
  48. },
  49. components: {},
  50. data: () => ({
  51. dialogVisible: false,
  52. dialogImageUrl: '',
  53. fileList: [],
  54. }),
  55. created() {
  56. if (this.data) {
  57. this.defalutProcess(this.data);
  58. }
  59. },
  60. watch: {
  61. data: {
  62. handler(val) {
  63. this.defalutProcess(val);
  64. },
  65. },
  66. },
  67. computed: {},
  68. methods: {
  69. handlePictureCardPreview(file) {
  70. this.dialogImageUrl = file.url;
  71. this.dialogVisible = true;
  72. },
  73. handleRemove(file) {
  74. return true;
  75. },
  76. outLimit() {
  77. this.$message.error('只允许上传1个文件');
  78. },
  79. onSuccess(response, file, fileList) {
  80. //将文件整理好传回父组件
  81. this.$emit('upload', { type: this.type, data: response });
  82. },
  83. defalutProcess(val) {
  84. if (typeof val === 'object' && _.get(val, length) !== undefined && val.length > 0) {
  85. let newArr = [];
  86. val.map(item => {
  87. let object = {};
  88. object.name = item.name;
  89. object.url = `${item.uri}`;
  90. newArr.push(object);
  91. });
  92. this.$set(this, `fileList`, newArr);
  93. } else if (typeof val === 'object' && _.get(val, length) === undefined) {
  94. let object = {};
  95. object.name = val.name;
  96. object.url = `${val.uri}`;
  97. this.$set(this, `fileList`, [object]);
  98. } else if (typeof val === 'string') {
  99. this.$set(this, `fileList`, [{ name: '附件', url: val }]);
  100. }
  101. },
  102. clickDown() {
  103. this.$emit('clickDown');
  104. },
  105. },
  106. };
  107. </script>
  108. <style lang="less" scoped>
  109. .isBtn {
  110. background-color: #e9021d;
  111. color: #ffffff;
  112. }
  113. .links {
  114. margin: 0 0 0 15px;
  115. }
  116. .tip {
  117. padding: 0 10px;
  118. display: inline-block;
  119. color: #999999;
  120. }
  121. </style>