// pages/uploadShow/uploadShow.js import VODUpload from '../../utils/aliyun-upload-sdk-1.0.1.min' const app = getApp() const util = require("../../utils/util") const urlDef = require("../../utils/urls") var uploader = null //阿里云视频上传对象 Page({ /** * 页面的初始数据 */ data: { array: [], arrIndex: 0, videoItems: [], videoFlag: 0, }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { let stu = wx.getStorageSync('student'); const urls = urlDef.urls; util.apiPost(urls.person_video_course + '&q.studentId=' + stu.studentId).then(rs => { this.setData({ array: rs }) }); this.videoContext = wx.createVideoContext('play-video'); // 创建 video 上下文 VideoContext 对象。 const that = this const createUpLoad = new VODUpload({ //阿里账号ID,必须有值 userId: "WaWQOn6gXod13WLEp8cr4ljUdvcbXJ", //网络原因失败时,重新上传次数,默认为3 retryCount: 3, //网络原因失败时,重新上传间隔时间,默认为2秒 retryDuration: 2, //开始上传 'onUploadstarted': function (uploadInfo) { wx.showLoading({ title: '上传中...', mask: true }) // console.log("onUploadStarted:" + uploadInfo.file.name + ", endpoint:" + uploadInfo.endpoint + ", bucket:" + uploadInfo.bucket + ", object:" + uploadInfo.object) //上传方式1,需要根据uploadInfo.videoId是否有值,调用点播的不同接口获取uploadauth和uploadAddress,如果videoId有值,调用刷新视频上传凭证接口,否则调用创建视频上传凭证接口 if (uploadInfo.videoId) { //如果uploadInfo.videoId存在,调用刷新视频上传凭证接口 } else { //如果uploadInfo.videoId不存在,调用获取视频上传地址和凭证接口 } //从点播服务获取的uploadAuth、uploadAddress和videoId,设置SDK // console.log(that.data) uploader.setUploadAuthAndAddress(uploadInfo, that.data.uploadAuth, that.data.uploadAddress, that.data.videoId); }, //文件上传成功 'onUploadSucceed': function (uploadInfo) { console.log("上传成功:" + JSON.stringify(uploadInfo)) let vid = uploadInfo.videoId let stu = wx.getStorageSync('student') var video = { name: vid, videoId: vid, orgId: stu.orgId, size: uploadInfo.fileSize, title: '', content: '' }; // 保存一个 video let urls = urlDef.urls util.apiPost(urls.video_save, video, 'application/json').then((rs) => { var json = { url: uploadInfo.videoId, playUrl: uploadInfo.url, imgUrl: uploadInfo.coverUrl } console.log('视频上传成功' + JSON.stringify(json)) var list = [json] that.setData({ videoItems: that.data.videoItems.concat(list) }) wx.showToast({ title: '上传成功', }) }) // console.log("onUploadSucceed: " + uploadInfo.file.name + ", endpoint:" + uploadInfo.endpoint + ", bucket:" + uploadInfo.bucket + ", object:" + uploadInfo.object); }, //文件上传失败 'onUploadFailed': function (uploadInfo, code, message) { // console.log("onUploadFailed: file:" + uploadInfo.file.name + ",code:" + code + ", message:" + message); wx.showToast({ title: '上传失败', }) }, //文件上传进度,单位:字节 'onUploadProgress': function (uploadInfo, totalSize, loadedPercent) { // console.log("onUploadProgress:file:" + uploadInfo.file.name + ", fileSize:" + totalSize + ", percent:" + Math.ceil(loadedPercent * 100) + "%"); }, //上传凭证超时 'onUploadTokenExpired': function (uploadInfo) { wx.showToast({ title: '上传超时', }) console.log("onUploadTokenExpired"); //实现时,根据uploadInfo.videoId调用刷新视频上传凭证接口重新获取UploadAuth //从点播服务刷新的uploadAuth,设置到SDK里 uploader.resumeUploadWithAuth(uploadAuth); }, //全部文件上传结束 'onUploadEnd': function (uploadInfo) { console.log("onUploadEnd: uploaded all the files"); } }); uploader = createUpLoad }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { }, bindPickerChange: function (e) { this.setData({ arrIndex: e.detail.value }) }, len: function (s) { return (s == null ? 0 : s.length); }, formSubmit: function (e) { if (this.data.videoItems.length == 0) { wx.showToast({ title: '请先上传视频', icon: 'none' }); return; } let entity = e.detail.value let stu = wx.getStorageSync('student') entity.personId = stu.studentId entity.orgId = stu.orgId entity.personType = 0; // 学生 if (this.len(entity.title) === 0) { wx.showToast({ title: '请先上输入视频标题', icon: 'none' }); return; } wx.showLoading({ title: '保存中...', mask: true }) const urls = urlDef.urls; entity.videoId = this.data.videoItems[0].url; util.apiPost(urls.person_video_save, entity, 'application/json').then(rs => { entity = rs; wx.showToast({ title: '保存成功,等待校方审核' }); setTimeout(() => { wx.navigateBack({ delta: 1, // 返回上一级页面。 success: function () { console.log('成功!') } }) }, 1000) }) }, addVideo: function () { if (this.data.videoItems.length > 0) { wx.showToast({ title: '请先删除视频', icon: 'none' }); return; } this.getVideoList(); // 选择视频 }, showVideo: function (e) { const index = e.currentTarget.dataset.index const curVideo = this.data.videoItems[index].playUrl this.setData({ curVideo }) this.videoContext.requestFullScreen({ // 设置全屏时视频的方向,不指定则根据宽高比自动判断。 direction: 90 // 屏幕逆时针90度 }); }, // 监听视频是否为全屏,否则直接关闭 bindfullscreenchange: function () { if (this.data.videoFlag == 0) { this.setData({ videoFlag: 1 }) } else { this.setData({ videoFlag: 0, curVideo: "" }) } }, delVideo: function (e) { const index = e.currentTarget.dataset.index let videos = this.data.videoItems; let video = videos[index] let delFiles = []; delFiles.push(video.url) const params = { 'ids[]': delFiles } const urls = urlDef.urls; util.apiPost(urls.video_delete, params).then((rs) => { this.data.videoItems.splice(index, 1); this.setData({ videoItems: this.data.videoItems }) }); }, getVideoList: function () { let that = this wx.chooseVideo({ camera: ['album'], success(res) { var file = { url: res.tempFilePath, coverUrl: res.thumbTempFilePath }; var userData = '{"Vod":{}}'; const urls = urlDef.urls; let createUrl = urls.video_create_upload + '?title=' + res.tempFilePath + '&fileName=' + res.tempFilePath; console.log('createUploadVideo ... '); util.apiPost(createUrl).then((rs) => { let d = JSON.parse(rs.data); // console.log('createUploadVideo ' + JSON.stringify(d)); that.setData({ uploadAuth: d.UploadAuth, uploadAddress: d.UploadAddress, videoId: d.VideoId, }) uploader.addFile(file, null, null, null, userData) uploader.startUpload(); // uploader.setUploadAuthAndAddress(uploadInfo, uploadAuth, uploadAddress, videoId); }, e => { console.log('createUploadVideo error ' + JSON.stringify(e)); }); } }) } })