anji-upload.vue 4.6 KB

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