import common from '../../utils/util'; import main from '../../utils/main'; const app = getApp(); var TaskList = []; var isPlaying = false; var innerAudioContext1; var timeoutPlayAudio; var tempPlayUrl = ""; var tempPlayUrlLocal = ""; var timeoutEveryCard = 0; Page({ data: { IsSelect: true, Color: { ID: 1 }, ImagePath: app.globalData.uploadImageUrl, }, onReady: function () { var that = this; innerAudioContext1 = wx.createInnerAudioContext(); innerAudioContext1.onPlay(() => { console.log('开始播放') }); innerAudioContext1.onError((res) => { console.log("innerAudioContext1.errMsg:" + res.errMsg); console.log("innerAudioContext1.errCode:" + res.errCode); that.audioCtx.setSrc(tempPlayUrl); that.audioCtx.play(); }); }, onLoad: function (options) { var that = this; that.setData({ Category:options.Category, BookID:options.BookID, UnitID:options.UnitID, LibraryName1:options.LibraryName1, LibraryName2:options.LibraryName2, BookName:options.BookName, UnitName:options.UnitName, TestTypeName:options.TestTypeName, IsShowAnswer: [0, 0], Color: main.getDetailColor(app.globalData.ColorIndex), Containnerheight: main.getWindowHeight(), }); that.init(options); that.getColor(); this.audioCtx = wx.createAudioContext('myAudio'); if (app.globalData.BaiduToken === "") main.getBaiduToken(); }, init:function(options){ var that = this; that.setData({ TaskInfo: {}, TestType: options.TestType, }); main.getData('GetMiaoguoTestExamine?UserID='+app.globalData.userInfo.UserID+'&BookID='+options.BookID+'&UnitName='+options.UnitName+'&UnitID='+options.UnitID+'&TestType='+options.TestType, function (data) { if (data) { TaskList=data; TaskList = common.randomArray(TaskList); that.setData({ TaskInfo: TaskList[0], TaskLength: TaskList.length, CurrentIndex: 0, }); //debugger; common.getStorageValue(that, "TestAutioPlay", 1, function () { if (that.data.TestAutioPlay == 1 && that.data.TestType == "write") { that.playSound({ currentTarget: { dataset: { content: TaskList[that.data.CurrentIndex].ReadString } } }) } }); } }); }, //进入下一张题卡 gotoNext: function (e) { var that = this; that.setData({ IsShowAnswer: [0, 0], }); var btnnumber = e.currentTarget.dataset.btnnumber; if (btnnumber>="0"){ that.setSoundFile(0); if (TaskList[that.data.CurrentIndex]){ if (btnnumber==="4") TaskList[that.data.CurrentIndex].Result = 1; else if (btnnumber === "0") TaskList[that.data.CurrentIndex].Result = -1; else if (btnnumber === "1") TaskList[that.data.CurrentIndex].Result = 0; } that.data.CurrentIndex++; } else{ that.setSoundFile(2); if (TaskList[that.data.CurrentIndex] && TaskList[that.data.CurrentIndex].Result) TaskList[that.data.CurrentIndex].Result=null; that.data.CurrentIndex--; } //结束 if (that.data.CurrentIndex>=TaskList.length){ that.onFinished(); } else if (that.data.CurrentIndex <0) { wx.showToast({ title: '到开始了', }); that.setSoundFile(-1); } else{ that.setData({ TaskInfo: TaskList[that.data.CurrentIndex], CurrentIndex: that.data.CurrentIndex, }); if (that.data.TestAutioPlay == 1 && that.data.TestType == "write") { that.playSound({ currentTarget: { dataset: { content: TaskList[that.data.CurrentIndex].ReadString } } }) } } //console.log(that.data.TestType); //console.log(JSON.stringify(that.data.TaskInfo)); }, onPullDownRefresh: function () { var that = this; that.setData({ IsShowAnswer: [0, 0], }); wx.stopPullDownRefresh(); }, onShowAnswer: function (e) { var that = this; var clicktype = e.currentTarget.dataset.type; if (!(clicktype && clicktype == "all" && app.globalData.ClickType == 0)) { that.setData({ IsShowAnswer: [1, 1], }); if (that.data.TestAutioPlay == 1 && that.data.TestType == "read") { if (TaskList[that.data.CurrentIndex] && TaskList[that.data.CurrentIndex].ReadString) that.playSound({ currentTarget: { dataset: { content: TaskList[that.data.CurrentIndex].ReadString } } }) } } }, getColor: function () { var that = this; if (wx.setBackgroundColor) { wx.setBackgroundColor({ backgroundColor: that.data.Color.BackColor, backgroundColorTop: that.data.Color.BackColor, backgroundColorBottom: that.data.Color.BackColor, }) } if (wx.setNavigationBarColor) { wx.setNavigationBarColor({ frontColor: "#ffffff", backgroundColor: that.data.Color.BackColor, }) } }, playSound: function (e) { clearTimeout(timeoutPlayAudio); var that = this; var str = e.currentTarget.dataset.content; if (str){ var url; if (str == "recorder") { url = e.currentTarget.dataset.soundmark; } else if (str.indexOf("英 [") >= 0 || str.indexOf("美 [") >= 0 || str.indexOf("baidu.com")>0 || str.indexOf("iciba.com")>0 || str.indexOf("myqcloud.com")>0) { str = str.replace("英 [", "["); str = str.replace("美 [", "["); url = e.currentTarget.dataset.soundmark; if (!url) url=str; } else if (str.indexOf("pinyin")>0){ url=str; } else if (e.currentTarget.dataset.soundmark && e.currentTarget.dataset.soundmark != "undefined") { var soundmark = e.currentTarget.dataset.soundmark; if (soundmark && soundmark.indexOf("http") < 0) { url = app.globalData.audioUrlBaidu; url = url.replace("[token]", app.globalData.BaiduToken); url = url.replace("[word]", soundmark); } else { url = soundmark; } } else { url = app.globalData.audioUrlBaidu; url = url.replace("[token]", app.globalData.BaiduToken); url = url.replace("[word]", str); } if (url.indexOf("http") > 0) url = url.substr(url.indexOf("http")); if (url.indexOf("'") > 0) url = common.ReplaceAllString(url, "'", ""); url = url.replace("http://", "https://"); url = encodeURI(url); if (!isPlaying || tempPlayUrl == "" || url != tempPlayUrl) { isPlaying = true; timeoutPlayAudio = setTimeout(function () { isPlaying = false; }, 60000); wx.showLoading({ title: '音频下载中', mask: true, }); setTimeout(function () { wx.hideLoading(); }, 30000); wx.downloadFile({ url: url, success(res) { wx.hideLoading(); tempPlayUrlLocal = res.tempFilePath; innerAudioContext1.src = res.tempFilePath; innerAudioContext1.play(); tempPlayUrl = url; }, fail(err) { wx.hideLoading(); if (url.indexOf("sp0.baidu.com")>0){ var soundmark=url.substring(url.indexOf("text=")+5,url.indexOf("&spd")); url = app.globalData.audioUrlYoudao; url = url.replace("[word]", soundmark); tempPlayUrlLocal = url; that.audioCtx.setSrc(url); that.audioCtx.play(); tempPlayUrl = url; } } }); } else { if (this.audioCtx) this.audioCtx.pause(); if (innerAudioContext1) innerAudioContext1.stop(); isPlaying = false; } } }, audioBindEnded: function () { isPlaying = false; }, audioBindError: function (err) { console.log(err); innerAudioContext1.src = tempPlayUrlLocal; innerAudioContext1.play(); main.getBaiduToken(); }, setSoundFile: function (isFinish) { //isFinish=3搁置 =2撤退 =1结束 =0正常 =-1开始 var that = this; var url = "/pages/sounds/"; if (isFinish == 1) { url += "end.mp3"; } else if (isFinish == 2) { url += "short3.mp3"; } else if (isFinish == 3) { url += "short1.mp3"; } else { url += "short2.mp3"; } if (isFinish >= 0) { innerAudioContext1.src = url; innerAudioContext1.play(); } wx.pageScrollTo({ scrollTop: 0, }); }, onAutoPlay:function(e){ var id=e.currentTarget.dataset.id; this.setData({ TestAutioPlay:id, }); wx.setStorageSync("TestAutioPlay",id); }, onFinished:function(){ if (TaskList.length > 0 && this.data.CurrentIndex > 0) { var TestTask = {}; TestTask.Category = this.data.Category; TestTask.TestType = this.data.TestType; TestTask.LibraryName1 = this.data.LibraryName1; TestTask.LibraryName2 = this.data.LibraryName2; TestTask.BookName = this.data.BookName; TestTask.UnitName = this.data.UnitName; TestTask.TestTypeName = this.data.TestTypeName; TestTask.BookID=this.data.BookID; TestTask.UnitID=this.data.UnitID; TestTask.List = TaskList; TestTask.ReportType="new"; app.globalData.TestTask=TestTask; wx.redirectTo({ url: './report', }); } else{ wx.navigateBack({ delta: 1, }); } }, onUnload:function(){ wx.removeStorageSync("TestTask2"); if (innerAudioContext1) { innerAudioContext1.stop(); } }, showBishunOrKaiti:function(e){ var that=this; main.downloadBishunKaitiImage( e.currentTarget.dataset.url, e.currentTarget.dataset.serverurl, e.currentTarget.dataset.type, that.data.TaskInfo.ContentNew.Field,function(fields){ that.data.TaskInfo.ContentNew.Field=fields; that.setData({ TaskInfo: that.data.TaskInfo, }); }); }, close: function (e) { wx.showModal({ title: '提醒', showCancel: true, content: "中途退出将不保留检验进度,重新检验时需从头开始。若完成检验,系统将生成检验报告并创建相应的记忆卡。", confirmText: "退出", cancelText: "取消", complete: (res) => { if (res.confirm) { wx.navigateBack({ delta: 1, }); } } }); }, onShareAppMessage: function () { return { title: app.globalData.ShareTitle, path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID, imageUrl: app.globalData.ShareImage, } }, })