index.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <template>
  2. <div v-loading="loading">
  3. <el-upload
  4. class="avatar-uploader"
  5. :action="uploadUrl"
  6. :headers="headers"
  7. :data="params"
  8. :name="name"
  9. :show-file-list="false"
  10. :on-success="handleAvatarSuccess"
  11. :before-upload="beforeAvatarUpload"
  12. :on-remove="handleRemove"
  13. :limit="1"
  14. >
  15. <template v-if="fellUrl">
  16. <video v-if="isVideo" :src="fellUrl" class="video" controls>
  17. <source :src="fellUrl" type="video/mp4">
  18. </video>
  19. <img v-else :src="fellUrl" class="avatar">
  20. </template>
  21. <i v-else class="el-icon-plus avatar-uploader-icon"/>
  22. <i v-if="fellUrl" class="el-icon-delete avatar-uploader-icon" @click.stop="handleRemove"/>
  23. </el-upload>
  24. </div>
  25. </template>
  26. <script>
  27. import { getToken } from '@/utils/auth'
  28. export default {
  29. name: 'Index',
  30. props: {
  31. value: {
  32. type: String,
  33. default: ''
  34. },
  35. name: {
  36. type: String,
  37. default: 'file'
  38. }
  39. },
  40. data() {
  41. return {
  42. loading:false,
  43. params: {},
  44. labelWidth: '120px',
  45. isVideo: false,
  46. uploadUrl: window.global.baseURL + 'upload/uploadVideo',
  47. // uploadUrl: window.global.uploadURL + 'upload/uploadVideo',
  48. headers: {
  49. 'Authorization': getToken()
  50. },
  51. fellUrl: this.value
  52. }
  53. },
  54. watch: {
  55. value(val) {
  56. this.fellUrl = Array.isArray(val) ? val[0] : val
  57. // 判断是否是视频类型
  58. this.isVideo = this.isVideoLink(val)
  59. }
  60. },
  61. methods: {
  62. isVideoLink(url) {
  63. const videoExtensions = /\.(mp4|m4v|webm|mov|avi|wmv)$/i
  64. return videoExtensions.test(url)
  65. },
  66. handleRemove(file, fileList) {
  67. this.fellUrl = ''
  68. this.$emit('input', '')
  69. },
  70. handleAvatarSuccess(res, file) {
  71. if (res.code === 0) {
  72. this.fellUrl = res.data.path
  73. this.$emit('input', res.data.path)
  74. } else {
  75. this.$message.error(res.msg)
  76. }
  77. this.loading=false
  78. },
  79. beforeAvatarUpload(file) {
  80. this.loading=true
  81. // console.log(file.type)
  82. // const isJPG =
  83. // file.type === 'image/jpeg' ||
  84. // file.type === 'image/png' ||
  85. // file.type === 'image/jpeg'
  86. // // 上传头像图片大小不能超过2MB!
  87. // const isLt2M = file.size / 1024 / 1024 < 2
  88. // if (!isLt2M) {
  89. // this.$message.error('上传头像图片大小不能超过 2MB!')
  90. // }
  91. // if (!isJPG) {
  92. // this.$message.error('上传头像图片只能是 JPG/PNG/JPEG 格式!')
  93. // }
  94. // if (!isLt2M) {
  95. // this.$message.error('上传头像图片大小不能超过 2MB!')
  96. // }
  97. // return isJPG && isLt2M
  98. }
  99. }
  100. }
  101. </script>
  102. <style scoped lang="scss">
  103. .avatar-uploader .el-upload {
  104. border: 1px dashed #d9d9d9;
  105. border-radius: 6px;
  106. cursor: pointer;
  107. position: relative;
  108. overflow: hidden;
  109. }
  110. .avatar-uploader .el-upload:hover {
  111. border-color: #409EFF;
  112. }
  113. .avatar-uploader-icon {
  114. font-size: 28px;
  115. color: #8c939d;
  116. width: 180px !important;
  117. height: 180px;
  118. line-height: 180px;
  119. text-align: center;
  120. }
  121. .avatar {
  122. z-index: 1;
  123. width: 180px !important;
  124. height: 180px !important;
  125. display: block !important;
  126. border-radius: 16px;
  127. }
  128. .video {
  129. z-index: 1;
  130. width: 180px !important;
  131. height: 180px !important;
  132. display: block !important;
  133. border-radius: 16px;
  134. }
  135. .el-icon-delete {
  136. position: absolute;
  137. z-index: 10;
  138. left: 0;
  139. top: 0;
  140. color: #fff;
  141. &:hover {
  142. color: #409EFF;
  143. }
  144. }
  145. .avatar-uploader-icon {
  146. border: 1px solid #ccc;
  147. border-radius: 16px;
  148. }
  149. </style>