index.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <template>
  2. <div>
  3. <el-upload
  4. :disabled="disabledUpload"
  5. ref="upload"
  6. v-if="type === 'file' ? true : hideUpload"
  7. :action="action"
  8. :headers="headers"
  9. :data="data"
  10. :show-file-list="false"
  11. :list-type="type === 'file' ? '':'picture-card'"
  12. :before-upload="beforeUpload"
  13. :on-success="onSuccess"
  14. :on-progress="onProgress"
  15. :on-error="onError"
  16. >
  17. <el-button v-if="type === 'file'" type="primary" style="margin:0">{{ title }}</el-button>
  18. <i v-else slot="default" class="el-icon-plus" />
  19. </el-upload>
  20. <div class="upload-img" v-if="type === 'file' ? false : hideUpload === false">
  21. <el-image :src="newUrl" />
  22. <span v-if="disabled === false" class="upload-btn">
  23. <i class="el-icon-delete" @click="handleRemove" />
  24. </span>
  25. </div>
  26. </div>
  27. </template>
  28. <script>
  29. // import { client } from '@/api/oss'
  30. import { getToken } from "@/utils/auth";
  31. export default {
  32. props: {
  33. type: {
  34. type: String,
  35. default: "image",
  36. },
  37. // 图片
  38. url: {
  39. type: String,
  40. default: "",
  41. },
  42. // 隐藏删除按钮
  43. disabled: {
  44. type: Boolean,
  45. default: false,
  46. },
  47. },
  48. data() {
  49. return {
  50. // 额外参数
  51. data: {
  52. multipartFile: new FormData(),
  53. },
  54. // 文件大小
  55. size: 0,
  56. // 请求头
  57. headers: {
  58. Authorization: "Bearer " + getToken(),
  59. },
  60. // 上传地址
  61. action: "",
  62. // 隐藏上传按钮
  63. hideUpload: true,
  64. // 禁用上传按钮
  65. disabledUpload: false,
  66. // 按钮标题
  67. title: "上传文件",
  68. // 图片
  69. newUrl: "",
  70. };
  71. },
  72. watch: {
  73. url(val) {
  74. this.newUrl = val;
  75. this.hideUpload = val ? false : true
  76. },
  77. },
  78. mounted() {
  79. if (this.url) {
  80. this.newUrl = this.url;
  81. this.hideUpload = false;
  82. }
  83. this.action =
  84. this.type === "image"
  85. ? `${process.env.VUE_APP_BASE_API}/system/file/picture/upload`
  86. : `${process.env.VUE_APP_BASE_API}/system/file/file/upload`;
  87. },
  88. methods: {
  89. // 上传服务器之前提交文件
  90. beforeUpload(file) {
  91. this.data.multipartFile = file;
  92. this.size = file.size;
  93. this.disabledUpload = true;
  94. },
  95. // 上传成功
  96. onSuccess(file) {
  97. this.hideUpload = false;
  98. this.disabledUpload = false;
  99. this.newUrl = file.data;
  100. this.title = "上传成功";
  101. this.$emit("success", {
  102. file: this.newUrl,
  103. size: this.size,
  104. });
  105. },
  106. // 上传时 进度条
  107. onProgress(file) {
  108. this.title = `${parseInt(file.percent - 1)}%`;
  109. },
  110. // 上传失败
  111. onError(err) {
  112. this.title = `上传失败`
  113. },
  114. // 删除
  115. handleRemove() {
  116. this.hideUpload = true;
  117. this.$emit("delete", (this.newUrl = ""));
  118. },
  119. // httpRequest(e) {
  120. // client.put(e.file.name, e.file).then(res => {
  121. // console.log(res);
  122. // })
  123. // },
  124. },
  125. };
  126. </script>
  127. <style lang="scss" scoped>
  128. .upload-img {
  129. position: relative;
  130. width: 148px;
  131. height: 148px;
  132. .upload-btn {
  133. position: absolute;
  134. top: 50%;
  135. left: 50%;
  136. width: 100%;
  137. height: 100%;
  138. transform: translate(-50%, -50%);
  139. transition: all 0.3s;
  140. background: rgb(0 0 0 / 50%);
  141. color: #fff;
  142. text-align: center;
  143. line-height: 148px;
  144. font-size: 24px;
  145. opacity: 0;
  146. i {
  147. cursor: pointer;
  148. }
  149. }
  150. .upload-btn:hover {
  151. opacity: 1;
  152. }
  153. }
  154. </style>