zhongshujie
2024-12-19 8aeddcf766df9f7e3061d4d098b57afdf53db896
packageBookService/pages/psychologyAnswer/psychologyAnswer.js
@@ -1,8 +1,5 @@
// packageBookService/pages/psychologyAnswer/psychologyAnswer.js
import {
  getPublicImage
} from "../../../assets/js/middleGround/tool";
import {
  loginInfo
} from '../../../assets/js/login';
const app = getApp();
@@ -12,6 +9,7 @@
   * 页面的初始数据
   */
  data: {
    questuionName: "", //试卷名称
    dataList: "",
    barHeight: "",
    navBarHeight: "",
@@ -31,6 +29,7 @@
    sliderValue: 0, // 字体滑块
    showDialog: false, // 未提交退出拦截弹窗
    showId: '',
    uuid: "",
    isShowDialog: false, // 测试报告弹窗是否显示
  },
@@ -38,21 +37,14 @@
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const indexof = options.listIndex;
    const questuionName = options.listName
    this.setData({
      questuionName: questuionName
    })
    const systInfo = wx.getSystemInfoSync();
    const menu = wx.getMenuButtonBoundingClientRect(); // 胶囊信息
    const navBarHeight =
      (menu.top - systInfo.statusBarHeight) * 2 + menu.height; // 导航栏高度
    this.setData({
      dataList: pastData,
      questionDataList: pastData.postData.list[indexof].list,
      questionDataTitle: pastData.postData.list[indexof].title,
      questionDataOptions: pastData.postData.list[indexof].optionList,
      scoreDataList: pastData.postData.list[indexof].scoreData
    });
    wx.setNavigationBarTitle({
      title: pastData.postData.list[indexof].name,
    })
    const token = wx.getStorageSync(app.config.tokenKey)
    if (!token) {
      loginInfo(app, (data) => {
@@ -290,22 +282,14 @@
  },
  // 切换题目
  changeSwiper(e) {
    let index = e.detail.index - 1 >= 0 ? e.detail.index - 1 : 0;
    const item = this.data.questionDataList[index];
    const lastItem = this.data.questionDataList[index + 1];
    let index = e.detail.index;
    const lastItem = this.data.questionDataList[index];
    this.setData({
      currentIndex: e.detail.index,
      showId: lastItem.id
    });
    if (
      (this.data.answerType == "collectQuestion" ||
        this.data.answerType == "errorQuestion") &&
      !item.isComplete
    ) {
      let flag = this.isHaveAnswer(item.userAnswer);
      if (flag) this.handleQuestion(index);
    }
  },
  // 我的错题,收藏查看解析按钮
  viewParsing() {
    if (
@@ -404,6 +388,12 @@
        radioChange(citem)
      }
    }
    const listLength = this.data.questionDataList.length - 1
    if (this.data.currentIndex < listLength) {
      this.setData({
        currentIndex: this.data.currentIndex + 1
      })
    }
  },
  // 输入框触发
  onChangeInput(e) {
@@ -479,10 +469,6 @@
    // 先遍历所有题目,将未批改的题目批改
    const qustionList = this.data.questionDataList;
    if (qustionList.length) this.handleQuestion();
    this.setData({
      isShowDialog: true,
      submitStatus: true,
    })
    // 记录用户答题数据
    let saveData = [];
    for (let index = 0; index < this.data.questionDataList.length; index++) {
@@ -493,16 +479,59 @@
        isRight: item.isRight,
      });
    }
    app.MG.identity.setUserKey({
      setKeyRequests: [{
        domain: "answerData",
        key: this.data.uuid,
        value: JSON.stringify({
          answerData: saveData,
          scoreData: this.data.scoreDataList
        }),
      }, ],
    });
    this.setData({
      loading: false,
      showDialog: false,
      isShowDialog: true,
      submitStatus: true,
    });
  },
  // 初始化函数
  async init() {
  async init(isReset) {
    this.setData({
      loading: false,
      isShowDialog: false,
      loading: true,
    });
    const questuionName = this.data.questuionName
    const questuionList = pastData.postData.list
    var listIndex = 0
    for (let index = 0; index < questuionList.length; index++) {
      const item = questuionList[index];
      if (item.name == questuionName) {
        listIndex = index
      }
    }
    const indexof = listIndex;
    this.setData({
      dataList: pastData,
      // questionDataList: pastData.postData.list[indexof].list,
      questionDataTitle: pastData.postData.list[indexof].title,
      questionDataOptions: pastData.postData.list[indexof].optionList,
      // scoreDataList: pastData.postData.list[indexof].scoreData,
      uuid: pastData.postData.list[indexof].name
    });
    wx.setNavigationBarTitle({
      title: pastData.postData.list[indexof].name,
    })
    const list = [...pastData.postData.list[indexof].list]
    const scoreList = [...pastData.postData.list[indexof].scoreData]
    if (isReset) {
      list.forEach(item => item.userAnswer = "")
      scoreList.forEach(item => {
        item.dispatch = "",
          item.score = 0
      })
    }
    this.getAnswerInfo(list, scoreList)
  },
  // 获取题库题目
@@ -510,7 +539,6 @@
    // 清空正确题数记录
    this.setData({
      noData: false,
      cardList: [],
      correctNum: 0,
    });
  },
