subject.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. show:0,
  14. flag:[
  15. {name:'正确',value:'对'},
  16. {name:'错误',value:'错'}
  17. ],
  18. rePost:0
  19. },
  20. /**
  21. * 生命周期函数--监听页面加载
  22. */
  23. onLoad: function (options) {
  24. // options.id=65
  25. // options.status=1
  26. wx.request({
  27. url: host+'/api/wx/paper/info',
  28. data: { id: options.id},
  29. method:'GET',
  30. success:res=>{
  31. this.setData({
  32. data:res.data.data,
  33. id:options.id,
  34. status:options.status,
  35. time:res.data.data.time_limit
  36. })
  37. if(options.status == 1) return
  38. this.countDown(res.data.data.time_limit)
  39. }
  40. })
  41. },
  42. countDown:function(time){
  43. var m=time-1,s=59,_this=this;
  44. var down=setInterval(function(){
  45. s--
  46. if(s==0 && m>0){
  47. m--;
  48. s=59
  49. }
  50. if(s==0 && m == 0){
  51. clearInterval(down)
  52. _this.post();
  53. }
  54. _this.setData({
  55. time:m+"分"+s+"秒"
  56. })
  57. },1000)
  58. },
  59. openAns(){
  60. this.setData({
  61. show:1
  62. })
  63. },
  64. closeAns(){
  65. this.setData({
  66. show:0
  67. })
  68. },
  69. //重做
  70. reDo(){
  71. this.setData({
  72. status:0,
  73. rePost:1
  74. })
  75. this.countDown(this.data.time)
  76. },
  77. answer:function(e){
  78. let id = e.target.dataset.id, value = e.detail.value
  79. let ans = this.data.ans
  80. let data=this.data.data
  81. let old=ans.filter(item=>item.id == id)
  82. if (old.length>0){
  83. for (let i = 0; i < ans.length; i++) {
  84. if (ans[i].id == id) {
  85. ans[i].answer = e.detail.value
  86. }
  87. }
  88. }else{
  89. ans.push({
  90. id:id,
  91. answer:value
  92. })
  93. }
  94. data.questions.forEach(item=>{
  95. if(id == item.id){
  96. item.post_answer=value
  97. }
  98. })
  99. this.setData({
  100. ans:ans,
  101. data:data
  102. })
  103. },
  104. // 答题卡题目跳转
  105. skip(e){
  106. this.setData({
  107. idx:e.target.dataset.id+1,
  108. show:0
  109. })
  110. },
  111. next:function(){
  112. const idx=this.data.idx+1
  113. const cur = this.data.idx -1
  114. this.setData({
  115. idx:idx
  116. })
  117. // if (!this.data.ans[cur].answer){
  118. // wx.showToast({
  119. // title: '请选择答案',
  120. // icon: 'none',
  121. // duration: 2000
  122. // })
  123. // }else{
  124. // this.setData({
  125. // idx:idx
  126. // })
  127. // }
  128. },
  129. prev: function () {
  130. const idx = this.data.idx - 1
  131. this.setData({
  132. idx: idx
  133. })
  134. },
  135. post:function(){
  136. var _this=this,txt='您确认要交卷吗?'
  137. if(this.data.ans.length < this.data.data.questions.length){
  138. if(this.data.data.paper_type == 'exam'){
  139. wx.showToast({
  140. title: '您还有题目没有作答,请完成后再交卷!',
  141. icon:'none'
  142. })
  143. return
  144. }
  145. txt='您还有题目没有作答,确认要交卷吗?'
  146. }
  147. let url=host + '/api/wx/postpaper'
  148. if(this.data.rePost){
  149. url=host + '/api/wx/paper/retry'
  150. }
  151. wx.showModal({
  152. title: '交卷确认',
  153. content: txt,
  154. success(res) {
  155. if (res.confirm) {
  156. wx.request({
  157. url: url,
  158. data: {
  159. paper_id: _this.data.id,
  160. questions: _this.data.ans
  161. },
  162. method: 'POST',
  163. success: res => {
  164. console.log(res.data)
  165. if (res.data.code == 0) {
  166. wx.showModal({
  167. title: res.data.data.score+'分',
  168. content: '您本次考试得分',
  169. showCancel: false,
  170. success(res) {
  171. if (res.confirm) {
  172. wx.navigateTo({
  173. url: '../online/online?tab=1',
  174. })
  175. }
  176. }
  177. })
  178. }
  179. }
  180. })
  181. }
  182. }
  183. })
  184. },
  185. /**
  186. * 生命周期函数--监听页面初次渲染完成
  187. */
  188. onReady: function () {
  189. },
  190. /**
  191. * 生命周期函数--监听页面显示
  192. */
  193. onShow: function () {
  194. },
  195. /**
  196. * 生命周期函数--监听页面隐藏
  197. */
  198. onHide: function () {
  199. },
  200. /**
  201. * 生命周期函数--监听页面卸载
  202. */
  203. onUnload: function () {
  204. },
  205. /**
  206. * 页面相关事件处理函数--监听用户下拉动作
  207. */
  208. onPullDownRefresh: function () {
  209. },
  210. /**
  211. * 页面上拉触底事件的处理函数
  212. */
  213. onReachBottom: function () {
  214. },
  215. /**
  216. * 用户点击右上角分享
  217. */
  218. onShareAppMessage: function () {
  219. }
  220. })