anji-upload.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <template>
  2. <div class="uploadImage">
  3. <el-upload
  4. :headers="headers"
  5. :limit="limit"
  6. :action="requestUrl"
  7. list-type="picture-card"
  8. :file-list="fileList"
  9. :on-preview="handlePictureCardPreview"
  10. :on-remove="handleRemove"
  11. :on-success="handleSuccess"
  12. :show-file-list="true"
  13. :before-upload="handleBeforeUpload"
  14. :class="fileList && fileList.length >= limit ? 'hide_box' : ''"
  15. >
  16. <i slot="default" class="el-icon-plus" />
  17. <div slot="file" slot-scope="{ file }" class="imgBox">
  18. <img class="el-upload-list__item-thumbnail" :src="file.url" alt="" />
  19. </div>
  20. </el-upload>
  21. <el-dialog :visible.sync="dialogVisibleImageUpload" :modal="false">
  22. <img width="100%" :src="imageUploadUrl" alt="" />
  23. </el-dialog>
  24. </div>
  25. </template>
  26. <script>
  27. import { getToken } from "@/utils/auth"; // get token from cookie
  28. export default {
  29. props: {
  30. upLoadUrl: {
  31. type: String,
  32. default: () => {
  33. return "";
  34. }
  35. },
  36. viewUrl: {
  37. type: String,
  38. default: () => {
  39. return process.env.BASE_API + "/file/download/";
  40. }
  41. },
  42. limit: {
  43. type: Number,
  44. default: () => {
  45. return 1;
  46. }
  47. },
  48. value: {
  49. type: String,
  50. default: () => {
  51. return "";
  52. }
  53. }
  54. },
  55. data() {
  56. return {
  57. imageUploadUrl: "",
  58. dialogVisibleImageUpload: false,
  59. fileList: []
  60. };
  61. },
  62. computed: {
  63. requestUrl() {
  64. if (this.upLoadUrl != null && this.upLoadUrl.trim() != "") {
  65. return process.env.BASE_API + this.upLoadUrl;
  66. } else {
  67. return process.env.BASE_API + "/file/upload";
  68. }
  69. },
  70. headers() {
  71. return {
  72. Authorization: getToken() // 直接从本地获取token就行
  73. };
  74. }
  75. },
  76. watch: {
  77. value: {
  78. handler(val) {
  79. this.echoUpload(this.value);
  80. },
  81. immediate: true
  82. }
  83. },
  84. mounted() {
  85. this.echoUpload(this.value);
  86. },
  87. methods: {
  88. handleRemove(file) {
  89. const fileList = [];
  90. this.fileList.forEach(el => {
  91. if (el.fileId != file.fileId) {
  92. fileList.push(el);
  93. }
  94. });
  95. this.fileList = fileList;
  96. this.change();
  97. },
  98. handlePictureCardPreview(file) {
  99. this.imageUploadUrl = file.url;
  100. this.dialogVisibleImageUpload = true;
  101. },
  102. // 上传成功的回调
  103. handleSuccess(response, file, fileList) {
  104. console.log(fileList);
  105. if (response.code != 200) {
  106. this.$message.error("上传失败");
  107. return;
  108. }
  109. this.fileList.push({
  110. url: file.response.data.urlPath,
  111. fileId: file.response.data.fileId
  112. });
  113. this.change();
  114. },
  115. // 回传出去
  116. change() {
  117. const fileList = this.fileList[0]["fileId"];
  118. this.$emit("input", fileList);
  119. this.$emit("change", fileList);
  120. },
  121. // 上传检验
  122. handleBeforeUpload(file) {
  123. const extension = file.name
  124. .split(".")
  125. [file.name.split(".").length - 1].toLowerCase();
  126. // .png|.jpg|.gif|.icon|.pdf|.xlsx|.xls|.csv|.mp4|.avi
  127. const extensionList = ["png", "jpg", "gif", "icon"];
  128. if (extensionList.indexOf(extension) < 0) {
  129. this.$message.warning("请上传正确的格式文件");
  130. return false;
  131. }
  132. return true;
  133. },
  134. // 回显
  135. echoUpload(val) {
  136. if (!val) {
  137. this.fileList = [];
  138. } else {
  139. const list = [{ url: this.viewUrl + val, fileId: val }];
  140. this.fileList = list;
  141. }
  142. }
  143. }
  144. };
  145. </script>
  146. <style lang="scss" scoped>
  147. .uploadImage .el-upload--picture-card {
  148. width: 60px;
  149. height: 60px;
  150. line-height: 65px;
  151. }
  152. .uploadImage .el-upload-list__item {
  153. width: 60px;
  154. height: 60px;
  155. }
  156. .hide_box .el-upload--picture-card {
  157. display: none;
  158. }
  159. .el-upload-list__item {
  160. line-height: 1.5;
  161. }
  162. .el-upload-list--picture-card .el-upload-list__item-actions {
  163. text-align: left;
  164. display: flex;
  165. flex-wrap: wrap;
  166. }
  167. .imgBox,
  168. .iconFont {
  169. width: 100% !important;
  170. height: 100% !important;
  171. }
  172. </style>