ImageUpload.vue 5.2 KB

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