answer.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // pages/contest/answer/answer.js
  2. import {
  3. answer
  4. } from '../../../api/api.js'
  5. let timer
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. pagination: {
  12. page: 1,
  13. rows: 10
  14. },
  15. show: false,
  16. pattern: false,
  17. isupdate: false,
  18. paperId: '',
  19. paperScore: 0,
  20. questionWrong: '',
  21. questionList: [],
  22. questionTotal: [],
  23. chooseList: [],
  24. time: 0,
  25. allAnswer: false
  26. },
  27. attendContest () {
  28. answer.attendContest({reqdata:{}},r => {
  29. let {paperId,questions} = r.object
  30. this.setData({
  31. paperId,
  32. questionTotal: this.shuffle(questions).map(item => {
  33. item.options = this.shuffle(item.options)
  34. return item
  35. })
  36. }, () => {
  37. this.setData({
  38. questionList: this.data.questionTotal.filter((item,index)=>index<10)
  39. })
  40. })
  41. })
  42. },
  43. showToggle(e) {
  44. this.setData({
  45. show: !this.data.show,
  46. allAnswer: this.data.chooseList.every(item => item || item === 0)
  47. })
  48. },
  49. patternToggle (e) {
  50. this.setData({
  51. pattern: !this.data.pattern
  52. })
  53. this.showToggle()
  54. this.bindNavTagger({currentTarget:{dataset:{index:1}}})
  55. },
  56. updateTagger (e) {
  57. this.setData({
  58. pattern: false,
  59. isupdate: true
  60. })
  61. this.bindNavTagger({currentTarget:{dataset:{index:1}}})
  62. },
  63. bindNavTagger(e) {
  64. let i = e.currentTarget.dataset.index
  65. this.setData({
  66. ['pagination.page']: i-0
  67. },()=>{
  68. if (wx.pageScrollTo) {
  69. wx.pageScrollTo({
  70. scrollTop: 0
  71. })
  72. } else {
  73. wx.showModal({
  74. title: '提示',
  75. content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
  76. })
  77. }
  78. this.setData({
  79. questionList: this.data.questionTotal.filter((item,index)=>index>=(i-1)*10&&index<i*10)
  80. })
  81. })
  82. },
  83. clickChoose (e) {
  84. let {questionindex,index} = e.currentTarget.dataset
  85. let {chooseList} = this.data
  86. chooseList[questionindex] = index-0
  87. this.setData({
  88. chooseList
  89. })
  90. },
  91. clickConfim (e) {
  92. let {paperId,chooseList,questionTotal,time} = this.data
  93. let paperScore = 0
  94. let questionWrong = []
  95. questionTotal.forEach((item,index) => {
  96. (chooseList[index]||chooseList[index]===0)&&item.options[chooseList[index]].optionAnswer
  97. ? paperScore += 2
  98. : questionWrong.push({[item.id]: (chooseList[index]||chooseList[index]===0)?item.options[chooseList[index]].optionId:''})
  99. })
  100. answer.submitUserAnswer({reqdata:{
  101. paperId,
  102. userId: wx.getStorageSync('id'),
  103. questionWrong: JSON.stringify(questionWrong),
  104. paperScore,
  105. paperDuration:time
  106. }},r => {
  107. wx.navigateTo({
  108. url: `/pages/contest/score/score?rank=${r.object}&score=${paperScore}`,
  109. })
  110. })
  111. },
  112. // 数组乱序
  113. shuffle(arr) {
  114. let length = arr.length,
  115. r = length,
  116. rand = 0;
  117. while (r) {
  118. rand = Math.floor(Math.random() * r--);
  119. [arr[r], arr[rand]] = [arr[rand], arr[r]];
  120. }
  121. return arr;
  122. },
  123. /**
  124. * 生命周期函数--监听页面加载
  125. */
  126. onLoad: function (options) {
  127. this.attendContest()
  128. timer = setInterval(() => {
  129. this.setData({
  130. time: ++this.data.time
  131. })
  132. }, 1000)
  133. let {chooseList} = this.data
  134. for (let index = 0; index < 50; index++) {
  135. chooseList[index] = null
  136. }
  137. this.setData({
  138. chooseList
  139. })
  140. },
  141. /**
  142. * 生命周期函数--监听页面初次渲染完成
  143. */
  144. onReady: function () {
  145. },
  146. /**
  147. * 生命周期函数--监听页面显示
  148. */
  149. onShow: function () {
  150. },
  151. /**
  152. * 生命周期函数--监听页面隐藏
  153. */
  154. onHide: function () {
  155. },
  156. /**
  157. * 生命周期函数--监听页面卸载
  158. */
  159. onUnload: function () {
  160. clearInterval(timer)
  161. },
  162. /**
  163. * 页面相关事件处理函数--监听用户下拉动作
  164. */
  165. onPullDownRefresh: function () {
  166. },
  167. /**
  168. * 页面上拉触底事件的处理函数
  169. */
  170. onReachBottom: function () {
  171. },
  172. /**
  173. * 用户点击右上角分享
  174. */
  175. onShareAppMessage: function () {
  176. }
  177. })