print.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. import common from '../../utils/util';
  2. import server from '../../utils/main';
  3. const app = getApp();
  4. var QuestionList = [];
  5. var isAndroid = false;
  6. Page({
  7. data: {
  8. Left: 0,
  9. },
  10. onLoad: function (options) {
  11. var id = options.id;
  12. id = 130;
  13. var QuestionTypeCategory=3;
  14. this.getList(id, QuestionTypeCategory);
  15. isAndroid = false;
  16. if (app.globalData.systemInfo.system && app.globalData.systemInfo.system.indexOf("Android") >= 0)
  17. isAndroid = true;
  18. },
  19. getList: function (id, QuestionTypeCategory) {
  20. var that = this;
  21. server.getLocalHost(function () {
  22. server.getData('GetQuestionTypesPrint?QuestionTypeCategory=' + QuestionTypeCategory+'&ID=' + id, function (data) {
  23. //console.log("UserInfo:" + data);
  24. if (data) {
  25. //var data=wx.getStorageSync("TestPrint");
  26. QuestionList = data;
  27. var ctx = wx.createCanvasContext("Canvas", that);
  28. ctx.setFillStyle('white');
  29. ctx.fillRect(0, 0, 1050, 1485);
  30. ctx.draw(true, function (n) {
  31. wx.canvasToTempFilePath({
  32. x: 0,
  33. y: 0,
  34. width: 2100,
  35. height: 2970,
  36. destWidth: 2100,
  37. destHeight: 2970,
  38. canvasId: 'Canvas',
  39. success: function (res2) {
  40. console.log(res2.tempFilePath);
  41. that.saveImage();
  42. }
  43. })
  44. });
  45. }
  46. });
  47. });
  48. },
  49. saveImage: function () {
  50. var item = QuestionList;
  51. var that = this;
  52. var ctx = wx.createCanvasContext("Canvas", that);
  53. ctx.setFillStyle('white');
  54. //ctx.fillRect(0, 0, 1050, 1485);
  55. ctx.fillRect(0, 0, 1050, 1485);
  56. ctx.fillStyle = "#000000";
  57. //计算数组最大长度
  58. var maxLength = 0;
  59. for (var l = 0; l < item.QuestionList.length; l++) {
  60. var num1 = item.QuestionList[l];
  61. if (num1.Vertical) {
  62. for (var n = 0; n < num1.Vertical.length; n++) {
  63. if (maxLength < num1.Vertical[n].Array.length)
  64. maxLength = num1.Vertical[n].Array.length;
  65. }
  66. }
  67. }
  68. var xStart = 60;
  69. var yStart = 247;
  70. var height = 150;
  71. if (item.QuestionTypeCategory == 2)
  72. height = 59;
  73. if (item.PageCount == 18)
  74. height = 210;
  75. else if (item.PageCount == 15)
  76. height = 250;
  77. //括号
  78. var index = 0;
  79. ctx.setFontSize(12);
  80. for (var i = 0; i < 3; i++) {
  81. for (var j = 0; j < item.PageCount / 3; j++) {
  82. index++;
  83. ctx.fillText("(", xStart + i * 310, yStart + j * height);
  84. ctx.fillText(")", xStart + i * 310 + 22, yStart + j * height);
  85. }
  86. }
  87. //编号
  88. index = 0;
  89. ctx.setTextAlign('center');
  90. var numberLeft = 12;
  91. if (isAndroid)
  92. numberLeft = 14;
  93. for (var i = 0; i < 3; i++) {
  94. for (var j = 0; j < item.PageCount / 3; j++) {
  95. index++;
  96. ctx.fillText(index, xStart + i * 310 + numberLeft, yStart + j * height);
  97. }
  98. }
  99. //算式
  100. index = 0;
  101. ctx.setFontSize(24);
  102. for (var i = 0; i < 3; i++) {
  103. for (var j = 0; j < item.PageCount / 3; j++) {
  104. index++;
  105. //横式
  106. if (item.QuestionTypeCategory == 2) {
  107. var A = item.QuestionList[index - 1].A.toString();
  108. if (item.QuestionList[index - 1].HiddenColumn == "A")
  109. A = "____";
  110. var OperateAB = item.QuestionList[index - 1].OperateAB.toString();
  111. console.log(OperateAB);
  112. var B = item.QuestionList[index - 1].B.toString();
  113. if (item.QuestionList[index - 1].HiddenColumn == "B")
  114. B = "____";
  115. var equation = A + " " + OperateAB + " " + B;
  116. if (item.QuestionList[index - 1].C) {
  117. var OperateBC = item.QuestionList[index - 1].OperateBC.toString();
  118. var C = item.QuestionList[index - 1].C.toString();
  119. equation += " " + OperateBC + " " + C;
  120. if (item.QuestionList[index - 1].D) {
  121. var OperateCD = item.QuestionList[index - 1].OperateCD.toString();
  122. var D = item.QuestionList[index - 1].D.toString();
  123. equation += " " + OperateCD + " " + D;
  124. }
  125. }
  126. equation += " = ";
  127. var R = item.QuestionList[index - 1].R.toString();
  128. if (item.QuestionList[index - 1].HiddenColumn == "R")
  129. R = "";
  130. equation += R;
  131. var num = xStart + i * 310 + 31;
  132. for (var n = 0; n < equation.length; n++) {
  133. var len = getLength(equation[n]);
  134. num += len;
  135. if (equation[n] == ".") {
  136. ctx.fillText(equation[n], num + 3, yStart + j * 59);
  137. } else {
  138. var h = 0;
  139. if (equation[n] == "_")
  140. h = 8;
  141. ctx.fillText(equation[n], num, yStart + j * 59 + h);
  142. }
  143. }
  144. }
  145. //竖式
  146. else if (item.QuestionTypeCategory == 3) {
  147. var num1 = item.QuestionList[index - 1];
  148. var isAnswer = false;
  149. var width = 250;
  150. if (num1.OperateAB == "÷")
  151. width=240;
  152. //console.log(num1);
  153. for (var n = 0; n < num1.Vertical.length; n++) {
  154. if (maxLength > num1.Vertical[n].Array.length) {
  155. if (num1.OperateAB != "÷")
  156. num1.Vertical[n].Array.splice(1, 0, "");
  157. }
  158. }
  159. console.log(num1);
  160. for (var n = 0; n < num1.Vertical.length; n++) {
  161. var num = xStart + i * 310 + width;
  162. for (var m = num1.Vertical[n].Array.length - 1; m >= 0; m--) {
  163. var str = "";
  164. if (num1.Vertical[n].Type == "line") {
  165. if (num1.OperateAB != "÷") {
  166. var lineLength = num1.Vertical[n].Array.length * 30 + 20;
  167. var lineTop = -15;
  168. if (num1.OperateAB == "÷") {
  169. lineLength = num1.A.toString().length * 30;
  170. lineTop = 0;
  171. }
  172. ctx.moveTo(num, yStart + j * height + n * 30 + lineTop);
  173. ctx.lineTo(num - lineLength, yStart + j * height + n * 30 + lineTop);
  174. ctx.stroke();
  175. if (num1.OperateAB != "÷")
  176. isAnswer = true;
  177. break;
  178. }
  179. }
  180. else {
  181. str = num1.Vertical[n].Array[m];
  182. if (num1.OperateAB == "÷" && str == "√") {
  183. isAnswer = true;
  184. var lineLength = num1.A.toString().length;
  185. var lineWidth = Math.floor(57 * (1 + num1.A.toString().length) / 2);
  186. ctx.drawImage("../../images/formula_division_size0" + lineLength + ".png", num - (num1.Vertical[n].Array.length - m) * 30 - 8, yStart + j * height + n * 30 - 31, lineWidth, 39);
  187. //break;
  188. }
  189. else if (num1.OperateAB == "÷" && n == 0) {
  190. }
  191. else if (num1.OperateAB == "÷" && n == 1) {
  192. ctx.fillText(str, num - (num1.Vertical[n].Array.length - m) * 10, yStart + j * height + n * 30);
  193. }
  194. else {
  195. ctx.fillText(str, num - (num1.Vertical[n].Array.length - m) * 30, yStart + j * height + n * 30);
  196. }
  197. }
  198. }
  199. if (isAnswer)
  200. break;
  201. }
  202. }
  203. }
  204. }
  205. ctx.setTextAlign('left');
  206. ctx.setTextBaseline('top');
  207. ctx.setFontSize(32);
  208. ctx.fillText(item.Name, 60, 45);
  209. ctx.fillText("99", 835, 50);
  210. ctx.setFontSize(12);
  211. ctx.fillText(item.CagegoryName, 60, 90);
  212. ctx.fillText("共" + item.QuestionList.length + "道题", 60, 125);
  213. ctx.fillText("第 1 / 1 页", 60, 147);
  214. ctx.fillText("答案检索号", 768, 60);
  215. ctx.setTextAlign('right');
  216. ctx.fillText("出题时间 " + item.CreateTime, 875, 94);
  217. ctx.fillText("微信扫一扫 计时看答案", 875, 111);
  218. ctx.fillText("《数学计算题》选题 打印 扫码 批改", 875, 137);
  219. ctx.drawImage("../../images/answer.png", 890, 54, 100, 100);
  220. ctx.draw(true, function (n) {
  221. wx.canvasToTempFilePath({
  222. x: 0,
  223. y: 0,
  224. width: 2100,
  225. height: 2970,
  226. destWidth: 2100,
  227. destHeight: 2970,
  228. canvasId: 'Canvas',
  229. success: function (res2) {
  230. console.log(res2.tempFilePath);
  231. wx.previewImage({
  232. current: res2.tempFilePath, // 当前显示图片的http链接
  233. urls: [res2.tempFilePath] // 需要预览的图片http链接列表
  234. });
  235. }
  236. })
  237. });
  238. function getLength(str) {
  239. switch (str) {
  240. case "0":
  241. case "1":
  242. case "2":
  243. case "3":
  244. case "4":
  245. case "5":
  246. case "6":
  247. case "7":
  248. case "8":
  249. case "9":
  250. return 15;
  251. case ".":
  252. return 8;
  253. default:
  254. return 12;
  255. }
  256. }
  257. },
  258. moveLeft: function () {
  259. this.setData({
  260. Left: this.data.Left + 375,
  261. });
  262. },
  263. moveRight: function () {
  264. this.setData({
  265. Left: this.data.Left - 375,
  266. });
  267. }
  268. });