subject.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. if(this.data.ans.length!=this.data.idx){
  71. wx.showToast({
  72. title: '请选择答案',
  73. icon: 'none',
  74. duration: 2000
  75. })
  76. }else{
  77. this.setData({
  78. idx:idx
  79. })
  80. }
  81. },
  82. prev: function () {
  83. const idx = this.data.idx - 1
  84. this.setData({
  85. idx: idx
  86. })
  87. },
  88. post:function(){
  89. var _this=this
  90. wx.showModal({
  91. title: '交卷确认',
  92. content: '您确认要交卷吗?',
  93. success(res) {
  94. if (res.confirm) {
  95. wx.request({
  96. url: host + '/api/wx/postpaper',
  97. data: {
  98. paper_id: _this.data.id,
  99. questions: _this.data.ans
  100. },
  101. method: 'POST',
  102. success: res => {
  103. console.log(res.data)
  104. if (res.data.code == 0) {
  105. wx.showModal({
  106. title: res.data.data.score+'分',
  107. content: '您本次考试得分',
  108. showCancel: false,
  109. success(res) {
  110. if (res.confirm) {
  111. wx.navigateTo({
  112. url: '../online/online?tab=1',
  113. })
  114. }
  115. }
  116. })
  117. }
  118. }
  119. })
  120. }
  121. }
  122. })
  123. },
  124. /**
  125. * 生命周期函数--监听页面初次渲染完成
  126. */
  127. onReady: function () {
  128. },
  129. /**
  130. * 生命周期函数--监听页面显示
  131. */
  132. onShow: function () {
  133. },
  134. /**
  135. * 生命周期函数--监听页面隐藏
  136. */
  137. onHide: function () {
  138. },
  139. /**
  140. * 生命周期函数--监听页面卸载
  141. */
  142. onUnload: function () {
  143. },
  144. /**
  145. * 页面相关事件处理函数--监听用户下拉动作
  146. */
  147. onPullDownRefresh: function () {
  148. },
  149. /**
  150. * 页面上拉触底事件的处理函数
  151. */
  152. onReachBottom: function () {
  153. },
  154. /**
  155. * 用户点击右上角分享
  156. */
  157. onShareAppMessage: function () {
  158. }
  159. })