dataUpload.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. :on-error="handleUploadError"
  10. :on-exceed="handleExceed"
  11. name="file"
  12. :on-remove="handleRemove"
  13. :show-file-list="true"
  14. :headers="headers"
  15. :file-list="fileList"
  16. :on-preview="handlePictureCardPreview"
  17. :class="{hide: this.fileList.length >= this.limit}"
  18. >
  19. <!-- :limit="limit"-->
  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. export default {
  45. name: 'DataImageUpload',
  46. props: {
  47. ImageUpload: Function,
  48. value: [String, Object, Array],
  49. limit: 1,
  50. // 图片数量限制
  51. // limit: {
  52. // type: Number,
  53. // default: 1,
  54. // },
  55. // 大小限制(MB)
  56. fileSize: {
  57. type: Number,
  58. default: 10,
  59. },
  60. // 文件类型, 例如['png', 'jpg', 'jpeg']
  61. fileType: {
  62. type: Array,
  63. default: () => ["png", "jpg", "jpeg"],
  64. },
  65. // 是否显示提示
  66. isShowTip: {
  67. type: Boolean,
  68. default: true
  69. }
  70. },
  71. data() {
  72. return {
  73. number: 0,
  74. uploadList: [],
  75. dialogImageUrl: "",
  76. dialogName: "",
  77. dialogVisible: false,
  78. hideUpload: false,
  79. uploadImgUrl: process.env.VUE_APP_BASE_API + "/file/upload", // 上传的图片服务器地址
  80. headers: {
  81. Authorization: "Bearer " + getToken(),
  82. },
  83. fileList: []
  84. };
  85. },
  86. watch: {
  87. value: {
  88. handler(val) {
  89. if (val) {
  90. // 首先将值转为数组
  91. const list = Array.isArray(val) ? val : this.value.split(',');
  92. // 然后将数组转为对象数组
  93. this.fileList = list.map(item => {
  94. if (typeof item === "string") {
  95. item = { name: item, url: item };
  96. }
  97. return item;
  98. });
  99. } else {
  100. this.fileList = [];
  101. return [];
  102. }
  103. },
  104. deep: true,
  105. immediate: true
  106. }
  107. },
  108. computed: {
  109. // 是否显示提示
  110. showTip() {
  111. return this.isShowTip && (this.fileType || this.fileSize);
  112. },
  113. },
  114. methods: {
  115. clear: function(){
  116. alert('2')
  117. },
  118. // 删除图片
  119. handleRemove(file, fileList) {
  120. const findex = this.fileList.map(f => f.name).indexOf(file.name);
  121. if(findex > -1) {
  122. this.fileList.splice(findex, 1);
  123. this.$emit("input", this.listToString(this.fileList));
  124. // this.ImageUpload.setUrl("")
  125. }
  126. },
  127. // 上传成功回调
  128. handleUploadSuccess(res) {
  129. this.uploadList.push({ name: res.data.url, url: res.data.webUrl });
  130. if (this.uploadList.length === this.number) {
  131. this.fileList = this.fileList.concat(this.uploadList);
  132. this.uploadList = [];
  133. this.number = 0;
  134. this.dialogName = res.data.url;
  135. // this.ImageUpload.setUrl(res.data.url)
  136. this.$emit("input", this.listToString(this.fileList));
  137. // this.$emit("input", JSON.stringify(this.fileList));
  138. this.$modal.closeLoading();
  139. }
  140. },
  141. // 上传前loading加载
  142. handleBeforeUpload(file) {
  143. let isImg = false;
  144. if (this.fileType.length) {
  145. let fileExtension = "";
  146. if (file.name.lastIndexOf(".") > -1) {
  147. fileExtension = file.name.slice(file.name.lastIndexOf(".") + 1);
  148. }
  149. isImg = this.fileType.some(type => {
  150. if (file.type.indexOf(type) > -1) return true;
  151. if (fileExtension && fileExtension.indexOf(type) > -1) return true;
  152. return false;
  153. });
  154. } else {
  155. isImg = file.type.indexOf("image") > -1;
  156. }
  157. if (!isImg) {
  158. this.$modal.msgError(`文件格式不正确, 请上传${this.fileType.join("/")}图片格式文件!`);
  159. return false;
  160. }
  161. if (this.fileSize) {
  162. const isLt = file.size / 1024 / 1024 < this.fileSize;
  163. if (!isLt) {
  164. this.$modal.msgError(`上传头像图片大小不能超过 ${this.fileSize} MB!`);
  165. return false;
  166. }
  167. }
  168. this.$modal.loading("正在上传图片,请稍候...");
  169. this.number++;
  170. },
  171. // 文件个数超出
  172. handleExceed() {
  173. this.$modal.msgError(`上传文件数量不能超过 ${this.limit} 个!`);
  174. },
  175. // 上传失败
  176. handleUploadError() {
  177. this.$modal.msgError("上传图片失败,请重试");
  178. this.$modal.closeLoading();
  179. },
  180. // 预览
  181. handlePictureCardPreview(file) {
  182. this.dialogImageUrl = file.url;
  183. this.dialogVisible = true;
  184. },
  185. // 对象转成指定字符串分隔
  186. listToString(list, separator) {
  187. let strs = "";
  188. separator = separator || ",";
  189. for (let i in list) {
  190. strs += list[i].url + separator;
  191. }
  192. return strs != '' ? strs.substr(0, strs.length - 1) : '';
  193. }
  194. }
  195. };
  196. </script>
  197. <style scoped lang="scss">
  198. // .el-upload--picture-card 控制加号部分
  199. ::v-deep.hide .el-upload--picture-card {
  200. display: none;
  201. }
  202. // 去掉动画效果
  203. ::v-deep .el-list-enter-active,
  204. ::v-deep .el-list-leave-active {
  205. transition: all 0s;
  206. }
  207. ::v-deep .el-list-enter, .el-list-leave-active {
  208. opacity: 0;
  209. transform: translateY(0);
  210. }
  211. </style>