Upload.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <template>
  2. <div>
  3. <el-upload
  4. v-loading.fullscreen.lock="mark"
  5. ref="upload"
  6. :action="newUrl"
  7. :headers="myHeaders"
  8. list-type="picture-card"
  9. :multiple="false"
  10. :limit="limit"
  11. :file-list="imgList"
  12. :on-success="handleAvatarSuccess"
  13. :on-error="handleError"
  14. :on-progress="onProgress"
  15. :before-upload="handleBeforeUpload"
  16. :disabled="limit <= value.length"
  17. >
  18. <i slot="default" class="el-icon-plus" />
  19. <div slot="file" slot-scope="{ file }">
  20. <img class="el-upload-list__item-thumbnail" :src="file.url" alt="" />
  21. <span class="el-upload-list__item-actions">
  22. <span
  23. class="el-upload-list__item-preview"
  24. @click="handlePictureCardPreview(file)"
  25. >
  26. <i class="el-icon-zoom-in" />
  27. </span>
  28. <!-- <span
  29. v-if="!disabled"
  30. class="el-upload-list__item-delete"
  31. @click="handleDownload(file)"
  32. >
  33. <i class="el-icon-download" />
  34. </span> -->
  35. <span
  36. v-if="!disabled"
  37. class="el-upload-list__item-delete"
  38. @click="handleRemove(file)"
  39. >
  40. <i class="el-icon-delete" />
  41. </span>
  42. </span>
  43. </div>
  44. </el-upload>
  45. <template v-if="config">
  46. 图片长宽比为
  47. <b style="color: #f56c6c">{{ config[0] }}:{{ config[1] }}</b>
  48. </template>
  49. <!-- <template v-if="fileSize">
  50. 大小不超过 <b style="color: #f56c6c">{{ fileSize }}MB</b>
  51. </template> -->
  52. <template v-if="limit">
  53. 最多上传 <b style="color: #f56c6c">{{ limit }}</b
  54. >张
  55. </template>
  56. <el-dialog :visible.sync="dialogVisible" :modal="false">
  57. <img width="100%" :src="dialogImageUrl" alt="" />
  58. </el-dialog>
  59. <!-- <el-button type="primary" >
  60. 指令方式
  61. </el-button> -->
  62. </div>
  63. </template>
  64. <script>
  65. import { UPLOAD_URL } from '@/const/urlKey';
  66. import { getToken } from '@/utils/auth';
  67. export default {
  68. name: 'Upload',
  69. props: {
  70. value: {
  71. type: Array,
  72. default: () => []
  73. },
  74. config: {
  75. type: Array,
  76. default: null
  77. },
  78. limit: {
  79. type: Number,
  80. default: 1
  81. },
  82. params: {
  83. type: String,
  84. default: ''
  85. },
  86. uploadUrl: {
  87. type: String,
  88. default: ''
  89. }
  90. },
  91. data() {
  92. return {
  93. mark: false,
  94. url: UPLOAD_URL,
  95. dialogImageUrl: '',
  96. dialogVisible: false,
  97. disabled: false,
  98. myHeaders: {
  99. authorization: 'Bearer swagger-ui',
  100. token: getToken()
  101. },
  102. imgList: this.value,
  103. tmp: [],
  104. loading: null
  105. };
  106. },
  107. computed: {
  108. newUrl() {
  109. const params = this.params;
  110. const url = this.uploadUrl;
  111. let baseUrl = UPLOAD_URL;
  112. if (url !== '') {
  113. baseUrl = url;
  114. }
  115. if (params === '') {
  116. return baseUrl;
  117. } else {
  118. return baseUrl + `?${params}`;
  119. }
  120. }
  121. },
  122. watch: {
  123. value: {
  124. handler(val) {
  125. // if (this.imgList.length < val.length) {
  126. this.imgList = val;
  127. console.log(this.imgList);
  128. // }
  129. },
  130. immediate: true
  131. }
  132. },
  133. methods: {
  134. onProgress() {
  135. this.mark = true;
  136. },
  137. handleError() {
  138. this.mark = false;
  139. this.$error('上传失败,请稍后重试...');
  140. this.loading.close();
  141. },
  142. handleAvatarSuccess(res, file, fileList) {
  143. if (file.response.code !== 200) {
  144. this.$refs.upload.clearFiles();
  145. this.$message.error('上传失败,请稍后再试~');
  146. this.$emit('input', []);
  147. return false;
  148. }
  149. const result =
  150. fileList.length > 0
  151. ? fileList.map(x => ({
  152. name: x.response ? x.response.data.filename : x.name,
  153. url: x.response ? x.response.data.fullPath : x.url,
  154. size: x.response ? x.response.data.size : x.size
  155. }))
  156. : [];
  157. this.$nextTick(() => {
  158. this.$emit('input', result);
  159. this.$message.success('上传成功');
  160. this.mark = false;
  161. this.loading.close();
  162. });
  163. },
  164. // handleChange(file, fileList) {
  165. // if (file.response) {
  166. // // this.imgList = this.tmp
  167. // }
  168. // console.log(file)
  169. // console.log(fileList)
  170. // // const result = fileList.length > 0 ? fileList.map(x => ({
  171. // // name: x.name,
  172. // // url: x.response.data.path
  173. // // // url: x.url
  174. // // })) : []
  175. // // this.imgList = result
  176. // // const result = this.tmp
  177. // // this.imgList = JSON.parse(JSON.stringify(this.tmp))
  178. // },
  179. handleRemove(file, fileList) {
  180. const result = this.imgList.filter(x => x.name !== file.name);
  181. this.$emit('input', result);
  182. },
  183. handlePictureCardPreview(file) {
  184. this.dialogImageUrl = file.url;
  185. this.dialogVisible = true;
  186. },
  187. handleBeforeUpload() {
  188. this.loading = this.$loading({
  189. lock: true,
  190. text: 'Loading',
  191. spinner: 'el-icon-loading',
  192. background: 'rgba(0, 0, 0, 0.7)'
  193. });
  194. }
  195. }
  196. };
  197. </script>
  198. <style scoped>
  199. .page-select {
  200. display: flex;
  201. justify-content: flex-end;
  202. align-items: center;
  203. padding: 20px;
  204. }
  205. </style>