subject.js 4.5 KB

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