闫增涛
2024-10-12 e81c27ae4f00a107b1bf67062c9f008686a8e19f
src/books/mathBook/view/components/index.vue
@@ -3,46 +3,47 @@
    <div id="searchDomBox" style="display: none">
      <div id="searchContent"></div>
    </div>
    <div
      class="page-content"
      :style="{
        fontSize: fontSize ? fontSize + 'px' : '16px',
        transform: `scale(${pageZoom ? pageZoom : 1})`,
        transformOrigin: 'center top',
      }"
    >
      <pageHeader
        v-if="showCatalogList.indexOf(1) > -1"
        :showPageList="loadPageList"
      ></pageHeader>
      <chapterOne
        v-if="showCatalogList.indexOf(2) > -1"
        :showPageList="loadPageList"
      ></chapterOne>
    </div>
    <div class="page-content" :style="{
      fontSize: fontSize ? fontSize + 'px' : '16px',
      transform: `scale(${pageZoom ? pageZoom : 1})`,
      transformOrigin: 'center top',
    }">
      <pageHeader v-if="showCatalogList.indexOf(0) > -1" :showPageList="loadPageList"></pageHeader>
      <chapterOne v-if="showCatalogList.indexOf(1) > -1" :showPageList="loadPageList" :questionData="questionDataMap">
      </chapterOne>
      <chapterTwo v-if="showCatalogList.indexOf(3) > -1" :showPageList="loadPageList" :questionData="questionDataMap">
      </chapterTwo>
      <chapterThree v-if="showCatalogList.indexOf(4) > -1" :showPageList="loadPageList" :questionData="questionDataMap">
      </chapterThree>
      <chapterFour v-if="showCatalogList.indexOf(5) > -1" :showPageList="loadPageList" :questionData="questionDataMap">
      </chapterFour>
      <chapterFive v-if="showCatalogList.indexOf(6) > -1" :showPageList="loadPageList" :questionData="questionDataMap">
      </chapterFive>
    </div>
  </div>
