UploadVideo.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <template>
  2. <div
  3. class="com-upload-image"
  4. @click="onSelect"
  5. :style="{'background-image':(cover?`url(${cover})`:'')}"
  6. :class="{'no-border':showBorder== false}"
  7. >
  8. <div :class="{'has-image':cover,hover:hover}">
  9. <upload ref="uploadFile" media-type="video" @file-change="onFileChange"></upload>
  10. <div class="btn-push" v-show="!cover">
  11. <i class="iconfont icon-works_add"></i>
  12. <span>{{tips}}</span>
  13. </div>
  14. <div class="btn-change">
  15. <button class="ui-button submit">{{$t("common.change")}}</button>
  16. <a class="btn-clear" v-show="showClear" @click.stop="onClear">
  17. <i class="iconfont icon-close"></i>
  18. </a>
  19. </div>
  20. </div>
  21. </div>
  22. </template>
  23. <script>
  24. import Upload from "./Upload";
  25. import { getPoster } from "@/utils/video";
  26. let $video = null;
  27. export default {
  28. name: "UploadImage",
  29. components: {
  30. Upload
  31. },
  32. props: {
  33. tips: String,
  34. poster: String,
  35. hover: {
  36. type: Boolean,
  37. default: false
  38. },
  39. showClear: {
  40. type: Boolean,
  41. default: true
  42. },
  43. showBorder: {
  44. type: Boolean,
  45. default: true
  46. },
  47. showBackground: {
  48. type: Boolean,
  49. default: true
  50. }
  51. },
  52. data() {
  53. return {
  54. base64: "",
  55. isClear: false
  56. };
  57. },
  58. computed: {
  59. cover() {
  60. if (this.isClear) {
  61. return null;
  62. }
  63. return this.poster || this.base64;
  64. }
  65. },
  66. methods: {
  67. onSelect() {
  68. this.$refs.uploadFile.click();
  69. },
  70. onClear() {
  71. this.$refs.uploadFile.clear();
  72. this.dataURL = "";
  73. this.isClear = true;
  74. this.$emit("file-change", null);
  75. },
  76. onFileChange(file, base64) {
  77. this.isClear = false;
  78. if (this.showBackground) {
  79. this.dataURL = file.dataURL;
  80. }
  81. getPoster(this.dataURL).then(poster => {
  82. this.base64 = poster
  83. file.poster = this.base64;
  84. this.$emit("file-change", file);
  85. });
  86. // if ($video == null) {
  87. // $video = $(
  88. // `<video muted style="display:none;z-index:-1"></video>`
  89. // ).appendTo("body")[0];
  90. // $video.addEventListener("canplay", () => {
  91. // $video.pause();
  92. // var $canvas = document.createElement("canvas");
  93. // var minWidth = 97;
  94. // var minHeight = 97;
  95. // if ($video.videoWidth == 0) {
  96. // $canvas.width = minWidth;
  97. // $canvas.height = minHeight;
  98. // } else {
  99. // var r1 = minWidth / $video.videoWidth;
  100. // var r2 = minHeight / $video.videoHeight;
  101. // var r = Math.max(r1, r2);
  102. // $canvas.width = $video.videoWidth * r;
  103. // $canvas.height = $video.videoHeight * r;
  104. // }
  105. // var ctx = $canvas.getContext("2d");
  106. // ctx.drawImage(
  107. // $video,
  108. // 0,
  109. // 0,
  110. // $video.videoWidth,
  111. // $video.videoHeight,
  112. // 0,
  113. // 0,
  114. // $canvas.width,
  115. // $canvas.height
  116. // );
  117. // this.base64 = $canvas.toDataURL("image/jpeg", 0.8);
  118. // file.poster = this.base64
  119. // this.$emit("file-change", file);
  120. // });
  121. // }
  122. // $video.src = this.dataURL;
  123. // $video.play();
  124. // getPoster
  125. }
  126. }
  127. };
  128. </script>
  129. <style lang="less" scoped>
  130. .com-upload-image {
  131. cursor: pointer;
  132. display: inline-block;
  133. min-width: 60px;
  134. min-height: 60px;
  135. width: 100%;
  136. height: 100%;
  137. border-radius: 2px;
  138. border: 1px solid #5d5d5d;
  139. position: relative;
  140. background: #414141;
  141. background-position: center center;
  142. background-size: cover;
  143. &.no-border {
  144. border: none;
  145. }
  146. > div {
  147. position: relative;
  148. display: flex;
  149. width: 100%;
  150. height: 100%;
  151. align-items: center;
  152. justify-content: center;
  153. flex-direction: column;
  154. font-size: 12px;
  155. color: @color;
  156. &.has-image {
  157. background-color: rgba(0, 0, 0, 0.5);
  158. .btn-change {
  159. display: block;
  160. }
  161. &.hover {
  162. background-color: transparent;
  163. opacity: 0;
  164. transition: opacity 0.2s ease-in;
  165. &:hover {
  166. opacity: 1;
  167. background-color: rgba(0, 0, 0, 0.5);
  168. .btn-change {
  169. display: block;
  170. }
  171. }
  172. .btn-change {
  173. display: none;
  174. }
  175. }
  176. }
  177. }
  178. .icon-works_add {
  179. font-size: 28px;
  180. }
  181. .icon-close {
  182. color: #fff;
  183. font-size: 12px;
  184. }
  185. .ui-button {
  186. width: 60px;
  187. }
  188. .btn-push {
  189. text-align: center;
  190. span {
  191. display: block;
  192. }
  193. }
  194. .btn-change {
  195. display: none;
  196. }
  197. .btn-clear {
  198. position: absolute;
  199. right: 0;
  200. top: 0;
  201. width: 20px;
  202. height: 20px;
  203. line-height: 20px;
  204. text-align: center;
  205. background-color: #c77a7a;
  206. border-radius: 50%;
  207. transform: translate(50%, -50%);
  208. &:hover {
  209. background-color: #e85353;
  210. }
  211. }
  212. }
  213. </style>