subject.js 4.7 KB

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