detail.js 15 KB

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