answer.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. let util = require("../../../../utils/util.js")
  2. Page({
  3. data: {
  4. alertStatus: false,//弹窗
  5. answerStatus: false,//答题状态
  6. info: {
  7. choiceOptions: [],
  8. rank:0,
  9. choiceQuestionNum: 0,//题目总数
  10. completeNum: 0,//答题人数
  11. question: '',//问题
  12. answer: "",//答案
  13. myAnswer: '',//选中
  14. radio: 0,//题目类型 0单选 1多选
  15. contentType: "",//内容类型
  16. id: '',//id
  17. explain: '',//答案解答
  18. },
  19. courseId: 0,//课程id
  20. rank: 0,//题目号
  21. playerId:0,//参赛者id
  22. },
  23. /**
  24. * 生命周期函数--监听页面加载
  25. */
  26. onLoad: function (options) {
  27. this.setData({ rank: options.rank, courseId: options.courseId, playerId: options.playerId})
  28. this.questionDetail();
  29. },
  30. close() {
  31. this.setData({ answerStatus: true })
  32. this.initAnswer();
  33. },
  34. changeQuestion(e){
  35. let rank = e.currentTarget.dataset.rank;
  36. if (rank < 1 || rank > this.data.info.choiceQuestionNum) return false;
  37. this.setData({ rank });
  38. this.questionDetail();
  39. },
  40. complete(){
  41. wx.navigateBack({
  42. delta: 1
  43. })
  44. },
  45. // 确认答案
  46. submitAnswer() {
  47. let that = this;
  48. util.ajax({
  49. func:'v2/course/question/confirm',
  50. data: { id: that.data.info.id, answer: that.data.info.myAnswer, playerId:that.data.playerId },
  51. method:'POST',
  52. contentType:'application/x-www-form-urlencoded'
  53. },function(res){
  54. if(res.code == 0){
  55. let alert = {};
  56. if (res.data.result == 1){//正确
  57. if (res.data.status==1){//有效
  58. alert= {icon:'answer-ok',title:'恭喜您,答对了',score:res.data.score}
  59. }else{
  60. alert = { icon: 'time-out', title: '恭喜您,答对了', content: res.data.message,status:1 }
  61. }
  62. }else{//错误
  63. alert={icon: 'answer-error',title:'很抱歉,答错了',score:res.data.score}
  64. }
  65. that.setData({
  66. alertStatus: true,
  67. alert: alert
  68. });
  69. }else
  70. util.showTips(res.reason);
  71. });
  72. },
  73. //显示图标
  74. initAnswer() {
  75. let items = this.data.info.choiceOptions, answer = this.data.info.answer,myAnswer=this.data.info.myAnswer;
  76. items.forEach((item, index) => {
  77. if (answer.toString().indexOf(item.option) != -1) {//需要显示的ok
  78. items[index].icon = "icon-mini-ok"
  79. } else if (myAnswer.toString().indexOf(item.option) != -1) {
  80. items[index].icon = "icon-mini-error"
  81. } else {
  82. items[index].icon = "";
  83. }
  84. })
  85. this.setData({info:this.data.info})
  86. },
  87. // checked发生改变的时候
  88. setAnswerChecked(e){
  89. let data = e.detail.value;
  90. if (typeof (data) == 'string'){
  91. this.data.info.myAnswer = data;
  92. }else{
  93. this.data.info.myAnswer = data.join(",");
  94. }
  95. this.setData({ info: this.data.info });
  96. this.initAnswerChecked();
  97. },
  98. //初始化checked
  99. initAnswerChecked() {
  100. let items = this.data.info.choiceOptions, answer = this.data.info.myAnswer;
  101. items.forEach((item, index) => {
  102. if (answer.indexOf(item.option) != -1) {
  103. items[index].checked = true;
  104. } else {
  105. items[index].checked = false;
  106. }
  107. });
  108. this.setData({ info: this.data.info });
  109. },
  110. // 获取题目详情
  111. questionDetail() {
  112. let that = this;
  113. util.ajax({
  114. func: "v2/course/question/detail",
  115. data: { courseId: that.data.courseId, playerId: that.data.playerId, rank: that.data.rank }
  116. }, function (res) {
  117. if (res.code == 0) {
  118. let data = res.data;
  119. if(data==null){
  120. wx.navigateBack({
  121. delta:1
  122. })
  123. }
  124. that.setData({ info: data, answerStatus: data.myAnswer==null?0: data.myAnswer.length });
  125. if (data.myAnswer!=null){
  126. if (data.myAnswer.length > 0){
  127. that.initAnswerChecked();
  128. that.initAnswer();
  129. }
  130. }
  131. } else {
  132. util.showTips(res.reason);
  133. }
  134. });
  135. }
  136. })