index.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <template>
  2. <div class="editPage__video">
  3. <el-upload
  4. class="uploader"
  5. list-type="picture-card"
  6. :action="uploadUrl"
  7. :on-success="handleSuccess"
  8. :before-upload="beforeUpload"
  9. :headers="headers"
  10. :on-error="handleError"
  11. :show-file-list="false"
  12. >
  13. <div v-if="uploadFlag" @mouseenter="mouseover" @mouseleave="mouseout">
  14. <i class="el-icon-success success-icon"></i>
  15. <div :class="{'hide': activeHover, 'success': !activeHover}">
  16. <span class="item-actions">
  17. <span
  18. class="item-preview"
  19. @click.stop="handlePreview()"
  20. >
  21. <i class="el-icon-zoom-in"></i>
  22. </span>
  23. <span
  24. class="item-delete"
  25. @click.stop="handleRemove()"
  26. >
  27. <i class="el-icon-delete"></i>
  28. </span>
  29. </span>
  30. </div>
  31. </div>
  32. <i v-else-if="uploadFlag === null" class="el-icon-plus uploader-icon"></i>
  33. <i v-else-if="!uploadFlag" class="el-icon-circle-close uploader-icon" style="color: red"></i>
  34. </el-upload>
  35. <!-- 上传提示 -->
  36. <div class="el-upload__tip" slot="tip" v-if="showTip">
  37. 请上传
  38. <template v-if="fileSize"> 大小不超过 <b style="color: #f56c6c">{{ fileSize }}MB</b> </template>
  39. <template v-if="fileType"> 格式为 <b style="color: #f56c6c">{{ fileType.join("/") }}</b> </template>
  40. 的文件
  41. </div>
  42. <el-dialog :visible.sync="dialogVisible" append-to-body width="800" title="预览">
  43. <video width="100%" v-if="videoUrl" controls="controls" :key="menuKey">
  44. <source :src="videoUrl" type="video/mp4" />
  45. </video>
  46. </el-dialog>
  47. </div>
  48. </template>
  49. <script>
  50. import { getAccessToken } from "@/utils/auth";
  51. export default {
  52. props: {
  53. value: [String, Object],
  54. // 大小限制(MB)
  55. fileSize: {
  56. type: Number,
  57. default: 300,
  58. },
  59. // 文件类型, 例如"video/mp4"
  60. fileType: {
  61. type: [String, Array],
  62. default: () =>["video/mp4"],
  63. },
  64. // 是否显示提示
  65. isShowTip: {
  66. type: Boolean,
  67. default: true
  68. }
  69. },
  70. data() {
  71. return {
  72. uploadFlag: null,
  73. activeHover: true,
  74. dialogVisible: false,
  75. videoUrl: null,
  76. // 视频上传
  77. uploadUrl: process.env.VUE_APP_BASE_API + "/admin-api/infra/file/upload", // 请求地址
  78. headers: { Authorization: "Bearer " + getAccessToken() }, // 设置上传的请求头部
  79. // 应付多个组件的情况 记录当前组件的key值
  80. menuKey: 1, // 用来强制刷新,
  81. }
  82. },
  83. watch: {
  84. value: {
  85. handler(val) {
  86. if (val) {
  87. this.videoUrl = val;
  88. this.uploadFlag = true;
  89. } else {
  90. this.videoUrl = null;
  91. this.uploadFlag = null;
  92. }
  93. },
  94. deep: true,
  95. immediate: true
  96. }
  97. },
  98. computed: {
  99. // 是否显示提示
  100. showTip() {
  101. return this.isShowTip && (this.fileType || this.fileSize);
  102. },
  103. },
  104. methods: {
  105. // 上传成功的函数
  106. handleSuccess(res) {
  107. ++this.menuKey;
  108. if(res.code === 0){
  109. this.uploadFlag = true;
  110. this.videoUrl = res.data;
  111. this.$emit("input", this.videoUrl);
  112. }else{
  113. this.uploadFlag = false;
  114. this.$message.error("错误!"+ res.msg);
  115. }
  116. },
  117. handleError(){
  118. this.uploadFlag = false;
  119. },
  120. beforeUpload(file) {
  121. const isMp4 = this.fileType.includes(file.type);
  122. const isLt300MB = file.size / 1024 / 1024 < 300;
  123. if (!isMp4) {
  124. this.$message.error("视频只能是"+ this.fileType.join("/") +"格式!");
  125. }
  126. if (!isLt300MB) {
  127. this.$message.error("上传视频大小不能超过 300MB!");
  128. }
  129. return isMp4 && isLt300MB;
  130. },
  131. // 预览
  132. handlePreview() {
  133. this.dialogVisible = true;
  134. },
  135. // 删除视频
  136. handleRemove() {
  137. this.videoUrl = null;
  138. this.uploadFlag = null;
  139. this.$emit("input", null);
  140. },
  141. mouseover(){
  142. this.activeHover = false;
  143. },
  144. mouseout(){
  145. this.activeHover = true;
  146. }
  147. }
  148. }
  149. </script>
  150. <style lang="scss">
  151. .editPage__video {
  152. .hide{
  153. visibility:hidden;
  154. }
  155. .success{
  156. position: relative;
  157. width: 78px;
  158. height: 78px;
  159. line-height: 78px;
  160. background-color: rgba(0,0,0,.5);
  161. transition: opacity .3s;
  162. cursor: default;
  163. .item-preview .el-icon-zoom-in{
  164. width: 30px;
  165. font-size: 20px;
  166. color: #f2f2f2;
  167. cursor: pointer;
  168. }
  169. .item-delete .el-icon-delete{
  170. width: 30px;
  171. font-size: 20px;
  172. color: #f2f2f2;
  173. cursor: pointer;
  174. }
  175. }
  176. .uploader-icon {
  177. font-size: 28px;
  178. color: #8c939d;
  179. width: 80px;
  180. height: 80px;
  181. line-height: 80px;
  182. text-align: center;
  183. position: absolute;
  184. left: 0;
  185. }
  186. .success-icon {
  187. font-size: 28px;
  188. color: green;
  189. width: 80px;
  190. height: 80px;
  191. line-height: 80px;
  192. text-align: center;
  193. position: absolute;
  194. left: 0;
  195. }
  196. .el-upload{
  197. width: 80px;
  198. height: 80px;
  199. }
  200. }
  201. </style>