index.vue 5.6 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. :file-list="fileList"
  15. :on-preview="handlePictureCardPreview"
  16. :class="{hide: this.fileList.length >= this.limit}"
  17. >
  18. <i class="el-icon-plus"></i>
  19. </el-upload>
  20. <!-- 上传提示 -->
  21. <!-- <div class="el-upload__tip" slot="tip" v-if="showTip">
  22. 请上传
  23. <template v-if="fileSize"> 大小不超过 <b style="color: #f56c6c">{{ fileSize }}MB</b> </template>
  24. <template v-if="fileType"> 格式为 <b style="color: #f56c6c">{{ fileType.join("/") }}</b> </template>
  25. 的文件
  26. </div> -->
  27. <el-dialog
  28. :visible.sync="dialogVisible"
  29. title="预览"
  30. width="800"
  31. append-to-body
  32. >
  33. <img
  34. :src="dialogImageUrl"
  35. style="display: block; max-width: 100%; margin: 0 auto"
  36. />
  37. </el-dialog>
  38. </div>
  39. </template>
  40. <script>
  41. import { getToken } from "@/utils/auth";
  42. export default {
  43. props: {
  44. value: [String, Object, Array],
  45. // 图片数量限制
  46. limit: {
  47. type: Number,
  48. default: 10,
  49. },
  50. // 大小限制(MB)
  51. fileSize: {
  52. type: Number,
  53. default: 5,
  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: process.env.VUE_APP_BASE_API,
  72. uploadImgUrl: process.env.VUE_APP_BASE_API+"/common/uploadOSS", // 上传的图片服务器地址
  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. }
  119. },
  120. // 上传成功回调
  121. handleUploadSuccess(res) {
  122. console.log(res)
  123. this.fileList.push({ name: res.url, url: res.url });
  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. console.log(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.replace(this.baseUrl, "") + 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>