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; var isFinished=0; Page({ data: { IsSelect: true, Color: { ID: 1 }, }, 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(); }); this.audioCtx = wx.createAudioContext('myAudio'); }, onLoad: function (options) { var that = this; that.setData({ IsShowAnswer: [0, 0], Color: main.getDetailColor(app.globalData.ColorIndex), Containnerheight: main.getWindowHeight(), }); if (options.type) that.init2(options); else that.init(options); that.getColor(); isFinished = 0; if (app.globalData.BaiduToken === "") main.getBaiduToken(); }, init: function (options) { var that = this; that.setData({ TaskInfo: {}, TestType: options.testtype, TestName1: options.name1, TestName2: options.name2, }); main.getData('GetHanziUnitWords?UnitID=' + options.unitsid, function (data) { if (data) { TaskList = []; var words = data; for (var k = 0; k < words.length; k++) { var taskInfo = { FontSize: 144, Content: [ { ContentType: 0, Content: [] }, { ContentType: 1, Content: [] }, { ContentType: 2, Content: [] }, ] }; taskInfo.Word=words[k].Name; var pinyinNormal = "(" + main.getPinyinNormal(words[k].Pinyin) + ")"; taskInfo.ReadString = words[k].Name + pinyinNormal + "," + words[k].CombineWords + "的" + words[k].Name + pinyinNormal; if (that.data.TestType == "read") { taskInfo.Content[0].Content = ["怎么念"]; var question = words[k].CombineWords; question = common.ReplaceAllString(question, words[k].Name, "(" + words[k].Name + ")"); taskInfo.Content[1].Content = question; var answer = "[读 src='" + taskInfo.ReadString + "']" + words[k].Pinyin + "[/读]"; taskInfo.Content[2].Content = answer; if (words[k].CombineWords.length > 6) { taskInfo.FontSize = 108; if (words[k].CombineWords.length > 10) { taskInfo.FontSize = 68; } } } else if (that.data.TestType == "write") { taskInfo.Content[0].Content = ["怎么写"]; var question = words[k].CombineWords; var question2 = common.ReplaceAllString(question, words[k].Name, words[k].Pinyin); question = "[读 src='" + taskInfo.ReadString + "']" + question2 + "[/读]" taskInfo.Content[1].Content = question; var answer = words[k].Name; answer += "[图 w='650' h='650']" + words[k].BiShunUrl + "[/图]"; taskInfo.Content[2].Content = answer; if (question2.length > 6) { taskInfo.FontSize = 108; if (question2.length > 10) { taskInfo.FontSize = 68; if (question2.length > 14) { taskInfo.FontSize = 48; } } } } taskInfo.ContentNew = main.changeStringToView(taskInfo.Content); TaskList.push(taskInfo); } TaskList = common.randomArray(TaskList); that.setData({ TaskInfo: TaskList[0], TaskLength: TaskList.length, CurrentIndex: 0, }); 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 } } }) } }); } }); }, init2: function (options) { var that = this; var TestTask2=wx.getStorageSync("TestTask2"); var name = TestTask2.Name.split("#"); that.setData({ TaskInfo: {}, ID:TestTask2.ID, TestType: TestTask2.TestType, TestName1: name[0], TestName2: name[1], }); TestTask2.List = common.randomArray(TestTask2.List); var list = []; var i = 0; for (var i = 0; i < TestTask2.List.length;i++){ if (TestTask2.List[i].Result){ list.push(TestTask2.List[i]); } } var currentIndex=list.length; for (var i = 0; i < TestTask2.List.length; i++) { if (!TestTask2.List[i].Result) { list.push(TestTask2.List[i]); } } TaskList = list; if (currentIndex >= TaskList.length || options.type=="restart"){ currentIndex=0; for (var i = 0; i < TaskList.length; i++) { delete TaskList[i].Result; } } that.setData({ TaskLength: TaskList.length, CurrentIndex: currentIndex, TaskInfo: TaskList[currentIndex], }); 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 (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); TaskList[that.data.CurrentIndex].Result=null; that.data.CurrentIndex--; } //结束 if (that.data.CurrentIndex>=TaskList.length){ isFinished = 1; 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") { 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; var url; if (str == "recorder") { url = e.currentTarget.dataset.soundmark; } else if (str.indexOf("英 [") >= 0 || str.indexOf("美 [") >= 0) { str = str.replace("英 [", "["); str = str.replace("美 [", "["); url = e.currentTarget.dataset.soundmark; } 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(); if (res.statusCode === 200) { tempPlayUrlLocal = res.tempFilePath; that.audioCtx.setSrc(res.tempFilePath); that.audioCtx.play(); tempPlayUrl = url; } }, fail(err) { wx.hideLoading(); } }); } else { 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); }, //获取导航栏高度 getBarInfo(e) { this.setData({ topBarHeight: e.detail.topBarHeight }) }, onFinished:function(){ if (TaskList.length > 0 && this.data.CurrentIndex > 0) { var TestTask = {}; if (this.data.ID) TestTask.ID=this.data.ID; TestTask.TestType = this.data.TestType; TestTask.IsFinished = isFinished; TestTask.Name = this.data.TestName1 + "#" + this.data.TestName2; TestTask.List = TaskList; wx.setStorageSync("TestTask", TestTask); wx.redirectTo({ url: './report?type=new', }); } else{ wx.navigateBack({ delta: 1, }); } }, onUnload:function(){ wx.removeStorageSync("TestTask2"); if (innerAudioContext1) { innerAudioContext1.stop(); } }, onShareAppMessage: function () { return { title: app.globalData.ShareTitle, path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID, imageUrl: app.globalData.ShareImage, } }, })