ImageUpload.vue 5.5 KB

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