litian
2025-03-27 c03853bf7e9594bcbef3cfe087b313d18aacb4bb
packageBookService/pages/bookServices/examination/examination.js
@@ -55,6 +55,9 @@
    showDialog: false, // 未提交退出拦截弹窗
    showId: '',
    isShowDialog: false, // 测试报告弹窗是否显示
    joinGroup: false, // 是否加入班级
    visible: false, // 加入班级弹窗是否打开
    classList: [] // 班级列表
  },
  /**
@@ -88,7 +91,6 @@
    }
    const token = wx.getStorageSync(app.config.tokenKey)
    if (!token) {
      loginInfo(app, (data) => {
        if (data) {
@@ -100,7 +102,6 @@
    } else {
      this.init()
    }
  },
  /**
@@ -554,7 +555,8 @@
    if (
      this.data.answerType == "option" ||
      this.data.answerType == "errorQuestion" ||
      this.data.answerType == "mock"
      this.data.answerType == "mock" ||
      this.data.answerType == 'interaction'
    ) {
      // 先遍历所有题目,将未批改的题目批改
      const qustionList = this.data.questionDataList;
@@ -617,14 +619,51 @@
      this.setData({
        isShowDialog: true
      })
    } else if (this.data.answerType == 'interaction') {
      this.toggleCountdown();
      this.handleClassQuestion()
    }
    this.setData({
      loading: false,
      showDialog: false,
    });
  },
  // 初始化函数
  async init() {
    // 获取图书详情,判断题库是否购买
    const query = {
      path: "*",
      queryType: "*",
      productId: this.data.bookId,
      favoriteTypes: "FavoriteBookCity",
      itemFields: {
        "SysType=": "CmsFolder",
      },
      coverSize: {
        height: 300,
      },
      fields: {},
    };
    const res = await app.MG.store.getProductDetail(query)
    const saleData = res.datas.saleMethod.find(item => item.Name.includes('题库'))
    if (saleData && !res.datas.purchasedSaleMethodIdList.includes(saleData.Id)) return wx.showModal({
      title: '提示',
      content: '请先购买题库再答题',
      confirmColor: "#ff6c00",
      cancelColor: "#949494",
      editable: false, //是否显示输入框
      showCancel: false,
      complete: () => {
        this.setData({
          submitStatus: true
        })
        wx.setStorageSync('tabValue', 'questionBank')
        wx.redirectTo({
          url: `/packageBookService/pages/bookServices/detail/index?id=${res.datas.id}&name=${res.datas.name}`,
        })
      }
    })
    this.setData({
      loading: true,
      subjectiveTotal: 0,
@@ -639,6 +678,13 @@
      // 测试答题
      await this.getCollectIdList(); // 获取收藏id列表
      await this.getErrorList(); // 获取错题id列表
    } else if (this.data.answerType == "interaction") {
      this.setData({
        countdownTime: 2 * 60 * 60 * 1000,
      });
      this.getCurrentClassList()
      this.getinteractionInfo()
      // 教学互动
    } else if (this.data.answerType == "collectQuestion") {
      this.setData({
        submitStatus: true,
@@ -747,6 +793,14 @@
        }, ],
      });
      this.init();
    } else if (this.data.answerType == 'interaction') {
      this.setData({
        countdownTime: 2 * 60 * 60 * 1000,
      });
      this.delinteractionInfo(() => {
        this.getQuestionList()
        this.clearTime();
      })
    } else {
      this.init();
      this.clearTime();
@@ -846,7 +900,6 @@
      let query = {
        path: "*",
        queryType: "*",
        productId: this.data.bookId,
        cmsPath: pathitem.productLinkPath,
        itemFields: {
@@ -880,9 +933,6 @@
          oldList = oldData.find(
            (item) => item.path == pathitem.productLinkPath
          ).infoList;
          this.setData({
            submitStatus: true,
          });
        }
        res.datas.cmsDatas[0].datas.forEach((item, index) => {
          if (this.data.storeInfo || this.data.jslx) {
@@ -901,14 +951,15 @@
          let oldObj = "";
          if (oldList)
            oldObj = oldList.find((oldItem) => oldItem.id == item.id);
          console.log(item.Embedded_QuestionBank_Stem, 111)
          let questionObj = {
            // num: index, // 题号
            id: item.id,
            type: pathitem.name,
            stem: item.Embedded_QuestionBank_QuestionType == "completion" ?
            stem: item.Embedded_QuestionBank_Stem ? item.Embedded_QuestionBank_QuestionType == "completion" ?
              JSON.parse(item.Embedded_QuestionBank_Stem)
              .stemTxt.replaceAll("<vacancy>", ",input,")
              .split(",") : JSON.parse(item.Embedded_QuestionBank_Stem), // 题干
              .split(",") : JSON.parse(item.Embedded_QuestionBank_Stem) : '-', // 题干
            answer: item.Embedded_QuestionBank_Answer, // 答案
            option: item.Embedded_QuestionBank_Option ?
              JSON.parse(item.Embedded_QuestionBank_Option) : "", // 选择题选项
@@ -1070,24 +1121,22 @@
            questionDataList: questionList,
            cardList: cardUpdatedList,
          });
          console.log(
            '题目', this.data.questionDataList
          );
        });
      });
    }
    // 有题目再开始倒计时
    if (this.data.questionDataList.length) {
      this.startCountdown();
      this.setData({
        loading: false,
        showId: this.data.questionDataList[0].id
      })
    } else {
      this.setData({
        noData: true
        noData: true,
        loading: false,
      })
    }
    this.setData({
      loading: false,
      showId: this.data.questionDataList[0].id
    });
  },
  // 批改题目 (练习,我的错题,我的收藏,,组卷)
  handleQuestion(index) {
@@ -2232,7 +2281,6 @@
  // 富文本处理
  formatRichText(html) {
    console.log(html);
    let newContent = html.replace(/<img[^>]*>/gi, function (match, capture) {
      match = match
        .replace(/style="[^"]+"/gi, "")
@@ -2266,4 +2314,250 @@
    );
    return newContent;
  },
});
  // 教学互动模式相关
  // 获取班级列表
  getCurrentClassList() {
    const query = {
      start: 0,
      size: 999,
      infoTypes: [],
    }
    app.MG.identity.joinedGroupByList(query).then((res) => {
      res.datas.forEach(async item => {
        item.topicId = null
        const data = await this.getTopicInfo(item.id)
        if (data) {
          item.topicId = data.id
        }
      })
      console.log('班级', res)
      if (res.datas.length) {
        this.setData({
          classList: res.datas
        })
        // 判断班级 是否所属于该书
        const classData = res.datas.find(item => item.linkProductDto.product.id == this.data.bookId)
        if (classData) {
          if (classData.userState == 'Normal') {
            // 加入班级
            this.setData({
              joinGroup: true
            })
          } else if (classData.userState == 'WaitValid') {
            this.setData({
              submitStatus: true
            })
            // 审核中
            wx.showModal({
              title: '提示',
              content: '请等待老师审核加入班级后答题',
              confirmColor: "#ff6c00",
              complete: (res) => {
                if (res.cancel) {
                  wx.navigateBack()
                }
                if (res.confirm) {
                  wx.navigateBack()
                }
              }
            })
          } else {
            // 拒绝
            this.setData({
              visible: true
            })
          }
        } else {
          // 未加入班级
          this.setData({
            visible: true
          })
        }
      } else {
        this.setData({
          visible: true,
        })
      }
    })
  },
  // 加入班级
  joinClass(e) {
    wx.showLoading({
      title: '加载中'
    })
    const data = {
      refCode: e.detail.value
    }
    app.MG.identity.joinGroupByRefCode(data).then((res) => {
      wx.hideLoading()
      if (res == '组不存在') {
        wx.showToast({
          icon: 'error',
          title: '无效的班级',
        })
      } else if (res == '组成员数量已最大,不能加入') {
        wx.showToast({
          icon: 'error',
          title: '班级成员数量已最大,不能加入',
        })
      } else if (res == '已经申请过加入此组') {
        wx.showToast({
          icon: 'error',
          title: '已经申请过加入此班级',
        })
      } else {
        wx.showToast({
          icon: "success",
          title: '已成功,等待审核中...',
        })
        this.setData({
          visible: false
        })
        this.getCurrentClassList()
      }
    })
  },
  // 获取班级topic
  async getTopicInfo(classId) {
    let query = {
      classId,
      refCodes: ["TeachInteraction"]
    }
    const res = await app.MG.edu.getClassTopic(query)
    return res[0] ?? null
  },
  // 获取旧教学互动答题数据
  getinteractionInfo() {
    app.MG.identity
      .getUserKey({
        domain: "interactionData",
        keys: [this.data.productLinkPath],
      })
      .then((res) => {
        if (res.length) {
          let value = JSON.parse(res[0].value);
          const scoreData = value.dataList.find(item => item.name == "客观题得分" && item.path == this.data.productLinkPath)
          this.setData({
            currentIndex: value.currentIndex ? value.currentIndex : 0,
            subjectiveNum: scoreData ? scoreData.score : 0,
            submitStatus: true
          });
          // 携带答题记录 获取题目
          this.getQuestionList(value.dataList);
        } else {
          this.getQuestionList(); // 获取题库题目
          this.setData({
            showDialog: true
          })
        }
      });
  },
  // 提交教学互动答题数据
  setinteractionInfo() {
    const list = []
    this.data.cardList.forEach(item => {
      list.push({
        catalogName: item.catalogName,
        path: item.path,
        infoList: item.infoList.map((citem) => ({
          id: citem.id,
          userAnswer: citem.userAnswer
        }))
      })
    })
    list.push({
      name: "客观题得分",
      score: this.data.subjectiveNum,
      path: this.data.productLinkPath,
    })
    app.MG.identity
      .setUserKey({
        setKeyRequests: [{
          domain: "interactionData",
          key: this.data.productLinkPath,
          value: JSON.stringify({
            currentIndex: this.data.currentIndex,
            dataList: list
          }),
        }, ],
      })
      .then((res) => {});
  },
  // 处理教学互动答题
  handleClassQuestion() {
    const flag = this.data.questionDataList.some(item => !item.userAnswer)
    if (flag) {
      // 没做完,保存即可
      const isAnswer = this.data.questionDataList.some(item => item.userAnswer)
      if (isAnswer) this.setinteractionInfo()
    } else {
      // 做完了且加入了班级,提交到message
      if (this.data.classList.length && this.data.joinGroup) {
        this.data.classList.forEach(item => {
          if (item.linkProductDto.product.id == this.data.bookId && item.topicId)
            this.newTopicMessage(item.topicId)
        })
        this.setinteractionInfo()
      } else {
        // 做完了,没加入班级
        this.setinteractionInfo()
      }
      this.setData({
        isShowDialog: true
      })
    }
    this.setData({
      submitStatus: true
    })
  },
  // 新建topicMessage
  newTopicMessage(topicId) {
    let content = []
    for (let cindex = 0; cindex < this.data.questionDataList.length; cindex++) {
      const citem = this.data.questionDataList[cindex];
      content.push({
        cmsItemId: citem.id,
        answer: citem.userAnswer,
        state: citem.questionType == 'shortAnswer' ? 'none' : citem.isRight,
        type: citem.questionType
      })
      const userInfo = JSON.parse(wx.getStorageSync(app.config.userInfoKey))
      const data = {
        description: '',
        icon: '',
        state: 'Normal',
        topicIdOrRefCode: String(topicId),
        name: this.data.answerTitle,
        content: JSON.stringify({
          bookId: this.data.bookId,
          userName: userInfo.name ? userInfo.name : userInfo.nickName ? userInfo.nickName : '-',
          path: this.data.productLinkPath,
          content
        }),
        type: 'Public',
        cmsTypeRefCode: '',
        newDataListRequest: []
      }
      app.MG.ugc.newTopicMessage(data).then((res) => {
        if (res) {
          this.setData({
            isShowDialog: true
          })
        }
      })
    }
  },
  // 删除教学互动答题记录
  delinteractionInfo(callback) {
    app.MG.identity
      .delUserKey({
        domain: 'interactionData',
        keys: [this.data.productLinkPath]
      })
      .then((res) => {
        if (callback) callback()
      })
  }
})