index.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <template>
  2. <div class="component-upload-image">
  3. <el-upload
  4. multiple
  5. :action="uploadImgUrl"
  6. list-type="picture-card"
  7. :on-success="handleUploadSuccess"
  8. :before-upload="handleBeforeUpload"
  9. :limit="limit"
  10. :on-error="handleUploadError"
  11. :on-exceed="handleExceed"
  12. name="file"
  13. :on-remove="handleRemove"
  14. :show-file-list="true"
  15. :headers="headers"
  16. :file-list="fileList"
  17. :on-preview="handlePictureCardPreview"
  18. :class="{hide: this.fileList.length >= this.limit}"
  19. >
  20. <i class="el-icon-plus"></i>
  21. </el-upload>
  22. <!-- 上传提示 -->
  23. <div class="el-upload__tip" slot="tip" v-if="showTip">
  24. 请上传
  25. <template v-if="fileSize"> 大小不超过 <b style="color: #f56c6c">{{ fileSize }}MB</b> </template>
  26. <template v-if="fileType"> 格式为 <b style="color: #f56c6c">{{ fileType.join("/") }}</b> </template>
  27. 的文件
  28. </div>
  29. <el-dialog
  30. :visible.sync="dialogVisible"
  31. title="预览"
  32. width="800"
  33. append-to-body
  34. >
  35. <img
  36. :src="dialogImageUrl"
  37. style="display: block; max-width: 100%; margin: 0 auto"
  38. />
  39. </el-dialog>
  40. </div>
  41. </template>
  42. <script>
  43. import { getToken } from "@/utils/auth";
  44. import { delOss } from "@/api/system/oss";
  45. export default {
  46. props: {
  47. value: [String, Object, Array],
  48. // 图片数量限制
  49. limit: {
  50. type: Number,
  51. default: 5,
  52. },
  53. // 大小限制(MB)
  54. fileSize: {
  55. type: Number,
  56. default: 5,
  57. },
  58. // 文件类型, 例如['png', 'jpg', 'jpeg']
  59. fileType: {
  60. type: Array,
  61. default: () => ["png", "jpg", "jpeg"],
  62. },
  63. // 是否显示提示
  64. isShowTip: {
  65. type: Boolean,
  66. default: true
  67. }
  68. },
  69. data() {
  70. return {
  71. number: 0,
  72. uploadList: [],
  73. dialogImageUrl: "",
  74. dialogVisible: false,
  75. hideUpload: false,
  76. baseUrl: process.env.VUE_APP_BASE_API,
  77. uploadImgUrl: process.env.VUE_APP_BASE_API + "/system/oss/upload", // 上传的图片服务器地址
  78. headers: {
  79. Authorization: "Bearer " + getToken(),
  80. },
  81. fileList: []
  82. };
  83. },
  84. watch: {
  85. value: {
  86. handler(val) {
  87. if (val) {
  88. // 首先将值转为数组
  89. const list = Array.isArray(val) ? val : this.value.split(',');
  90. // 然后将数组转为对象数组
  91. this.fileList = list.map(item => {
  92. // 字符串回显处理 如果此处存的是url可直接回显 如果存的是id需要调用接口查出来
  93. if (typeof item === "string") {
  94. item = { name: item, url: item };
  95. } else {
  96. // 此处name使用ossId 防止删除出现重名
  97. item = { name: item.ossId, url: item.url, ossId: item.ossId };
  98. }
  99. return item;
  100. });
  101. } else {
  102. this.fileList = [];
  103. return [];
  104. }
  105. },
  106. deep: true,
  107. immediate: true
  108. }
  109. },
  110. computed: {
  111. // 是否显示提示
  112. showTip() {
  113. return this.isShowTip && (this.fileType || this.fileSize);
  114. },
  115. },
  116. methods: {
  117. // 删除图片
  118. handleRemove(file, fileList) {
  119. const findex = this.fileList.map(f => f.name).indexOf(file.name);
  120. if(findex > -1) {
  121. let ossId = this.fileList[findex].ossId;
  122. delOss(ossId);
  123. this.fileList.splice(findex, 1);
  124. this.$emit("input", this.fileList);
  125. }
  126. },
  127. // 上传成功回调
  128. handleUploadSuccess(res) {
  129. if (res.code == 200) {
  130. this.uploadList.push({ name: res.data.fileName, url: res.data.url, ossId: res.data.ossId });
  131. if (this.uploadList.length === this.number) {
  132. this.fileList = this.fileList.concat(this.uploadList);
  133. this.uploadList = [];
  134. this.number = 0;
  135. this.$emit("input", this.fileList);
  136. this.$modal.closeLoading();
  137. }
  138. } else {
  139. this.$modal.msgError(res.msg);
  140. this.$modal.closeLoading();
  141. }
  142. },
  143. // 上传前loading加载
  144. handleBeforeUpload(file) {
  145. let isImg = false;
  146. if (this.fileType.length) {
  147. let fileExtension = "";
  148. if (file.name.lastIndexOf(".") > -1) {
  149. fileExtension = file.name.slice(file.name.lastIndexOf(".") + 1);
  150. }
  151. isImg = this.fileType.some((type) => {
  152. if (file.type.indexOf(type) > -1) return true;
  153. if (fileExtension && fileExtension.indexOf(type) > -1) return true;
  154. return false;
  155. });
  156. } else {
  157. isImg = file.type.indexOf("image") > -1;
  158. }
  159. if (!isImg) {
  160. this.$modal.msgError(`文件格式不正确, 请上传${this.fileType.join("/")}图片格式文件!`);
  161. return false;
  162. }
  163. if (this.fileSize) {
  164. const isLt = file.size / 1024 / 1024 < this.fileSize;
  165. if (!isLt) {
  166. this.$modal.msgError(`上传头像图片大小不能超过 ${this.fileSize} MB!`);
  167. return false;
  168. }
  169. }
  170. this.$modal.loading("正在上传图片,请稍候...");
  171. this.number++;
  172. },
  173. // 文件个数超出
  174. handleExceed() {
  175. this.$modal.msgError(`上传文件数量不能超过 ${this.limit} 个!`);
  176. },
  177. // 上传失败
  178. handleUploadError(res) {
  179. this.$modal.msgError("上传图片失败,请重试");
  180. this.$modal.closeLoading();
  181. },
  182. // 预览
  183. handlePictureCardPreview(file) {
  184. this.dialogImageUrl = file.url;
  185. this.dialogVisible = true;
  186. },
  187. }
  188. };
  189. </script>
  190. <style scoped lang="scss">
  191. // .el-upload--picture-card 控制加号部分
  192. ::v-deep.hide .el-upload--picture-card {
  193. display: none;
  194. }
  195. // 去掉动画效果
  196. ::v-deep .el-list-enter-active,
  197. ::v-deep .el-list-leave-active {
  198. transition: all 0s;
  199. }
  200. ::v-deep .el-list-enter, .el-list-leave-active {
  201. opacity: 0;
  202. transform: translateY(0);
  203. }
  204. </style>