index2.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. <template>
  2. <view class="ezy-xuexi-page">
  3. <view class="icon-title-navBar-box">
  4. <text class="nav-bar-title">学习</text>
  5. </view>
  6. <view v-if="existData" class="ezy-page-body xuexi-page-body" ref="containerRef">
  7. <view class="xxjl-card-box-padding">
  8. <view class="xxjl-card-box">
  9. <!-- 显示内容 -->
  10. <view class="card-body-box">
  11. <img :src="banbenInfo.cover" />
  12. <view class="body-right">
  13. <view class="right-name">{{ banbenInfo.chanpinName }}</view>
  14. <view>等级:{{ banbenInfo.dengjiName }}</view>
  15. <view>版本:{{ banbenInfo.name }}</view>
  16. <view>单元:{{ banbenInfo.curDanyuanName }}</view>
  17. <view>课程:{{ banbenInfo.curKechengName }}</view>
  18. </view>
  19. </view>
  20. <view class="card-progress-box">
  21. <view class="xx-progress-box">
  22. <view>学习进度</view>
  23. <progress :percent="curProcess" class="xx-progress" stroke-width="20"
  24. backgroundColor="#3c7dfd" activeColor="#ffd11c" />
  25. </view>
  26. <ezyActiveVue class="ezy-btn-active jxxx-btn" @aclick="handlePlay(banbenInfo,'jixu')">
  27. </ezyActiveVue>
  28. </view>
  29. </view>
  30. </view>
  31. <view class="xx-item-list">
  32. <view class="xx-item-title cccAAA2">— 以下为当前等级课程目录 —</view>
  33. <template v-if="unitPositions.length">
  34. <ezyActiveVue class="ezy-list-item-active item-dy-box item-fixed cccAAAB"
  35. @aclick="handleClickDanyuan(danyuanList[activeAAAIndex])"
  36. :id="`unit-title-${danyuanList[activeAAAIndex].danyuanId}`">
  37. <view class="dy-left-box">L{{ banbenInfo.dengjiId }}</view>
  38. <view class="dy-right-box">
  39. <view class="right-content">
  40. <view class="dy-name">{{ danyuanList[activeAAAIndex].danyuanName }}</view>
  41. <view>{{ danyuanList[activeAAAIndex].danyuanIntro }}</view>
  42. </view>
  43. </view>
  44. </ezyActiveVue>
  45. </template>
  46. <view v-for="(danyuanItem,index) in danyuanList" :key="danyuanItem.danyuanId">
  47. <!-- 第一单元 -->
  48. <ezyActiveVue v-show="false" v-if="index ==0"
  49. class="ezy-list-item-active item-dy-box item-fixed cccAAA"
  50. @aclick="handleClickDanyuan(danyuanItem)" :id="`unit-title-${danyuanItem.danyuanId}`">
  51. <view class="dy-left-box">L{{ banbenInfo.dengjiId }}</view>
  52. <view class="dy-right-box">
  53. <view class="right-content">
  54. <view class="dy-name">{{ danyuanItem.danyuanName }}</view>
  55. <view>{{ danyuanItem.danyuanIntro }}</view>
  56. </view>
  57. </view>
  58. </ezyActiveVue>
  59. <!-- 其他单元 -->
  60. <view v-if="index !=0" class="xx-item-title cccAAA" :id="`unit-title-${danyuanItem.danyuanId}`">—
  61. {{ danyuanItem.danyuanName }}
  62. {{ danyuanItem.danyuanIntro }} —
  63. </view>
  64. <ezyActiveVue class="ezy-list-item-active xx-item-box" v-for="jieItem in danyuanItem.jieList"
  65. :key="jieItem.jieId" @aclick="handlePlay(jieItem,'play')">
  66. <view class="xx-item-status"
  67. :class="jieItem.wanchengFlag == 1 ? 'completed-status' : 'uncompleted-status'">
  68. </view>
  69. <img :src="jieItem.cover" />
  70. <view class="xx-text-box">
  71. <view>{{ jieItem.jieName }}</view>
  72. <view>{{ jieItem.jieIntro }}</view>
  73. </view>
  74. <view class="xx-item-btn"></view>
  75. </ezyActiveVue>
  76. </view>
  77. <view class="xx-item-title">本级别最后一单元啦~</view>
  78. <view class="xx-more-btn" @click="moreBtn"></view>
  79. </view>
  80. <!-- 回到顶部 -->
  81. <view class="go-top-btn" v-if="showTop" @click="handleTop"></view>
  82. </view>
  83. <view v-if="!existData">
  84. </view>
  85. <danyuanInfoVue ref="dyRef" v-if="isShow" @close="isShow= false"></danyuanInfoVue>
  86. <custom-tab-bar :show="true" :current-index="currentTabIndex" />
  87. </view>
  88. </template>
  89. <script>
  90. import ezyActiveVue from "@/components/ezyActive/ezyActive.vue";
  91. import CustomTabBar from '@/components/custom-tabbar/index.vue';
  92. import cacheManager from "@/utils/cacheManager.js";
  93. import {
  94. shuxueChanpinBanbenInfo,
  95. shuxueSave
  96. } from "@/api/chanpinneirong.js"
  97. import {
  98. onLoad,
  99. onShow,
  100. onHide,
  101. onUnload
  102. } from "@dcloudio/uni-app"
  103. import danyuanInfoVue from '@/pages/xinshuxue/components/danyuanInfo.vue';
  104. import {
  105. toast
  106. } from '../../utils/common';
  107. import {
  108. updateXuexiProcess
  109. } from "./useNeirongShuxue"
  110. import {
  111. nextTick
  112. } from "vue";
  113. export default {
  114. data() {
  115. return {
  116. //-------------- start ---------------/
  117. // 单元位置信息
  118. unitPositions: [],
  119. // 节流定时器
  120. throttleTimer: null,
  121. activeAAAIndex: 0,
  122. defaultD: 0,
  123. showTop: false,
  124. //-------------- end ---------------/
  125. canExitApp: false,
  126. danyuanList: [],
  127. currentUnitName: "",
  128. isShow: true,
  129. banbenInfo: {},
  130. banbenId: '',
  131. danyuanId: '',
  132. chanpinId: '',
  133. dengjiId: '',
  134. curProcess: '',
  135. currentTabIndex: 1,
  136. cacheManagerLocal: null,
  137. hasCache: false, // 是否有缓存的标志
  138. needRefresh: false, // 是否需要刷新数据的标志
  139. existData: true,
  140. currentVisibleUnit: '', // 当前可见的单元名称
  141. observer: null, // IntersectionObserver实例
  142. lastToastUnitId: null, // 上一次toast的单元ID,防止重复触发
  143. isFixed: false,
  144. }
  145. },
  146. components: {
  147. CustomTabBar,
  148. danyuanInfoVue,
  149. ezyActiveVue
  150. },
  151. onShow() {
  152. this.currentTabIndex = 1
  153. const cacheData = cacheManager.get('xuexi-shuxue');
  154. if (cacheData) {
  155. console.log('使用缓存数据');
  156. this.updateFromCache();
  157. } else {
  158. console.log('重新请求数据');
  159. this.hasCache = false;
  160. const cacheDataAuth = cacheManager.get('auth');
  161. this.initFromOptions(cacheDataAuth);
  162. }
  163. // 数据加载完成后初始化观察器
  164. this.$nextTick(() => {
  165. setTimeout(() => {
  166. this.initObserver();
  167. }, 300);
  168. });
  169. // 页面显示时重新计算单元位置
  170. this.$nextTick(() => {
  171. setTimeout(() => {
  172. this.doReady()
  173. }, 100);
  174. });
  175. },
  176. onHide() {
  177. console.log('学习页面隐藏')
  178. this.needRefresh = true;
  179. // 销毁观察器
  180. if (this.observer) {
  181. this.observer.disconnect();
  182. this.observer = null;
  183. }
  184. },
  185. onUnload() {
  186. // 页面卸载时销毁观察器
  187. if (this.observer) {
  188. this.observer.disconnect();
  189. this.observer = null;
  190. }
  191. },
  192. onLoad(options) {
  193. uni.hideTabBar()
  194. },
  195. methods: {
  196. moreBtn() {
  197. uni.switchTab({
  198. url: '/pages/chanpinXuanze/index'
  199. })
  200. },
  201. // 初始化IntersectionObserver
  202. initObserver() {
  203. if (this.observer) {
  204. this.observer.disconnect();
  205. this.observer = null;
  206. }
  207. // 监听每个单元标题的位置
  208. this.observer = uni.createIntersectionObserver(this).relativeToViewport({
  209. top: 0, // 监听进入屏幕顶部的标题
  210. bottom: 0
  211. });
  212. this.danyuanList.forEach((item, index) => {
  213. const titleId = `unit-title-${item.danyuanId}`;
  214. this.observer.observe(`#${titleId}`, (res) => {
  215. if (res.intersectionRatio > 0) {
  216. // 标题在屏幕顶部区域内
  217. if (this.lastToastUnitId !== item.danyuanId) {
  218. this.lastToastUnitId = item.danyuanId;
  219. // console.log(`显示${item.danyuanName}`);
  220. // console.log('item', item);
  221. if (item.danyuanName != "第一单元") {
  222. this.isFixed = true
  223. } else {
  224. this.isFixed = false
  225. }
  226. // uni.showToast({
  227. // title: `${item.danyuanName}`,
  228. // icon: 'none',
  229. // duration: 1500
  230. // });
  231. }
  232. }
  233. });
  234. });
  235. // 页面初始时显示第一个单元
  236. if (this.danyuanList.length > 0) {
  237. this.lastToastUnitId = this.danyuanList[0].danyuanId;
  238. }
  239. },
  240. // 从参数初始化方法
  241. initFromOptions(options) {
  242. console.log('options', options);
  243. this.chanpinId = options.chanpinId;
  244. this.danyuanId = options.danyuanId;
  245. this.banbenId = options.banbenId;
  246. this.dengjiId = options.dengjiId;
  247. this.loadDataFromApi();
  248. },
  249. // 从缓存更新数据方法
  250. updateFromCache() {
  251. updateXuexiProcess()
  252. const cacheData = cacheManager.get('xuexi-shuxue');
  253. console.log('cacheData 从缓存更新数据方法', cacheData);
  254. if (cacheData) {
  255. this.banbenInfo = {
  256. ...cacheData
  257. };
  258. this.curProcess = cacheData.curProcess;
  259. this.danyuanList = [...(cacheData.danyuanList || [])];
  260. // 数据更新后重新初始化观察器
  261. this.$nextTick(() => {
  262. setTimeout(() => {
  263. this.initObserver();
  264. }, 300);
  265. });
  266. }
  267. },
  268. shouldUseCache(options, cacheData) {
  269. if (options.banbenId && cacheData.banbenId === options.banbenId) {
  270. return true;
  271. }
  272. return false;
  273. },
  274. // 改成了
  275. loadDataFromApi() {
  276. this.banbenInfo = {}
  277. this.curProcess = ''
  278. this.danyuanList = []
  279. const req = {
  280. banbenId: this.banbenId
  281. }
  282. shuxueChanpinBanbenInfo(req).then(res => {
  283. if (res.code === 0) {
  284. this.banbenInfo = res.data;
  285. this.curProcess = res.data.curProcess * 100;
  286. this.danyuanList = res.data.danyuanList || [];
  287. if (!this.danyuanList) {
  288. this.existData = false
  289. }
  290. // 保存到缓存(新增了参数保存)
  291. const cacheData = {
  292. ...res.data,
  293. banbenId: this.banbenId,
  294. chanpinId: this.chanpinId,
  295. danyuanId: this.danyuanId,
  296. dengjiId: this.dengjiId
  297. };
  298. cacheManager.set('xuexi-shuxue', cacheData);
  299. this.hasCache = true;
  300. // 更新全局auth信息
  301. cacheManager.updateObject('auth', {
  302. chanpinId: res.data.chanpinId,
  303. banbenId: this.banbenId,
  304. danyuanId: res.data.curDanyuanId,
  305. dengjiId: res.data.dengjiId
  306. });
  307. this.danyuanId = res.data.curDanyuanId
  308. this.dengjiId = res.data.dengjiId
  309. this.chanpinId = res.data.chanpinId
  310. // 数据加载完成后初始化观察器
  311. this.$nextTick(() => {
  312. setTimeout(() => {
  313. this.initObserver();
  314. }, 300);
  315. });
  316. }
  317. }).catch(res => {
  318. cacheManager.remove("xuexi-shuxue");
  319. this.hasCache = false;
  320. toast("加载失败,请重试");
  321. });
  322. },
  323. getJieAndDanyuan(data, jieId) {
  324. // 循环单元
  325. for (let danyuan of data.danyuanList) {
  326. // 循环节
  327. for (let jie of danyuan.jieList) {
  328. if (jie.jieId == jieId) {
  329. return {
  330. danyuan: danyuan,
  331. jie: jie
  332. }
  333. }
  334. }
  335. }
  336. return null;
  337. },
  338. // 统一保存和跳转方法
  339. async saveAndNavigate(jieId, type, da, code) {
  340. if (code == 'jixu') { // 追加参数判断 非继续 [临时]
  341. if (!this.banbenId || !this.danyuanId) {
  342. toast("banbenId或者danyuanId 丢失")
  343. return false
  344. }
  345. }
  346. let req = {
  347. "banbenId": this.banbenId,
  348. "danyuanId": da.danyuanId, // 修改当时版本ID为数据内版本Id [临时]
  349. "jieId": jieId
  350. }
  351. try {
  352. const res = await shuxueSave(req);
  353. console.log('res', res);
  354. if (res.code == 0 && res.data) {
  355. let curJieAndDanyuan = this.getJieAndDanyuan(this.banbenInfo, jieId);
  356. console.log('curJieAndDanyuan', curJieAndDanyuan);
  357. if (!curJieAndDanyuan) {
  358. toast("未找到课程信息");
  359. return false;
  360. }
  361. // 更新缓存
  362. const cacheData = cacheManager.get('xuexi-shuxue') || {};
  363. cacheData.curDanyuanName = curJieAndDanyuan.danyuan.danyuanName;
  364. cacheData.curKechengName = curJieAndDanyuan.jie.jieIntro;
  365. cacheData.curJieId = jieId;
  366. cacheData.type = curJieAndDanyuan.jie.type;
  367. cacheManager.set('xuexi-shuxue', cacheData);
  368. // 跳转到学习页面
  369. if (type == 1) {
  370. uni.navigateTo({
  371. url: `/pages/xinshuxue/lookShipin?jieId=${jieId}`
  372. })
  373. } else {
  374. uni.navigateTo({
  375. url: `/pages/xinshuxue/unitTest?jieId=${jieId}`
  376. })
  377. }
  378. } else {
  379. toast("保存位置出错");
  380. return false;
  381. }
  382. } catch (error) {
  383. toast("保存失败");
  384. return false;
  385. }
  386. },
  387. handlePlay(da, code) {
  388. console.log('da', da);
  389. let jieId = null;
  390. if (code == 'jixu') {
  391. if (!da.curJieId) {
  392. toast("无课程ID");
  393. return;
  394. }
  395. jieId = da.curJieId;
  396. } else {
  397. jieId = da.jieId;
  398. }
  399. // [临时]
  400. this.saveAndNavigate(jieId, da.type, da, code);
  401. },
  402. handleClickDanyuan(item) {
  403. // 更新为点击的动态单元Id [临时]
  404. if (!item.danyuanId) {
  405. toast("this.danyuanId丢失")
  406. return false
  407. }
  408. this.isShow = true;
  409. setTimeout(() => {
  410. // 更新为点击的动态单元Id [临时]
  411. this.$refs.dyRef.handleShow(item.danyuanId)
  412. }, 100)
  413. },
  414. handleBack() {
  415. uni.navigateTo({
  416. url: `/pages/chanpinXuanze/banben?dengjiId=` + this.dengjiId
  417. })
  418. },
  419. // 计算单元位置
  420. calculateUnitPositions() {
  421. this.unitPositions = [];
  422. // 使用selectAll获取所有单元元素
  423. uni
  424. .createSelectorQuery()
  425. .in(this)
  426. .selectAll(".cccAAA")
  427. .boundingClientRect((rects) => {
  428. this.unitPositions = [...rects]
  429. })
  430. .exec();
  431. setTimeout(() => {
  432. uni
  433. .createSelectorQuery()
  434. .in(this)
  435. .selectAll(".cccAAAB")
  436. .boundingClientRect((rects) => {
  437. this.defaultD = rects[0].top;
  438. })
  439. .exec();
  440. },500)
  441. },
  442. // 绑定滚动事件
  443. bindScrollEvent() {
  444. const checkScroll = () => {
  445. const query = uni.createSelectorQuery().in(this)
  446. query.select('.cccAAA2').boundingClientRect()
  447. query.exec((res) => {
  448. let arr = []
  449. this.unitPositions.forEach((item, index) => {
  450. if ((item.top + res[0].top) < this.defaultD+20) {
  451. arr.push(index)
  452. }
  453. if (res[0].top< 0) {
  454. this.showTop = true
  455. } else {
  456. this.showTop = false
  457. }
  458. })
  459. if (arr.length) {
  460. if (this.activeAAAIndex != arr[arr.length - 1]) {
  461. // console.log('当前数据', this.danyuanList[arr[arr.length - 1]])
  462. this.activeAAAIndex = arr[arr.length - 1];
  463. }
  464. }
  465. })
  466. clearTimeout(this.scrollTimer)
  467. this.scrollTimer = null;
  468. this.scrollTimer = setTimeout(checkScroll, 200) // 每200ms检查一次
  469. }
  470. checkScroll()
  471. },
  472. // 执行初始化
  473. doReady() {
  474. // 页面加载完成后初始化
  475. this.$nextTick(() => {
  476. this.bindScrollEvent();
  477. this.calculateUnitPositions();
  478. // 添加初始调试信息
  479. console.log("页面初始化完成,单元位置:", this.unitPositions);
  480. });
  481. },
  482. handleTop() {
  483. console.log('ccc', this.$refs.containerRef.$el.scrollTop)
  484. this.$refs.containerRef.$el.scrollTop = 0;
  485. }
  486. },
  487. }
  488. </script>