index2.vue 14 KB

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