index.vue 5.8 KB

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