index2.vue 14 KB

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