index.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. }
  90. },
  91. deep: true,
  92. immediate: true
  93. }
  94. },
  95. computed: {
  96. // 是否显示提示
  97. showTip() {
  98. return this.isShowTip && (this.fileType || this.fileSize);
  99. },
  100. },
  101. methods: {
  102. // 上传成功的函数
  103. handleSuccess(res) {
  104. ++this.menuKey;
  105. if(res.code === 0){
  106. this.uploadFlag = true;
  107. this.videoUrl = res.data;
  108. this.$emit("input", this.videoUrl);
  109. }else{
  110. this.uploadFlag = false;
  111. this.$message.error("错误!"+ res.msg);
  112. }
  113. },
  114. handleError(){
  115. this.uploadFlag = false;
  116. },
  117. beforeUpload(file) {
  118. const isMp4 = this.fileType.includes(file.type);
  119. const isLt300MB = file.size / 1024 / 1024 < 300;
  120. if (!isMp4) {
  121. this.$message.error("视频只能是"+ this.fileType.join("/") +"格式!");
  122. }
  123. if (!isLt300MB) {
  124. this.$message.error("上传视频大小不能超过 300MB!");
  125. }
  126. return isMp4 && isLt300MB;
  127. },
  128. // 预览
  129. handlePreview() {
  130. this.dialogVisible = true;
  131. },
  132. // 删除视频
  133. handleRemove() {
  134. this.videoUrl = null;
  135. this.uploadFlag = null;
  136. this.$emit("input", null);
  137. },
  138. mouseover(){
  139. this.activeHover = false;
  140. },
  141. mouseout(){
  142. this.activeHover = true;
  143. }
  144. }
  145. }
  146. </script>
  147. <style lang="scss">
  148. .editPage__video {
  149. .hide{
  150. visibility:hidden;
  151. }
  152. .success{
  153. position: relative;
  154. width: 78px;
  155. height: 78px;
  156. line-height: 78px;
  157. background-color: rgba(0,0,0,.5);
  158. transition: opacity .3s;
  159. cursor: default;
  160. .item-preview .el-icon-zoom-in{
  161. width: 30px;
  162. font-size: 20px;
  163. color: #f2f2f2;
  164. cursor: pointer;
  165. }
  166. .item-delete .el-icon-delete{
  167. width: 30px;
  168. font-size: 20px;
  169. color: #f2f2f2;
  170. cursor: pointer;
  171. }
  172. }
  173. .uploader-icon {
  174. font-size: 28px;
  175. color: #8c939d;
  176. width: 80px;
  177. height: 80px;
  178. line-height: 80px;
  179. text-align: center;
  180. position: absolute;
  181. left: 0;
  182. }
  183. .success-icon {
  184. font-size: 28px;
  185. color: green;
  186. width: 80px;
  187. height: 80px;
  188. line-height: 80px;
  189. text-align: center;
  190. position: absolute;
  191. left: 0;
  192. }
  193. .el-upload{
  194. width: 80px;
  195. height: 80px;
  196. }
  197. }
  198. </style>