| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402 |
- <template>
- <view class="ezy-xuexi-page">
- <view class="icon-title-navBar-box">
- <text class="nav-bar-title">学习</text>
- </view>
- <view v-if="existData" class="ezy-page-body xuexi-page-body">
- <scroll-view scroll-y>
- <view class="xxjl-card-box-padding">
- <view class="xxjl-card-box">
- <!-- 显示内容 -->
- <view class="card-body-box">
- <img :src="banbenInfo.cover" />
- <view class="body-right">
- <view class="right-name">{{ banbenInfo.chanpinName }}</view>
- <view>等级:{{banbenInfo.dengjiName}}</view>
- <view>版本:{{banbenInfo.name}}</view>
- <view>单元:{{banbenInfo.curDanyuanName}}</view>
- <view>课程:{{banbenInfo.curKechengName}}</view>
- </view>
- </view>
- <view class="card-progress-box">
- <view class="xx-progress-box">
- <view>学习进度</view>
- <progress :percent="curProcess" class="xx-progress" stroke-width="20"
- backgroundColor="#3c7dfd" activeColor="#ffd11c" />
- </view>
- <view class="jxxx-btn" @click="handlePlay(banbenInfo,'jixu')"></view>
- </view>
- </view>
- </view>
- <view class="xx-item-list">
- <view class="xx-item-title">— 以下为当前等级课程目录 —</view>
- <view v-for="(danyuanItem,index) in danyuanList" :key="danyuanItem.danyuanId">
- <!-- 第一单元 -->
- <view v-if="index ==0" class="item-dy-box" :class="{ 'item-fixed': isFixed }" @click="handleClickDanyuan" :id="`unit-title-${danyuanItem.danyuanId}`">
- <view class="dy-left-box">L{{banbenInfo.dengjiId}}</view>
- <view class="dy-right-box">
- <view class="right-content">
- <view class="dy-name">{{danyuanItem.danyuanName}}</view>
- <view>{{danyuanItem.danyuanIntro}}</view>
- </view>
- </view>
- </view>
- <!-- 其他单元 -->
- <view v-if="index !=0" class="xx-item-title" :id="`unit-title-${danyuanItem.danyuanId}`">— {{danyuanItem.danyuanName}}
- {{danyuanItem.danyuanIntro}} —
- </view>
-
- <view class="xx-item-box" v-for="jieItem in danyuanItem.jieList" :key="jieItem.jieId"
- @click="handlePlay(jieItem,'play')">
- <view class="xx-item-status"
- :class="jieItem.wanchengFlag == 1 ? 'completed-status' : 'uncompleted-status'">
- </view>
- <img :src="jieItem.cover" />
- <view class="xx-text-box">
- <view>{{ jieItem.jieName }}</view>
- <view>{{ jieItem.jieIntro }}</view>
- </view>
- <view class="xx-item-btn"></view>
- </view>
- </view>
- </view>
- <!-- 回到顶部 -->
- <view class="go-top-btn" v-if="false"></view>
-
- </scroll-view>
- </view>
- <view v-if="!existData">
- </view>
- <danyuanInfoVue ref="dyRef" v-if="isShow" @close="isShow= false"></danyuanInfoVue>
- <custom-tab-bar :show="true" :current-index="currentTabIndex" />
-
- </view>
- </template>
- <script>
- import CustomTabBar from '@/components/custom-tabbar/index.vue';
- import cacheManager from "@/utils/cacheManager.js";
- import {
- shuxueChanpinBanbenInfo,
- shuxueSave
- } from "@/api/chanpinneirong.js"
- import {
- onLoad,
- onShow,
- onHide,
- onUnload
- } from "@dcloudio/uni-app"
- import danyuanInfoVue from '@/pages/xinshuxue/components/danyuanInfo.vue';
- import {
- toast
- } from '../../utils/common';
- import {updateXuexiProcess} from "./useNeirongShuxue"
- export default {
- data() {
- return {
- canExitApp: false,
- danyuanList: [],
- currentUnitName: "",
- isShow: true,
- banbenInfo: {},
- banbenId: '',
- danyuanId: '',
- chanpinId: '',
- dengjiId: '',
- curProcess: '',
- currentTabIndex: 1,
- cacheManagerLocal: null,
- hasCache: false, // 是否有缓存的标志
- needRefresh: false, // 是否需要刷新数据的标志
- existData: true,
- currentVisibleUnit: '', // 当前可见的单元名称
- observer: null, // IntersectionObserver实例
- lastToastUnitId: null ,// 上一次toast的单元ID,防止重复触发
- isFixed:false,
- }
- },
- components: {
- CustomTabBar,
- danyuanInfoVue
- },
- onShow() {
- this.currentTabIndex = 1
- const cacheData = cacheManager.get('xuexi-shuxue');
- if (cacheData) {
- console.log('使用缓存数据');
- this.updateFromCache();
- } else {
- console.log('重新请求数据');
- this.hasCache = false;
- const cacheDataAuth = cacheManager.get('auth');
- this.initFromOptions(cacheDataAuth);
- }
- // 数据加载完成后初始化观察器
- this.$nextTick(() => {
- setTimeout(() => {
- this.initObserver();
- }, 300);
- });
- },
- onHide() {
- console.log('学习页面隐藏')
- this.needRefresh = true;
- // 销毁观察器
- if (this.observer) {
- this.observer.disconnect();
- this.observer = null;
- }
- },
- onUnload() {
- // 页面卸载时销毁观察器
- if (this.observer) {
- this.observer.disconnect();
- this.observer = null;
- }
- },
- onLoad(options) {
- uni.hideTabBar()
- },
- methods: {
- // 初始化IntersectionObserver
- initObserver() {
- if (this.observer) {
- this.observer.disconnect();
- this.observer = null;
- }
-
- // 监听每个单元标题的位置
- this.observer = uni.createIntersectionObserver(this).relativeToViewport({
- top: 0, // 监听进入屏幕顶部的标题
- bottom: 0
- });
-
- this.danyuanList.forEach((item, index) => {
- const titleId = `unit-title-${item.danyuanId}`;
- this.observer.observe(`#${titleId}`, (res) => {
- if (res.intersectionRatio > 0) {
- // 标题在屏幕顶部区域内
- if (this.lastToastUnitId !== item.danyuanId) {
- this.lastToastUnitId = item.danyuanId;
- console.log(`显示${item.danyuanName}`);
-
- console.log('item',item);
- if(item.danyuanName !="第一单元"){
- this.isFixed = true
- }else{
- this.isFixed = false
- }
- // uni.showToast({
- // title: `${item.danyuanName}`,
- // icon: 'none',
- // duration: 1500
- // });
- }
- }
- });
- });
-
- // 页面初始时显示第一个单元
- if (this.danyuanList.length > 0) {
- this.lastToastUnitId = this.danyuanList[0].danyuanId;
- }
- },
- // 从参数初始化方法
- initFromOptions(options) {
- console.log('options', options);
- this.chanpinId = options.chanpinId;
- this.danyuanId = options.danyuanId;
- this.banbenId = options.banbenId;
- this.dengjiId = options.dengjiId;
- this.loadDataFromApi();
- },
- // 从缓存更新数据方法
- updateFromCache() {
- updateXuexiProcess()
- const cacheData = cacheManager.get('xuexi-shuxue');
- console.log('cacheData 从缓存更新数据方法', cacheData);
- if (cacheData) {
- this.banbenInfo = {
- ...cacheData
- };
- this.curProcess = cacheData.curProcess;
- this.danyuanList = [...(cacheData.danyuanList || [])];
- // 数据更新后重新初始化观察器
- this.$nextTick(() => {
- setTimeout(() => {
- this.initObserver();
- }, 300);
- });
- }
- },
- shouldUseCache(options, cacheData) {
- if (options.banbenId && cacheData.banbenId === options.banbenId) {
- return true;
- }
- return false;
- },
- // 改成了
- loadDataFromApi() {
- this.banbenInfo = {}
- this.curProcess = ''
- this.danyuanList = []
- const req = {
- banbenId: this.banbenId
- }
- shuxueChanpinBanbenInfo(req).then(res => {
- if (res.code === 0) {
- this.banbenInfo = res.data;
- this.curProcess =res.data.curProcess * 100;
- this.danyuanList = res.data.danyuanList || [];
- if (!this.danyuanList) {
- this.existData = false
- }
- // 保存到缓存(新增了参数保存)
- const cacheData = {
- ...res.data,
- banbenId: this.banbenId,
- chanpinId: this.chanpinId,
- danyuanId: this.danyuanId,
- dengjiId: this.dengjiId
- };
- cacheManager.set('xuexi-shuxue', cacheData);
- this.hasCache = true;
- // 更新全局auth信息
- cacheManager.updateObject('auth', {
- chanpinId: this.chanpinId,
- banbenId: this.banbenId,
- danyuanId: this.danyuanId,
- dengjiId: this.dengjiId
- });
- // 数据加载完成后初始化观察器
- this.$nextTick(() => {
- setTimeout(() => {
- this.initObserver();
- }, 300);
- });
- }
- }).catch(res => {
- cacheManager.remove("xuexi-shuxue");
- this.hasCache = false;
- toast("加载失败,请重试");
- });
- },
- getJieAndDanyuan(data, jieId) {
- // 循环单元
- for (let danyuan of data.danyuanList) {
- // 循环节
- for (let jie of danyuan.jieList) {
- if (jie.jieId == jieId) {
- return {
- danyuan: danyuan,
- jie: jie
- }
- }
- }
- }
- return null;
- },
- // 统一保存和跳转方法
- async saveAndNavigate(jieId, type) {
- if (!this.banbenId || !this.danyuanId) {
- toast("banbenId或者danyuanId 丢失")
- return false
- }
- let req = {
- "banbenId": this.banbenId,
- "danyuanId": this.danyuanId,
- "jieId": jieId
- }
- try {
- const res = await shuxueSave(req);
- console.log('res', res);
- if (res.code == 0 && res.data) {
- let curJieAndDanyuan = this.getJieAndDanyuan(this.banbenInfo, jieId);
- console.log('curJieAndDanyuan', curJieAndDanyuan);
- if (!curJieAndDanyuan) {
- toast("未找到课程信息");
- return false;
- }
- // 更新缓存
- const cacheData = cacheManager.get('xuexi-shuxue') || {};
- cacheData.curDanyuanName = curJieAndDanyuan.danyuan.danyuanName;
- cacheData.curKechengName = curJieAndDanyuan.danyuan.danyuanIntro;
- cacheData.curJieId = jieId;
- cacheData.type = curJieAndDanyuan.jie.type;
- cacheManager.set('xuexi-shuxue', cacheData);
- // 跳转到学习页面
- if (type == 1) {
- uni.navigateTo({
- url: `/pages/xinshuxue/lookShipin?jieId=${jieId}`
- })
- } else {
- uni.navigateTo({
- url: `/pages/xinshuxue/unitTest?jieId=${jieId}`
- })
- }
- } else {
- toast("保存位置出错");
- return false;
- }
- } catch (error) {
- toast("保存失败");
- return false;
- }
- },
- handlePlay(da, code) {
- console.log('da', da);
- let jieId = null;
- if (code == 'jixu') {
- if (!da.curJieId) {
- toast("无课程ID");
- return;
- }
- jieId = da.curJieId;
- } else {
- jieId = da.jieId;
- }
- this.saveAndNavigate(jieId, da.type);
- },
- handleClickDanyuan() {
- if (!this.danyuanId) {
- toast("this.danyuanId丢失")
- return false
- }
- this.isShow = true;
- setTimeout(() => {
- this.$refs.dyRef.handleShow(this.danyuanId)
- }, 100)
- },
- handleBack() {
- uni.navigateTo({
- url: `/pages/chanpinXuanze/banben?dengjiId=` + this.dengjiId
- })
- },
- }
- }
- </script>
|