import common from '../../utils/util'; import main from '../../utils/main'; const app = getApp(); var iTimeout = 0, intervalSound = 0, intervalRefresh = 0; var arrImage = []; var isCopying = false; var isPlaying = false; 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, }); console.log("MiaoguoCardID:" + id); this.audioCtx = []; if (options.type == "share") { wx.setNavigationBarTitle({ title: "来自分享" }); } else if (options.type == "show") { wx.setNavigationBarTitle({ title: "查看题卡" }); } }, 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 () { clearTimeout(iTimeout); clearInterval(intervalSound); }, init: function () { var that = this; var list = app.globalData.CardList; var card = {}; var prevId = 0; var 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; 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; } } } this.setData({ ID: card.MiaoguoCardID, Field: card.Field, Tags: card.Tags, PrevID: prevId, NextID: nextId, FontSize: card.FontSize, LimitTimeStr: limitTimeStr, IsTodayPractice: isTodayPractice, }); }, 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: '处理中', }); 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; intervalSound = setInterval(function () { that.audioCtx[index].setSrc(arr[index]); that.audioCtx[index].play(); index++; if (index >= arr.length) { clearInterval(intervalSound); } }, 1000); var fieldid = e.currentTarget.dataset.fieldid; var content = that.data.Field[fieldid]; var selectIndex = 0; for (var i = 0; i < content.length; i++) { if (content[i].Type == 'sound') { if (content[i].Content == e.currentTarget.dataset.content) { content[i].SoundImageName = "gif"; selectIndex = i; break; } } } that.setData({ Field: that.data.Field, }); iTimeout = setTimeout(function () { content[selectIndex].SoundImageName = "png"; that.data.Field[fieldid] = content; that.setData({ Field: that.data.Field, }); }, 2000); } }, onMore: function () { var that = this; wx.showActionSheet({ itemList: ["详细信息", "编辑题卡", "删除题卡"], success(res) { //console.log(res.tapIndex); if (res.tapIndex == 0) { wx.navigateTo({ url: './cardInfo?id=' + that.data.MiaoguoCardID, }); } else if (res.tapIndex == 1) { wx.navigateTo({ url: './add?type=edit2&type2=1&id=' + that.data.MiaoguoCardID, }); } else if (res.tapIndex == 2) { that.deleteCard(); } }, }) }, 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, }); }); } } }); }, onPreview: function (e) { clearTimeout(iTimeout); var id = e.currentTarget.dataset.id; wx.redirectTo({ url: './preview?type=show&id=' + id, }); }, 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, }); } }, 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, } }, })