index.vue 6.7 KB

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