answer.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import commonConfig from "../../common/common"
  2. let app = getApp()
  3. Page({
  4. data: {
  5. data: [],//all题列表
  6. record: [],//提交给后台数据组
  7. id: 0,//当前swiper-item的索引值
  8. result_id: '',//考试结果id
  9. duration: ''//倒计时
  10. },
  11. onLoad(options) {
  12. let that = this
  13. if (options.result_status == 1) {
  14. dd.getStorage({
  15. key: 'record',
  16. success: function (res) {
  17. let record = res.data
  18. if (record && record.length != 0) {
  19. dd.getStorage({
  20. key: 'data',
  21. success: function (r) {
  22. console.log(r.data)
  23. if (r.data) {
  24. that.setData({
  25. data: r.data
  26. })
  27. that.setData({
  28. record:record
  29. })
  30. }
  31. },
  32. fail: function (res) {
  33. dd.alert({ content: res.errorMessage });
  34. }
  35. })
  36. that.setData({
  37. id: record.indexOf(null)
  38. })
  39. } else {
  40. that.getExamQuestions(options.exam_id)
  41. that.setData({
  42. id: 0
  43. })
  44. }
  45. },
  46. fail: function (res) {
  47. dd.alert({ content: res.errorMessage });
  48. }
  49. });
  50. } else {
  51. that.getExamQuestions(options.exam_id)
  52. }
  53. dd.setStorage({
  54. key: 'answerid',
  55. data: {
  56. exam_id: options.exam_id,
  57. result_id: options.result_id
  58. }
  59. });
  60. that.setData({
  61. result_id: options.result_id
  62. })
  63. that.getdjs()
  64. },
  65. onUnload() {
  66. // 页面被关闭
  67. let record = this.data.record
  68. console.log('页面被关闭')
  69. dd.setStorage({
  70. key: 'record',
  71. data: record
  72. });
  73. // dd.showToast({
  74. // type: 'none',
  75. // content: '暂时离开',
  76. // duration: 3000
  77. // });
  78. },
  79. getdjs() {
  80. let params = {
  81. result_id: this.data.result_id
  82. }
  83. app.https('POST', app.globaldata.apiUrl.getRemainingTime, JSON.stringify(params), {
  84. type: false
  85. }).then(res => {
  86. this.setData({
  87. djs: res.data.data.time
  88. })
  89. let examSurplusTime = res.data.data.time;//获取考试剩余时间
  90. // let examSurplusTime = 60;
  91. this.examDjsFn(examSurplusTime)
  92. })
  93. },
  94. examDjsFn(examSurplusTime) {
  95. let timer = null;
  96. let that = this
  97. let time = examSurplusTime * 1000
  98. timer = setInterval(() => {
  99. let h, m, s
  100. if (time >= 0) {
  101. m = Math.floor(time / 1000 / 60 % 60);
  102. s = Math.floor(time / 1000 % 60);
  103. m = m < 10 ? ('0' + m) : m;
  104. s = s < 10 ? ('0' + s) : s;
  105. let duration = m + ':' + s;
  106. that.setData({
  107. duration: duration
  108. })
  109. time = time - 1000
  110. } else {
  111. clearInterval(timer);
  112. let duration = '00:00'
  113. that.setData({
  114. duration: duration
  115. })
  116. dd.showToast({
  117. type: 'fail',
  118. content: '答题超时,将为你自动提交!',
  119. duration: 3000,
  120. success: () => {
  121. that.formSubmit()
  122. },
  123. });
  124. }
  125. }, 1000);
  126. },
  127. onSubmit(e) {},
  128. radioChange(e) {
  129. console.log(e)
  130. let record = this.data.record
  131. let id = this.data.id;
  132. record[id] = { question_id: e.currentTarget.dataset.id, answer: [{ key: e.detail.value, checked: 1 }] };
  133. this.setData({
  134. record: record,
  135. })
  136. let data = commonConfig.filterData(this.data.record, this.data.data)
  137. dd.setStorage({
  138. key: 'data',
  139. data: data
  140. })
  141. console.log(this.data.record)
  142. },
  143. checkedChange(e) {
  144. let record = this.data.record
  145. let value = e.detail.value
  146. let id = this.data.id;
  147. let answers = []
  148. answers = value.map((item) => {
  149. return {
  150. key: item,
  151. checked: 1
  152. }
  153. })
  154. record[id] = { question_id: e.currentTarget.dataset.id, answer: answers };
  155. console.log(record)
  156. this.setData({
  157. record: record,
  158. })
  159. let data = commonConfig.filterData(this.data.record, this.data.data)
  160. dd.setStorage({
  161. key: 'data',
  162. data: data
  163. })
  164. console.log(this.data.record)
  165. },
  166. swiperChange(e) {
  167. this.setData({
  168. id: e.detail.current
  169. })
  170. },
  171. getExamQuestions(exam_id) {
  172. let that = this;
  173. let params = {
  174. exam_id: exam_id
  175. }
  176. app.https('POST', app.globaldata.apiUrl.getExamQuestions, JSON.stringify(params), {
  177. type: false
  178. }).then(res => {
  179. that.setData({
  180. record: new Array(res.data.data.questions.length),
  181. data: res.data.data.questions
  182. })
  183. dd.setStorage({
  184. key: 'data',
  185. data: res.data.data.questions
  186. })
  187. })
  188. },
  189. lastq() {
  190. if (this.data.id != 0) {
  191. this.setData({
  192. id: this.data.id - 1,
  193. })
  194. }
  195. },
  196. nextq() {
  197. if (this.data.id < this.data.data.length) {
  198. this.setData({
  199. id: this.data.id + 1,
  200. })
  201. }
  202. },
  203. formSubmit() {//提交
  204. let params = {
  205. result_id: this.data.result_id,
  206. record: this.data.record
  207. }
  208. app.https('POST', app.globaldata.apiUrl.subExam, JSON.stringify(params), {
  209. type: false
  210. }).then(res => {
  211. console.log(res)
  212. dd.reLaunch({
  213. url: "../result/result?scores=" + res.data.data.scores + "&qualified=" + res.data.data.qualified
  214. })
  215. })
  216. }
  217. });