index.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <template>
  2. <div class="component-upload-image">
  3. <div class="upload" v-if="isVisible">
  4. <el-upload
  5. multiple
  6. :action="uploadImgUrl"
  7. list-type="picture-card"
  8. :on-success="handleUploadSuccess"
  9. :before-upload="handleBeforeUpload"
  10. :limit="limit"
  11. :on-error="handleUploadError"
  12. :on-exceed="handleExceed"
  13. name="file"
  14. :on-remove="handleRemove"
  15. :show-file-list="true"
  16. :headers="headers"
  17. :file-list="fileList"
  18. :on-preview="handlePictureCardPreview"
  19. :class="{ hide: this.fileList.length >= this.limit }"
  20. >
  21. <i class="el-icon-plus"></i>
  22. </el-upload>
  23. <!-- 上传提示 -->
  24. <div class="el-upload__tip" slot="tip" v-if="showTip">
  25. 请上传
  26. <template v-if="fileSize">
  27. 大小不超过 <b style="color: #f56c6c">{{ fileSize }}MB</b>
  28. </template>
  29. <template v-if="fileType">
  30. 格式为 <b style="color: #f56c6c">{{ fileType.join("/") }}</b>
  31. </template>
  32. 的文件
  33. </div>
  34. <el-dialog
  35. :visible.sync="dialogVisible"
  36. title="预览"
  37. width="800"
  38. append-to-body
  39. >
  40. <img
  41. :src="dialogImageUrl"
  42. style="display: block; max-width: 100%; margin: 0 auto"
  43. />
  44. </el-dialog>
  45. </div>
  46. <!-- 加载 -->
  47. <div class="loadbox" v-else>
  48. <h5>加载中</h5>
  49. <i class="el-icon-loading"></i>
  50. </div>
  51. </div>
  52. </template>
  53. <script>
  54. import { getToken } from "@/utils/auth";
  55. export default {
  56. props: {
  57. value: [String, Object, Array],
  58. // 图片数量限制
  59. limit: {
  60. type: Number,
  61. default: 5,
  62. },
  63. // 大小限制(MB)
  64. fileSize: {
  65. type: Number,
  66. default: 5,
  67. },
  68. // 文件类型, 例如['png', 'jpg', 'jpeg']
  69. fileType: {
  70. type: Array,
  71. default: () => ["png", "jpg", "jpeg"],
  72. },
  73. // 是否显示提示
  74. isShowTip: {
  75. type: Boolean,
  76. default: true,
  77. },
  78. },
  79. data() {
  80. return {
  81. number: 0,
  82. uploadList: [],
  83. dialogImageUrl: "",
  84. dialogVisible: false,
  85. hideUpload: false,
  86. baseUrl: process.env.VUE_APP_BASE_API,
  87. uploadImgUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址
  88. headers: {
  89. Authorization: "Bearer " + getToken(),
  90. },
  91. fileList: [],
  92. isVisible: true, // 控制盒子显示状态
  93. };
  94. },
  95. watch: {
  96. value: {
  97. handler(val) {
  98. if (val) {
  99. // 首先将值转为数组
  100. const list = Array.isArray(val) ? val : this.value.split(",");
  101. // 然后将数组转为对象数组
  102. this.fileList = list.map((item) => {
  103. if (typeof item === "string") {
  104. if (item.indexOf(this.baseUrl) === -1) {
  105. // item = { name: this.baseUrl + item, url: this.baseUrl + item };k
  106. item = { name: item, url: item };
  107. } else {
  108. item = { name: item, url: item };
  109. }
  110. }
  111. return item;
  112. });
  113. } else {
  114. this.fileList = [];
  115. return [];
  116. }
  117. },
  118. deep: true,
  119. immediate: true,
  120. },
  121. },
  122. computed: {
  123. // 是否显示提示
  124. showTip() {
  125. return this.isShowTip && (this.fileType || this.fileSize);
  126. },
  127. },
  128. methods: {
  129. // 删除图片
  130. handleRemove(file, fileList) {
  131. const findex = this.fileList.map((f) => f.name).indexOf(file.name);
  132. if (findex > -1) {
  133. this.fileList.splice(findex, 1);
  134. this.$emit("onRemoved", this.listToString(this.fileList));
  135. }
  136. },
  137. // 上传成功回调
  138. handleUploadSuccess(res) {
  139. this.uploadList.push({ name: res.fileName, url: res.url });
  140. if (this.uploadList.length === this.number) {
  141. this.fileList = this.fileList.concat(this.uploadList);
  142. this.uploadList = [];
  143. this.number = 0;
  144. this.isVisible = true;
  145. // this.$emit("onUploaded", this.listToString(this.fileList));
  146. this.$emit("onUploaded", this.fileList);
  147. this.$modal.closeLoading();
  148. }
  149. },
  150. // 上传前loading加载
  151. handleBeforeUpload(file) {
  152. this.isVisible = false;
  153. let isImg = false;
  154. if (this.fileType.length) {
  155. let fileExtension = "";
  156. if (file.name.lastIndexOf(".") > -1) {
  157. fileExtension = file.name.slice(file.name.lastIndexOf(".") + 1);
  158. }
  159. isImg = this.fileType.some((type) => {
  160. if (file.type.indexOf(type) > -1) return true;
  161. if (fileExtension && fileExtension.indexOf(type) > -1) return true;
  162. return false;
  163. });
  164. } else {
  165. isImg = file.type.indexOf("image") > -1;
  166. }
  167. if (!isImg) {
  168. this.$modal.msgError(
  169. `文件格式不正确, 请上传${this.fileType.join("/")}图片格式文件!`
  170. );
  171. this.isVisible = true;
  172. return false;
  173. }
  174. if (this.fileSize) {
  175. const isLt = file.size / 1024 / 1024 < this.fileSize;
  176. if (!isLt) {
  177. this.$modal.msgError(`上传头像图片大小不能超过 ${this.fileSize} MB!`);
  178. this.isVisible = true;
  179. return false;
  180. }
  181. }
  182. this.$modal.loading("正在上传图片,请稍候...");
  183. this.number++;
  184. },
  185. // 文件个数超出
  186. handleExceed() {
  187. this.$modal.msgError(`上传文件数量不能超过 ${this.limit} 个!`);
  188. },
  189. // 上传失败
  190. handleUploadError() {
  191. this.$modal.msgError("上传图片失败,请重试");
  192. this.$modal.closeLoading();
  193. },
  194. // 预览
  195. handlePictureCardPreview(file) {
  196. console.log(file,'预览的刘德华');
  197. this.dialogImageUrl = file.url;
  198. this.dialogVisible = true;
  199. },
  200. // 对象转成指定字符串分隔
  201. listToString(list, separator) {
  202. let strs = "";
  203. separator = separator || ",";
  204. for (let i in list) {
  205. strs += list[i].url.replace(this.baseUrl, "") + separator;
  206. }
  207. return strs != "" ? strs.substr(0, strs.length - 1) : "";
  208. },
  209. },
  210. };
  211. </script>
  212. <style scoped lang="scss">
  213. // .el-upload--picture-card 控制加号部分
  214. ::v-deep.hide .el-upload--picture-card {
  215. display: none;
  216. }
  217. // 去掉动画效果
  218. ::v-deep .el-list-enter-active,
  219. ::v-deep .el-list-leave-active {
  220. transition: all 0s;
  221. }
  222. ::v-deep .el-list-enter,
  223. .el-list-leave-active {
  224. opacity: 0;
  225. transform: translateY(0);
  226. }
  227. .loadbox {
  228. width: 100%;
  229. height: 100%;
  230. background: rgba($color: #fff, $alpha: 0.9);
  231. // position: absolute;
  232. // left: 0;
  233. // top: 0;
  234. // z-index: 200;
  235. // padding: 18% 45%;
  236. // box-sizing: border-box;
  237. h5 {
  238. font-size: 15px;
  239. }
  240. .el-icon-loading {
  241. font-size: 30px;
  242. margin-left: 2%;
  243. }
  244. }
  245. </style>