123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686 |
- // pages/doTask/doTask.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")
- const recordManager = wx.getRecorderManager() //录音对象
- const audioContext = wx.createInnerAudioContext() //音频播放对象
- var uploader = null //阿里云视频上传对象
- const urls = urlDef.urls;
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- uploadIng: false,
- homeworkId: null,
- talkFlag: false,
- closeName: '开始录音',
- videoList: [],
- videoFlag: 0,
- imgList: [],
- audioList: [],
- audioPause: false,
- content: '',
- delItems: [],
- contents: null,
- },
- renderTime: function (date) {
- var dateee = new Date(date).toJSON();
- return new Date(+new Date(dateee) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')
- },
- getRecord: function () {
- const that = this
- if (that.data.audioList.length >= 6) {
- wx.showToast({
- title: '最多上传6段录音',
- icon: 'none'
- })
- return
- }
- if (that.data.talkFlag) {
- recordManager.stop()
- recordManager.onStop((res) => { //监听录音停止的事件
- console.log("监听录音停止事件", res)
- if (res.duration < 1000) {
- wx.showToast({
- title: '录音时间太短',
- icon: 'none'
- })
- return;
- } else {
- console.log(res)
- var tempFilePath = res.tempFilePath; // 文件临时路径
- console.log("文件临时路径", tempFilePath)
- wx.uploadFile({
- filePath: tempFilePath,
- name: 'name',
- url: urls.file_upload,
- header: util.getHeaders(),
- success(res) {
- wx.showToast({
- title: '上传成功',
- })
- const rs = JSON.parse(JSON.parse(JSON.stringify(res.data)))
- const list = [{
- url: rs.data[0].url
- }]
- that.setData({
- audioList: that.data.audioList.concat(list)
- })
- },
- fail(res) {
- wx.showToast({
- title: '上传失败',
- icon: 'none'
- })
- },
- complete(res) {
- wx.hideLoading({
- success: (res) => {},
- })
- }
- })
- }
- });
- that.setData({
- talkFlag: false,
- closeName: '开始录音'
- })
- } else {
- wx.getSetting({
- success(res) {
- if (!res.authSetting['scope.record']) {
- wx.authorize({
- scope: 'scope.record',
- success() {
- // 用户已经同意小程序使用录音功能,后续调用 wx.startRecord 接口不会弹窗询问
- const options = {}
- recordManager.start(options)
- },
- fail() {
- wx.openSetting({
- withSubscriptions: true,
- })
- }
- })
- } else {
- that.setData({
- talkFlag: true,
- closeName: '正在录音,再次点击结束录音'
- })
- const options = {}
- recordManager.start(options)
- }
- }
- })
- }
- },
- showAudio: function (e) {
- const index = e.currentTarget.dataset.index
- if (this.data.curAudioIndex != index) {
- this.setData({
- curAudioIndex: index,
- })
- audioContext.src = this.data.audioList[index].url
- audioContext.play()
- audioContext.onEnded((res) => {
- this.setData({
- curAudioIndex: '-1',
- })
- })
- } else {
- if (this.data.audioPause) {
- audioContext.play()
- this.setData({
- audioPause: false
- })
- } else {
- audioContext.pause()
- this.setData({
- audioPause: true
- })
- }
- }
- },
- delAudio: function (e) {
- const index = e.currentTarget.dataset.index
- // this.data.audioList.splice(index, 1);
- // this.setData({
- // audioList: this.data.audioList
- // })
- let records = this.data.audioList;
- let record = records[index];
- record.delete = 1
- this.setData({
- audioList: records
- })
- },
- uploadTap: function () {
- const that = this
- if (that.data.imgList.length >= 6) {
- wx.showToast({
- title: '最多上传6张图片',
- icon: 'none'
- })
- return
- }
- wx.chooseImage({
- count: 6,
- sourceType: ['album', 'camera'],
- success(res) {
- wx.showLoading({
- title: '上传中...',
- })
- for (var i in res.tempFilePaths) {
- wx.uploadFile({
- filePath: res.tempFilePaths[i],
- name: 'name',
- url: urls.file_upload,
- header: util.getHeaders(),
- success(res) {
- wx.showToast({
- title: '上传成功',
- })
- const rs = JSON.parse(JSON.parse(JSON.stringify(res.data)))
- const list = [{
- url: rs.data[0].url
- }]
- that.setData({
- imgList: that.data.imgList.concat(list)
- })
- },
- fail(res) {
- wx.showToast({
- title: '上传失败',
- icon: none
- })
- },
- complete(res) {
- wx.hideLoading({
- success: (res) => {},
- })
- }
- })
- }
- }
- })
- },
- showImg: function (e) {
- const index = e.currentTarget.dataset.index
- let images = this.data.imgList;
- let imgs = []
- images.map(i => {
- imgs.push(i.url)
- })
- wx.previewImage({
- current: images[index].url,
- urls: imgs,
- })
- },
- delImg: function (e) {
- const index = e.currentTarget.dataset.index
- // this.data.imgList.splice(index, 1);
- let images = this.data.imgList;
- let image = images[index]
- image.delete = 1
- this.setData({
- imgList: images
- })
- },
- upLoadVideo: function () {
- const that = this
- if (that.data.videoList.length >= 6) {
- wx.showToast({
- title: '最多上传6部视频',
- icon: 'none'
- })
- return
- }
- 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,
- })
- wx.showLoading({
- title: '上传中...',
- })
- // that.setData({
- // uploadIng: true
- // })
- uploader.addFile(file, null, null, null, userData)
- uploader.startUpload();
- // uploader.setUploadAuthAndAddress(uploadInfo, uploadAuth, uploadAddress, videoId);
- }, e => {
- console.log('createUploadVideo error ' + JSON.stringify(e));
- });
- }
- })
- },
- showVideo: function (e) {
- const index = e.currentTarget.dataset.index
- const curVideo = this.data.videoList[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
- // this.data.videoList.splice(index, 1);
- // this.setData({
- // videoList: this.data.videoList
- // })
- let videos = this.data.videoList;
- let video = videos[index]
- video.delete = 1
- this.setData({
- videoList: videos
- })
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function (options) {
- let urls = urlDef.urls;
- let stu = wx.getStorageSync('student')
- let homeworkId = options.homeworkId;
- let studentId = stu.studentId;
- this.setData({
- homeworkId: homeworkId
- })
- let params = {
- 'q.homeworkId': homeworkId,
- 'q.studentId': studentId
- }
- util.apiPost(urls.homework_receive, params).then(rs => {});
- util.apiPost(urls.my_homework, params).then(rs => {
- if (rs.length > 0) {
- let entity = rs[0]
- let contents;
- try {
- contents = entity
- contents.receiveDate = this.renderTime(contents.receiveDate)
- this.setData({
- contents
- })
- } catch (e) {
- contents = {};
- console.error(e);
- }
- this.setData({
- content: contents.content
- })
- let images = contents.images == null ? [] : contents.images;
- images.forEach(r => {
- r.url = urls.oss_file + 'image/' + r.url
- });
- let records = contents.records == null ? [] : contents.records;
- records.forEach(r => {
- r.url = urls.oss_file + 'file/' + r.url
- });
- let videos = contents.videos == null ? [] : contents.videos;
- this.setData({
- imgList: images,
- audioList: records,
- videoList: videos
- });
- videos.forEach(r => {
- util.apiPost(urls.video_loadInfo + r.url).then(rs => {
- r.imgUrl = rs.img
- r.playUrl = rs.url
- this.reloadVideos(r)
- });
- });
- }
- });
- 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) {
- console.log("开始上传")
- // 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({
- videoList: that.data.videoList.concat(list)
- })
- wx.showToast({
- title: '上传成功',
- })
- that.setData({
- uploadIng: false
- })
- })
- // 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
- },
- removeUpload: function () {
- uploader.stopUpload()
- uploader.cleanList();
- wx.showToast({
- title: '取消上传',
- icon: 'none'
- })
- this.setData({
- uploadIng: false
- })
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady: function () {
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow: function () {
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide: function () {
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload: function () {
- audioContext.stop()
- },
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh: function () {
- },
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom: function () {
- },
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage: function () {
- },
- doConfirmComplete: function () {
- console.log('homeworkId ' + this.data.homeworkId)
- let contents = {};
- let dlist = [];
- let images = [];
- if (this.data.imgList.length > 0) {
- this.data.imgList.forEach(r => {
- console.log(r.url)
- let fid = r.url.substring(r.url.lastIndexOf('/') + 1)
- if (r.delete == 1) {
- dlist.push({
- id: fid,
- type: 'image'
- })
- } else {
- images.push({
- url: fid
- })
- }
- });
- }
- let records = [];
- if (this.data.audioList.length > 0) {
- this.data.audioList.forEach(r => {
- console.log(r.url,2)
- let fid = r.url.substring(r.url.lastIndexOf('/') + 1)
- if (r.delete == 1) {
- dlist.push({
- id: fid,
- type: 'record'
- })
- } else {
- records.push({
- url: fid
- })
- }
- });
- }
- let videos = [];
- if (this.data.videoList.length > 0) {
- this.data.videoList.forEach(r => {
- if (r.delete == 1) {
- dlist.push({
- id: r.url,
- type: 'video'
- })
- } else {
- videos.push({
- url: r.url
- })
- }
- });
- }
- this.setData({
- delItems: dlist
- })
- contents.images = images;
- contents.videos = videos;
- contents.records = records;
- contents.content = this.data.content;
- let stu = wx.getStorageSync('student')
- let data = {
- 'q.content': JSON.stringify(contents),
- 'q.status': 1,
- 'q.studentId': stu.studentId,
- 'q.homeworkId': this.data.homeworkId
- }
- util.apiPost(urls.complete_homework, data).then(rs => {
- const msgParams = {
- 'homeworkId': this.data.homeworkId,
- 'studentId': stu.studentId
- };
- util.apiPost(urls.homework_complete_send_message, msgParams).then((rs) => {
- console.log('发送通知 ' + JSON.stringify(rs));
- });
- wx.showToast({
- title: '已完成作业'
- })
- setTimeout(() => {
- wx.navigateBack({
- delta: 2, // 返回上一级页面。
- success: function () {
- console.log('成功!')
- }
- })
- }, 1000)
- // 删除文件
- if (this.data.delItems.length > 0) {
- // 视频的删除跟文件的删除不一样
- let delFiles = [];
- let delVideos = [];
- this.data.delItems.forEach(r => {
- if (r.type === 'video') {
- delVideos.push(r.id);
- } else {
- delFiles.push(r.id);
- }
- });
- if (delFiles.length > 0) {
- const params = {
- 'keys': delFiles
- };
- util.apiPost(urls.file_delete, params).then((rs) => {
- console.log('删除文件 ... ' + JSON.stringify(params));
- });
- }
- if (delVideos.length > 0) {
- const params = {
- 'ids[]': delVideos
- }
- util.apiPost(urls.video_delete, params).then((rs) => {
- console.log('删除视频 ... ' + JSON.stringify(params));
- });
- }
- }
- })
- },
- reloadVideos: function (v) {
- let videos = this.data.videoList
- videos.map(o => {
- if (o.url == v.url) {
- o = v;
- }
- })
- this.setData({
- videoList: videos
- })
- },
- doInput: function (e) {
- this.setData({
- content: e.detail.value
- })
- }
- })
|