print.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import common from '../../utils/util';
  2. import server from '../../utils/main';
  3. const app = getApp()
  4. Page({
  5. data: {
  6. Left: 0,
  7. },
  8. onLoad: function(options) {
  9. var id = options.id;
  10. id = 179;
  11. this.getList(id);
  12. },
  13. getList: function(id) {
  14. var that = this;
  15. server.getLocalHost(function() {
  16. server.getData('GetQuestionTypesPrint?ID=' + id, function(data) {
  17. //console.log("UserInfo:" + data);
  18. if (data) {
  19. that.saveImage(data);
  20. }
  21. });
  22. });
  23. },
  24. saveImage: function(item) {
  25. var that = this;
  26. var ctx = wx.createCanvasContext("Canvas", that);
  27. ctx.setFillStyle('white');
  28. ctx.fillRect(0, 0, 1050, 1485);
  29. ctx.setTextAlign('left');
  30. ctx.setTextBaseline('top');
  31. ctx.fillStyle = "#000000";
  32. ctx.setFontSize(32);
  33. ctx.fillText(item.Name, 60, 45);
  34. ctx.setFontSize(12);
  35. ctx.fillText(item.CagegoryName, 60, 90);
  36. ctx.fillText("共" + item.QuestionList.length + "道题", 60, 125);
  37. ctx.fillText("第 1 / 1 页", 60, 147);
  38. ctx.fillText("答案检索号", 768, 60);
  39. ctx.setTextAlign('right');
  40. ctx.fillText("出题时间 " + item.CreateTime, 875, 94);
  41. ctx.fillText("微信扫一扫 计时看答案", 875, 111);
  42. ctx.fillText("《数学计算题》选题 打印 扫码 批改", 875, 137);
  43. ctx.setFontSize(32);
  44. ctx.fillText("99", 875, 50);
  45. ctx.drawImage("../../images/answer.png", 890, 54, 100, 100);
  46. ctx.setTextBaseline('middle');
  47. var xStart = 60;
  48. var yStart = 247;
  49. var index = 0;
  50. for (var i = 0; i < 3; i++) {
  51. for (var j = 0; j < 20; j++) {
  52. index++;
  53. ctx.setTextAlign('left');
  54. ctx.setFontSize(12);
  55. ctx.fillText("(", xStart + i * 310, yStart + j * 59);
  56. ctx.setTextAlign('center');
  57. ctx.fillText(index, xStart + i * 310 + 13, yStart + j * 59);
  58. ctx.setTextAlign('left');
  59. ctx.fillText(")", xStart + i * 310 + 22, yStart + j * 59);
  60. ctx.setFontSize(24);
  61. var A = item.QuestionList[index - 1].A.toString();
  62. if (item.QuestionList[index - 1].HiddenColumn == "A")
  63. A = "____";
  64. var OperateAB = item.QuestionList[index - 1].OperateAB.toString();
  65. console.log(OperateAB);
  66. var B = item.QuestionList[index - 1].B.toString();
  67. if (item.QuestionList[index - 1].HiddenColumn == "B")
  68. B = "____";
  69. var equation = A + " " + OperateAB + " " + B;
  70. if (item.QuestionList[index - 1].C) {
  71. var OperateBC = item.QuestionList[index - 1].OperateBC.toString();
  72. var C = item.QuestionList[index - 1].C.toString();
  73. equation += " " + OperateBC + " " + C;
  74. if (item.QuestionList[index - 1].D) {
  75. var OperateCD = item.QuestionList[index - 1].OperateCD.toString();
  76. var D = item.QuestionList[index - 1].D.toString();
  77. equation += " " + OperateCD + " " + D;
  78. }
  79. }
  80. equation += " = ";
  81. var R = item.QuestionList[index - 1].R.toString();
  82. if (item.QuestionList[index - 1].HiddenColumn == "R")
  83. R = "";
  84. equation += R;
  85. var num = xStart + i * 310 + 31;
  86. ctx.setTextAlign('center');
  87. for (var n = 0; n < equation.length; n++) {
  88. var len = getLength(equation[n]);
  89. num += len;
  90. if (equation[n] == ".") {
  91. ctx.fillText(equation[n], num + 3, yStart + j * 59);
  92. } else {
  93. var h=0;
  94. if (equation[n]=="_")
  95. h=8;
  96. ctx.fillText(equation[n], num, yStart + j * 59+h);
  97. }
  98. }
  99. }
  100. }
  101. ctx.draw(true, function(n) {
  102. wx.canvasToTempFilePath({
  103. x: 0,
  104. y: 0,
  105. width: 2100,
  106. height: 2970,
  107. destWidth: 2100,
  108. destHeight: 2970,
  109. canvasId: 'Canvas',
  110. success: function (res2) {
  111. console.log(res2.tempFilePath);
  112. wx.previewImage({
  113. current: res2.tempFilePath, // 当前显示图片的http链接
  114. urls: [res2.tempFilePath] // 需要预览的图片http链接列表
  115. });
  116. }
  117. })
  118. });
  119. function getLength(str) {
  120. switch (str) {
  121. case "0":
  122. case "1":
  123. case "2":
  124. case "3":
  125. case "4":
  126. case "5":
  127. case "6":
  128. case "7":
  129. case "8":
  130. case "9":
  131. return 15;
  132. case ".":
  133. return 8;
  134. default:
  135. return 12;
  136. }
  137. }
  138. },
  139. moveLeft: function() {
  140. this.setData({
  141. Left: this.data.Left + 375,
  142. });
  143. },
  144. moveRight: function() {
  145. this.setData({
  146. Left: this.data.Left - 375,
  147. });
  148. }
  149. });