detail.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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 isAndroid = false;
  8. const waitTime = ["", "10秒钟", "15秒钟", "20秒钟", "25秒钟", "半分钟", "35秒钟", "40秒钟", "45秒钟"];
  9. Page({
  10. data: {
  11. version: app.globalData.version,
  12. FileUrl: app.globalData.fileUrl,
  13. IsShowMenu: false,
  14. filter: 'horizontal',
  15. //filter: 'vertical',
  16. PageNumber: 1,
  17. PageCount: 60,
  18. IsFinished: false,
  19. arrPage: [],
  20. ShowLoading: false,
  21. },
  22. onLoad: function (options) {
  23. var id = options.id;
  24. this.setData({
  25. Containnerheight: server.getWindowHeight(),
  26. ID: id,
  27. });
  28. var systemInfo = wx.getSystemInfoSync();
  29. if (systemInfo.system && systemInfo.system.indexOf("Android") >= 0) {
  30. times = 2;
  31. }
  32. this.getDetail(id);
  33. isAndroid = false;
  34. if (app.globalData.systemInfo.system && app.globalData.systemInfo.system.indexOf("Android") >= 0)
  35. isAndroid = true;
  36. },
  37. getDetail: function (id) {
  38. var item;
  39. var data = wx.getStorageSync('QuestionTypeList');
  40. for (var i = 0; i < data.length; i++) {
  41. for (var k = 0; k < data[i].List.length; k++) {
  42. if (id == data[i].List[k].ID) {
  43. item = data[i].List[k];
  44. item.Example[0] = item.Example[0].replace("+", " + ").replace("-", " - ").replace("×", " × ").replace("÷", " ÷ ");
  45. item.Example[1] = item.Example[1].replace("+", " + ").replace("-", " - ").replace("×", " × ").replace("÷", " ÷ ");
  46. break;
  47. }
  48. }
  49. }
  50. this.setData({
  51. QuestionTypeItem: item,
  52. });
  53. },
  54. gotoAnswer: function (e) {
  55. wx.navigateTo({
  56. url: '../main/answer',
  57. });
  58. },
  59. getFinish: function () {
  60. this.setData({
  61. IsFinished: true,
  62. });
  63. this.getList(this.data.ID);
  64. },
  65. getList: function (id) {
  66. var that = this;
  67. var pageNum = that.data.PageNumber;
  68. var QuestionTypeCategory = 3;
  69. if (that.data.filter == "horizontal")
  70. QuestionTypeCategory = 2;
  71. pageIndex = 0;
  72. server.getData('GetQuestionTypesPrint?ID=' + id + '&QuestionTypeCategory=' + QuestionTypeCategory + '&PageNum=' + pageNum, function (data) {
  73. //console.log("UserInfo:" + data);
  74. if (data) {
  75. that.setData({
  76. ShowLoading: true,
  77. arrPage: [],
  78. WaitTime: waitTime[that.data.PageNumber],
  79. });
  80. QuestionItem = data;
  81. if (isAndroid) {
  82. var ctx = wx.createCanvasContext("Canvas", that);
  83. ctx.fillRect(0, 0, 1050, 1485);
  84. ctx.draw(true, function (n) {
  85. wx.canvasToTempFilePath({
  86. x: 0,
  87. y: 0,
  88. width: 2100,
  89. height: 2970,
  90. canvasId: 'Canvas',
  91. success: function (res2) {
  92. that.saveImage();
  93. }
  94. })
  95. });
  96. }
  97. else {
  98. that.saveImage();
  99. }
  100. }
  101. });
  102. },
  103. saveLocalData: function (data) {
  104. var list = wx.getStorageSync("AnswerList");
  105. if (list && list.length > 0) {
  106. list.sort(function (a, b) {
  107. if (a.CreateTime == b.CreateTime) {
  108. return b.AnswerID - b.AnswerID;
  109. } else {
  110. return b.CreateTime > a.CreateTime;
  111. }
  112. });
  113. var id = list[0].AnswerID + 1;
  114. if (list.length >= 100) {
  115. id = list[list.length-1].AnswerID;
  116. }
  117. for (var i = 0; i < list.length; i++) {
  118. if (list[i].AnswerID == id) {
  119. list.splice(i, 1);
  120. }
  121. }
  122. data.AnswerID = id;
  123. list.unshift(data);
  124. }
  125. else {
  126. data.AnswerID = 1;
  127. list = [];
  128. list.push(data);
  129. }
  130. wx.setStorageSync("AnswerList", list);
  131. return data;
  132. },
  133. saveImage: function () {
  134. var that = this;
  135. var item = QuestionItem;
  136. item = that.saveLocalData(item);
  137. var ctx = wx.createCanvasContext("Canvas", that);
  138. ctx.setFillStyle('white');
  139. ctx.fillRect(0, 0, 2100, 2970);
  140. ctx.fillStyle = "#000000";
  141. //计算数组最大长度
  142. var maxLength = 0;
  143. for (var l = 0; l < item.QuestionList.length; l++) {
  144. var num1 = item.QuestionList[l];
  145. if (num1.Vertical) {
  146. for (var n = 0; n < num1.Vertical.length; n++) {
  147. if (maxLength < num1.Vertical[n].Array.length)
  148. maxLength = num1.Vertical[n].Array.length;
  149. }
  150. }
  151. }
  152. var xStart = 60;
  153. var yStart = 247;
  154. var height = 150;
  155. if (item.QuestionTypeCategory == 2)
  156. height = 59;
  157. if (item.PageCount == 18)
  158. height = 210;
  159. else if (item.PageCount == 15)
  160. height = 250;
  161. //括号
  162. var index = 0;
  163. ctx.setFontSize(12);
  164. for (var i = 0; i < 3; i++) {
  165. for (var j = 0; j < item.PageCount / 3; j++) {
  166. index++;
  167. ctx.fillText("(", xStart + i * 310, yStart + j * height);
  168. ctx.fillText(")", xStart + i * 310 + 22, yStart + j * height);
  169. }
  170. }
  171. //编号
  172. index = 0;
  173. ctx.setTextAlign('center');
  174. var numberLeft = 12;
  175. if (isAndroid)
  176. numberLeft = 14;
  177. for (var i = 0; i < 3; i++) {
  178. for (var j = 0; j < item.PageCount / 3; j++) {
  179. index++;
  180. ctx.fillText(index, xStart + i * 310 + numberLeft, yStart + j * height);
  181. }
  182. }
  183. //算式
  184. index = that.data.arrPage.length*item.PageCount;
  185. ctx.setFontSize(24);
  186. for (var i = 0; i < 3; i++) {
  187. for (var j = 0; j < item.PageCount / 3; j++) {
  188. index++;
  189. //横式
  190. if (item.QuestionTypeCategory == 2) {
  191. var A = item.QuestionList[index - 1].A.toString();
  192. if (item.QuestionList[index - 1].HiddenColumn == "A")
  193. A = "____";
  194. var OperateAB = item.QuestionList[index - 1].OperateAB.toString();
  195. //console.log(OperateAB);
  196. var B = item.QuestionList[index - 1].B.toString();
  197. if (item.QuestionList[index - 1].HiddenColumn == "B")
  198. B = "____";
  199. var equation = A + " " + OperateAB + " " + B;
  200. if (item.QuestionList[index - 1].C) {
  201. var OperateBC = item.QuestionList[index - 1].OperateBC.toString();
  202. var C = item.QuestionList[index - 1].C.toString();
  203. equation += " " + OperateBC + " " + C;
  204. if (item.QuestionList[index - 1].D) {
  205. var OperateCD = item.QuestionList[index - 1].OperateCD.toString();
  206. var D = item.QuestionList[index - 1].D.toString();
  207. equation += " " + OperateCD + " " + D;
  208. }
  209. }
  210. equation += " = ";
  211. var R = item.QuestionList[index - 1].R.toString();
  212. if (item.QuestionList[index - 1].HiddenColumn == "R")
  213. R = "";
  214. equation += R;
  215. var num = xStart + i * 310 + 31;
  216. for (var n = 0; n < equation.length; n++) {
  217. var len = getLength(equation[n]);
  218. num += len;
  219. if (equation[n] == ".") {
  220. ctx.fillText(equation[n], num + 3, yStart + j * 59);
  221. } else {
  222. var h = 0;
  223. if (equation[n] == "_")
  224. h = 8;
  225. ctx.fillText(equation[n], num, yStart + j * 59 + h);
  226. }
  227. }
  228. }
  229. //竖式
  230. else if (item.QuestionTypeCategory == 3) {
  231. var num1 = item.QuestionList[index - 1];
  232. var isAnswer = false;
  233. var width = 250;
  234. if (num1.OperateAB == "÷")
  235. width = 240;
  236. //console.log(num1);
  237. for (var n = 0; n < num1.Vertical.length; n++) {
  238. if (maxLength > num1.Vertical[n].Array.length) {
  239. if (num1.OperateAB != "÷")
  240. num1.Vertical[n].Array.splice(1, 0, "");
  241. }
  242. }
  243. console.log(num1);
  244. for (var n = 0; n < num1.Vertical.length; n++) {
  245. var num = xStart + i * 310 + width;
  246. for (var m = num1.Vertical[n].Array.length - 1; m >= 0; m--) {
  247. var str = "";
  248. if (num1.Vertical[n].Type == "line") {
  249. var lineLength = num1.Vertical[n].Array.length * 30 + 20;
  250. if (num1.OperateAB == "÷") {
  251. lineLength = num1.A.toString().length * 30;
  252. }
  253. ctx.moveTo(num, yStart + j * height + n * 30);
  254. ctx.lineTo(num - lineLength, yStart + j * height + n * 30);
  255. ctx.stroke();
  256. if (num1.OperateAB != "÷")
  257. isAnswer = true;
  258. break;
  259. }
  260. else {
  261. str = num1.Vertical[n].Array[m];
  262. if (num1.OperateAB == "÷" && str == "√") {
  263. isAnswer = true;
  264. ctx.drawImage("../../images/formula_division_size02.png", num - (num1.Vertical[n].Array.length - m) * 30 - 20, yStart + j * height + n * 30 - 31, 56, 76);
  265. //break;
  266. }
  267. else if (num1.OperateAB == "÷" && n == 0) {
  268. }
  269. else if (num1.OperateAB == "÷" && n == 1) {
  270. ctx.fillText(str, num - (num1.Vertical[n].Array.length - m) * 10, yStart + j * height + n * 30);
  271. }
  272. else {
  273. ctx.fillText(str, num - (num1.Vertical[n].Array.length - m) * 30, yStart + j * height + n * 30);
  274. }
  275. }
  276. }
  277. if (isAnswer)
  278. break;
  279. }
  280. }
  281. }
  282. }
  283. ctx.setTextAlign('left');
  284. ctx.setTextBaseline('top');
  285. ctx.setFontSize(32);
  286. ctx.fillText(item.Name, 60, 45);
  287. ctx.setFontSize(12);
  288. ctx.fillText(item.CagegoryName, 60, 90);
  289. ctx.fillText("共" + item.QuestionList.length + "道题", 60, 125);
  290. ctx.fillText("第 1 / 1 页", 60, 147);
  291. ctx.fillText("答案检索号", 768, 60);
  292. ctx.setTextAlign('right');
  293. ctx.fillText("出题时间 " + item.CreateTime, 875, 94);
  294. ctx.fillText("微信扫一扫 计时看答案", 875, 111);
  295. ctx.fillText("《数学计算题》选题 打印 扫码 批改", 875, 137);
  296. ctx.setFontSize(32);
  297. ctx.fillText(item.AnswerID, 872, 50);
  298. ctx.drawImage("../../images/answer.png", 890, 54, 100, 100);
  299. ctx.draw(true, function (n) {
  300. var w = 1050,
  301. h = 1485;
  302. wx.canvasToTempFilePath({
  303. x: 0,
  304. y: 0,
  305. width: w * times,
  306. height: h * times,
  307. //destWidth: w*times,
  308. //destHeight: h*times,
  309. canvasId: 'Canvas',
  310. success: function (res2) {
  311. console.log(res2.tempFilePath);
  312. wx.saveImageToPhotosAlbum({
  313. filePath: res2.tempFilePath,
  314. success(res3) {
  315. console.log("save:" + res3.savedFilePath);
  316. that.data.arrPage.push(res2.tempFilePath);
  317. that.setData({
  318. arrPage: that.data.arrPage,
  319. });
  320. if (that.data.arrPage.length < that.data.PageNumber) {
  321. pageIndex++;
  322. that.saveImage();
  323. } else {
  324. that.setData({
  325. ShowLoading: false,
  326. });
  327. wx.previewImage({
  328. current: that.data.arrPage[0], // 当前显示图片的http链接
  329. urls: that.data.arrPage // 需要预览的图片http链接列表
  330. });
  331. }
  332. },
  333. fail(err3) {
  334. console.log(err3);
  335. },
  336. })
  337. }
  338. })
  339. });
  340. function getLength(str) {
  341. switch (str) {
  342. case "0":
  343. case "1":
  344. case "2":
  345. case "3":
  346. case "4":
  347. case "5":
  348. case "6":
  349. case "7":
  350. case "8":
  351. case "9":
  352. return 15;
  353. case ".":
  354. return 8;
  355. default:
  356. return 12;
  357. }
  358. }
  359. },
  360. changePageNumber: function (e) {
  361. var id = Number(e.currentTarget.dataset.id);
  362. this.data.PageNumber += id;
  363. if (this.data.PageNumber < 1)
  364. this.data.PageNumber = 1;
  365. else if (this.data.PageNumber > 8)
  366. this.data.PageNumber = 8;
  367. this.setData({
  368. PageNumber: this.data.PageNumber,
  369. });
  370. },
  371. showImage: function (e) {
  372. var that = this;
  373. var id = e.currentTarget.dataset.id;
  374. wx.previewImage({
  375. current: that.data.arrPage[id], // 当前显示图片的http链接
  376. urls: that.data.arrPage // 需要预览的图片http链接列表
  377. });
  378. },
  379. closeLoading: function () {
  380. var that = this;
  381. that.setData({
  382. ShowLoading: false,
  383. IsFinished: false,
  384. });
  385. },
  386. menuClick: function (e) {
  387. this.data.filter = e.currentTarget.dataset.id;
  388. if (this.data.filter == "horizontal") {
  389. this.data.PageCount = this.data.QuestionTypeItem.QuestionNumber2;
  390. } else if (this.data.filter == "vertical") {
  391. this.data.PageCount = this.data.QuestionTypeItem.QuestionNumber3;
  392. }
  393. this.setData({
  394. filter: this.data.filter,
  395. IsShowMenu: false,
  396. PageCount: this.data.PageCount,
  397. });
  398. },
  399. closeMenu: function () {
  400. this.setData({
  401. IsShowMenu: false,
  402. });
  403. },
  404. openMenu: function () {
  405. this.setData({
  406. IsShowMenu: true,
  407. });
  408. },
  409. onShareAppMessage: function () {
  410. return {
  411. title: '',
  412. path: 'pages/index/index',
  413. success: function (res) { },
  414. fail: function (err) {
  415. console.log(err);
  416. },
  417. complete: function (res) {
  418. console.log(res);
  419. },
  420. }
  421. },
  422. })