quiz.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. Page({
  2. data: {
  3. question: {}, // 当前题目
  4. options: [], // 格式化后的选项
  5. selectedIndex: null, // 用户选中的选项索引
  6. score: 0, // 用户得分
  7. showResult: false, // 是否显示结果弹窗
  8. isCorrect: false, // 答案是否正确
  9. resultText: "", // 结果提示文本
  10. explanation: "", // 答案解析
  11. loading: false // 加载状态
  12. },
  13. onLoad() {
  14. this.loadQuestion();
  15. },
  16. // 从API加载随机题目
  17. loadQuestion() {
  18. this.setData({ loading: true });
  19. wx.request({
  20. url: 'http://127.0.0.1:8000/api/questions/random/',
  21. method: 'GET',
  22. success: (res) => {
  23. if (res.statusCode === 200) {
  24. this.processQuestionData(res.data);
  25. } else {
  26. this.handleError('题目加载失败');
  27. }
  28. },
  29. fail: (err) => {
  30. this.handleError('网络请求失败');
  31. this.useLocalData(); // 失败时使用本地测试数据
  32. },
  33. complete: () => {
  34. this.setData({ loading: false });
  35. }
  36. });
  37. },
  38. // 处理API返回的题目数据
  39. processQuestionData(apiData) {
  40. // 确保数据完整性
  41. if (!apiData.choices || apiData.choices.length === 0) {
  42. this.handleError('题目数据不完整');
  43. return this.useLocalData();
  44. }
  45. this.setData({
  46. question: apiData,
  47. explanation: apiData.explanation || "暂无解析"
  48. });
  49. // 格式化选项为A/B/C/D
  50. const options = apiData.choices.map((choice, index) => ({
  51. id: choice.id,
  52. key: String.fromCharCode(65 + index), // A, B, C...
  53. value: choice.text,
  54. isCorrect: choice.is_correct
  55. }));
  56. this.setData({
  57. options,
  58. selectedIndex: null,
  59. showResult: false
  60. });
  61. },
  62. // 用户选择选项
  63. selectOption(e) {
  64. this.setData({
  65. selectedIndex: e.currentTarget.dataset.index
  66. });
  67. },
  68. // 提交答案
  69. submitAnswer() {
  70. if (this.data.selectedIndex === null) {
  71. wx.showToast({ title: '请先选择答案', icon: 'none' });
  72. return;
  73. }
  74. const selectedOption = this.data.options[this.data.selectedIndex];
  75. const isCorrect = selectedOption.isCorrect;
  76. const scoreChange = isCorrect ? 2 : -1;
  77. this.setData({
  78. showResult: true,
  79. isCorrect,
  80. score: Math.max(0, this.data.score + scoreChange),
  81. resultText: isCorrect ?
  82. '回答正确!+2分' :
  83. `回答错误!正确答案:${this.getCorrectAnswer()}`
  84. });
  85. },
  86. // 获取正确答案文本
  87. getCorrectAnswer() {
  88. const correctOption = this.data.options.find(opt => opt.isCorrect);
  89. return correctOption ? `${correctOption.key}. ${correctOption.value}` : '';
  90. },
  91. // 下一题
  92. nextQuestion() {
  93. this.loadQuestion();
  94. },
  95. // 错误处理
  96. handleError(msg) {
  97. console.error(msg);
  98. wx.showToast({ title: msg, icon: 'none' });
  99. },
  100. onShow(){
  101. this.recordFeatureUsage('quiz-main');
  102. },
  103. recordFeatureUsage(featureName) {
  104. const app = getApp();
  105. console.log('发送的Token:', app.globalData.userInfo.token);
  106. console.log('发送的featureName:', featureName);
  107. wx.request({
  108. url: 'http://127.0.0.1:8000/api/record-usage/',
  109. method: 'POST',
  110. header: {
  111. 'Authorization': `Token ${app.globalData.userInfo.token}`,
  112. 'Content-Type': 'application/json' // 确保这个请求头存在
  113. },
  114. data: JSON.stringify({ // 关键修改:必须使用JSON.stringify
  115. feature_name: featureName // 参数名必须与后端一致
  116. }),
  117. success: (res) => {
  118. console.log('行为记录响应:', res.data);
  119. },
  120. fail: (err) => {
  121. console.error('请求失败:', err);
  122. }
  123. });
  124. },
  125. })