upload.vue 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <template>
  2. <div id="upload">
  3. <el-upload
  4. v-if="url"
  5. ref="upload"
  6. :action="url"
  7. list-type="picture-card"
  8. :file-list="fileList"
  9. :limit="limit"
  10. :on-exceed="outLimit"
  11. :on-preview="handlePictureCardPreview"
  12. :before-remove="handleRemove"
  13. :on-success="onSuccess"
  14. >
  15. <template>
  16. <i class="el-icon-plus"></i>
  17. </template>
  18. </el-upload>
  19. <el-dialog :visible.sync="dialogVisible">
  20. <img width="100%" :src="dialogImageUrl" alt="" />
  21. </el-dialog>
  22. </div>
  23. </template>
  24. <script>
  25. import _ from 'lodash';
  26. export default {
  27. name: 'upload',
  28. props: {
  29. url: { type: null },
  30. limit: { type: Number },
  31. data: { type: null },
  32. type: { type: String },
  33. },
  34. components: {},
  35. data: () => ({
  36. dialogVisible: false,
  37. dialogImageUrl: '',
  38. fileList: [],
  39. }),
  40. created() {
  41. if (this.data) {
  42. this.defalutProcess(this.data);
  43. }
  44. },
  45. watch: {
  46. data: {
  47. handler(val) {
  48. this.defalutProcess(val);
  49. },
  50. },
  51. },
  52. computed: {},
  53. methods: {
  54. handlePictureCardPreview(file) {
  55. this.dialogImageUrl = file.url;
  56. this.dialogVisible = true;
  57. },
  58. handleRemove(file) {
  59. return true;
  60. },
  61. outLimit() {
  62. this.$message.error('只允许上传1张');
  63. },
  64. onSuccess(response, file, fileList) {
  65. //将文件整理好传回父组件
  66. this.$emit('upload', { type: this.type, data: response });
  67. },
  68. defalutProcess(val) {
  69. if (typeof val === 'object' && _.get(val, length) !== undefined && val.length > 0) {
  70. let newArr = [];
  71. val.map(item => {
  72. let object = {};
  73. object.name = item.name;
  74. object.url = `${item.uri}`;
  75. newArr.push(object);
  76. });
  77. this.$set(this, `fileList`, newArr);
  78. } else if (typeof val === 'object' && _.get(val, length) === undefined) {
  79. let object = {};
  80. object.name = val.name;
  81. object.url = `${val.uri}`;
  82. this.$set(this, `fileList`, [object]);
  83. } else if (typeof val === 'string') {
  84. this.$set(this, `fileList`, [{ name: '附件', url: val }]);
  85. }
  86. },
  87. },
  88. };
  89. </script>
  90. <style lang="less" scoped></style>