import common from '../../utils/util'; import main from '../../utils/main'; const app = getApp(); var intervalSound = 0, intervalRefresh = 0; var arrImage = []; var isCopying = false; var isPlaying = false; var isCollecting = false; var playList=[]; Page({ data: { IsExistCard: false, }, onLoad: function (options) { wx.hideShareMenu(); var that = this; isCopying = false; var id = 0; if (options.id) id = options.id; that.setData({ Containnerheight: main.getWindowHeight(), ShowType: options.type, MiaoguoCardID: id, Color: main.getDetailColor(app.globalData.ColorIndex), }); console.log("MiaoguoCardID:" + id); this.audioCtx = []; if (options.type == "show") { wx.setNavigationBarTitle({ title: "查看题卡" }); if (wx.setBackgroundColor) { wx.setBackgroundColor({ backgroundColor: that.data.Color.BackColor, backgroundColorTop: "#ffffff", backgroundColorBottom: that.data.Color.BackColor, }) } } else{ if (options.type == "share") { wx.setNavigationBarTitle({ title: "来自分享" }); } else if (options.type == "play") { wx.setNavigationBarTitle({ title: "幻灯片" }); if (that.data.ShowType == "play") { playList = []; for (var i = 0; i < app.globalData.CardList.length;i++){ playList.push(app.globalData.CardList[i].MiaoguoCardID); } playList = common.randomArray(playList); that.data.MiaoguoCardID = playList[0]; that.setData({ MiaoguoCardID: that.data.MiaoguoCardID, CurrentIndex:0, PlayListCount:playList.length, }); } } 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, }) } } }, onShow: function () { var that = this; if (that.data.ShowType) if (that.data.ShowType == "share") { var url = "GetMiaoguoCardInfo?UserID=" + app.globalData.introducer + "&MiaoguoCardID=" + that.data.MiaoguoCardID; main.getData(url, function (data) { if (data) { app.globalData.CardList = [data]; that.init(); } }); } else that.init(); }, onUnload: function () { clearInterval(intervalSound); }, onPullDownRefresh: function () { var that=this; if (that.data.ShowType == "play") { that.setData({ IsShowAnswer: [0,0], }); } wx.stopPullDownRefresh(); }, init: function () { var that = this; var list = app.globalData.CardList; var card = {}; var prevId = 0,nextId = 0; for (var i = 0; i < list.length; i++) { if (this.data.MiaoguoCardID == list[i].MiaoguoCardID) { if (i > 0) prevId = list[i - 1].MiaoguoCardID; else prevId = 0; if (i < list.length - 1) nextId = list[i + 1].MiaoguoCardID; else nextId = 0; card = main.changeStringToView(list[i].Content); card.MiaoguoCardID = list[i].MiaoguoCardID; card.LimitTime = list[i].LimitTime; card.FontSize = list[i].FontSize; card.IsCollect=list[i].IsCollect; break; } } if (!card.FontSize) card.FontSize = 46; arrImage = card.Images; //console.log("arrImage:" + arrImage); var limitTimeStr = common.formatDateCHS(card.LimitTime); var isTodayPractice = true; var task = app.globalData.TaskToday; if (task && task.ListNew && (task.ListNew.length > 0 || task.ListHistory.length > 0 || task.ListReview.length > 0)) { for (var i = 0; i < task.ListNew.length; i++) { if (task.ListNew[i].MiaoguoCardID == that.data.MiaoguoCardID) { isTodayPractice = false; break; } } for (var i = 0; i < task.ListHistory.length; i++) { if (task.ListHistory[i].MiaoguoCardID == that.data.MiaoguoCardID) { isTodayPractice = false; break; } } for (var i = 0; i < task.ListReview.length; i++) { if (task.ListReview[i].MiaoguoCardID == that.data.MiaoguoCardID) { isTodayPractice = false; break; } } } var TagWidth = 0; if (card.Tags && card.Tags.length > 0) { TagWidth = 92 + (card.Tags.length - 1) * 122; for (var j = 0; j < card.Tags.length; j++) { if (card.Tags[j].length > 2) { TagWidth += 46 * (card.Tags[j].length - 2); } } } var IsShowAnswer=[1,1]; if (that.data.ShowType == "play") { IsShowAnswer=[0, 0]; if (this.data.CurrentIndex <= 0) { prevId = 0; } else prevId = 1; if (this.data.CurrentIndex >=playList.length-1) { nextId = 0; } else nextId=1; } this.setData({ ID: card.MiaoguoCardID, Field: card.Field, Tags: card.Tags, TagWidth: TagWidth, PrevID: prevId, NextID: nextId, FontSize: card.FontSize, LimitTimeStr: limitTimeStr, IsTodayPractice: isTodayPractice, IsCollect: card.IsCollect, IsShowAnswer: IsShowAnswer, }); wx.pageScrollTo({ scrollTop: 0, }); }, practiceToday: function () { var that = this; wx.showModal({ title: '要今天练吗?', content: '这张题卡今天必须练习', success(res) { if (res.confirm) { var obj = { ID: that.data.ID, BtnNumber: -1, LearningType: 2, } that.saveCard(obj, function () { var time = common.formatTime(new Date()); that.setData({ LimitTimeStr: common.formatDateCHS(time), IsTodayPractice: false, }); var list = app.globalData.CardList; for (var i = 0; i < list.length; i++) { if (that.data.ID == list[i].MiaoguoCardID) { list[i].LimitTime = time; list[i].LimitTimeStr = "今天"; break; } } app.globalData.CardList = list; wx.showToast({ title: '改为今天练', icon: 'none', }); }); } else if (res.cancel) { } } }) }, shelveTask: function () { //搁置题卡,今天不练 var that = this; wx.showModal({ title: '今天不练吗?', content: '这张题卡今天不练习', success(res) { if (res.confirm) { var url = "UpdateMiaoguoCardToday?"; url += "ID=" + that.data.MiaoguoCardID; url += "&UserID=" + app.globalData.userInfo.UserID; url += "&IntervalTime=1d"; url += "&BtnNumber=1"; url += "&LearningType=3"; url += "&Duration=0"; url += "&IsUndo=0"; main.getData(url, function (data) { var time = common.formatTime(common.addDate("d", 1, new Date())); that.setData({ LimitTimeStr: common.formatDateCHS(time), IsTodayPractice: true, }); var list = app.globalData.CardList; for (var i = 0; i < list.length; i++) { if (that.data.ID == list[i].MiaoguoCardID) { list[i].LimitTime = time; list[i].LimitTimeStr = "明天"; break; } } app.globalData.CardList = list; wx.showToast({ title: '改为明天练', icon: 'none', }); }); } else if (res.cancel) { } } }) }, saveCard: function (obj, callback) { var url = "UpdateMiaoguoCardToday?"; url += "ID=" + obj.ID; url += "&UserID=" + app.globalData.userInfo.UserID; url += "&BtnNumber=" + obj.BtnNumber; url += "&LearningType=" + obj.LearningType; main.getData(url, function (data) { callback(); }); }, copyCard: function () { if (!isCopying) { isCopying = true; setTimeout(function () { isCopying = false; }, 15000); wx.showLoading({ title: '处理中', }); setTimeout(function () { wx.hideLoading(); }, 5000); var that = this; var url = "CollectMiaoguoCard?"; url += "MiaoguoCardID=" + that.data.MiaoguoCardID; url += "&UserIDSource=" + app.globalData.introducer; url += "&UserIDTarget=" + app.globalData.userInfo.UserID; main.getData(url, function (data) { wx.hideLoading(); var title = "题卡已存在!"; var image = "universalpic_wrong_white_120x120"; if (data == 1) { title = "已保存"; image = "universalpic_saved_white_120x120"; } wx.showToast({ title: title, mask: true, image: "../images/" + image + ".png", duration: 2000, success: function () { that.setData({ IsExistCard: true, }); } }); }); } }, playSound: function (e) { if (!isPlaying) { isPlaying = true; setTimeout(function () { isPlaying = false; }, 3000); var that = this; var str = e.currentTarget.dataset.content; var url; 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); } var arr = url.split(","); for (var i = 0; i < arr.length; i++) { this.audioCtx.push(wx.createAudioContext('myAudio' + i)); } var index = 0; if (arr.length > 1) { intervalSound = setInterval(function () { that.audioCtx[index].setSrc(arr[index]); that.audioCtx[index].play(); index++; if (index >= arr.length) { clearInterval(intervalSound); } }, 1000); } else { if (arr[index].indexOf("http") > 0) arr[index] = arr[index].substr(arr[index].indexOf("http")); arr[index] = arr[index].replace("http://", "https://"); that.audioCtx[index].setSrc(arr[index]); that.audioCtx[index].play(); } } }, getDetailInfo:function(){ wx.navigateTo({ url: './cardInfo?id=' + this.data.MiaoguoCardID, }); }, deleteCard: function () { var that = this; wx.showModal({ title: '提醒', content: '这张题卡要删除吗?', success(res) { if (res.confirm) { var url = 'DeleteMiaoguoCard?UserID=' + app.globalData.userInfo.UserID; url += "&ID=" + that.data.MiaoguoCardID; main.getData(url, function (data) { var list = app.globalData.CardList; for (var i = 0; i < list.length; i++) { if (that.data.MiaoguoCardID == list[i].MiaoguoCardID) { list.splice(i, 1); break; } } app.globalData.CardList = list; var taskToday = app.globalData.TaskToday; taskToday.CardNumber--; app.globalData.TaskToday = taskToday; wx.navigateBack({ delta: 1, }); }); } } }); }, previewNext: function (e) { var that = this; var id = e.currentTarget.dataset.id; that.setData({ MiaoguoCardID: id, }); that.init(); }, close: function () { wx.navigateBack({ delta: 1, }); }, editField: function (e) { if (this.data.ShowType == "show") { var that = this; // var fieldid = e.currentTarget.dataset.id; // wx.navigateTo({ // url: './add?type=edit3&id=' + that.data.MiaoguoCardID + '&fieldid=' + fieldid, // }); wx.navigateTo({ url: './add?type=edit&id=' + that.data.MiaoguoCardID, }); } }, gotoCollect: function () { var that = this; if (!isCollecting){ isCollecting = true; setTimeout(function () { isCollecting = false; }, 3000); if (!that.data.IsCollect) { that.data.IsCollect = 1; } else { that.data.IsCollect = 0; } that.setData({ IsCollect: that.data.IsCollect }); main.setCollect(that.data.MiaoguoCardID, that.data.IsCollect, function () { var list = app.globalData.CardList; for (var i = 0; i < list.length; i++) { if (that.data.MiaoguoCardID == list[i].MiaoguoCardID) { list[i].IsCollect = that.data.IsCollect; break; } } }); } }, onShowAnswer:function(){ this.setData({ IsShowAnswer:[1,1] }); }, onPlayAgain:function(){ playList.push(this.data.MiaoguoCardID); this.setData({ PlayListCount: playList.length, NextID:1, }); wx.showToast({ title: '稍后回放', mask:true, duration:500, }); }, playNext: function (e) { var that = this; var div = e.currentTarget.dataset.div; if (div==1){ this.data.CurrentIndex++; if (this.data.CurrentIndex>=playList.length){ this.data.CurrentIndex = playList.length-1; } } else { this.data.CurrentIndex--; if (this.data.CurrentIndex<0) this.data.CurrentIndex=0; } that.setData({ CurrentIndex: this.data.CurrentIndex, MiaoguoCardID: playList[this.data.CurrentIndex], PlayListCount: playList.length, }); that.init(); }, showImage: function (e) { var name = e.currentTarget.dataset.name; wx.previewImage({ current: name, urls: arrImage, }); }, onBindError: function (e) { var that = this; var serverUrl = e.currentTarget.dataset.serverurl; var oldTempUrl = e.currentTarget.dataset.name; //console.log("serverUrl1:" + serverUrl); wx.downloadFile({ url: serverUrl, // 仅为示例,并非真实的资源 success(res) { // 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调,业务需要自行判断是否下载到了想要的内容 if (res.statusCode === 200) { //console.log("serverUrl2:" + serverUrl); //console.log(res.tempFilePath); main.saveTempImage(serverUrl, res.tempFilePath); for (var i = 0; i < arrImage.length; i++) { if (arrImage[i] == oldTempUrl) { arrImage[i] = res.tempFilePath; } } } } }); clearTimeout(intervalRefresh); intervalRefresh = setTimeout(function () { that.init(); }, 1000); }, returnDefault: function () { wx.reLaunch({ url: './default?IsStart=1', }); }, onShareAppMessage: function () { var that = this; this.setData({ ShowType: 'sharepreview' }); setTimeout(function () { that.setData({ ShowType: 'show' }); }, 1000); return { title: '我的分享', path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID + '&Share=1&MiaoguoCardID=' + this.data.MiaoguoCardID, imageUrl: app.globalData.ShareImage, } }, })