detail.js 14 KB

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