index2.vue 13 KB

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