index.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import {
  2. formatSeconds
  3. } from '../../../utils/util.js'
  4. let app = getApp()
  5. Page({
  6. data: {
  7. spinShow: false,
  8. paperId: null,
  9. form: {},
  10. timer: null,
  11. doTime: 0,
  12. remainTime: 0,
  13. remainTimeStr: '',
  14. modalShow: false,
  15. result: 0,
  16. timeOutShow: false
  17. },
  18. onLoad: function(options) {
  19. let paperId = options.id
  20. let _this = this
  21. _this.setData({
  22. spinShow: true
  23. });
  24. app.formPost('/api/wx/student/exampaper/select/' + paperId, null)
  25. .then(res => {
  26. _this.setData({
  27. spinShow: false
  28. });
  29. if (res.code === 1) {
  30. _this.setData({
  31. form: res.response,
  32. paperId: paperId,
  33. remainTime: res.response.suggestTime * 60
  34. });
  35. _this.timeReduce()
  36. }
  37. }).catch(e => {
  38. _this.setData({
  39. spinShow: false
  40. });
  41. app.message(e, 'error')
  42. })
  43. },
  44. timeReduce() {
  45. let _this = this
  46. let timer = setInterval(function() {
  47. let remainTime = _this.data.remainTime
  48. if (remainTime <= 0) {
  49. _this.timeOut()
  50. } else {
  51. _this.setData({
  52. remainTime: remainTime - 1,
  53. remainTimeStr: formatSeconds(remainTime),
  54. doTime: _this.data.doTime + 1
  55. });
  56. }
  57. }, 1000)
  58. _this.setData({
  59. timer: timer
  60. });
  61. },
  62. onUnload() {
  63. clearInterval(this.data.timer)
  64. },
  65. returnRecord() {
  66. wx.reLaunch({
  67. url: '/pages/record/index',
  68. });
  69. },
  70. timeOut() {
  71. clearInterval(this.data.timer)
  72. this.setData({
  73. timeOutShow: true
  74. });
  75. },
  76. formSubmit: function(e) {
  77. let _this = this
  78. if (this.data.timer) {
  79. clearInterval(this.data.timer)
  80. }
  81. wx.showLoading({
  82. title: '提交中',
  83. mask: true
  84. })
  85. e.detail.value.id = this.data.paperId
  86. e.detail.value.doTime = this.data.doTime
  87. app.formPost('/api/wx/student/exampaper/answer/answerSubmit', e.detail.value)
  88. .then(res => {
  89. if (res.code === 1) {
  90. _this.setData({
  91. modalShow: true,
  92. result: res.response
  93. });
  94. } else {
  95. app.message(res.response, 'error')
  96. }
  97. wx.hideLoading()
  98. }).catch(e => {
  99. wx.hideLoading()
  100. app.message(e, 'error')
  101. })
  102. }
  103. })