uploadShow.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // pages/uploadShow/uploadShow.js
  2. import VODUpload from '../../utils/aliyun-upload-sdk-1.0.1.min'
  3. const app = getApp()
  4. const util = require("../../utils/util")
  5. const urlDef = require("../../utils/urls")
  6. var uploader = null //阿里云视频上传对象
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. array: [],
  13. arrIndex: 0,
  14. videoItems: [],
  15. videoFlag: 0,
  16. },
  17. /**
  18. * 生命周期函数--监听页面加载
  19. */
  20. onLoad: function (options) {
  21. let stu = wx.getStorageSync('student');
  22. const urls = urlDef.urls;
  23. util.apiPost(urls.person_video_course + '&q.studentId=' + stu.studentId).then(rs => {
  24. this.setData({ array: rs })
  25. });
  26. this.videoContext = wx.createVideoContext('play-video'); // 创建 video 上下文 VideoContext 对象。
  27. const that = this
  28. const createUpLoad = new VODUpload({
  29. //阿里账号ID,必须有值
  30. userId: "WaWQOn6gXod13WLEp8cr4ljUdvcbXJ",
  31. //网络原因失败时,重新上传次数,默认为3
  32. retryCount: 3,
  33. //网络原因失败时,重新上传间隔时间,默认为2秒
  34. retryDuration: 2,
  35. //开始上传
  36. 'onUploadstarted': function (uploadInfo) {
  37. wx.showLoading({
  38. title: '上传中...',
  39. mask: true
  40. })
  41. // console.log("onUploadStarted:" + uploadInfo.file.name + ", endpoint:" + uploadInfo.endpoint + ", bucket:" + uploadInfo.bucket + ", object:" + uploadInfo.object)
  42. //上传方式1,需要根据uploadInfo.videoId是否有值,调用点播的不同接口获取uploadauth和uploadAddress,如果videoId有值,调用刷新视频上传凭证接口,否则调用创建视频上传凭证接口
  43. if (uploadInfo.videoId) {
  44. //如果uploadInfo.videoId存在,调用刷新视频上传凭证接口
  45. } else {
  46. //如果uploadInfo.videoId不存在,调用获取视频上传地址和凭证接口
  47. }
  48. //从点播服务获取的uploadAuth、uploadAddress和videoId,设置SDK
  49. // console.log(that.data)
  50. uploader.setUploadAuthAndAddress(uploadInfo, that.data.uploadAuth, that.data.uploadAddress, that.data.videoId);
  51. },
  52. //文件上传成功
  53. 'onUploadSucceed': function (uploadInfo) {
  54. console.log("上传成功:" + JSON.stringify(uploadInfo))
  55. let vid = uploadInfo.videoId
  56. let stu = wx.getStorageSync('student')
  57. var video = { name: vid, videoId: vid, orgId: stu.orgId, size: uploadInfo.fileSize, title: '', content: '' };
  58. // 保存一个 video
  59. let urls = urlDef.urls
  60. util.apiPost(urls.video_save, video, 'application/json').then((rs) => {
  61. var json = {
  62. url: uploadInfo.videoId,
  63. playUrl: uploadInfo.url,
  64. imgUrl: uploadInfo.coverUrl
  65. }
  66. console.log('视频上传成功' + JSON.stringify(json))
  67. var list = [json]
  68. that.setData({
  69. videoItems: that.data.videoItems.concat(list)
  70. })
  71. wx.showToast({
  72. title: '上传成功',
  73. })
  74. })
  75. // console.log("onUploadSucceed: " + uploadInfo.file.name + ", endpoint:" + uploadInfo.endpoint + ", bucket:" + uploadInfo.bucket + ", object:" + uploadInfo.object);
  76. },
  77. //文件上传失败
  78. 'onUploadFailed': function (uploadInfo, code, message) {
  79. // console.log("onUploadFailed: file:" + uploadInfo.file.name + ",code:" + code + ", message:" + message);
  80. wx.showToast({
  81. title: '上传失败',
  82. })
  83. },
  84. //文件上传进度,单位:字节
  85. 'onUploadProgress': function (uploadInfo, totalSize, loadedPercent) {
  86. // console.log("onUploadProgress:file:" + uploadInfo.file.name + ", fileSize:" + totalSize + ", percent:" + Math.ceil(loadedPercent * 100) + "%");
  87. },
  88. //上传凭证超时
  89. 'onUploadTokenExpired': function (uploadInfo) {
  90. wx.showToast({
  91. title: '上传超时',
  92. })
  93. console.log("onUploadTokenExpired");
  94. //实现时,根据uploadInfo.videoId调用刷新视频上传凭证接口重新获取UploadAuth
  95. //从点播服务刷新的uploadAuth,设置到SDK里
  96. uploader.resumeUploadWithAuth(uploadAuth);
  97. },
  98. //全部文件上传结束
  99. 'onUploadEnd': function (uploadInfo) {
  100. console.log("onUploadEnd: uploaded all the files");
  101. }
  102. });
  103. uploader = createUpLoad
  104. },
  105. /**
  106. * 生命周期函数--监听页面初次渲染完成
  107. */
  108. onReady: function () {
  109. },
  110. /**
  111. * 生命周期函数--监听页面显示
  112. */
  113. onShow: function () {
  114. },
  115. /**
  116. * 生命周期函数--监听页面隐藏
  117. */
  118. onHide: function () {
  119. },
  120. /**
  121. * 生命周期函数--监听页面卸载
  122. */
  123. onUnload: function () {
  124. },
  125. /**
  126. * 页面相关事件处理函数--监听用户下拉动作
  127. */
  128. onPullDownRefresh: function () {
  129. },
  130. /**
  131. * 页面上拉触底事件的处理函数
  132. */
  133. onReachBottom: function () {
  134. },
  135. /**
  136. * 用户点击右上角分享
  137. */
  138. onShareAppMessage: function () {
  139. },
  140. bindPickerChange: function (e) {
  141. this.setData({
  142. arrIndex: e.detail.value
  143. })
  144. },
  145. len: function (s) {
  146. return (s == null ? 0 : s.length);
  147. },
  148. formSubmit: function (e) {
  149. if (this.data.videoItems.length == 0) {
  150. wx.showToast({ title: '请先上传视频', icon: 'none' });
  151. return;
  152. }
  153. let entity = e.detail.value
  154. let stu = wx.getStorageSync('student')
  155. entity.personId = stu.studentId
  156. entity.orgId = stu.orgId
  157. entity.personType = 0; // 学生
  158. if (this.len(entity.title) === 0) {
  159. wx.showToast({ title: '请先上输入视频标题', icon: 'none' });
  160. return;
  161. }
  162. wx.showLoading({
  163. title: '保存中...',
  164. mask: true
  165. })
  166. const urls = urlDef.urls;
  167. entity.videoId = this.data.videoItems[0].url;
  168. util.apiPost(urls.person_video_save, entity, 'application/json').then(rs => {
  169. entity = rs;
  170. wx.showToast({ title: '保存成功,等待校方审核' });
  171. setTimeout(() => {
  172. wx.navigateBack({
  173. delta: 1, // 返回上一级页面。
  174. success: function () {
  175. console.log('成功!')
  176. }
  177. })
  178. }, 1000)
  179. })
  180. },
  181. addVideo: function () {
  182. if (this.data.videoItems.length > 0) {
  183. wx.showToast({ title: '请先删除视频', icon: 'none' });
  184. return;
  185. }
  186. this.getVideoList(); // 选择视频
  187. },
  188. showVideo: function (e) {
  189. const index = e.currentTarget.dataset.index
  190. const curVideo = this.data.videoItems[index].playUrl
  191. this.setData({
  192. curVideo
  193. })
  194. this.videoContext.requestFullScreen({ // 设置全屏时视频的方向,不指定则根据宽高比自动判断。
  195. direction: 90 // 屏幕逆时针90度
  196. });
  197. },
  198. // 监听视频是否为全屏,否则直接关闭
  199. bindfullscreenchange: function () {
  200. if (this.data.videoFlag == 0) {
  201. this.setData({
  202. videoFlag: 1
  203. })
  204. } else {
  205. this.setData({
  206. videoFlag: 0,
  207. curVideo: ""
  208. })
  209. }
  210. },
  211. delVideo: function (e) {
  212. const index = e.currentTarget.dataset.index
  213. let videos = this.data.videoItems;
  214. let video = videos[index]
  215. let delFiles = [];
  216. delFiles.push(video.url)
  217. const params = { 'ids[]': delFiles }
  218. const urls = urlDef.urls;
  219. util.apiPost(urls.video_delete, params).then((rs) => {
  220. this.data.videoItems.splice(index, 1);
  221. this.setData({
  222. videoItems: this.data.videoItems
  223. })
  224. });
  225. },
  226. getVideoList: function () {
  227. let that = this
  228. wx.chooseVideo({
  229. camera: ['album'],
  230. success(res) {
  231. var file = {
  232. url: res.tempFilePath,
  233. coverUrl: res.thumbTempFilePath
  234. };
  235. var userData = '{"Vod":{}}';
  236. const urls = urlDef.urls;
  237. let createUrl = urls.video_create_upload + '?title=' + res.tempFilePath + '&fileName=' + res.tempFilePath;
  238. console.log('createUploadVideo ... ');
  239. util.apiPost(createUrl).then((rs) => {
  240. let d = JSON.parse(rs.data);
  241. // console.log('createUploadVideo ' + JSON.stringify(d));
  242. that.setData({
  243. uploadAuth: d.UploadAuth,
  244. uploadAddress: d.UploadAddress,
  245. videoId: d.VideoId,
  246. })
  247. uploader.addFile(file, null, null, null, userData)
  248. uploader.startUpload();
  249. // uploader.setUploadAuthAndAddress(uploadInfo, uploadAuth, uploadAddress, videoId);
  250. }, e => {
  251. console.log('createUploadVideo error ' + JSON.stringify(e));
  252. });
  253. }
  254. })
  255. }
  256. })