detail.js 12 KB

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