| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602 |
- 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();
- });
-
- },
- onLoad: function (options) {
- var that = this;
- that.setData({
- BookID:options.bookid,
- IsShowAnswer: [0, 0],
- Color: main.getDetailColor(app.globalData.ColorIndex),
- Containnerheight: main.getWindowHeight(),
- });
- if (options.type)
- that.init2(options);
- else{
- if (options.bookid<100){
- if (options.bookid>=25 && options.bookid<=42)
- that.initAncientPoetry(options);
- else if (options.bookid>=43 && options.bookid<=44)
- that.initPinyin(options);
- else if (options.bookid>=45 && options.bookid<=60)
- that.initCi(options);
- else
- that.init(options);
- }
- else
- that.initEng(options);
- }
- that.getColor();
-
- this.audioCtx = wx.createAudioContext('myAudio');
- 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.GetHanziUnitWords(options.unitsid,that.data.TestType, function (data) {
- if (data) {
- TaskList=data;
- 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 } } })
- }
- });
- }
- });
- },
- initPinyin: function (options) {
- var that = this;
- that.setData({
- TaskInfo: {},
- TestType: options.testtype,
- TestName1: options.name1,
- TestName2: options.name2,
- });
- main.getData('GetPinyinUnitWords?UserID='+app.globalData.userInfo.UserID+'&BookID='+options.bookid+'&UnitID='+options.unitsid+'&Word='+options.name2, function (data) {
- if (data) {
- TaskList = [];
- var list = data;
- for (var k = 0; k < list.length; k++) {
- var taskInfo = list[k];
-
- taskInfo.TagWidth=230;
- taskInfo.FontSize=108;
- 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 } } })
- }
- });
- }
- });
- },
- initCi: function (options) {
- var that = this;
- that.setData({
- TaskInfo: {},
- TestType: options.testtype,
- TestName1: options.name1,
- TestName2: options.name2,
- });
- main.getData('GetHanziUnitWords?UserID='+app.globalData.userInfo.UserID+'&UnitID='+options.unitsid+'&TestType='+options.testtype, function (data) {
- if (data) {
- TaskList = [];
- var list = data;
- for (var k = 0; k < list.length; k++) {
- var taskInfo = list[k];
-
- taskInfo.TagWidth=140;
- taskInfo.FontSize=64;
- 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],
- BookID:TestTask2.BookID,
- });
- if (that.data.BookID<=12)
- 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 } } })
- }
- });
- },
- initAncientPoetry:function(options){
- var that=this;
- that.setData({
- TaskInfo: {},
- TestType: options.testtype,
- TestName1: options.name1,
- TestName2: "古诗文",
- });
- main.getData('GetTestAncientPoetryList?UserID='+app.globalData.userInfo.UserID+'&BookID='+options.bookid, function (data) {
- if (data) {
- TaskList = [];
- var list = data;
- for (var k = 0; k < list.length; k++) {
- var taskInfo = list[k];
-
- taskInfo.TagWidth=336;
- if (taskInfo.Word.length>8)
- taskInfo.FontSize=48;
- else
- taskInfo.FontSize=64;
- taskInfo.ContentNew = main.changeStringToView(taskInfo.Content);
- TaskList.push(taskInfo);
- }
- //TaskList = common.randomArray(TaskList);
- that.setData({
- TaskInfo: TaskList[0],
- TaskLength: TaskList.length,
- CurrentIndex: 0,
- });
- }
- });
- },
- initEng: function (options) {
- var that = this;
- that.setData({
- TaskInfo: {},
- TestType: options.testtype,
- TestName1: options.name1,
- TestName2: options.name2,
- UnitID:options.unitsid,
- });
- var wordStr="";
- if (options.bookid==100){
- wordStr="A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
- }
- else{
- var arrEnglist=wx.getStorageSync("EnglishAll");
- if (arrEnglist)
- arrEnglist=JSON.parse(arrEnglist);
- else
- arrEnglist=[];
-
- for(var i=0;i<arrEnglist.length;i++){
- if (arrEnglist[i].ID==Number(options.bookid)){
- for(var j=0;j<arrEnglist[i].Units.length;j++){
- if (arrEnglist[i].Units[j].ID==options.unitsid){
- wordStr=arrEnglist[i].Units[j].Words.join(",");
- break;
- }
- }
- }
- }
- }
-
- main.getData('GetTestEnglishWords?UserID='+app.globalData.userInfo.UserID+'&BookID='+options.bookid+'&LessonID='+options.unitsid+'&TestType='+options.testtype+'&Words=' + wordStr, function (data) {
- if (data) {
- TaskList = [];
- var words = data;
- for (var k = 0; k < words.length; k++) {
- var taskInfo = words[k];
- if (options.testtype=="read"){
- taskInfo.TagWidth=212;
- taskInfo.FontSize=108;
- if (taskInfo.Content[1].Content.length>=16)
- taskInfo.FontSize=48;
- else if (taskInfo.Content[1].Content.length>=12)
- taskInfo.FontSize=64;
- }
- else{
- taskInfo.TagWidth=186;
- taskInfo.FontSize=36;
- }
- 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 } } })
- }
- });
- }
- });
-
- },
- //进入下一张题卡
- 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){
- 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") {
- 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 = {};
- 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;
- if (this.data.UnitID)
- TestTask.Name+="#"+this.data.UnitID
- TestTask.List = TaskList;
- TestTask.BookID=this.data.BookID;
- wx.setStorageSync("TestTask", TestTask);
- wx.redirectTo({
- url: './report?type=new',
- });
- }
- 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,
- });
- });
- },
- onShareAppMessage: function () {
- return {
- title: app.globalData.ShareTitle,
- path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
- imageUrl: app.globalData.ShareImage,
- }
- },
- })
|