video.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. const util = require('../../../../utils/util.js');
  2. const app = getApp();
  3. import VODUpload from '../../../../utils/aliyun-upload-sdk-1.0.0.min.js'
  4. Page({
  5. data: {
  6. index: null,
  7. show: false,
  8. info: {},
  9. isRestUpload: false,
  10. courseId: '',
  11. alert: {}
  12. },
  13. /**
  14. * 生命周期函数--监听页面初次渲染完成
  15. */
  16. onReady: function () {
  17. var that = this;
  18. let imgUrl
  19. var uploader = new VODUpload({
  20. timeout: 60000,
  21. region: "cn-shanghai",
  22. // 添加文件成功
  23. addFileSuccess: function (uploadInfo) {
  24. console.log("添加文件成功")
  25. },
  26. // 开始上传
  27. onUploadstarted: function (uploadInfo) {
  28. console.log('文件开始上传...');
  29. var url;
  30. let filName = that.data.playerId + '.mp4'
  31. url = util.config.apiServer + "vod/auth.do?fileName=" + filName + '&cate=course';
  32. wx.request({
  33. 'url': url,
  34. success: (res => {
  35. if (res.data.code === 0) {
  36. imgUrl = res.data.url + res.data.address.FileName
  37. var akInfo = res.data;
  38. uploader.setUploadAuthAndAddress(uploadInfo, akInfo.uploadAuth, akInfo.uploadAddress, akInfo.videoId);
  39. } else {
  40. util.showTips('上传失败,请重新上传');
  41. uploader.stopUpload();
  42. }
  43. }),
  44. fail: (res => {
  45. util.showTips(res.reason);
  46. uploader.stopUpload();
  47. })
  48. });
  49. },
  50. // 文件上传成功
  51. onUploadSucceed: function (uploadInfo) {
  52. console.log('文件上传成功!')
  53. that.setData({
  54. percent: 100
  55. });
  56. that.data.info.myVideo = imgUrl;
  57. that.setData({
  58. info: that.data.info
  59. });
  60. that.uploadAli()
  61. wx.hideLoading();
  62. },
  63. // 文件上传失败
  64. onUploadFailed: function (uploadInfo, code, message) {
  65. console.log('文件上传失败!')
  66. util.showTips('上传失败,请重新上传');
  67. uploader.stopUpload();
  68. },
  69. // 上传凭证超时
  70. onUploadTokenExpired: function (uploadInfo) {},
  71. // 全部文件上传结束
  72. onUploadEnd: function (uploadInfo) {
  73. }
  74. });
  75. this.setData({
  76. uploader: uploader
  77. });
  78. },
  79. onLoad(options) {
  80. this.setData({
  81. courseId: options.courseId || '',
  82. playerId: options.playerId || ''
  83. });
  84. this.getInfo();
  85. },
  86. getInfo() {
  87. let that = this;
  88. util.ajax({
  89. func: 'v2/course/video/detail',
  90. data: {
  91. "courseId": that.data.courseId,
  92. "playerId": that.data.playerId
  93. }
  94. }, function (res) {
  95. if (res.code == 0) {
  96. that.setData({
  97. info: res.data,
  98. isRestUpload: !util.isEmpty(res.data.myVideo)
  99. });
  100. } else
  101. util.showTips(res.reason);
  102. })
  103. },
  104. videoPlay(event) {
  105. let e = event.currentTarget,
  106. index = e.dataset['index'];
  107. if (!this.data.index) {
  108. this.setData({
  109. index
  110. });
  111. this.videoContext = wx.createVideoContext('video_' + index)
  112. this.videoContext.play();
  113. this.videoContext.requestFullScreen();
  114. } else {
  115. this.videoContext = wx.createVideoContext('video_' + this.data.index)
  116. this.videoContext.stop();
  117. this.videoContext.exitFullScreen();
  118. this.setData({
  119. index
  120. });
  121. this.videoContext = wx.createVideoContext('video_' + index);
  122. this.videoContext.play();
  123. this.videoContext.requestFullScreen();
  124. }
  125. },
  126. playEnd() {
  127. this.setData({
  128. index: null
  129. });
  130. },
  131. fullscreen(e) {
  132. if (!e.detail.fullScreen) {
  133. this.videoContext.stop();
  134. this.setData({
  135. index: null
  136. });
  137. }
  138. },
  139. close() {
  140. this.setData({
  141. isRestUpload: true
  142. });
  143. },
  144. // 视频上传
  145. addVideo() {
  146. let that = this;
  147. wx.chooseMedia({
  148. count: 1,
  149. mediaType: ["video"],
  150. type: "video",
  151. sourceType: ["album", "camera"],
  152. maxDuration: 30,
  153. camera: "back",
  154. success(res) {
  155. if (parseInt(res.tempFiles[0].duration / 60) > util.config.duration) {
  156. util.showTips("您上传的视频时间太长,请限制在" + util.config.duration + "分钟之内。");
  157. return false;
  158. }
  159. wx.showLoading({
  160. title: "上传中…",
  161. });
  162. // api拿不到真实文件名
  163. let name = res.tempFiles[0].tempFilePath.split('tmp/')
  164. var file = {
  165. url: res.tempFiles[0].tempFilePath,
  166. name: name
  167. };
  168. that.setData({
  169. videosName: file.name
  170. });
  171. var uploader = that.data.uploader;
  172. var userData = '{"Vod":{}}'
  173. uploader.addFile(file, null, null, null, userData)
  174. that.data.uploader.startUpload();
  175. },
  176. });
  177. },
  178. uploadAli() {
  179. var _this = this;
  180. util.ajax({
  181. func: "v2/course/video/confirm",
  182. data: {
  183. video: _this.data.info.myVideo,
  184. sampleId: _this.data.info.id,
  185. playerId: _this.data.playerId
  186. },
  187. method: 'POST',
  188. contentType: 'application/x-www-form-urlencoded'
  189. }, function (res) {
  190. if (res.code == 0) {
  191. let alert = {
  192. title: res.data.reupload == 0 ? "恭喜您,上传成功" : "恭喜您,重传成功"
  193. };
  194. if (res.data.status == 1) {
  195. alert.icon = 'upload-ok';
  196. alert.score = res.data.score;
  197. } else {
  198. alert.content = res.data.message;
  199. alert.icon = res.data.overtime == 0 ? 'reset-upload' : 'time-out';
  200. alert.status = 1;
  201. }
  202. _this.setData({
  203. alert,
  204. show: true
  205. });
  206. } else
  207. util.showTips(res.reason);
  208. });
  209. }
  210. })