index.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. let reset = options.reset
  24. wx.setNavigationBarTitle({
  25. title: app.globalData.selectQuestion.name
  26. })
  27. let height = wx.getSystemInfoSync().windowHeight;
  28. let li = app.globalData.selectQuestion.list || {};
  29. if(reset != 1){
  30. for (let i = 0; i < li.length; i++) {
  31. const item = li[i];
  32. let l = []
  33. for (let key in item.options) {
  34. l.push({
  35. name: key,
  36. option: item.options[key],
  37. checked: false
  38. })
  39. }
  40. li[i].options = l;
  41. }
  42. }
  43. this.answer = new Array(li.length).fill([]);
  44. this.setData({
  45. list: li,
  46. height: [
  47. height * 0.07,
  48. height * 0.86,
  49. ],
  50. data: {
  51. current: 1,
  52. total: app.globalData.selectQuestion.list.length
  53. }
  54. })
  55. this.actTime = new Date();
  56. this.interval = setInterval(() => {
  57. let d = new Date();
  58. let c = d - this.actTime;
  59. let Q = app.globalData.selectQuestion;
  60. if (c >= Q.duration) {
  61. wx.showModal({
  62. title: "提示",
  63. content: '时间到请点击确认后交卷',
  64. showCancel: false,
  65. success: this.isSubmit
  66. })
  67. return clearInterval(this.interval);
  68. }
  69. let longTime = Q.duration - c;
  70. let h = Math.floor(longTime / 3600000);
  71. let m = Math.floor(longTime % 3600000 / 60000);
  72. let s = Math.floor(longTime % 3600000 % 60000 / 1000);
  73. let time = "";
  74. if (h) {
  75. time += h > 9 ? h : '0' + h;
  76. time += ":"
  77. }
  78. time += m > 9 ? m : '0' + m;
  79. time += ":"
  80. time += s > 9 ? s : '0' + s;
  81. this.setData({
  82. countDown: time
  83. })
  84. }, 500)
  85. },
  86. prov() {
  87. let data = this.data.data;
  88. data.current--;
  89. this.setData({
  90. data: data
  91. })
  92. },
  93. next() {
  94. let data = this.data.data;
  95. data.current++;
  96. this.setData({
  97. data: data
  98. })
  99. },
  100. changeSwiper(r) {
  101. let data = this.data.data;
  102. data.current = r.detail.current + 1;
  103. this.setData({
  104. data: data
  105. })
  106. },
  107. changeGroup(e) {
  108. let answer = this.answer;
  109. if (typeof e.detail.value === 'string') {
  110. // 单选
  111. answer[this.data.data.current - 1] = [e.detail.value]
  112. } else {
  113. // 复选
  114. answer[this.data.data.current - 1] = e.detail.value
  115. }
  116. this.answer = answer
  117. },
  118. submit() {
  119. console.log(this.answer);
  120. let queNum = [];
  121. for (let i = 0; i < this.answer.length; i++) {
  122. const v = this.answer[i];
  123. if (!v.length) queNum.push(i + 1)
  124. }
  125. const modal = {
  126. title: "提示",
  127. content: '确认提交',
  128. success: this.isSubmit
  129. }
  130. if (queNum.length) modal.content = '第' + queNum.join(",") + '题未作答,是否确认提交'
  131. wx.showModal(modal)
  132. },
  133. isSubmit() {
  134. let p = {
  135. queId: app.globalData.selectQuestion._id,
  136. userId: app.globalData.userInfo._openid,
  137. startTime: this.actTime,
  138. istrue: [],
  139. endTime: new Date(),
  140. answer: this.answer
  141. };
  142. p.duration = p.endTime - p.startTime;
  143. let score = 0;
  144. for (let i = 0; i < app.globalData.selectQuestion.list.length; i++) {
  145. let an = this.answer[i].join(",")
  146. const v = app.globalData.selectQuestion.list[i];
  147. let isAdd = true;
  148. v.answer.map(r => {
  149. isAdd = (new RegExp(r.select)).test(an)
  150. })
  151. p.istrue.push(isAdd);
  152. if (v.answer.length === this.answer[i].length && isAdd) score++;
  153. }
  154. p.score = score;
  155. wx.showLoading();
  156. wx.cloud.callFunction({
  157. name: "quickstartFunctions",
  158. data: {
  159. type: "createQuestionRecord",
  160. data: p
  161. }
  162. }).then(res => {
  163. wx.hideLoading()
  164. if(res.result.code !== 0) return wx.showToast({
  165. title: '网络繁忙'
  166. })
  167. wx.reLaunch({
  168. url: '/pages/result/index?data=' + JSON.stringify(res.result.data || {}),
  169. })
  170. }).catch(err => {
  171. console.log(err)
  172. wx.hideLoading()
  173. })
  174. },
  175. /**
  176. * 生命周期函数--监听页面初次渲染完成
  177. */
  178. onReady: function () {
  179. },
  180. /**
  181. * 生命周期函数--监听页面显示
  182. */
  183. onShow: function () {
  184. },
  185. /**
  186. * 生命周期函数--监听页面隐藏
  187. */
  188. onHide: function () {
  189. },
  190. /**
  191. * 生命周期函数--监听页面卸载
  192. */
  193. onUnload: function () {
  194. if (this.interval) clearInterval(this.interval);
  195. console.log()
  196. },
  197. /**
  198. * 页面相关事件处理函数--监听用户下拉动作
  199. */
  200. onPullDownRefresh: function () {
  201. },
  202. /**
  203. * 页面上拉触底事件的处理函数
  204. */
  205. onReachBottom: function () {
  206. },
  207. /**
  208. * 用户点击右上角分享
  209. */
  210. onShareAppMessage: function () {
  211. }
  212. })