videoUpload.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <template>
  2. <view class="upload_wrap_preview">
  3. <!-- 除了 APP 平台,其它平台均存在的代码 -->
  4. <!-- #ifndef APP-PLUS-->
  5. <view style="height: 0">
  6. <video
  7. @fullscreenchange="fullscreenchange"
  8. id="cVideo"
  9. :src="videoSrc"
  10. ></video>
  11. </view>
  12. <!-- #endif-->
  13. <!-- 仅出现在 App 平台下的代码 -->
  14. <!-- #ifdef APP-PLUS-->
  15. <view style="display: none">
  16. <video
  17. @fullscreenchange="fullscreenchange"
  18. id="cVideo"
  19. :src="videoSrc"
  20. ></video>
  21. </view>
  22. <!-- #endif-->
  23. <view
  24. :key="i"
  25. v-for="(item, i) in fileList"
  26. class="video_box"
  27. @click.prevent="openVideo(item)"
  28. >
  29. <template v-if="!$attrs.disabled">
  30. <!-- 删除 icon -->
  31. <view
  32. @click.stop="deleteVideo(item, i)"
  33. v-if="item.status !== 'uploading'"
  34. >
  35. <u-icon
  36. name="close"
  37. color="#ffffff"
  38. size="10"
  39. class="del_icon"
  40. ></u-icon>
  41. </view>
  42. <!-- 上传中loading -->
  43. <view
  44. class="upload_status"
  45. v-if="item.status === 'uploading'"
  46. >
  47. <u-loading-icon size="22" mode="circle" color="#ffffff" />
  48. <text v-if="item.message" class="upload_status_message">
  49. {{ item.message + item.progress }}
  50. </text>
  51. </view>
  52. <!-- 上传成功 -->
  53. <view v-if="item.status === 'success'" class="upload_success">
  54. <view class="upload_success_icon">
  55. <u-icon name="checkmark" color="#ffffff" size="12">
  56. </u-icon>
  57. </view>
  58. </view>
  59. <!-- 上传失败 -->
  60. <view v-if="item.status === 'error'" class="upload_status">
  61. <u-icon
  62. color="#ffffff"
  63. size="26"
  64. name="error-circle"
  65. ></u-icon>
  66. <text class="upload_status_message">
  67. {{ item.message || '上传失败' }}
  68. </text>
  69. </view>
  70. </template>
  71. <view>
  72. <u-icon color="#80CBF9" size="26" name="movie"></u-icon>
  73. <text class="upload_video_text">视频</text>
  74. </view>
  75. </view>
  76. <u-upload v-on="_listeners" v-bind="_attrs">
  77. <slot></slot>
  78. </u-upload>
  79. </view>
  80. </template>
  81. <script>
  82. import { uploadFile } from '@/api/com';
  83. export default {
  84. name: 'videoUpload',
  85. inheritAttrs: false,
  86. model: {
  87. prop: 'value',
  88. event: 'change',
  89. },
  90. props: {
  91. maxSize: Number, // 单位是字节(Byte)
  92. value: {
  93. // 视频列表
  94. type: Array,
  95. default: () => [],
  96. },
  97. },
  98. data() {
  99. return {
  100. fileList: [],
  101. videoSrc: '',
  102. videoContext: null,
  103. };
  104. },
  105. watch: {
  106. value: {
  107. handler(list) {
  108. this.fileList = list;
  109. },
  110. immediate: true,
  111. },
  112. },
  113. computed: {
  114. _attrs() {
  115. return {
  116. ...this.$attrs,
  117. accept: 'video',
  118. fileList: this.fileList,
  119. compressed: false, //不压缩视频,压缩视频会很慢
  120. uploadIcon: 'play-circle-fill',
  121. multiple: false, //开启后afterRead 参数拿到的file是一个对象(视频不支持多选)
  122. maxSize: 200 * 1024 * 1024, // 默认两百兆,
  123. };
  124. },
  125. _listeners() {
  126. return {
  127. ...this.$listeners,
  128. afterRead: this.afterRead,
  129. delete: () => {},
  130. };
  131. },
  132. },
  133. mounted() {
  134. this.videoContext = uni.createVideoContext('cVideo', this);
  135. },
  136. methods: {
  137. fullscreenchange(e) {
  138. // 当视频进入和退出全屏时触发,event.detail = {fullScreen, direction},direction取为 vertical 或 horizontal
  139. if (!e.detail.fullScreen) {
  140. this.videoContext.stop(); // 停止播放视频
  141. }
  142. },
  143. openVideo(item) {
  144. // 显示播放视频
  145. if (item.status === 'uploading' || item.status === 'error') {
  146. return;
  147. }
  148. console.log(item);
  149. this.videoSrc = item.url;
  150. this.$nextTick(() => {
  151. this.videoContext.requestFullScreen({ direction: 0 }); // 全屏
  152. this.videoContext.play(); // 播放视频
  153. });
  154. },
  155. deleteVideo(e) {
  156. // 删除file
  157. if (this.$listeners.delete) {
  158. this.$emit('delete', e);
  159. return;
  160. }
  161. this.fileList.splice(e.index, 1); // 删除
  162. this.$emit('change', this.fileList); // v-model
  163. },
  164. async afterRead(e) {
  165. //新增file
  166. if (this.$listeners.afterRead) {
  167. // 外部自定义
  168. this.$emit('afterRead', e);
  169. return;
  170. }
  171. const { file } = e;
  172. const maxSize = this.maxSize; // 字节 byte
  173. const maxSizeMb = (this.maxSize / 1024 / 1024).toFixed(0); // 转为兆
  174. if (maxSize && e.file.size > maxSize) {
  175. uni.showToast({
  176. icon: 'none',
  177. title: `视频不能大于${maxSizeMb}兆`,
  178. });
  179. return;
  180. }
  181. try {
  182. const obj = {
  183. ...file,
  184. status: 'uploading',
  185. message: '上传中',
  186. progress: '0%',
  187. };
  188. this.fileList.push(obj);
  189. const config = {
  190. getTask: (task) => {
  191. task.onProgressUpdate((res) => {
  192. obj.progress = res.progress + '%';
  193. });
  194. },
  195. };
  196. const { data } = await uploadFile(file.url, config);
  197. const fileIdx = this.fileList.length - 1;
  198. // 上传成功 todo 这里是把旧的删除替换新的 http_url 并改变status
  199. this.fileList.splice(fileIdx, 1, {
  200. status: 'success',
  201. message: '',
  202. url: data.http_url,
  203. noPrefixUrl: data.url, // 没有前缀的
  204. });
  205. this.$emit('change', this.fileList);
  206. } catch (e) {
  207. // 上传失败
  208. this.fileList.splice(this.fileList.length - 1, 1);
  209. this.$emit('change', this.fileList);
  210. console.error(e);
  211. }
  212. },
  213. },
  214. };
  215. </script>
  216. <style scoped lang="scss">
  217. .upload_wrap_preview {
  218. display: flex;
  219. flex-wrap: wrap;
  220. #cVideo {
  221. opacity: 0;
  222. z-index: -1;
  223. width: 0 !important;
  224. height: 0;
  225. overflow: hidden;
  226. /deep/ .uni-video-container {
  227. width: 0 !important;
  228. height: 0;
  229. }
  230. }
  231. /deep/ .u-upload__wrap__preview {
  232. display: none !important;
  233. }
  234. .video_box {
  235. position: relative;
  236. background-color: #f2f2f2;
  237. display: flex;
  238. flex-direction: column;
  239. justify-content: center;
  240. align-items: center;
  241. border-radius: 2px;
  242. margin: 0 8px 8px 0;
  243. overflow: hidden;
  244. width: 80px;
  245. height: 80px;
  246. .upload_status {
  247. position: absolute;
  248. top: 0;
  249. bottom: 0;
  250. left: 0;
  251. right: 0;
  252. background-color: rgba(0, 0, 0, 0.5);
  253. display: flex;
  254. flex-direction: column;
  255. align-items: center;
  256. justify-content: center;
  257. z-index: 2;
  258. .upload_status_message {
  259. margin-top: 5px;
  260. font-size: 12px;
  261. color: #ffffff;
  262. }
  263. }
  264. .upload_success {
  265. position: absolute;
  266. bottom: 0;
  267. right: 0;
  268. display: flex;
  269. flex-direction: row;
  270. border-style: solid;
  271. border-top-color: transparent;
  272. border-left-color: transparent;
  273. border-bottom-color: #5ac725;
  274. border-right-color: #5ac725;
  275. border-width: 9px;
  276. align-items: center;
  277. justify-content: center;
  278. .upload_success_icon {
  279. position: absolute;
  280. -webkit-transform: scale(0.7);
  281. transform: scale(0.7);
  282. bottom: -10px;
  283. right: -10px;
  284. }
  285. }
  286. .upload_video_text {
  287. font-size: 11px;
  288. color: #909193;
  289. }
  290. ._video {
  291. width: 100%;
  292. height: 100%;
  293. }
  294. .del_icon {
  295. position: absolute;
  296. top: 0;
  297. right: 0;
  298. background-color: #373737;
  299. height: 14px;
  300. width: 14px;
  301. display: flex;
  302. flex-direction: row;
  303. border-bottom-left-radius: 100px;
  304. align-items: center;
  305. justify-content: center;
  306. z-index: 3;
  307. /deep/.uicon-close {
  308. position: absolute;
  309. top: 1px !important;
  310. right: 0;
  311. transform: scale(0.7);
  312. }
  313. }
  314. }
  315. }
  316. </style>