index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // miniprogram/pages/answer/index.js
  2. const app = getApp();
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. interval: undefined,
  8. actTime: undefined,
  9. answer: [],
  10. data: {
  11. countDown: "",
  12. list: [],
  13. height: [],
  14. data: {
  15. current: 0,
  16. total: 0
  17. }
  18. },
  19. /**
  20. * 生命周期函数--监听页面加载
  21. */
  22. onLoad: function (options) {
  23. wx.setNavigationBarTitle({
  24. title: app.globalData.selectQuestion.name
  25. })
  26. let height = wx.getSystemInfoSync().windowHeight;
  27. let li = app.globalData.selectQuestion.list || {};
  28. for (let i = 0; i < li.length; i++) {
  29. const item = li[i];
  30. let l = []
  31. for (let key in item.options) {
  32. l.push({
  33. name: key,
  34. option: item.options[key],
  35. checked: false
  36. })
  37. }
  38. li[i].options = l;
  39. }
  40. this.answer = new Array(li.length).fill([]);
  41. this.setData({
  42. list: li,
  43. height: [
  44. height * 0.07,
  45. height * 0.86,
  46. ],
  47. data: {
  48. current: 1,
  49. total: app.globalData.selectQuestion.list.length
  50. }
  51. })
  52. this.actTime = new Date();
  53. this.interval = setInterval(() => {
  54. let d = new Date();
  55. let c = d - this.actTime;
  56. let Q = app.globalData.selectQuestion;
  57. if (c >= Q.duration) return clearInterval(this.interval);
  58. let longTime = Q.duration - c;
  59. let h = Math.floor(longTime / 3600000);
  60. let m = Math.floor(longTime % 3600000 / 60000);
  61. let s = Math.floor(longTime % 3600000 % 60000 / 1000);
  62. let time = "";
  63. if (h) {
  64. time += h > 9 ? h : '0' + h;
  65. time += ":"
  66. }
  67. time += m > 9 ? m : '0' + m;
  68. time += ":"
  69. time += s > 9 ? s : '0' + s;
  70. this.setData({
  71. countDown: time
  72. })
  73. }, 500)
  74. },
  75. prov() {
  76. let data = this.data.data;
  77. data.current--;
  78. this.setData({
  79. data: data
  80. })
  81. },
  82. next() {
  83. let data = this.data.data;
  84. data.current++;
  85. this.setData({
  86. data: data
  87. })
  88. },
  89. changeSwiper(r) {
  90. let data = this.data.data;
  91. data.current = r.detail.current + 1;
  92. this.setData({
  93. data: data
  94. })
  95. },
  96. changeGroup(e) {
  97. let answer = this.answer;
  98. if (typeof e.detail.value === 'string') {
  99. // 单选
  100. answer[this.data.data.current - 1] = [e.detail.value]
  101. } else {
  102. // 复选
  103. answer[this.data.data.current - 1] = e.detail.value
  104. }
  105. this.answer = answer
  106. },
  107. submit() {
  108. console.log(this.answer);
  109. let queNum = [];
  110. for (let i = 0; i < this.answer.length; i++) {
  111. const v = this.answer[i];
  112. if (!v.length) queNum.push(i + 1)
  113. }
  114. const modal = {
  115. title: "提示",
  116. content: '确认提交',
  117. success: this.isSubmit
  118. }
  119. if (queNum.length) modal.content = '第' + queNum.join(",") + '题未作答,是否确认提交'
  120. wx.showModal(modal)
  121. },
  122. isSubmit() {
  123. this.answer;
  124. let p = {
  125. queId: app.globalData.selectQuestion._id,
  126. userId: app.globalData.userInfo._id,
  127. startTime: this.actTime,
  128. endTime: new Date(),
  129. answer: this.answer
  130. };
  131. let score = 0;
  132. for (let i = 0; i < app.globalData.selectQuestion.list.length; i++) {
  133. let an = this.answer[i].join(",")
  134. const v = app.globalData.selectQuestion.list[i];
  135. let isAdd = true;
  136. v.answer.map(r => {
  137. isAdd = (new RegExp(r.select)).test(an)
  138. })
  139. console.log(isAdd)
  140. if (v.answer.length === this.answer[i].length && isAdd) score++;
  141. }
  142. p.score = score;
  143. let recordList = app.globalData.userInfo.recordList || [];
  144. recordList ? recordList.push(p) : recordList = [p];
  145. },
  146. /**
  147. * 生命周期函数--监听页面初次渲染完成
  148. */
  149. onReady: function () {
  150. },
  151. /**
  152. * 生命周期函数--监听页面显示
  153. */
  154. onShow: function () {
  155. },
  156. /**
  157. * 生命周期函数--监听页面隐藏
  158. */
  159. onHide: function () {
  160. },
  161. /**
  162. * 生命周期函数--监听页面卸载
  163. */
  164. onUnload: function () {
  165. if (this.interval) clearInterval(this.interval);
  166. },
  167. /**
  168. * 页面相关事件处理函数--监听用户下拉动作
  169. */
  170. onPullDownRefresh: function () {
  171. },
  172. /**
  173. * 页面上拉触底事件的处理函数
  174. */
  175. onReachBottom: function () {
  176. },
  177. /**
  178. * 用户点击右上角分享
  179. */
  180. onShareAppMessage: function () {
  181. }
  182. })