import common from '../../utils/util'; import server from '../../utils/main'; const app = getApp(); var times = 1; var pageIndex = 0; var QuestionItem = []; var tempAnswerList=[]; var arrAnswerID=[]; var isAndroid = false; const waitTime = ["", "10秒钟", "15秒钟", "20秒钟", "25秒钟"]; Page({ data: { version: app.globalData.version, FileUrl: app.globalData.fileUrl, IsShowMenu: false, filter: 'horizontal', //filter: 'vertical', PageNumber: 1, PageCount: 60, IsFinished: false, arrPage: [], ShowLoading: false, IsDistabled: "", NameClass:"", }, onLoad: function (options) { var id = options.id; this.setData({ Containnerheight: server.getWindowHeight(), ID: id, }); var systemInfo = wx.getSystemInfoSync(); if (systemInfo.system && systemInfo.system.indexOf("Android") >= 0) { times = 2; } this.getDetail(id); isAndroid = false; if (app.globalData.systemInfo.system && app.globalData.systemInfo.system.indexOf("Android") >= 0) isAndroid = true; }, onUnload:function(){ wx.hideLoading(); }, getDetail: function (id) { var item; var data = wx.getStorageSync('QuestionTypeList'); for (var i = 0; i < data.length; i++) { for (var k = 0; k < data[i].List.length; k++) { if (id == data[i].List[k].ID) { item = data[i].List[k]; item.Example[0] = item.Example[0].replace("+", " + ").replace("-", " - ").replace("×", " × ").replace("÷", " ÷ "); item.Example[1] = item.Example[1].replace("+", " + ").replace("-", " - ").replace("×", " × ").replace("÷", " ÷ "); if (item.Name.length>12) item.NameClass="text2"; break; } } } this.setData({ QuestionTypeItem: item, }); //有竖式则缺省竖式 if (item.QuestionNumber3 > 0) { this.menuClick({ currentTarget: { dataset: { id: "vertical" } } }) } }, gotoAnswer: function (e) { wx.navigateTo({ url: '../main/answer', }); }, getFinish: function () { wx.hideLoading(); arrAnswerID = []; tempAnswerList=[]; this.getAnswerID(); this.getList(this.data.ID); }, getList: function (id) { var that = this; var pageNum = that.data.PageNumber; var QuestionTypeCategory = 3; if (that.data.filter == "horizontal") QuestionTypeCategory = 2; pageIndex = 0; server.getData('GetQuestionTypesPrint?ID=' + id + '&QuestionTypeCategory=' + QuestionTypeCategory + '&PageNum=' + pageNum, function (data) { //console.log("UserInfo:" + data); if (data) { that.setData({ ShowLoading: true, IsFinished: true, arrPage: [], IsDistabled: "", WaitTime: waitTime[that.data.PageNumber], }); QuestionItem = data; if (isAndroid) { var ctx = wx.createCanvasContext("Canvas", that); ctx.fillRect(0, 0, 1050, 1485); ctx.draw(true, function (n) { wx.canvasToTempFilePath({ x: 0, y: 0, width: 2100, height: 2970, canvasId: 'Canvas', success: function (res2) { that.saveImage(); } }) }); } else { that.saveImage(); } } }); }, getAnswerID: function () { var list = wx.getStorageSync("AnswerList"); var id=1; if (list && list.length > 0) { list.sort(function (a, b) { return b.AnswerID - a.AnswerID; }); id = list[0].AnswerID + 1; } for (var i = 0; i < this.data.PageNumber;i++) arrAnswerID.push(id+i); }, saveImage: function () { var that = this; var item = QuestionItem; item.AnswerID = arrAnswerID.shift(); var ctx = wx.createCanvasContext("Canvas", that); ctx.setFillStyle('white'); ctx.fillRect(0, 0, 2100, 2970); ctx.fillStyle = "#000000"; //计算数组最大长度 var maxLength = 0; for (var l = 0; l < item.QuestionList.length; l++) { var num1 = item.QuestionList[l]; if (num1.Vertical) { for (var n = 0; n < num1.Vertical.length; n++) { if (maxLength < num1.Vertical[n].Array.length) maxLength = num1.Vertical[n].Array.length; } } } var xStart = 60; var yStart = 247; var height = 150; if (item.QuestionTypeCategory == 2) height = 59; if (item.PageCount == 18) height = 210; else if (item.PageCount == 15) height = 250; //括号 var index = 0; ctx.setFontSize(12); for (var i = 0; i < 3; i++) { for (var j = 0; j < item.PageCount / 3; j++) { index++; ctx.fillText("(", xStart + i * 310, yStart + j * height); ctx.fillText(")", xStart + i * 310 + 22, yStart + j * height); } } //编号 index = 0; ctx.setTextAlign('center'); var numberLeft = 12; if (isAndroid) numberLeft = 14; for (var i = 0; i < 3; i++) { for (var j = 0; j < item.PageCount / 3; j++) { index++; ctx.fillText(index, xStart + i * 310 + numberLeft, yStart + j * height); } } //算式 index = that.data.arrPage.length * item.PageCount; ctx.setFontSize(24); for (var i = 0; i < 3; i++) { for (var j = 0; j < item.PageCount / 3; j++) { index++; //横式 if (item.QuestionTypeCategory == 2) { var A = item.QuestionList[index - 1].A.toString(); if (item.QuestionList[index - 1].HiddenColumn == "A") A = "____"; var OperateAB = item.QuestionList[index - 1].OperateAB.toString(); //console.log(OperateAB); var B = item.QuestionList[index - 1].B.toString(); if (item.QuestionList[index - 1].HiddenColumn == "B") B = "____"; var equation = A + " " + OperateAB + " " + B; if (item.QuestionList[index - 1].C) { var OperateBC = item.QuestionList[index - 1].OperateBC.toString(); var C = item.QuestionList[index - 1].C.toString(); equation += " " + OperateBC + " " + C; if (item.QuestionList[index - 1].D) { var OperateCD = item.QuestionList[index - 1].OperateCD.toString(); var D = item.QuestionList[index - 1].D.toString(); equation += " " + OperateCD + " " + D; } } equation += " = "; var R = item.QuestionList[index - 1].R.toString(); if (item.QuestionList[index - 1].HiddenColumn == "R") R = ""; equation += R; var num = xStart + i * 310 + 31; for (var n = 0; n < equation.length; n++) { var len = getLength(equation[n]); num += len; if (equation[n] == ".") { ctx.fillText(equation[n], num + 3, yStart + j * 59); } else { var h = 0; if (equation[n] == "_") h = 8; ctx.fillText(equation[n], num, yStart + j * 59 + h); } } } //竖式 else if (item.QuestionTypeCategory == 3) { var num1 = item.QuestionList[index - 1]; var isAnswer = false; var width = 250; if (num1.OperateAB == "÷") width = 240; //console.log(num1); for (var n = 0; n < num1.Vertical.length; n++) { if (maxLength > num1.Vertical[n].Array.length) { if (num1.OperateAB != "÷"){ var str=""; if (num1.Vertical[n].Type=="line") str="1" num1.Vertical[n].Array.splice(1, 0, str); } } } console.log(num1); for (var n = 0; n < num1.Vertical.length; n++) { var num = xStart + i * 310 + width; for (var m = num1.Vertical[n].Array.length - 1; m >= 0; m--) { var str = ""; if (num1.Vertical[n].Type == "line") { var lineLength = num1.Vertical[n].Array.length * 30 + 20; if (num1.OperateAB == "÷") { lineLength = num1.A.toString().length * 30; } ctx.moveTo(num, yStart + j * height + n * 30); ctx.lineTo(num - lineLength, yStart + j * height + n * 30); ctx.stroke(); if (num1.OperateAB != "÷") isAnswer = true; break; } else { str = num1.Vertical[n].Array[m]; if (num1.OperateAB == "÷" && str == "√") { isAnswer = true; ctx.drawImage("../../images/formula_division_size02.png", num - (num1.Vertical[n].Array.length - m) * 30 - 20, yStart + j * height + n * 30 - 31, 56, 76); //break; } else if (num1.OperateAB == "÷" && n == 0) { } else if (num1.OperateAB == "÷" && n == 1) { ctx.fillText(str, num - (num1.Vertical[n].Array.length - m) * 10, yStart + j * height + n * 30); } else { ctx.fillText(str, num - (num1.Vertical[n].Array.length - m) * 30, yStart + j * height + n * 30); } } } if (isAnswer) break; } } } } ctx.setTextAlign('left'); ctx.setTextBaseline('top'); ctx.setFontSize(32); ctx.fillText(item.Name, 60, 45); ctx.setFontSize(12); ctx.fillText(item.CagegoryName, 60, 90); ctx.fillText("共" + item.QuestionList.length + "道题", 60, 125); ctx.fillText("第 1 / 1 页", 60, 147); ctx.fillText("答案检索号", 768, 60); ctx.setTextAlign('right'); ctx.fillText("出题时间 " + item.CreateTime, 875, 94); ctx.fillText("微信扫一扫 计时看答案", 875, 111); ctx.fillText("《数学计算题》选题 打印 扫码 批改", 875, 137); ctx.setFontSize(32); ctx.fillText(item.AnswerID, 872, 50); ctx.drawImage("../../images/answer.png", 890, 54, 100, 100); ctx.draw(true, function (n) { var w = 1050, h = 1485; wx.canvasToTempFilePath({ x: 0, y: 0, width: w * times, height: h * times, //destWidth: w*times, //destHeight: h*times, canvasId: 'Canvas', success: function (res2) { console.log(res2.tempFilePath); that.data.arrPage.push(res2.tempFilePath); item.TempImagePath=res2.tempFilePath; tempAnswerList.push(JSON.parse(JSON.stringify(item))); that.setData({ arrPage: that.data.arrPage, }); if (that.data.arrPage.length < that.data.PageNumber) { pageIndex++; that.saveImage(); } else { //生成图片完成 that.setData({ ShowLoading: false, }); wx.pageScrollTo({ scrollTop: 710, duration: 300 }); // wx.previewImage({ // current: that.data.arrPage[0], // 当前显示图片的http链接 // urls: that.data.arrPage // 需要预览的图片http链接列表 // }); //存列表 var tempList = wx.getStorageSync("AnswerList"); if (!tempList) tempList=[]; for(var x=0;x= id && y < id + tempItem.PageCount){ arr.push(tempItem.QuestionList[y]); } } tempItem.QuestionList=arr; tempList.unshift(tempItem); } wx.setStorageSync("AnswerList", tempList); } } }) }); function getLength(str) { switch (str) { case "0": case "1": case "2": case "3": case "4": case "5": case "6": case "7": case "8": case "9": return 15; case ".": return 8; default: return 12; } } }, //保存图片文件 saveImageFile: function () { if (this.data.IsDistabled == "") { wx.showLoading({ title: "保存中..." }); var that = this; var count = 0; for (var i = 0; i < that.data.arrPage.length; i++) { wx.saveImageToPhotosAlbum({ filePath: that.data.arrPage[i], success(res3) { console.log("save:" + res3.savedFilePath); count++ if (count == that.data.arrPage.length) { wx.hideLoading(); wx.showModal({ title: "保存完成", showCancel: false, content: "请打开手机相册查看图片!", }); that.setData({ IsDistabled: "Distabled", }); wx.pageScrollTo({ scrollTop: 1010, duration: 300 }); } }, fail(err3) { console.log(err3); }, }) } } }, changePageNumber: function (e) { var id = Number(e.currentTarget.dataset.id); this.data.PageNumber += id; if (this.data.PageNumber < 1) this.data.PageNumber = 1; else if (this.data.PageNumber > 4) this.data.PageNumber = 4; this.setData({ PageNumber: this.data.PageNumber, }); }, showImage: function (e) { var that = this; var id = e.currentTarget.dataset.id; wx.previewImage({ current: that.data.arrPage[id], // 当前显示图片的http链接 urls: that.data.arrPage // 需要预览的图片http链接列表 }); }, closeLoading: function () { var that = this; that.setData({ ShowLoading: false, IsFinished: false, }); }, menuClick: function (e) { this.data.filter = e.currentTarget.dataset.id; if (this.data.filter == "horizontal") { this.data.PageCount = this.data.QuestionTypeItem.QuestionNumber2; } else if (this.data.filter == "vertical") { this.data.PageCount = this.data.QuestionTypeItem.QuestionNumber3; } this.setData({ filter: this.data.filter, IsShowMenu: false, PageCount: this.data.PageCount, }); }, closeMenu: function () { this.setData({ IsShowMenu: false, }); }, openMenu: function () { this.setData({ IsShowMenu: true, }); }, onShareAppMessage: function () { return { title: '', path: 'pages/index/index', success: function (res) { }, fail: function (err) { console.log(err); }, complete: function (res) { console.log(res); }, } }, })