index2.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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">
  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="false"></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. //-------------- end ---------------/
  124. canExitApp: false,
  125. danyuanList: [],
  126. currentUnitName: "",
  127. isShow: true,
  128. banbenInfo: {},
  129. banbenId: '',
  130. danyuanId: '',
  131. chanpinId: '',
  132. dengjiId: '',
  133. curProcess: '',
  134. currentTabIndex: 1,
  135. cacheManagerLocal: null,
  136. hasCache: false, // 是否有缓存的标志
  137. needRefresh: false, // 是否需要刷新数据的标志
  138. existData: true,
  139. currentVisibleUnit: '', // 当前可见的单元名称
  140. observer: null, // IntersectionObserver实例
  141. lastToastUnitId: null, // 上一次toast的单元ID,防止重复触发
  142. isFixed: false,
  143. }
  144. },
  145. components: {
  146. CustomTabBar,
  147. danyuanInfoVue,
  148. ezyActiveVue
  149. },
  150. onShow() {
  151. this.currentTabIndex = 1
  152. const cacheData = cacheManager.get('xuexi-shuxue');
  153. if (cacheData) {
  154. console.log('使用缓存数据');
  155. this.updateFromCache();
  156. } else {
  157. console.log('重新请求数据');
  158. this.hasCache = false;
  159. const cacheDataAuth = cacheManager.get('auth');
  160. this.initFromOptions(cacheDataAuth);
  161. }
  162. // 数据加载完成后初始化观察器
  163. this.$nextTick(() => {
  164. setTimeout(() => {
  165. this.initObserver();
  166. }, 300);
  167. });
  168. // 页面显示时重新计算单元位置
  169. this.$nextTick(() => {
  170. setTimeout(() => {
  171. this.doReady()
  172. }, 100);
  173. });
  174. },
  175. onHide() {
  176. console.log('学习页面隐藏')
  177. this.needRefresh = true;
  178. // 销毁观察器
  179. if (this.observer) {
  180. this.observer.disconnect();
  181. this.observer = null;
  182. }
  183. },
  184. onUnload() {
  185. // 页面卸载时销毁观察器
  186. if (this.observer) {
  187. this.observer.disconnect();
  188. this.observer = null;
  189. }
  190. },
  191. onLoad(options) {
  192. uni.hideTabBar()
  193. },
  194. methods: {
  195. moreBtn() {
  196. uni.switchTab({
  197. url: '/pages/chanpinXuanze/index'
  198. })
  199. },
  200. // 初始化IntersectionObserver
  201. initObserver() {
  202. if (this.observer) {
  203. this.observer.disconnect();
  204. this.observer = null;
  205. }
  206. // 监听每个单元标题的位置
  207. this.observer = uni.createIntersectionObserver(this).relativeToViewport({
  208. top: 0, // 监听进入屏幕顶部的标题
  209. bottom: 0
  210. });
  211. this.danyuanList.forEach((item, index) => {
  212. const titleId = `unit-title-${item.danyuanId}`;
  213. this.observer.observe(`#${titleId}`, (res) => {
  214. if (res.intersectionRatio > 0) {
  215. // 标题在屏幕顶部区域内
  216. if (this.lastToastUnitId !== item.danyuanId) {
  217. this.lastToastUnitId = item.danyuanId;
  218. // console.log(`显示${item.danyuanName}`);
  219. // console.log('item', item);
  220. if (item.danyuanName != "第一单元") {
  221. this.isFixed = true
  222. } else {
  223. this.isFixed = false
  224. }
  225. // uni.showToast({
  226. // title: `${item.danyuanName}`,
  227. // icon: 'none',
  228. // duration: 1500
  229. // });
  230. }
  231. }
  232. });
  233. });
  234. // 页面初始时显示第一个单元
  235. if (this.danyuanList.length > 0) {
  236. this.lastToastUnitId = this.danyuanList[0].danyuanId;
  237. }
  238. },
  239. // 从参数初始化方法
  240. initFromOptions(options) {
  241. console.log('options', options);
  242. this.chanpinId = options.chanpinId;
  243. this.danyuanId = options.danyuanId;
  244. this.banbenId = options.banbenId;
  245. this.dengjiId = options.dengjiId;
  246. this.loadDataFromApi();
  247. },
  248. // 从缓存更新数据方法
  249. updateFromCache() {
  250. updateXuexiProcess()
  251. const cacheData = cacheManager.get('xuexi-shuxue');
  252. console.log('cacheData 从缓存更新数据方法', cacheData);
  253. if (cacheData) {
  254. this.banbenInfo = {
  255. ...cacheData
  256. };
  257. this.curProcess = cacheData.curProcess;
  258. this.danyuanList = [...(cacheData.danyuanList || [])];
  259. // 数据更新后重新初始化观察器
  260. this.$nextTick(() => {
  261. setTimeout(() => {
  262. this.initObserver();
  263. }, 300);
  264. });
  265. }
  266. },
  267. shouldUseCache(options, cacheData) {
  268. if (options.banbenId && cacheData.banbenId === options.banbenId) {
  269. return true;
  270. }
  271. return false;
  272. },
  273. // 改成了
  274. loadDataFromApi() {
  275. this.banbenInfo = {}
  276. this.curProcess = ''
  277. this.danyuanList = []
  278. const req = {
  279. banbenId: this.banbenId
  280. }
  281. shuxueChanpinBanbenInfo(req).then(res => {
  282. if (res.code === 0) {
  283. this.banbenInfo = res.data;
  284. this.curProcess = res.data.curProcess * 100;
  285. this.danyuanList = res.data.danyuanList || [];
  286. if (!this.danyuanList) {
  287. this.existData = false
  288. }
  289. // 保存到缓存(新增了参数保存)
  290. const cacheData = {
  291. ...res.data,
  292. banbenId: this.banbenId,
  293. chanpinId: this.chanpinId,
  294. danyuanId: this.danyuanId,
  295. dengjiId: this.dengjiId
  296. };
  297. cacheManager.set('xuexi-shuxue', cacheData);
  298. this.hasCache = true;
  299. // 更新全局auth信息
  300. cacheManager.updateObject('auth', {
  301. chanpinId: res.data.chanpinId,
  302. banbenId: this.banbenId,
  303. danyuanId: res.data.curDanyuanId,
  304. dengjiId: res.data.dengjiId
  305. });
  306. this.danyuanId = res.data.curDanyuanId
  307. this.dengjiId = res.data.dengjiId
  308. this.chanpinId = res.data.chanpinId
  309. // 数据加载完成后初始化观察器
  310. this.$nextTick(() => {
  311. setTimeout(() => {
  312. this.initObserver();
  313. }, 300);
  314. });
  315. }
  316. }).catch(res => {
  317. cacheManager.remove("xuexi-shuxue");
  318. this.hasCache = false;
  319. toast("加载失败,请重试");
  320. });
  321. },
  322. getJieAndDanyuan(data, jieId) {
  323. // 循环单元
  324. for (let danyuan of data.danyuanList) {
  325. // 循环节
  326. for (let jie of danyuan.jieList) {
  327. if (jie.jieId == jieId) {
  328. return {
  329. danyuan: danyuan,
  330. jie: jie
  331. }
  332. }
  333. }
  334. }
  335. return null;
  336. },
  337. // 统一保存和跳转方法
  338. async saveAndNavigate(jieId, type, da, code) {
  339. if (code == 'jixu') { // 追加参数判断 非继续 [临时]
  340. if (!this.banbenId || !this.danyuanId) {
  341. toast("banbenId或者danyuanId 丢失")
  342. return false
  343. }
  344. }
  345. let req = {
  346. "banbenId": this.banbenId,
  347. "danyuanId": da.danyuanId, // 修改当时版本ID为数据内版本Id [临时]
  348. "jieId": jieId
  349. }
  350. try {
  351. const res = await shuxueSave(req);
  352. console.log('res', res);
  353. if (res.code == 0 && res.data) {
  354. let curJieAndDanyuan = this.getJieAndDanyuan(this.banbenInfo, jieId);
  355. console.log('curJieAndDanyuan', curJieAndDanyuan);
  356. if (!curJieAndDanyuan) {
  357. toast("未找到课程信息");
  358. return false;
  359. }
  360. // 更新缓存
  361. const cacheData = cacheManager.get('xuexi-shuxue') || {};
  362. cacheData.curDanyuanName = curJieAndDanyuan.danyuan.danyuanName;
  363. cacheData.curKechengName = curJieAndDanyuan.jie.jieIntro;
  364. cacheData.curJieId = jieId;
  365. cacheData.type = curJieAndDanyuan.jie.type;
  366. cacheManager.set('xuexi-shuxue', cacheData);
  367. // 跳转到学习页面
  368. if (type == 1) {
  369. uni.navigateTo({
  370. url: `/pages/xinshuxue/lookShipin?jieId=${jieId}`
  371. })
  372. } else {
  373. uni.navigateTo({
  374. url: `/pages/xinshuxue/unitTest?jieId=${jieId}`
  375. })
  376. }
  377. } else {
  378. toast("保存位置出错");
  379. return false;
  380. }
  381. } catch (error) {
  382. toast("保存失败");
  383. return false;
  384. }
  385. },
  386. handlePlay(da, code) {
  387. console.log('da', da);
  388. let jieId = null;
  389. if (code == 'jixu') {
  390. if (!da.curJieId) {
  391. toast("无课程ID");
  392. return;
  393. }
  394. jieId = da.curJieId;
  395. } else {
  396. jieId = da.jieId;
  397. }
  398. // [临时]
  399. this.saveAndNavigate(jieId, da.type, da, code);
  400. },
  401. handleClickDanyuan(item) {
  402. // 更新为点击的动态单元Id [临时]
  403. if (!item.danyuanId) {
  404. toast("this.danyuanId丢失")
  405. return false
  406. }
  407. this.isShow = true;
  408. setTimeout(() => {
  409. // 更新为点击的动态单元Id [临时]
  410. this.$refs.dyRef.handleShow(item.danyuanId)
  411. }, 100)
  412. },
  413. handleBack() {
  414. uni.navigateTo({
  415. url: `/pages/chanpinXuanze/banben?dengjiId=` + this.dengjiId
  416. })
  417. },
  418. // 计算单元位置
  419. calculateUnitPositions() {
  420. this.unitPositions = [];
  421. // 使用selectAll获取所有单元元素
  422. uni
  423. .createSelectorQuery()
  424. .in(this)
  425. .selectAll(".cccAAA")
  426. .boundingClientRect((rects) => {
  427. this.unitPositions = [...rects]
  428. })
  429. .exec();
  430. setTimeout(() => {
  431. uni
  432. .createSelectorQuery()
  433. .in(this)
  434. .selectAll(".cccAAAB")
  435. .boundingClientRect((rects) => {
  436. console.log('cccccccccc', 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) {
  451. arr.push(index)
  452. }
  453. })
  454. if (arr.length) {
  455. if (this.activeAAAIndex != arr[arr.length - 1]) {
  456. console.log('当前数据', this.danyuanList[arr[arr.length - 1]])
  457. this.activeAAAIndex = arr[arr.length - 1];
  458. }
  459. }
  460. })
  461. clearTimeout(this.scrollTimer)
  462. this.scrollTimer = null;
  463. this.scrollTimer = setTimeout(checkScroll, 200) // 每200ms检查一次
  464. }
  465. checkScroll()
  466. },
  467. // 执行初始化
  468. doReady() {
  469. // 页面加载完成后初始化
  470. this.$nextTick(() => {
  471. this.bindScrollEvent();
  472. this.calculateUnitPositions();
  473. // 添加初始调试信息
  474. console.log("页面初始化完成,单元位置:", this.unitPositions);
  475. });
  476. }
  477. },
  478. }
  479. </script>