Upload.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <template>
  2. <div>
  3. <el-upload
  4. ref="upload"
  5. :action="newUrl"
  6. :headers="myHeaders"
  7. list-type="picture-card"
  8. :multiple="false"
  9. :limit="1"
  10. :file-list="imgList"
  11. :on-success="handleAvatarSuccess"
  12. :disabled="limit <= value.length"
  13. >{{ newUrl }}
  14. <i slot="default" class="el-icon-plus" />
  15. <div slot="file" slot-scope="{ file }">
  16. <img class="el-upload-list__item-thumbnail" :src="file.url" alt="" />
  17. <span class="el-upload-list__item-actions">
  18. <span
  19. class="el-upload-list__item-preview"
  20. @click="handlePictureCardPreview(file)"
  21. >
  22. <i class="el-icon-zoom-in" />
  23. </span>
  24. <!-- <span
  25. v-if="!disabled"
  26. class="el-upload-list__item-delete"
  27. @click="handleDownload(file)"
  28. >
  29. <i class="el-icon-download" />
  30. </span> -->
  31. <span
  32. v-if="!disabled"
  33. class="el-upload-list__item-delete"
  34. @click="handleRemove(file)"
  35. >
  36. <i class="el-icon-delete" />
  37. </span>
  38. </span>
  39. </div>
  40. </el-upload>
  41. <el-dialog :visible.sync="dialogVisible" :modal="false">
  42. <img width="100%" :src="dialogImageUrl" alt="" />
  43. </el-dialog>
  44. </div>
  45. </template>
  46. <script>
  47. import { UPLOAD_URL } from '@/const/urlKey';
  48. import { getToken } from '@/utils/auth';
  49. export default {
  50. name: 'Upload',
  51. props: {
  52. value: {
  53. type: Array,
  54. default: () => []
  55. },
  56. limit: {
  57. type: Number,
  58. default: 1
  59. },
  60. params: {
  61. type: String,
  62. default: ''
  63. },
  64. url: {
  65. type: String,
  66. default: ''
  67. }
  68. },
  69. data() {
  70. return {
  71. url: UPLOAD_URL,
  72. dialogImageUrl: '',
  73. dialogVisible: false,
  74. disabled: false,
  75. myHeaders: {
  76. authorization: 'Bearer swagger-ui',
  77. token: getToken()
  78. },
  79. imgList: this.value,
  80. tmp: []
  81. };
  82. },
  83. computed: {
  84. newUrl() {
  85. const params = this.params;
  86. const url = this.url;
  87. let baseUrl = UPLOAD_URL;
  88. if (url !== '') {
  89. baseUrl = url;
  90. }
  91. if (params === '') {
  92. return baseUrl;
  93. } else {
  94. return baseUrl + `?${params}`;
  95. }
  96. }
  97. },
  98. watch: {
  99. value: {
  100. handler(val) {
  101. // if (this.imgList.length < val.length) {
  102. this.imgList = val;
  103. console.log(this.imgList);
  104. // }
  105. },
  106. immediate: true
  107. }
  108. },
  109. methods: {
  110. beforeAvatarUpload(file) {
  111. console.log();
  112. },
  113. handleAvatarSuccess(res, file, fileList) {
  114. if (file.response.code !== 200) {
  115. this.$refs.upload.clearFiles();
  116. this.$message.error('上传失败,请稍后再试~');
  117. this.$emit('input', []);
  118. return false;
  119. }
  120. const result =
  121. fileList.length > 0
  122. ? fileList.map(x => ({
  123. name: x.name,
  124. url: process.env.VUE_APP_BASE_IMG + x.response.data.fullPath
  125. }))
  126. : [];
  127. this.$emit('input', result);
  128. },
  129. // handleChange(file, fileList) {
  130. // if (file.response) {
  131. // // this.imgList = this.tmp
  132. // }
  133. // console.log(file)
  134. // console.log(fileList)
  135. // // const result = fileList.length > 0 ? fileList.map(x => ({
  136. // // name: x.name,
  137. // // url: x.response.data.path
  138. // // // url: x.url
  139. // // })) : []
  140. // // this.imgList = result
  141. // // const result = this.tmp
  142. // // this.imgList = JSON.parse(JSON.stringify(this.tmp))
  143. // },
  144. handleRemove(file, fileList) {
  145. const result = fileList
  146. ? fileList.map(x => ({
  147. name: x.name,
  148. url: x.response.data.path
  149. }))
  150. : [];
  151. this.$emit('input', result);
  152. },
  153. handlePictureCardPreview(file) {
  154. this.dialogImageUrl = file.url;
  155. this.dialogVisible = true;
  156. }
  157. }
  158. };
  159. </script>
  160. <style scoped>
  161. .page-select {
  162. display: flex;
  163. justify-content: flex-end;
  164. align-items: center;
  165. padding: 20px;
  166. }
  167. </style>