answer.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. statusBar: 0
  27. },
  28. attendContest () {
  29. answer.attendContest({reqdata:{}},r => {
  30. let {paperId,questions} = r.object
  31. this.setData({
  32. paperId,
  33. questionTotal: this.shuffle(questions).map(item => {
  34. item.options = this.shuffle(item.options)
  35. return item
  36. })
  37. }, () => {
  38. timer = setInterval(() => {
  39. this.setData({
  40. time: ++this.data.time
  41. })
  42. }, 1000)
  43. this.setData({
  44. questionList: this.data.questionTotal.filter((item,index)=>index<10)
  45. })
  46. })
  47. })
  48. },
  49. goHome (e) {
  50. wx.showModal({
  51. title: '确定要离开吗?',
  52. content: '一旦离开,本次答题成绩不予记录!',
  53. confirmColor: '#ff0000',
  54. success (res) {
  55. if (res.confirm) {
  56. wx.reLaunch({
  57. url: '/pages/home/home',
  58. })
  59. } else if (res.cancel) {
  60. console.log('用户点击取消')
  61. }
  62. }
  63. })
  64. },
  65. showToggle(e) {
  66. this.setData({
  67. show: !this.data.show,
  68. allAnswer: this.data.chooseList.every(item => item || item === 0)
  69. })
  70. },
  71. patternToggle (e) {
  72. this.setData({
  73. pattern: !this.data.pattern
  74. })
  75. this.showToggle()
  76. this.bindNavTagger({currentTarget:{dataset:{index:1}}})
  77. },
  78. updateTagger (e) {
  79. this.setData({
  80. pattern: false,
  81. isupdate: true
  82. })
  83. this.bindNavTagger({currentTarget:{dataset:{index:1}}})
  84. },
  85. bindNavTagger(e) {
  86. let i = e.currentTarget.dataset.index
  87. this.setData({
  88. ['pagination.page']: i-0
  89. },()=>{
  90. if (wx.pageScrollTo) {
  91. wx.pageScrollTo({
  92. scrollTop: 0
  93. })
  94. } else {
  95. wx.showModal({
  96. title: '提示',
  97. content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
  98. })
  99. }
  100. this.setData({
  101. questionList: this.data.questionTotal.filter((item,index)=>index>=(i-1)*10&&index<i*10)
  102. })
  103. })
  104. },
  105. clickChoose (e) {
  106. let {questionindex,index} = e.currentTarget.dataset
  107. let {chooseList} = this.data
  108. chooseList[questionindex] = index-0
  109. this.setData({
  110. chooseList
  111. })
  112. },
  113. clickConfim (e) {
  114. let {paperId,chooseList,questionTotal,time} = this.data
  115. let paperScore = 0
  116. let questionWrong = []
  117. questionTotal.forEach((item,index) => {
  118. (chooseList[index]||chooseList[index]===0)&&item.options[chooseList[index]].optionAnswer
  119. ? paperScore += 2
  120. : questionWrong.push({[item.id]: (chooseList[index]||chooseList[index]===0)?item.options[chooseList[index]].optionId:''})
  121. })
  122. answer.submitUserAnswer({reqdata:{
  123. paperId,
  124. userId: wx.getStorageSync('id'),
  125. questionWrong: JSON.stringify(questionWrong),
  126. paperScore,
  127. paperDuration:time
  128. }},r => {
  129. let {rank,id} = r.object
  130. wx.navigateTo({
  131. url: `/pages/contest/score/score?rank=${rank}&id=${id}&score=${paperScore}`,
  132. })
  133. })
  134. },
  135. // 数组乱序
  136. shuffle(arr) {
  137. let length = arr.length,
  138. r = length,
  139. rand = 0;
  140. while (r) {
  141. rand = Math.floor(Math.random() * r--);
  142. [arr[r], arr[rand]] = [arr[rand], arr[r]];
  143. }
  144. return arr;
  145. },
  146. /**
  147. * 生命周期函数--监听页面加载
  148. */
  149. onLoad: function (options) {
  150. this.attendContest()
  151. let {chooseList} = this.data
  152. for (let index = 0; index < 50; index++) {
  153. chooseList[index] = null
  154. }
  155. this.setData({
  156. chooseList
  157. })
  158. wx.getSystemInfo({
  159. success: e => {
  160. this.setData({
  161. statusBar: e.statusBarHeight + 13
  162. })
  163. }
  164. })
  165. },
  166. /**
  167. * 生命周期函数--监听页面初次渲染完成
  168. */
  169. onReady: function () {
  170. },
  171. /**
  172. * 生命周期函数--监听页面显示
  173. */
  174. onShow: function () {
  175. },
  176. /**
  177. * 生命周期函数--监听页面隐藏
  178. */
  179. onHide: function () {
  180. },
  181. /**
  182. * 生命周期函数--监听页面卸载
  183. */
  184. onUnload: function () {
  185. clearInterval(timer)
  186. },
  187. /**
  188. * 页面相关事件处理函数--监听用户下拉动作
  189. */
  190. onPullDownRefresh: function () {
  191. },
  192. /**
  193. * 页面上拉触底事件的处理函数
  194. */
  195. onReachBottom: function () {
  196. },
  197. /**
  198. * 用户点击右上角分享
  199. */
  200. onShareAppMessage: function () {
  201. }
  202. })