detail.js 17 KB

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