subject.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // pages/subject/subject.js
  2. const app = getApp()
  3. const host = app.globalData.host;
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. data:{},
  10. idx:1,
  11. ans:[],
  12. id:''
  13. },
  14. /**
  15. * 生命周期函数--监听页面加载
  16. */
  17. onLoad: function (options) {
  18. wx.request({
  19. url: host+'/api/wx/paper/info',
  20. data: { id: options.id},
  21. method:'GET',
  22. success:res=>{
  23. this.setData({
  24. data:res.data.data,
  25. id:options.id
  26. })
  27. this.countDown(res.data.data.time_limit)
  28. }
  29. })
  30. },
  31. countDown:function(time){
  32. var m=time-1,s=59,_this=this;
  33. var down=setInterval(function(){
  34. s--
  35. if(s==0 && m>0){
  36. m--;
  37. s=59
  38. }
  39. if(s==0 && m == 0){
  40. clearInterval(down)
  41. _this.post();
  42. }
  43. _this.setData({
  44. time:m+"分"+s+"秒"
  45. })
  46. },1000)
  47. },
  48. answer:function(e){
  49. let id = e.target.dataset.id, value = e.detail.value
  50. let ans = this.data.ans
  51. let old=ans.filter(item=>item.id == id)
  52. if (old.length>0){
  53. for (let i = 0; i < ans.length; i++) {
  54. if (ans[i].id == id) {
  55. ans[i].answer = e.detail.value
  56. }
  57. }
  58. }else{
  59. ans.push({
  60. id:id,
  61. answer:value
  62. })
  63. }
  64. this.setData({
  65. ans:ans
  66. })
  67. },
  68. next:function(){
  69. const idx=this.data.idx+1
  70. this.setData({
  71. idx:idx
  72. })
  73. },
  74. prev: function () {
  75. const idx = this.data.idx - 1
  76. this.setData({
  77. idx: idx
  78. })
  79. },
  80. post:function(){
  81. var _this=this
  82. wx.showModal({
  83. title: '交卷确认',
  84. content: '您确认要交卷吗?',
  85. success(res) {
  86. if (res.confirm) {
  87. wx.request({
  88. url: host + '/api/wx/postpaper',
  89. data: {
  90. paper_id: _this.data.id,
  91. questions: _this.data.ans
  92. },
  93. method: 'POST',
  94. success: res => {
  95. console.log(res.data)
  96. if (res.data.code == 0) {
  97. wx.showModal({
  98. title: res.data.data.score+'分',
  99. content: '您本次考试得分',
  100. showCancel: false,
  101. success(res) {
  102. if (res.confirm) {
  103. wx.navigateTo({
  104. url: '../online/online?tab=1',
  105. })
  106. }
  107. }
  108. })
  109. }
  110. }
  111. })
  112. }
  113. }
  114. })
  115. },
  116. /**
  117. * 生命周期函数--监听页面初次渲染完成
  118. */
  119. onReady: function () {
  120. },
  121. /**
  122. * 生命周期函数--监听页面显示
  123. */
  124. onShow: function () {
  125. },
  126. /**
  127. * 生命周期函数--监听页面隐藏
  128. */
  129. onHide: function () {
  130. },
  131. /**
  132. * 生命周期函数--监听页面卸载
  133. */
  134. onUnload: function () {
  135. },
  136. /**
  137. * 页面相关事件处理函数--监听用户下拉动作
  138. */
  139. onPullDownRefresh: function () {
  140. },
  141. /**
  142. * 页面上拉触底事件的处理函数
  143. */
  144. onReachBottom: function () {
  145. },
  146. /**
  147. * 用户点击右上角分享
  148. */
  149. onShareAppMessage: function () {
  150. }
  151. })