index.vue 6.1 KB

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