submitVideo - 副本 (2).vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <template>
  2. <view class="task-form-item form-item-media">
  3. <view class="form-lable">
  4. <text><text class="form-must" v-if="rule && rule.required">* </text>视频:</text>
  5. <text class="form-media-num">({{videos.length}}/{{num}})</text>
  6. </view>
  7. <view class="form-img-video">
  8. <view v-if="videos.length <= num " class="updata-btn img-video-item" @click.stop="onChooseVideo()">
  9. <text class="iconfont_yige">&#xe69f;</text>
  10. <text>请上传视频</text>
  11. </view>
  12. <view class="img-video-box img-video-item" v-for="(item,index) in videos">
  13. <view class="video-cover" @click.stop="onplay(item)" />
  14. <view class="del-img-video iconfont_yige" @click.stop="delVldeo(index)">&#xe867;</view>
  15. <video :show-center-play-btn="false" :controls="false" class="img-video" :src="item"
  16. v-if="!checkPage"></video>
  17. </view>
  18. </view>
  19. <uni-popup ref="videoPlayRef" maskBg="rgba(0, 0, 0,.7)" @change="popupChange">
  20. <view class="video-play" style="width: 100vw;">
  21. <video autoplay v-if="playVideoUrl" style="width: 100%;max-height: 60vh;" :src="playVideoUrl" />
  22. </view>
  23. </uni-popup>
  24. </view>
  25. </template>
  26. <script>
  27. import {
  28. getcheckBigFile_Api,
  29. setUpload_tag_Api,
  30. getMargeTag_Api
  31. } from "@/api/task.js"
  32. import {
  33. getFileMd5,
  34. cutFile
  35. } from "@/utils/tool.js"
  36. import {
  37. uplaodVideo
  38. } from "@/utils/uplaodVideo"
  39. export default {
  40. name: 'submitVideo',
  41. model: {
  42. prop: ['videos'],
  43. event: ['onChange']
  44. },
  45. props: {
  46. checkPage: {
  47. type: Boolean,
  48. default: false
  49. },
  50. num: {
  51. type: Number,
  52. default: 1
  53. },
  54. rule: {
  55. type: Object,
  56. default: null
  57. },
  58. videos: {
  59. type: Array,
  60. default: () => [],
  61. require: true
  62. },
  63. },
  64. data() {
  65. return {
  66. fullscreenLoading: false,
  67. Storage_key: 'partUploadData',
  68. File_V: null,
  69. tempFilePath: null,
  70. playVideoUrl: '',
  71. uploadFileObj: {
  72. tagsList: [],
  73. uploadUrls: []
  74. },
  75. }
  76. },
  77. watch: {
  78. fullscreenLoading: {
  79. handler(newL, oldL) {
  80. if (newL) {
  81. uni.showLoading({
  82. title: '上传视频中',
  83. mask: true
  84. })
  85. } else {
  86. uni.hideLoading()
  87. }
  88. },
  89. }
  90. },
  91. mounted() {
  92. },
  93. methods: {
  94. popupChange(e) {
  95. if (!e || !e.show) {
  96. this.playVideoUrl = null;
  97. }
  98. },
  99. // 播放视频
  100. onplay(url) {
  101. this.playVideoUrl = url;
  102. this.$nextTick(() => {
  103. this.$refs.videoPlayRef.open();
  104. this.videoContext = uni.createVideoContext('myVideo');
  105. this.videoContext.play()
  106. })
  107. },
  108. delVldeo(index) {
  109. this.videos.splice(index, 1);
  110. },
  111. onChooseVideo() {
  112. try {
  113. uni.removeStorageSync(this.Storage_key);
  114. } catch (e) {
  115. // error
  116. }
  117. try {
  118. uni.chooseVideo({
  119. sourceType: ['camera', 'album'],
  120. compressed: true, // 压缩
  121. success: res => {
  122. plus.io.resolveLocalFileSystemURL(res.tempFilePath, (entry) => {
  123. entry.file(async (ent) => {
  124. // this.file = ent
  125. console.log('plus.io = ', ent)
  126. // uploadVideo是我从其他的js文件导入的方法
  127. await uplaodVideo(ent)
  128. })
  129. })
  130. return
  131. console.log('fileMd5 1= ', res)
  132. this.File_V = res;
  133. this.tempFilePath = res.tempFilePath;
  134. this.onCheck_Md5(res.tempFilePath, res)
  135. },
  136. fail: err => {
  137. console.log('fileMd5 err = ', err)
  138. }
  139. });
  140. } catch (e) {
  141. console.log('fileMd5 e= ', e)
  142. //TODO handle the exception
  143. }
  144. },
  145. async onCheck_Md5(tempFilePath, File) {
  146. console.log('onCheck_Md5 e= ', tempFilePath, File)
  147. const fileMd5 = await getFileMd5(File);
  148. console.log('fileMd5 2= ', fileMd5)
  149. if (!fileMd5) return
  150. this.fullscreenLoading = true
  151. getcheckBigFile_Api({
  152. fileMd5: fileMd5,
  153. originalName: File.name || File.tempFilePath
  154. }).then(res => {
  155. console.log('b = res ', res)
  156. const data = res.data;
  157. if (!data.bigStatus) {
  158. this.saveFileChunk(File, tempFilePath, fileMd5, data.uploadId);
  159. } else {
  160. this.unloadVideos(data.url);
  161. }
  162. }).catch(err => {
  163. console.log('b = ', err)
  164. this.fullscreenLoading = false;
  165. uni.showToast({
  166. title: '上传失败',
  167. icon: 'none'
  168. })
  169. })
  170. },
  171. splitFileIntoChunks(file, chunkSize) {
  172. const fileSize = file.size;
  173. const chunks = Math.ceil(fileSize / chunkSize);
  174. const fileChunks = [];
  175. for (let i = 0; i < chunks; i++) {
  176. const start = i * chunkSize;
  177. const end = Math.min(start + chunkSize, fileSize);
  178. const chunk = file.slice(start, end);
  179. fileChunks.push(chunk);
  180. }
  181. return fileChunks;
  182. },
  183. // 保存文件片段到后台
  184. async saveFileChunk(File, tempFilePath, fileMd5, uploadId) {
  185. const chunkSize = 1024 * 1024 * 5;
  186. console.log('b = res ', chunkSize)
  187. // 获取文件信息
  188. try {
  189. let fileSize = File.size; // 文件大小,单位Byte
  190. const fileChunks = this.splitFileIntoChunks(File, fileSize)
  191. console.log('fileChunks = ', fileChunks)
  192. return false
  193. // 循环处理每个切片
  194. let chunkList = []
  195. for (let i = 0; i < chunkCount; i++) {
  196. let start = i * chunkSize;
  197. let end = Math.min(start + chunkSize, fileSize);
  198. function chunkPush(page = 0) {
  199. chunkList.push(File.slice(start, end))
  200. // that.uploadFileObj.uploadUrls.push({});
  201. if (page * chunkSize < fileRaw.size) {
  202. chunkPush(page + 1)
  203. }
  204. }
  205. chunkPush()
  206. console.log('1111111 = ', chunkList)
  207. }
  208. return
  209. } catch (e) {
  210. //TODO handle the exception
  211. console.log('------------- ', e)
  212. }
  213. // cutFile(File).then(cutFileList => {
  214. // console.log('fileMd5 3 = ', cutFileList)
  215. // let num = 0;
  216. // let list = [];
  217. // let uploadFileObj = []
  218. // for (let i = 0; i < cutFileList.length; i++) {
  219. // console.log('fileMd5 = ', i)
  220. // let p = new Promise((resolve, reject) => {
  221. // const item = cutFileList[i];
  222. // uni.uploadFile({
  223. // url: this.$mConfig.baseUrl + '/common/upload-tag',
  224. // formData: {
  225. // file: item.blob,
  226. // fileMd5: fileMd5,
  227. // uploadId: uploadId,
  228. // curPartSize: item.blob.size,
  229. // partNumber: item.partNumber
  230. // },
  231. // success: (uploadFileRes) => {
  232. // const JSON_data = uploadFileRes.data;
  233. // const data = JSON.parse(JSON_data);
  234. // const tag = data.data.tag;
  235. // uploadFileObj.push(tag)
  236. // resolve()
  237. // },
  238. // fail(fail) {
  239. // reject()
  240. // }
  241. // })
  242. // });
  243. // list.push(p)
  244. // }
  245. // Promise.all(list).then(() => {
  246. // this.bigFileMergeFun(uploadFileObj, fileMd5, uploadId)
  247. // }).catch(() => {
  248. // this.fullscreenLoading = false;
  249. // uni.showToast({
  250. // title: '上传失败',
  251. // icon: 'none'
  252. // })
  253. // })
  254. // })
  255. },
  256. // 合并
  257. bigFileMergeFun(uploadFileObj, fileMd5, uploadId) {
  258. console.log('bigFileMergeFun 4= ', uploadFileObj)
  259. let param = {
  260. fileMd5: fileMd5,
  261. tagsList: uploadFileObj,
  262. uploadId: uploadId
  263. }
  264. getMargeTag_Api(param).then(res => {
  265. console.log('getMargeTag_Api 5= ', res)
  266. const url = res.data.url;
  267. this.unloadVideos(url)
  268. }).catch(err => {
  269. this.fullscreenLoading = false;
  270. uni.showToast({
  271. title: '上传失败',
  272. icon: 'none'
  273. })
  274. })
  275. },
  276. unloadVideos(url) {
  277. this.videos.push(url)
  278. this.fullscreenLoading = false;
  279. uni.showToast({
  280. title: '上传成功',
  281. icon: 'none'
  282. })
  283. }
  284. }
  285. }
  286. </script>
  287. <style lang="scss" scoped>
  288. @import '~./submint.scss';
  289. .video-cover {
  290. position: absolute;
  291. left: 0;
  292. right: 0;
  293. top: 0;
  294. bottom: 0;
  295. z-index: 2;
  296. }
  297. </style>