course.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. (function() {
  2. var $ = qcVideo.get('$');
  3. var Version = qcVideo.get('Version');
  4. var account = accountDone();
  5. })();
  6. /**
  7. *
  8. * @param upBtnId 上传按钮ID
  9. * @param secretId 云api secretId
  10. * @param isTranscode 是否转码
  11. * @param isWatermark 是否设置水印
  12. * @param [transcodeNotifyUrl] 转码成功后的回调
  13. * @param [classId] 分类ID
  14. */
  15. function accountDone() {
  16. var $ = qcVideo.get('$'),
  17. ErrorCode = qcVideo.get('ErrorCode'),
  18. Log = qcVideo.get('Log'),
  19. JSON = qcVideo.get('JSON'),
  20. util = qcVideo.get('util'),
  21. Code = qcVideo.get('Code'),
  22. Version = qcVideo.get('Version');
  23. //您的secretKey
  24. var secret_key = $('#secret_key').val() || '';
  25. qcVideo.uploader.init(
  26. //1: 上传基础条件
  27. {
  28. web_upload_url: 'http://vod.qcloud.com/v2/index.php',
  29. secretId: qcloud_secret_id, // 云api secretId
  30. secretKey: qcloud_secret_key,
  31. //server端实现逻辑
  32. // 1:首先 将argStr ,使用secretKey做sha1加密,得到结果 result
  33. // 2:最后 将result做base64后返回
  34. getSignature: function(argStr, cb) {
  35. //注意:出于安全考虑, 服务端接收argStr这个参数后,需要校验其中的Action参数是否为 "MultipartUploadVodFile",用来证明该参数标识上传请求
  36. $.ajax({
  37. 'dataType': 'json',
  38. 'url': APP_ROOT + 'm.php?m=VideoPlayback&a=sign&argStr=' + encodeURIComponent(argStr),
  39. 'success': function(d) {
  40. cb(d['result']);
  41. }
  42. });
  43. },
  44. upBtnId: 'btn_upload', //上传按钮ID(任意页面元素ID)
  45. isTranscode: true, //是否转码
  46. isWatermark: false //是否设置水印
  47. ,
  48. after_sha_start_upload: false //sha计算完成后,开始上传 (默认关闭立即上传)
  49. ,
  50. sha1js_path: APP_ROOT + 'admin/Tpl/default/Common/js/calculator_worker_sha1.js' //计算sha1的位置
  51. ,
  52. disable_multi_selection: true //禁用多选 ,默认为false
  53. ,
  54. transcodeNotifyUrl: APP_ROOT + "m.php?m=Course&a=video_callback" //(转码成功后的回调地址)isTranscode==true,时开启; 回调url的返回数据格式参考 http://www.qcloud.com/wiki/v2/MultipartUploadVodFile
  55. ,
  56. classId: null
  57. }
  58. //2: 回调
  59. , {
  60. /**
  61. * 更新文件状态和进度
  62. * @param args { id: 文件ID, size: 文件大小, name: 文件名称, status: 状态, percent: 进度 speed: 速度, errorCode: 错误码,serverFileId: 后端文件ID }
  63. */
  64. onFileUpdate: function(args) {
  65. // console.log(args)
  66. // var $line = $('#' + args.id);
  67. // if (!$line.get(0)) {
  68. // $('#result').append('<div class="line" id="' + args.id + '"></div>');
  69. // $line = $('#' + args.id);
  70. // }
  71. // var finalFileId = '';
  72. // $line.html('' + '文件名:' + args.name + ' >> 大小:' + util.getHStorage(args.size) + ' >> 状态:' + util.getFileStatusName(args.status) + '' + (args.percent ? ' >> 进度:' + args.percent + '%' : '') + (args.speed ? ' >> 速度:' + args.speed + '' : '') + '<span data-act="del" class="delete">删除</span>' + finalFileId);
  73. switch (args.code) {
  74. case 1:
  75. case 3:
  76. $('#start_upload').hide();
  77. message = (args.percent ? args.percent : '0') + '%' + '(' + args.code_name + (args.speed ? ' 速度:' + args.speed + '' : '') + ')';
  78. $('#progress').show();
  79. $('#file_name').html(args.name);
  80. $('#sha').css('width', args.percent + '%');
  81. $('#sha>span').html(message);
  82. break;
  83. case 2:
  84. $('#start_upload').show();
  85. message = '100%' + '(' + args.code_name + ')';
  86. $('#sha').css('width', '100%').removeClass('progress-bar-striped active');
  87. $('#sha>span').html(message);
  88. break;
  89. case 4:
  90. case 5:
  91. message = (args.percent ? args.percent : '0') + '%' + '(' + args.code_name + (args.speed ? ' 速度:' + args.speed + '' : '') + ')';
  92. $('#up').css('width', args.percent + '%');
  93. $('#up>span').html(message);
  94. break;
  95. case 6:
  96. $('#start_upload').hide();
  97. message = '100%' + '(' + args.code_name + ')';
  98. $('#up').css('width', '100%').removeClass('progress-bar-striped active');
  99. $('#up>span').html(message);
  100. break;
  101. }
  102. if (args.code == Code.UPLOAD_DONE) {
  103. $('#file_id').val(args.serverFileId);
  104. $('#up>span').html('100% 上传完成!');
  105. getVideoUrlById = function() {
  106. $.post(APP_ROOT + "m.php?m=Course&a=getVideoUrlById", {
  107. id: args.serverFileId
  108. }, function(result) {
  109. if (result.status == '1') {
  110. $('#video').attr("src", result.url);
  111. $('#up>span').html('100% 上传完成!');
  112. } else {
  113. $('#up>span').html('100% 上传完成!(' + result.error + ')');
  114. $('#video').attr("src", '');
  115. console.log(result);
  116. setTimeout(getVideoUrlById, 1750);
  117. }
  118. }, 'json');
  119. }
  120. getVideoUrlById();
  121. }
  122. },
  123. /**
  124. * 文件状态发生变化
  125. * @param info { done: 完成数量 , fail: 失败数量 , sha: 计算SHA或者等待计算SHA中的数量 , wait: 等待上传数量 , uploading: 上传中的数量 }
  126. */
  127. onFileStatus: function(info) {
  128. // $('#count').text('各状态总数-->' + JSON.stringify(info));
  129. console.log(info)
  130. },
  131. /**
  132. * 上传时错误文件过滤提示
  133. * @param args {code:{-1: 文件类型异常,-2: 文件名异常} , message: 错误原因 , solution: 解决方法}
  134. */
  135. onFilterError: function(args) {
  136. var msg = 'message:' + args.message + (args.solution ? (';solution==' + args.solution) : '');
  137. // $('#error').html(msg);
  138. }
  139. });
  140. //事件绑定
  141. $('#start_upload').on('click', function() {
  142. //@api 上传
  143. qcVideo.uploader.startUpload();
  144. });
  145. $('#stop_upload').on('click', function() {
  146. //@api 暂停上传
  147. qcVideo.uploader.stopUpload();
  148. });
  149. $('#re_upload').on('click', function() {
  150. //@api 恢复上传(错误文件重新)
  151. qcVideo.uploader.reUpload();
  152. });
  153. $('#result').on('click', '[data-act="del"]', function(e) {
  154. var $line = $(this).parent();
  155. var fileId = $line.get(0).id;
  156. Log.debug('delete', fileId);
  157. // $line.remove();
  158. //@api 删除文件
  159. qcVideo.uploader.deleteFile(fileId);
  160. });
  161. };