</template>
<script>
import axios from "axios";
import pageHeader from "./header.vue";
import chapterOne from "./chapter001.vue";
// import chapterTwo from "./chapter002.vue";
// import chapterThree from "./chapter003.vue"
// import chapterFour from './chapter004.vue'
// import chapterFive from './chapter005.vue'
// import chapterSix from './chapter006.vue'
import chapterTwo from "./chapter002.vue";
import chapterThree from "./chapter003.vue";
import chapterFour from './chapter004.vue'
import chapterFive from './chapter005.vue'
import NoteIcon from "@/assets/images/biji.png";
import _ from "lodash";
import Swiper from "swiper/bundle";
import "swiper/swiper-bundle.css";
import Viewer from "viewerjs";
import "viewerjs/dist/viewer.css";
import getQuestionList from "@/assets/methods/examination";
export default {
  name:"pageContent",
  name: "pageContent",
  data() {
    return {
      catalogLength: 2, // 总章节数
      catalogLength: 6, // 总章节数
      showCatalogList: [], // 显示的章节
      loadThreshold: 300, // 触发加载阈值
      throttleThreshold: 100, // 节流阈值
@@ -54,6 +55,8 @@
      questionDataMap: {},
      renderSignMap: {},
      highlightData: null,
      questionId: {},
      collectId: [],
    };
  },
  computed: {
@@ -102,7 +105,7 @@
  },
  mounted() {
    // 默认加载章节
    this.showCatalogList = [1];
    this.showCatalogList = [0];
    // 滚动监听节流
    this.throttledScrollHandler = _.throttle(
      this.scrollFun,
@@ -158,36 +161,40 @@
    }, 500);
    // 测试页面跳转
    // setTimeout(() => {
    //   this.gotoPage(1, 10);
    //   setTimeout(() => {
    //     this.renderSign("Highlight", {
    //       id: "2ACA9359",
    //       txt: "题一学习主题一 运动",
    //       page: "10",
    //       type: "Highlight",
    //       color: "#F5E12A"
    //     });
    // setTimeout(() => {
    //   this.delSign({
    //     ids: ["2ACA9359"]
    //   });
    // }, 2000);
    //   }, 5000);
    setTimeout(() => {
      this.gotoPage(3, 96);
    //   //   setTimeout(() => {
    //   //     this.renderSign("Highlight", {
    //   //       id: "2ACA9359",
    //   //       txt: "题一学习主题一 运动",
    //   //       page: "10",
    //   //       type: "Highlight",
    //   //       color: "#F5E12A"
    //   //     });
    //   // setTimeout(() => {
    //   //   this.delSign({
    //   //     ids: ["2ACA9359"]
    //   //   });
    //   // }, 2000);
    //   //   }, 5000);
    // const pageDom = (this.container ? this.container : document)
    //   .querySelector("#app")
    //   .querySelectorAll(".page-box");
    // 检索
    // console.log(this.searchTextByPage("保护内脏器官"), "searchTextByPage");
    // 检索跳转
    // this.searchItemLocation({
    //   catalog: 2,
    //   page: 10,
    //   txt: " 运动系统是由骨、骨连结和骨骼肌三部分组成的。全身的骨通过骨连结组成人体骨骼(见图1-1)。骨骼是人体的支架,具有保护内脏器官、供肌肉附着和作为肌肉运动的杠杆等作用。在神经系统的支配下,肌肉收缩牵动所附着的骨绕着关节转动,使身体产生各种动作。所以,运动系统具有运动、支持和保护等功能,幼年时期的骨骼还具有造血功能。 ",
    //   txtIndex: 57
    // });
    // }, 5000);
    //   // const pageDom = (this.container ? this.container : document)
    //   //   .querySelector("#app")
    //   //   .querySelectorAll(".page-box");
    //   // 检索
    //   // console.log(this.searchTextByPage("保护内脏器官"), "searchTextByPage");
    //   // 检索跳转
    //   // this.searchItemLocation({
    //   //   catalog: 2,
    //   //   page: 10,
    //   //   txt: " 运动系统是由骨、骨连结和骨骼肌三部分组成的。全身的骨通过骨连结组成人体骨骼(见图1-1)。骨骼是人体的支架,具有保护内脏器官、供肌肉附着和作为肌肉运动的杠杆等作用。在神经系统的支配下,肌肉收缩牵动所附着的骨绕着关节转动,使身体产生各种动作。所以,运动系统具有运动、支持和保护等功能,幼年时期的骨骼还具有造血功能。 ",
    //   //   txtIndex: 57
    //   // });
    }, 500);
    // 获取题目id列表
    this.getQuestionId();
    this.getCollect()
  },
  methods: {
    // setZoom1() {
@@ -206,6 +213,9 @@
    // },
    // 滚动监听
    scrollFun(event) {
      this.handleVideoPicture()
      // 判断向上滚动还是向下滚动
      if (event.target.scrollTop > this.previousScrollTop) {
        // 向下
@@ -260,6 +270,7 @@
            this.catalogLength - 2,
            this.catalogLength - 1,
            this.catalogLength,
          ];
        } else {
          this.showCatalogList = [catalog - 1, catalog, catalog + 1];
@@ -309,7 +320,10 @@
          this.container ? this.container : document
        ).querySelector(`[page="${data.page}"]`);
        // 创建 createTreeWalker 迭代器,用于遍历文本节点,保存到一个数组
        const treeWalker = document.createTreeWalker(pageDom, NodeFilter.SHOW_TEXT);
        const treeWalker = document.createTreeWalker(
          pageDom,
          NodeFilter.SHOW_TEXT
        );
        const allTextNodes = [];
        let currentNode = treeWalker.nextNode();
        while (currentNode) {
@@ -441,8 +455,8 @@
          page && pageThemeColor && pageThemeColor[page]
            ? pageThemeColor[page]
            : chapterNum && chapterThemeColor && chapterThemeColor[chapterNum]
            ? chapterThemeColor[chapterNum]
            : bookThemeColor;
              ? chapterThemeColor[chapterNum]
              : bookThemeColor;
        if (themeColor) {
          domItem.style.color = themeColor;
        }
@@ -459,8 +473,8 @@
          page && pageThemeColor && pageThemeColor[page]
            ? pageThemeColor[page]
            : chapterNum && chapterThemeColor && chapterThemeColor[chapterNum]
            ? chapterThemeColor[chapterNum]
            : bookThemeColor;
              ? chapterThemeColor[chapterNum]
              : bookThemeColor;
        if (themeColor) {
          domItem.style.backgroundColor = themeColor;
        }
@@ -477,8 +491,8 @@
          page && pageThemeColor && pageThemeColor[page]
            ? pageThemeColor[page]
            : chapterNum && chapterThemeColor && chapterThemeColor[chapterNum]
            ? chapterThemeColor[chapterNum]
            : bookThemeColor;
              ? chapterThemeColor[chapterNum]
              : bookThemeColor;
        if (themeColor) {
          domItem.style.borderColor = themeColor;
        }
@@ -537,30 +551,18 @@
            // 添加页码
            this.loadPageList.push(Number(page));
            const catalog = catalogDom.getAttribute("num");
            // if (!this.questionDataMap[page]) {
            //   if (testData && testData[catalog]) {
            //     if (testData[catalog][page]) {
            //       if (Array.isArray(testData[catalog][page])) {
            //         this.questionDataMap[page] = await getQuestionList(
            //           page,
            //           testData[catalog][page],
            //           this.config.activeBook
            //         );
            //       } else {
            //         const obj = {};
            //         for (let key in testData[catalog][page]) {
            //           obj[key] = await getQuestionList(
            //             [],
            //             testData[catalog][page][key],
            //             this.config.activeBook
            //           );
            //         }
            //         this.questionDataMap[page] = obj;
            //       }
            //       console.log("题目", this.questionDataMap);
            //     }
            //   }
            // }
            if (!this.questionDataMap[page]) {
              if (this.questionId && this.questionId[catalog]) {
                if (this.questionId[catalog][page]) {
                  this.questionDataMap[page] = await this.getQuestion(catalog, page);
                  console.log("题目", this.questionDataMap);
                }
              }
            }
            // 渲染这一页的标记
            for (const key in this.renderSignMap) {
              if (this.renderSignMap[key][page]) {
@@ -574,7 +576,10 @@
              // 高亮行
              setTimeout(() => {
                // 获取页面所有text节点
                const pageTextList = document.createTreeWalker(target, NodeFilter.SHOW_TEXT);
                const pageTextList = document.createTreeWalker(
                  target,
                  NodeFilter.SHOW_TEXT
                );
                // 匹配关键字
                const allPageTextNodes = [];
                let currentNode = pageTextList.nextNode();
@@ -618,23 +623,19 @@
          autoplay: {
            //自动开始
            delay: 3000, //时间间隔
            disableOnInteraction: false //*手动操作轮播图后不会暂停*
            disableOnInteraction: false, //*手动操作轮播图后不会暂停*
          },
          paginationClickable: true,
          slidesPerView: 1, // 一组三个
          spaceBetween: 30, // 间隔
          // 如果需要前进后退按钮
          navigation: {
            nextEl: dom.querySelector(
              ".swiper-button-next"
            ),
            prevEl: dom.querySelector(
              ".swiper-button-prev"
            )
            nextEl: dom.querySelector(".swiper-button-next"),
            prevEl: dom.querySelector(".swiper-button-prev"),
          },
          // 窗口变化,重新init,针对F11全屏和放大缩小,必须加
          observer: true,
          observeParents: true
          observeParents: true,
          // // 如果需要分页器
          // pagination: {
          //   el: (this.container ? this.container : document).querySelector(
@@ -657,12 +658,8 @@
          spaceBetween: 30, // 间隔
          // 如果需要前进后退按钮
          navigation: {
            nextEl: dom.querySelector(
              ".swiper-button-next"
            ),
            prevEl: dom.querySelector(
              ".swiper-button-prev"
            )
            nextEl: dom.querySelector(".swiper-button-next"),
            prevEl: dom.querySelector(".swiper-button-prev"),
          },
          // 窗口变化,重新init,针对F11全屏和放大缩小,必须加
          observer: true,
@@ -681,8 +678,8 @@
              var paginationInfoEl = dom.querySelector(".pageBox");
              if (paginationInfoEl)
                paginationInfoEl.textContent = currentPage + "/" + totalPages;
            }
          }
            },
          },
        });
      }
    },
