uopladFile.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import PlvVideoUpload from '@polyv/vod-upload-js-sdk'
  2. import api from '@/api/api'
  3. // import md5 from 'js-md5'
  4. // 此方法主要用于加密一些保利威的专用参数 如果是后端传递的 可以打掉
  5. function getToken(videoUpload, array) {
  6. const ptime = array.ptime
  7. const userid = 'd5f6d309fe'
  8. // const secretkey = 'xpPrYdcbA1'
  9. // const writeToken = '8f14a371-9d02-4ec1-922d-54d7b4f79dca'
  10. const hash = array.hash
  11. const sign = array.sign
  12. // const hash = md5(ptime + writeToken)
  13. // const sign = md5(secretkey + ptime)
  14. videoUpload.updateUserData({ ptime, hash, sign, userid })
  15. videoUpload.startAll()
  16. }
  17. // 由于保利威的一些机制 你需要三分钟就重新加密你的 ptime 如果你打掉了就不行
  18. function autoUpdateUserData(timer, videoUpload, array) {
  19. getToken(videoUpload, array)
  20. if (timer) {
  21. clearTimeout(timer)
  22. timer = null
  23. }
  24. timer = setTimeout(() => {
  25. autoUpdateUserData(timer, videoUpload, array)
  26. }, 3 * 50 * 1000)
  27. }
  28. /**
  29. * @Date: 2021/3/26
  30. * @param: files -> 一个文件的数组 注意 是【数组】
  31. * @param: fileSetting -> 主要是用来传递给保利威的属性
  32. * @param: callback -> 上传进度,成功,失败 的回调 tips:回调太多 我就拿了这三个比较有用的
  33. */
  34. export function uploadFile(files, fileSetting, callback) {
  35. const videoUpload = new PlvVideoUpload({
  36. region: 'line1', // (可选)上传线路, 默认line1
  37. events: {
  38. Error: (err) => { // 错误事件回调
  39. console.log(err);
  40. },
  41. UploadComplete: () => { } // 全部上传任务完成回调
  42. }
  43. })
  44. console.log(videoUpload,'init')
  45. new Promise((resolve, reject) => {
  46. api.inquirepolyvvideogetPolyvUpload().then(res => {
  47. resolve(res)
  48. })
  49. }).then(res => {
  50. autoUpdateUserData(null, videoUpload, res.data)
  51. var filet = [files]
  52. Array.from(filet).forEach((file, index) => {
  53. console.log(videoUpload, 1121)
  54. const uploader = videoUpload.addFile(file, {
  55. // 上传视频进度的回调
  56. FileProgress: ({ progress }) => {
  57. const progressSize = (progress * 100).toFixed(2)
  58. callback(index, progressSize)
  59. },
  60. // 上传视频成功的回调
  61. FileSucceed: ({ fileData }) => {
  62. callback(index, fileData)
  63. },
  64. // 上传视频失败的回调
  65. onFileFailed: ({ errData }) => {
  66. callback(index, errData)
  67. }
  68. }, fileSetting)
  69. // console.log(uploader)
  70. })
  71. })
  72. /**
  73. * 这里的调用是上传全部
  74. * 本来我也是一个一个调的
  75. * 但是考虑到对于有多选上传不是很友好
  76. * 就无论你传递几个视频 这里都统一一起上传
  77. * */
  78. }