@@ -555,7 +583,9 @@
      const scoreItem = scoreData[cindex];
      if (scoreItem.range == 'all') {
        questionList.forEach((item) => {
          scoreItem.score += item.score
          if (item.score) {
            scoreItem.score += item.score
          }
        })
      } else {
        questionList.forEach((item, index) => {
@@ -624,16 +654,90 @@
      })
      .then((res) => {});
  },
  async restart() {
    this.setData({
      isShowDialog: false,
      currentIndex: 0,
      submitStatus: false,
    });
    await app.MG.identity.setUserKey({
      setKeyRequests: [{
        domain: "answerData",
        key: this.data.uuid,
        value: JSON.stringify({
          answerData: [],
        }),
      }, ],
    });
    this.init(true);
  },
  openIsShowDialog() {
    this.setData({
      isShowDialog: true,
    })
  },
  // 获取旧答题数据
  getAnswerInfo(arr, scoreListArr) {
    let newArr = [...arr];
    let scoreArr = [...scoreListArr];
    app.MG.identity
      .getUserKey({
        domain: "answerData",
        keys: [this.data.uuid],
      })
      .then((res) => {
        try {
          const data = JSON.parse(res[0].value)
          if (data.answerData?.length > 0 && newArr.length) {
            this.setData({
              isShowDialog: true,
              submitStatus: true,
            })
            newArr.forEach(item => {
              const obj = data.answerData.find(citem => citem.id == item.id)
              if (obj?.answer) {
                item.userAnswer = obj.answer
              } else {
                item.userAnswer = ""
              }
            })
          }
          if (data.scoreData?.length > 0 && scoreArr.length) {
            scoreArr.forEach(item => {
              const obj = data.scoreData.find(citem => citem.name == item.name)
              if (obj?.score) {
                item.score = obj.score
              } else {
                item.score = 0
              }
              if (obj?.dispatch) {
                item.dispatch = obj.dispatch
              } else {
                item.dispatch = ""
              }
            })
          }
          this.setData({
            questionDataList: newArr,
            scoreDataList: scoreArr,
            loading: false,
          })
        } catch (error) {
          this.setData({
            questionDataList: newArr,
            scoreDataList: scoreArr,
            loading: false,
          })
          console.log(error);
        }
        const e = {
          detail: {
            index: 0
          }
        };
        this.changeSwiper(e)
      });
  },
  // 记录模考答题数据
  saveMockData() {
@@ -670,7 +774,7 @@
    app.MG.identity
      .setUserKey({
        setKeyRequests: [{
          domain: "mockAnswerData",
          domain: "answerData",
          key: this.data.uuid,
          value: JSON.stringify({
            currentIndex: this.data.currentIndex,