index.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 + "/system/oss/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. item = { name: item, url: item };
  90. }
  91. return item;
  92. });
  93. } else {
  94. this.fileList = [];
  95. return [];
  96. }
  97. },
  98. deep: true,
  99. immediate: true
  100. }
  101. },
  102. computed: {
  103. // 是否显示提示
  104. showTip() {
  105. return this.isShowTip && (this.fileType || this.fileSize);
  106. },
  107. },
  108. methods: {
  109. // 删除图片
  110. handleRemove(file, fileList) {
  111. const findex = this.fileList.map(f => f.name).indexOf(file.name);
  112. if(findex > -1) {
  113. this.fileList.splice(findex, 1);
  114. this.$emit("input", this.listToString(this.fileList));
  115. }
  116. },
  117. // 上传成功回调
  118. handleUploadSuccess(res) {
  119. if (res.code == 200) {
  120. this.fileList.push({ name: res.data.fileName, url: res.data.url });
  121. this.$emit("input", this.listToString(this.fileList));
  122. this.loading.close();
  123. } else {
  124. this.$message.error(res.msg);
  125. this.loading.close();
  126. }
  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(res) {
  169. this.$message({
  170. type: "error",
  171. message: "上传失败",
  172. });
  173. this.loading.close();
  174. },
  175. // 预览
  176. handlePictureCardPreview(file) {
  177. this.dialogImageUrl = file.url;
  178. this.dialogVisible = true;
  179. },
  180. // 对象转成指定字符串分隔
  181. listToString(list, separator) {
  182. let strs = "";
  183. separator = separator || ",";
  184. for (let i in list) {
  185. strs += list[i].url + separator;
  186. }
  187. return strs != '' ? strs.substr(0, strs.length - 1) : '';
  188. }
  189. }
  190. };
  191. </script>
  192. <style scoped lang="scss">
  193. // .el-upload--picture-card 控制加号部分
  194. ::v-deep.hide .el-upload--picture-card {
  195. display: none;
  196. }
  197. // 去掉动画效果
  198. ::v-deep .el-list-enter-active,
  199. ::v-deep .el-list-leave-active {
  200. transition: all 0s;
  201. }
  202. ::v-deep .el-list-enter, .el-list-leave-active {
  203. opacity: 0;
  204. transform: translateY(0);
  205. }
  206. </style>