detail.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. import common from '../../utils/util';
  2. import server from '../../utils/main';
  3. const app = getApp();
  4. var times = 1;
  5. var pageIndex = 0;
  6. var QuestionItem = [];
  7. var tempAnswerList = [];
  8. var arrAnswerID = [];
  9. var isAndroid = false;
  10. const waitTime = ["", "10秒钟", "15秒钟", "20秒钟", "25秒钟"];
  11. Page({
  12. data: {
  13. version: app.globalData.version,
  14. IsShowMenu: false,
  15. filter: 'horizontal',
  16. //filter: 'vertical',
  17. PageNumber: 1,
  18. PageCount: 60,
  19. IsFinished: false,
  20. arrPage: [],
  21. ShowLoading: false,
  22. IsDistabled: "",
  23. NameClass: "",
  24. AddDisabled: "",
  25. SubDisabled: "btnAddSubDisabled",
  26. HelpHidden7: true,
  27. HelpHidden3: true,
  28. HelpHidden8: true,
  29. ShowAndroidNotice: false,
  30. },
  31. onLoad: function (options) {
  32. var that = this;
  33. var id = options.id;
  34. isAndroid = false;
  35. if (app.globalData.systemInfo.system && app.globalData.systemInfo.system.indexOf("Android") >= 0)
  36. isAndroid = true;
  37. this.setData({
  38. Containnerheight: server.getWindowHeight(),
  39. ID: id,
  40. ShowAndroidNotice: isAndroid,
  41. });
  42. var systemInfo = wx.getSystemInfoSync();
  43. if (systemInfo.system && systemInfo.system.indexOf("Android") >= 0) {
  44. times = 2;
  45. }
  46. this.getDetail(id);
  47. if (id > 0) {
  48. common.getStorageValue(this, "HelpHidden3", false, function () {
  49. that.showHelp();
  50. });
  51. }
  52. else {
  53. common.getStorageValue(this, "HelpHidden8", false, function () {
  54. that.showHelp8();
  55. });
  56. }
  57. },
  58. onShow:function(){
  59. if (this.data.ID==-1)
  60. this.getDetail(this.data.ID);
  61. },
  62. onUnload: function () {
  63. wx.hideLoading();
  64. },
  65. getDetail: function (id) {
  66. var item = {};
  67. if (id > 0) {
  68. var data = wx.getStorageSync('QuestionTypeList');
  69. for (var i = 0; i < data.length; i++) {
  70. for (var k = 0; k < data[i].List.length; k++) {
  71. if (id == data[i].List[k].ID) {
  72. item = data[i].List[k];
  73. item.Example[0] = item.Example[0].replace("+", " + ").replace("-", " - ").replace("×", " × ").replace("÷", " ÷ ");
  74. item.Example[1] = item.Example[1].replace("+", " + ").replace("-", " - ").replace("×", " × ").replace("÷", " ÷ ");
  75. item.IconName = app.globalData.fileUrl + item.IconName;
  76. if (item.Name.length > 12)
  77. item.NameClass = "text2";
  78. break;
  79. }
  80. }
  81. }
  82. }
  83. else {
  84. item.Name = "我的错题集";
  85. var list = wx.getStorageSync('ErrorList');
  86. item.ErrorList = list;
  87. }
  88. this.setData({
  89. QuestionTypeItem: item,
  90. });
  91. //有竖式则缺省竖式
  92. if (item.QuestionNumber3 > 0) {
  93. this.menuClick({ currentTarget: { dataset: { id: "vertical" } } })
  94. }
  95. },
  96. gotoError: function (e) {
  97. wx.navigateTo({
  98. url: '../main/errorlist',
  99. });
  100. },
  101. gotoAnswer: function (e) {
  102. wx.navigateTo({
  103. url: '../main/answer',
  104. });
  105. this.closeHelp2();
  106. },
  107. getFinish: function () {
  108. server.playSound("/sounds/click.mp3");
  109. this.setData({
  110. ShowLoading: true,
  111. BtnText: "全部保存到相册",
  112. });
  113. wx.hideLoading();
  114. arrAnswerID = [];
  115. tempAnswerList = [];
  116. this.getAnswerID();
  117. if (this.data.ID > 0) {
  118. this.getList(this.data.ID);
  119. }
  120. else{
  121. this.getErrorList();
  122. }
  123. this.closeHelp();
  124. },
  125. //重新生成试卷
  126. ResetFinish: function () {
  127. var that = this;
  128. wx.showLoading({
  129. title: "正在重置",
  130. complete: function () {
  131. var list = that.data.arrPage;
  132. for (var i = 0; i < list.length; i++) {
  133. wx.removeSavedFile({
  134. filePath: list[i].TempImagePath
  135. });
  136. }
  137. that.setData({
  138. arrPage: [],
  139. PageNumber: 1,
  140. });
  141. var tempList = wx.getStorageSync("AnswerList");
  142. for (var i = 0; i < list.length; i++) {
  143. tempList.shift();
  144. }
  145. wx.setStorageSync("AnswerList", tempList);
  146. wx.hideLoading();
  147. that.getFinish();
  148. }
  149. });
  150. },
  151. getList: function (id) {
  152. var that = this;
  153. var pageNum = that.data.PageNumber;
  154. var QuestionTypeCategory = 3;
  155. if (that.data.filter == "horizontal")
  156. QuestionTypeCategory = 2;
  157. pageIndex = 0;
  158. server.getData('GetQuestionTypesPrint?ID=' + id + '&QuestionTypeCategory=' + QuestionTypeCategory + '&PageNum=' + pageNum, function (data) {
  159. //console.log("UserInfo:" + data);
  160. if (data) {
  161. that.setData({
  162. IsFinished: true,
  163. arrPage: [],
  164. IsDistabled: "",
  165. WaitTime: waitTime[that.data.PageNumber],
  166. });
  167. QuestionItem = data;
  168. QuestionItem.IconName = that.data.QuestionTypeItem.IconName;
  169. if (isAndroid) {
  170. console.log("android");
  171. var ctx = wx.createCanvasContext("Canvas", that);
  172. ctx.fillRect(0, 0, 1050, 1485);
  173. ctx.draw(true, function (n) {
  174. wx.canvasToTempFilePath({
  175. x: 0,
  176. y: 0,
  177. width: 2100,
  178. height: 2970,
  179. canvasId: 'Canvas',
  180. success: function (res2) {
  181. that.saveImage();
  182. }
  183. })
  184. });
  185. }
  186. else {
  187. that.saveImage();
  188. }
  189. }
  190. });
  191. },
  192. getErrorList: function () {
  193. var that = this;
  194. QuestionItem = that.data.QuestionTypeItem;
  195. var pageNum = QuestionItem.ErrorList.length > 60 ? 60 : QuestionItem.ErrorList.length;
  196. var QuestionTypeCategory = 2;
  197. pageIndex = 0;
  198. that.setData({
  199. IsFinished: true,
  200. arrPage: [],
  201. IsDistabled: "",
  202. WaitTime: waitTime[that.data.PageNumber],
  203. });
  204. if (isAndroid) {
  205. console.log("android");
  206. var ctx = wx.createCanvasContext("Canvas", that);
  207. ctx.fillRect(0, 0, 1050, 1485);
  208. ctx.draw(true, function (n) {
  209. wx.canvasToTempFilePath({
  210. x: 0,
  211. y: 0,
  212. width: 2100,
  213. height: 2970,
  214. canvasId: 'Canvas',
  215. success: function (res2) {
  216. that.saveImage();
  217. }
  218. })
  219. });
  220. }
  221. else {
  222. that.saveImage();
  223. }
  224. },
  225. getAnswerID: function () {
  226. var list = wx.getStorageSync("AnswerList");
  227. var id = 1;
  228. if (list && list.length > 0) {
  229. list.sort(function (a, b) {
  230. return b.AnswerID - a.AnswerID;
  231. });
  232. id = list[0].AnswerID + 1;
  233. }
  234. for (var i = 0; i < this.data.PageNumber; i++)
  235. arrAnswerID.push(id + i);
  236. },
  237. saveImage: function () {
  238. var that = this;
  239. var item = QuestionItem;
  240. item.AnswerID = arrAnswerID.shift();
  241. var ctx = wx.createCanvasContext("Canvas", that);
  242. ctx.setTextAlign('left');
  243. ctx.setTextBaseline('top');
  244. ctx.setFontSize(12);
  245. ctx.setFillStyle('white');
  246. ctx.fillRect(0, 0, 2100, 2970);
  247. ctx.fillStyle = "#000000";
  248. var xStart = 60;
  249. var yStart = 247;
  250. var height = 150;
  251. if (item.QuestionTypeCategory == 2)
  252. height = 59;
  253. if (item.PageCount == 18)
  254. height = 210;
  255. else if (item.PageCount == 15)
  256. height = 250;
  257. //括号
  258. var index = 0;
  259. for (var i = 0; i < 3; i++) {
  260. for (var j = 0; j < item.PageCount / 3; j++) {
  261. index++;
  262. ctx.fillText("(", xStart + i * 310, yStart + j * height);
  263. ctx.fillText(")", xStart + i * 310 + 22, yStart + j * height);
  264. }
  265. }
  266. //编号
  267. index = 0;
  268. ctx.setTextAlign('center');
  269. var numberLeft = 12;
  270. if (isAndroid)
  271. numberLeft = 14;
  272. for (var i = 0; i < 3; i++) {
  273. for (var j = 0; j < item.PageCount / 3; j++) {
  274. index++;
  275. ctx.fillText(index, xStart + i * 310 + numberLeft, yStart + j * height);
  276. }
  277. }
  278. //算式
  279. index = that.data.arrPage.length * item.PageCount;
  280. ctx.setFontSize(24);
  281. for (var i = 0; i < 3; i++) {
  282. for (var j = 0; j < item.PageCount / 3; j++) {
  283. index++;
  284. //横式
  285. if (item.QuestionTypeCategory == 2) {
  286. var A = item.QuestionList[index - 1].A.toString();
  287. if (item.QuestionList[index - 1].HiddenColumn == "A")
  288. A = "____";
  289. var OperateAB = item.QuestionList[index - 1].OperateAB.toString();
  290. //console.log(OperateAB);
  291. var B = item.QuestionList[index - 1].B.toString();
  292. if (item.QuestionList[index - 1].HiddenColumn == "B")
  293. B = "____";
  294. var equation = A + " " + OperateAB + " " + B;
  295. if (item.QuestionList[index - 1].C) {
  296. var OperateBC = item.QuestionList[index - 1].OperateBC.toString();
  297. var C = item.QuestionList[index - 1].C.toString();
  298. equation += " " + OperateBC + " " + C;
  299. if (item.QuestionList[index - 1].D) {
  300. var OperateCD = item.QuestionList[index - 1].OperateCD.toString();
  301. var D = item.QuestionList[index - 1].D.toString();
  302. equation += " " + OperateCD + " " + D;
  303. }
  304. }
  305. equation += " = ";
  306. var R = item.QuestionList[index - 1].R.toString();
  307. if (item.QuestionList[index - 1].HiddenColumn == "R")
  308. R = "";
  309. equation += R;
  310. var num = xStart + i * 310 + 31;
  311. for (var n = 0; n < equation.length; n++) {
  312. var len = getLength(equation[n]);
  313. num += len;
  314. if (equation[n] == ".") {
  315. ctx.fillText(equation[n], num + 3, yStart + j * 59 - 5);
  316. } else {
  317. var h = -8;
  318. if (equation[n] == "_")
  319. h = 0;
  320. ctx.fillText(equation[n], num, yStart + j * 59 + h);
  321. }
  322. }
  323. }
  324. //竖式
  325. else if (item.QuestionTypeCategory == 3) {
  326. var num1 = item.QuestionList[index - 1];
  327. var isAnswer = false;
  328. var width = 250;
  329. if (num1.OperateAB == "÷")
  330. width = 240;
  331. for (var n = 0; n < num1.Vertical.length; n++) {
  332. var num = xStart + i * 310 + width;
  333. var num2 = 0;
  334. for (var m = num1.Vertical[n].Array.length - 1; m >= 0; m--) {
  335. var str = "";
  336. if (num1.Vertical[n].Type == "line") {
  337. if (num1.OperateAB != "÷") {
  338. //var lineLength = num1.Vertical[n].Array.length * 30 + 20;
  339. var lineLength = 6 * 30 + 20;
  340. var lineTop = 4;
  341. //console.log(yStart + j * height + n * 30 + lineTop);
  342. ctx.moveTo(num, yStart + j * height + n * 30 + lineTop);
  343. ctx.lineTo(num - lineLength, yStart + j * height + n * 30 + lineTop);
  344. ctx.stroke();
  345. isAnswer = true;
  346. break;
  347. }
  348. }
  349. else {
  350. str = num1.Vertical[n].Array[m];
  351. if (num1.OperateAB == "÷" && str == "√") {
  352. isAnswer = true;
  353. var lineLength = num1.A.toString().length;
  354. var lineWidth = Math.floor(57 * (1 + num1.A.toString().length) / 2);
  355. ctx.drawImage("../../images/formula_division_size0" + lineLength + ".png", num - (num1.Vertical[n].Array.length - m) * 30 - 8, yStart + j * height + n * 30 - 10, lineWidth, 39);
  356. //break;
  357. }
  358. else if (num1.OperateAB == "÷" && n == 0) {
  359. }
  360. else if (num1.OperateAB == "÷" && n == 1) {
  361. ctx.fillText(str, num - (num1.Vertical[n].Array.length - m) * 10, yStart + j * height + n * 30);
  362. }
  363. else {
  364. if (str == ".") {
  365. ctx.fillText(str, num - (num1.Vertical[n].Array.length - m) * 30 + 15, yStart + j * height + n * 30);
  366. num2 = 30;
  367. }
  368. else if (str == "+" || str == "-" || str == "−" || str == "×" || str == "÷") {
  369. var lineLength = 6 * 30;
  370. ctx.fillText(str, num - lineLength, yStart + j * height + n * 30);
  371. num2 = 30;
  372. }
  373. else
  374. ctx.fillText(str, num - (num1.Vertical[n].Array.length - m) * 30 + num2, yStart + j * height + n * 30);
  375. }
  376. }
  377. }
  378. if (isAnswer)
  379. break;
  380. }
  381. }
  382. }
  383. }
  384. ctx.setTextAlign('left');
  385. ctx.setFontSize(32);
  386. ctx.fillText(item.Name, 60, 45);
  387. ctx.setFontSize(12);
  388. ctx.fillText(item.CagegoryName, 60, 90);
  389. ctx.fillText("共" + item.PageCount + "道题", 60, 125);
  390. ctx.fillText("第 1 / 1 页", 60, 147);
  391. ctx.fillText("答案检索号", 768, 60);
  392. ctx.setTextAlign('right');
  393. ctx.fillText("出题时间 " + item.CreateTime, 875, 94);
  394. ctx.fillText("微信扫一扫 计时看答案", 875, 111);
  395. ctx.fillText("《数学计算题》选题 打印 扫码 批改", 875, 137);
  396. ctx.setFontSize(32);
  397. ctx.fillText(item.AnswerID, 872, 50);
  398. ctx.drawImage("../../images/answer.png", 890, 54, 100, 100);
  399. ctx.draw(true, function (n) {
  400. var w = 1050,
  401. h = 1485;
  402. wx.canvasToTempFilePath({
  403. x: 0,
  404. y: 0,
  405. width: w * times,
  406. height: h * times,
  407. destWidth: w * times,
  408. destHeight: h * times,
  409. canvasId: 'Canvas',
  410. success: function (res2) {
  411. console.log(res2.tempFilePath);
  412. that.data.arrPage.push(res2.tempFilePath);
  413. item.TempImagePath = res2.tempFilePath;
  414. tempAnswerList.push(JSON.parse(JSON.stringify(item)));
  415. that.setData({
  416. arrPage: that.data.arrPage,
  417. });
  418. if (that.data.arrPage.length < that.data.PageNumber) {
  419. pageIndex++;
  420. that.saveImage();
  421. }
  422. else {
  423. //生成图片完成
  424. setTimeout(function () {
  425. that.setData({
  426. ShowLoading: false,
  427. });
  428. wx.pageScrollTo({
  429. scrollTop: 0,
  430. duration: 300
  431. });
  432. // wx.previewImage({
  433. // current: that.data.arrPage[0], // 当前显示图片的http链接
  434. // urls: that.data.arrPage // 需要预览的图片http链接列表
  435. // });
  436. //存列表
  437. var tempList = wx.getStorageSync("AnswerList");
  438. if (!tempList)
  439. tempList = [];
  440. for (var x = 0; x < tempList.length; x++) {
  441. tempList[x].IsNew = 0;
  442. }
  443. for (var x = 0; x < tempAnswerList.length; x++) {
  444. var tempItem = tempAnswerList[x];
  445. var id = x * tempItem.PageCount;
  446. var arr = [];
  447. for (var y = 0; y < tempItem.QuestionList.length; y++) {
  448. if (y >= id && y < id + tempItem.PageCount) {
  449. arr.push(tempItem.QuestionList[y]);
  450. }
  451. }
  452. tempItem.QuestionList = arr;
  453. tempItem.IsNew = 1;
  454. tempList.unshift(tempItem);
  455. }
  456. wx.setStorageSync("AnswerList", tempList);
  457. common.getStorageValue(that, "HelpHidden7", false, function () {
  458. that.showHelp2();
  459. });
  460. }, 3000);
  461. }
  462. }
  463. })
  464. });
  465. function getLength(str) {
  466. switch (str) {
  467. case "0":
  468. case "1":
  469. case "2":
  470. case "3":
  471. case "4":
  472. case "5":
  473. case "6":
  474. case "7":
  475. case "8":
  476. case "9":
  477. return 15;
  478. case ".":
  479. return 8;
  480. default:
  481. return 12;
  482. }
  483. }
  484. },
  485. //保存图片文件
  486. saveImageFile: function () {
  487. if (this.data.IsDistabled == "") {
  488. wx.showLoading({ title: "保存中..." });
  489. var that = this;
  490. var count = 0;
  491. for (var i = 0; i < that.data.arrPage.length; i++) {
  492. wx.saveImageToPhotosAlbum({
  493. filePath: that.data.arrPage[i],
  494. success(res3) {
  495. console.log("save:" + res3.savedFilePath);
  496. count++
  497. if (count == that.data.arrPage.length) {
  498. wx.hideLoading();
  499. wx.showModal({
  500. title: "保存完成",
  501. showCancel: false,
  502. content: "请打开手机相册查看图片!",
  503. });
  504. that.setData({
  505. IsDistabled: "Distabled",
  506. BtnText: "已保存",
  507. });
  508. wx.pageScrollTo({
  509. scrollTop: 400,
  510. duration: 300
  511. });
  512. }
  513. },
  514. fail(err3) {
  515. console.log(err3);
  516. wx.openSetting();
  517. },
  518. complete() {
  519. wx.hideLoading();
  520. }
  521. })
  522. }
  523. }
  524. },
  525. changePageNumber: function (e) {
  526. var id = Number(e.currentTarget.dataset.id);
  527. this.data.PageNumber += id;
  528. var addDisabled = "", subDisabled = "";
  529. if (this.data.PageNumber <= 1) {
  530. this.data.PageNumber = 1;
  531. subDisabled = "btnAddSubDisabled";
  532. }
  533. else if (this.data.PageNumber >= 4) {
  534. this.data.PageNumber = 4;
  535. addDisabled = "btnAddSubDisabled";
  536. }
  537. this.setData({
  538. PageNumber: this.data.PageNumber,
  539. AddDisabled: addDisabled,
  540. SubDisabled: subDisabled,
  541. });
  542. },
  543. showImage: function (e) {
  544. var that = this;
  545. var id = e.currentTarget.dataset.id;
  546. wx.previewImage({
  547. current: that.data.arrPage[id], // 当前显示图片的http链接
  548. urls: that.data.arrPage // 需要预览的图片http链接列表
  549. });
  550. },
  551. closeLoading: function () {
  552. var that = this;
  553. that.setData({
  554. ShowLoading: false,
  555. IsFinished: false,
  556. });
  557. },
  558. menuClick: function (e) {
  559. this.data.filter = e.currentTarget.dataset.id;
  560. if (this.data.filter == "horizontal") {
  561. this.data.PageCount = this.data.QuestionTypeItem.QuestionNumber2;
  562. } else if (this.data.filter == "vertical") {
  563. this.data.PageCount = this.data.QuestionTypeItem.QuestionNumber3;
  564. }
  565. this.setData({
  566. filter: this.data.filter,
  567. IsShowMenu: false,
  568. PageCount: this.data.PageCount,
  569. });
  570. },
  571. closeMenu: function () {
  572. this.setData({
  573. IsShowMenu: false,
  574. });
  575. },
  576. openMenu: function () {
  577. this.setData({
  578. IsShowMenu: true,
  579. });
  580. },
  581. showHelp: function () {
  582. if (!this.data.HelpHidden3) {
  583. this.audioCtx = wx.createAudioContext('myAudio');
  584. var str = "嘿嘿,找到我了吗?我在车间的窗户里制作例题呢。如果这是你要的题型,去点“出题”按钮就对了。还要告诉你,一张卷子就是一张图片,题目数量请看提示。";
  585. server.playAudio(this.audioCtx, str);
  586. }
  587. },
  588. closeHelp: function () {
  589. this.setData({
  590. HelpHidden3: true,
  591. });
  592. wx.setStorageSync("HelpHidden3", true);
  593. this.audioCtx = wx.createAudioContext('myAudio');
  594. this.audioCtx.pause();
  595. },
  596. showHelp2: function () {
  597. if (!this.data.HelpHidden7) {
  598. this.audioCtx = wx.createAudioContext('myAudio');
  599. var str = "卷子出炉咯,棒棒哒!快去手机相册打印吧。我会等在首页上的答案资料室为你继续带路的。";
  600. server.playAudio(this.audioCtx, str);
  601. }
  602. },
  603. closeHelp2: function () {
  604. this.setData({
  605. HelpHidden7: true,
  606. });
  607. wx.setStorageSync("HelpHidden7", true);
  608. this.audioCtx = wx.createAudioContext('myAudio');
  609. this.audioCtx.pause();
  610. },
  611. showHelp8: function () {
  612. if (!this.data.HelpHidden8) {
  613. this.audioCtx = wx.createAudioContext('myAudio');
  614. var str = "发现错误是提高成绩的第一步。点一下题目数量右侧的图标,当有错题后,能查看和整理错题清单。";
  615. server.playAudio(this.audioCtx, str);
  616. }
  617. },
  618. closeHelp8: function () {
  619. this.setData({
  620. HelpHidden8: true,
  621. });
  622. wx.setStorageSync("HelpHidden8", true);
  623. this.audioCtx = wx.createAudioContext('myAudio');
  624. this.audioCtx.pause();
  625. },
  626. onShareAppMessage: function () {
  627. return {
  628. title: app.globalData.ShareTitle,
  629. path: 'pages/index/index?UserID=' + app.globalData.userInfo.UserID,
  630. imageUrl: app.globalData.ShareImageUrl,
  631. }
  632. },
  633. })