@@ -710,11 +707,10 @@
      const pageData = {
        pageHeader,
        chapterOne,
        // chapterTwo,
        // chapterThree,
        // chapterFour,
        // chapterFive,
        // chapterSix,
        chapterTwo,
        chapterThree,
        chapterFour,
        chapterFive,
      };
      // 遍历所有章节文件
      for (const key in pageData) {
@@ -726,7 +722,7 @@
          propsData: {
            showPageList: [],
            questionData: {},
            isSearch: true
            isSearch: true,
          },
        });
        pageExample.$mount(
@@ -758,7 +754,7 @@
              propsData: {
                showPageList: [pageNum],
                questionData: {},
                isSearch: true
                isSearch: true,
              },
            });
            pageExample.$mount(
@@ -772,7 +768,10 @@
              .querySelector(`[page="${pageNum}"]`);
            if (thisPageDom) {
              // 获取页面所有text节点
              const pageTextList = document.createTreeWalker(thisPageDom, NodeFilter.SHOW_TEXT);
              const pageTextList = document.createTreeWalker(
                thisPageDom,
                NodeFilter.SHOW_TEXT
              );
              // 匹配关键字
              const allPageTextNodes = [];
              let currentNode = pageTextList.nextNode();
@@ -811,17 +810,131 @@
      // 记录高亮信息
      this.highlightData = data;
      // 跳转
      this.gotoPage(data.catalog, data.page, () => {});
      this.gotoPage(data.catalog, data.page, () => { });
    },
    // 获取题目列表
    getQuestionId() {
      axios
        .get(this.config.activeBook.resourceUrl + "/question.json")
        .then((res) => {
          this.questionId = res.data.data;
        });
    },
    // 获取题目收藏id列表
    getCollect() {
      if(!localStorage.getItem(this.config.tokenKey)) return false
      this.MG.identity
        .getUserKey({
          domain: "collectData",
          keys: [this.config.activeBook.bookId],
        })
        .then((res) => {
          try {
            const collect = JSON.parse(res[0].value);
            if (collect.length) {
              this.collectId = collect.find(item => item.type == 'json').collectList
            }
          } catch (error) {
            console.log("暂无数据");
          }
        }).catch(res => {
          console.log('index 请求题目收藏id报错');
        })
    },
    // 获取章节题目
    async getQuestion(num, page) {
      let cardList = [
        {
          catalogName: "单选题",
          infoList: [],
        },
        {
          catalogName: "判断题",
          infoList: [],
        },
        {
          catalogName: "多选题",
          infoList: [],
        },
        {
          catalogName: "填空题",
          infoList: [],
        },
        {
          catalogName: "简答题",
          infoList: [],
        },
      ];
      if (this.questionId) {
        if (this.questionId[num] && this.questionId[num][page]) {
          const res = await axios.get(
            this.config.activeBook.resourceUrl + "/question-" + num + ".json"
          );
          console.log(1, res);
          if (!res.data) return [];
          for (let index = 0; index < res.data.data.length; index++) {
            const item = res.data.data[index];
            // 数学公式加类名去修改样式
            if (item.type && item.type == 'material') {
              if (!item.infoList.length) return false
              item.infoList.forEach(citem => {
                if (citem.answer) citem.answer = citem.answer.replace(/\<math/gi, '<math class="examination-math"')
              })
            } else {
              if (item.answer) item.answer = item.answer.replace(/\<math/gi, '<math class="examination-math"')
            }
            item.isCollect = this.collectId.indexOf(item.id) > -1 ? true : false
            if (this.questionId[num][page].indexOf(item.id) > -1) {
              if (item.type && item.type == "material") {
                cardList.push(item);
              } else {
                if (item.questionType == "judge") {
                  cardList[1].infoList.push(item);
                } else if (item.questionType == "singleChoice") {
                  cardList[0].infoList.push(item);
                } else if (item.questionType == "multipleChoice") {
                  cardList[2].infoList.push(item);
                } else if (item.questionType == "completion") {
                  cardList[3].infoList.push(item);
                } else if (item.questionType == "shortAnswer") {
                  cardList[4].infoList.push(item);
                }
              }
            }
          }
          return cardList.filter((item) => item.infoList.length > 0);
        }
      } else {
        return [];
      }
    },
    // 视频小窗
    handleVideoPicture() {
      let doms = (
        this.container ? this.container : document
      ).querySelectorAll(".video");
      doms = Array.from(doms)
      if (!doms.length) return false
      const playVudio = doms
        .reverse()
        .find((item) => item.paused == false);
      if (playVudio) {
        const bottomGap = playVudio.getBoundingClientRect().bottom;
        const topGap = playVudio.getBoundingClientRect().top;
        if (bottomGap < 0 || topGap > window.innerHeight) {
          playVudio.requestPictureInPicture();
        }
      }
    }
  },
  components: {
    pageHeader,
    chapterOne,
    // chapterTwo,
    // chapterThree,
    // chapterFour,
    // chapterFive,
    // chapterSix,
    chapterTwo,
    chapterThree,
    chapterFour,
    chapterFive,
  },
};
</script>
@@ -831,6 +944,7 @@
  width: 100%;
  height: 100%;
  overflow: auto;
  .page-content {
    max-width: 816px;
    min-width: 375px;