index.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // miniprogram/pages/answer/index.js
  2. const app = getApp();
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. interval: undefined,
  8. actTime: undefined,
  9. answer: [],
  10. data: {
  11. imgLi:[],
  12. countDown: "",
  13. list: [],
  14. height: [],
  15. data: {
  16. current: 0,
  17. total: 0
  18. }
  19. },
  20. /**
  21. * 生命周期函数--监听页面加载
  22. */
  23. onLoad: function (options) {
  24. let reset = options.reset
  25. wx.setNavigationBarTitle({
  26. title: app.globalData.selectQuestion.name
  27. })
  28. let height = wx.getSystemInfoSync().windowHeight;
  29. let li = app.globalData.selectQuestion.list || {};
  30. if(reset != 1){
  31. for (let i = 0; i < li.length; i++) {
  32. const item = li[i];
  33. let l = []
  34. for (let key in item.options) {
  35. l.push({
  36. name: key,
  37. option: item.options[key],
  38. checked: false
  39. })
  40. }
  41. li[i].options = l;
  42. }
  43. }
  44. this.answer = new Array(li.length).fill([]);
  45. this.setData({
  46. list: li,
  47. imgLi: new Array(li.length).fill({}),
  48. height: [
  49. height * 0.07,
  50. height * 0.86,
  51. ],
  52. data: {
  53. current: 1,
  54. total: app.globalData.selectQuestion.list.length
  55. }
  56. })
  57. this.actTime = new Date();
  58. this.interval = setInterval(() => {
  59. let d = new Date();
  60. let c = d - this.actTime;
  61. let Q = app.globalData.selectQuestion;
  62. if (c >= Q.duration) {
  63. wx.showModal({
  64. title: "提示",
  65. content: '时间到请点击确认后交卷',
  66. showCancel: false,
  67. success: this.isSubmit
  68. })
  69. return clearInterval(this.interval);
  70. }
  71. let longTime = Q.duration - c;
  72. let h = Math.floor(longTime / 3600000);
  73. let m = Math.floor(longTime % 3600000 / 60000);
  74. let s = Math.floor(longTime % 3600000 % 60000 / 1000);
  75. let time = "";
  76. if (h) {
  77. time += h > 9 ? h : '0' + h;
  78. time += ":"
  79. }
  80. time += m > 9 ? m : '0' + m;
  81. time += ":"
  82. time += s > 9 ? s : '0' + s;
  83. this.setData({
  84. countDown: time
  85. })
  86. }, 500)
  87. },
  88. loadimage(e){
  89. const li = this.data.imgLi || [];
  90. const sys = wx.getSystemInfoSync();
  91. li[e.currentTarget.dataset.index] = {
  92. width: sys.windowWidth,
  93. height: e.detail.height/e.detail.width * sys.windowWidth
  94. }
  95. this.setData({
  96. imgLi: li
  97. })
  98. },
  99. prov() {
  100. let data = this.data.data;
  101. data.current--;
  102. this.setData({
  103. data: data
  104. })
  105. },
  106. next() {
  107. let data = this.data.data;
  108. data.current++;
  109. this.setData({
  110. data: data
  111. })
  112. },
  113. changeSwiper(r) {
  114. let data = this.data.data;
  115. data.current = r.detail.current + 1;
  116. this.setData({
  117. data: data
  118. })
  119. },
  120. changeGroup(e) {
  121. let answer = this.answer;
  122. if (typeof e.detail.value === 'string') {
  123. // 单选
  124. answer[this.data.data.current - 1] = [e.detail.value]
  125. } else {
  126. // 复选
  127. answer[this.data.data.current - 1] = e.detail.value
  128. }
  129. this.answer = answer
  130. },
  131. submit() {
  132. console.log(this.answer);
  133. let queNum = [];
  134. for (let i = 0; i < this.answer.length; i++) {
  135. const v = this.answer[i];
  136. if (!v.length) queNum.push(i + 1)
  137. }
  138. const modal = {
  139. title: "提示",
  140. content: '确认提交',
  141. success: this.isSubmit
  142. }
  143. if (queNum.length) modal.content = '第' + queNum.join(",") + '题未作答,是否确认提交'
  144. wx.showModal(modal)
  145. },
  146. isSubmit() {
  147. let p = {
  148. queId: app.globalData.selectQuestion._id,
  149. userId: app.globalData.userInfo._openid,
  150. startTime: this.actTime,
  151. istrue: [],
  152. endTime: new Date(),
  153. answer: this.answer
  154. };
  155. p.duration = p.endTime - p.startTime;
  156. let score = 0;
  157. for (let i = 0; i < app.globalData.selectQuestion.list.length; i++) {
  158. let an = this.answer[i].join(",")
  159. const v = app.globalData.selectQuestion.list[i];
  160. let isAdd = true;
  161. v.answer.map(r => {
  162. isAdd = (new RegExp(r.select)).test(an)
  163. })
  164. p.istrue.push(isAdd);
  165. if (v.answer.length === this.answer[i].length && isAdd) score++;
  166. }
  167. p.score = score;
  168. wx.showLoading();
  169. wx.cloud.callFunction({
  170. name: "quickstartFunctions",
  171. data: {
  172. type: "createQuestionRecord",
  173. data: p
  174. }
  175. }).then(res => {
  176. wx.hideLoading()
  177. if(res.result.code !== 0) return wx.showToast({
  178. title: '网络繁忙'
  179. })
  180. wx.reLaunch({
  181. url: '/pages/result/index?data=' + JSON.stringify(res.result.data || {}),
  182. })
  183. }).catch(err => {
  184. console.log(err)
  185. wx.hideLoading()
  186. })
  187. },
  188. /**
  189. * 生命周期函数--监听页面初次渲染完成
  190. */
  191. onReady: function () {
  192. },
  193. /**
  194. * 生命周期函数--监听页面显示
  195. */
  196. onShow: function () {
  197. },
  198. /**
  199. * 生命周期函数--监听页面隐藏
  200. */
  201. onHide: function () {
  202. },
  203. /**
  204. * 生命周期函数--监听页面卸载
  205. */
  206. onUnload: function () {
  207. if (this.interval) clearInterval(this.interval);
  208. console.log()
  209. },
  210. /**
  211. * 页面相关事件处理函数--监听用户下拉动作
  212. */
  213. onPullDownRefresh: function () {
  214. },
  215. /**
  216. * 页面上拉触底事件的处理函数
  217. */
  218. onReachBottom: function () {
  219. },
  220. /**
  221. * 用户点击右上角分享
  222. */
  223. onShareAppMessage: function () {
  224. }
  225. })