index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. <template>
  2. <view class="ezy-xuexi-page3">
  3. <view class="icon-title-navBar-box">
  4. <text class="nav-bar-title">学习</text>
  5. </view>
  6. <!-- 吸顶单元标题 -->
  7. <ezyActiveVue
  8. v-if="currentStickyTitle"
  9. class="item-dy-box item-fixed"
  10. @aclick="handleClickDanyuan(currentDanyuanId)"
  11. >
  12. <view class="ezy-list-item-active item-dy-body">
  13. <view class="dy-left-box">L{{ currentStickyDengjiId }}</view>
  14. <view class="dy-right-box">
  15. <view class="right-content">
  16. <view class="dy-name">{{ currentStickyTitle }}</view>
  17. <view>{{ currentStickyIntro }}</view>
  18. </view>
  19. </view>
  20. </view>
  21. </ezyActiveVue>
  22. <!-- 滚动区域 -->
  23. <scroll-view
  24. v-if="existData"
  25. class="ezy-page-body xuexi-page-body"
  26. scroll-y
  27. :scroll-top="scrollTop"
  28. @scroll="handleScroll"
  29. >
  30. <view class="xxjl-card-box-padding">
  31. <view class="xxjl-card-box">
  32. <!-- 显示内容 -->
  33. <view class="card-body-box">
  34. <img :src="banbenInfo.cover" />
  35. <view class="body-right">
  36. <view class="right-name">{{ banbenInfo.chanpinName }}</view>
  37. <view>等级:{{banbenInfo.dengjiName}}</view>
  38. <view>版本:{{banbenInfo.name}}</view>
  39. <view>单元:{{banbenInfo.curDanyuanName}}</view>
  40. <view>课程:{{banbenInfo.curKechengName}}</view>
  41. </view>
  42. </view>
  43. <view class="card-progress-box">
  44. <view class="xx-progress-box">
  45. <view>学习进度</view>
  46. <progress :percent="curProcess" class="xx-progress" stroke-width="20"
  47. backgroundColor="#3c7dfd" activeColor="#ffd11c" />
  48. </view>
  49. <ezyActiveVue class="ezy-btn-active jxxx-btn" @aclick="handlePlay(banbenInfo,'jixu')"></ezyActiveVue>
  50. </view>
  51. </view>
  52. </view>
  53. <view class="xx-item-list">
  54. <!-- <view class="xx-item-title">— 以下为当前等级课程目录 —</view> -->
  55. <view v-for="(danyuanItem, index) in danyuanList" :key="danyuanItem.danyuanId">
  56. <!-- 单元标题(带唯一ID,用于位置查询) -->
  57. <view class="xx-item-title" :id="`title-${danyuanItem.danyuanId}`">
  58. — {{ danyuanItem.danyuanName }} {{ danyuanItem.danyuanIntro }} —
  59. </view>
  60. <!-- 节列表 -->
  61. <ezyActiveVue
  62. class="ezy-list-item-active xx-item-box"
  63. v-for="jieItem in danyuanItem.jieList"
  64. :key="jieItem.jieId"
  65. @aclick="handlePlay(jieItem,'play')"
  66. >
  67. <view
  68. class="xx-item-status"
  69. :class="jieItem.wanchengFlag == 1 ? 'completed-status' : 'uncompleted-status'"
  70. ></view>
  71. <img :src="jieItem.cover" />
  72. <view class="xx-text-box">
  73. <view>{{ jieItem.jieName }}</view>
  74. <view>{{ jieItem.jieIntro }}</view>
  75. </view>
  76. <view class="xx-item-btn"></view>
  77. </ezyActiveVue>
  78. </view>
  79. <view class="xx-item-title">本级别最后一单元啦~</view>
  80. <view class="xx-more-btn" @click="moreBtn"></view>
  81. </view>
  82. </scroll-view>
  83. <!-- 回到顶部 -->
  84. <view v-if="currentStickyTitle" class="go-top-btn" @click="goTopBtn"></view>
  85. <!-- 无数据占位 -->
  86. <view v-if="!existData" class="ezy-page-body xuexi-page-body"></view>
  87. <!-- 弹窗组件 -->
  88. <danyuanInfoVue ref="dyRef" v-if="isShow" @close="isShow = false"></danyuanInfoVue>
  89. <!-- 底部 tabBar -->
  90. <custom-tab-bar :show="true" :current-index="currentTabIndex" />
  91. </view>
  92. </template>
  93. <script>
  94. import ezyActiveVue from "@/components/ezyActive/ezyActive.vue";
  95. import CustomTabBar from '@/components/custom-tabbar/index.vue';
  96. import cacheManager from "@/utils/cacheManager.js";
  97. import {
  98. shuxueChanpinBanbenInfo,
  99. shuxueSave
  100. } from "@/api/chanpinneirong.js"
  101. import {
  102. onLoad,
  103. onShow,
  104. onHide,
  105. onUnload
  106. } from "@dcloudio/uni-app"
  107. import danyuanInfoVue from '@/pages/xinshuxue/components/danyuanInfo.vue';
  108. import {
  109. toast
  110. } from '../../utils/common';
  111. import {updateXuexiProcess} from "./useNeirongShuxue"
  112. export default {
  113. data() {
  114. return {
  115. canExitApp: false,
  116. danyuanList: [],
  117. banbenInfo: {},
  118. banbenId: '',
  119. danyuanId: '',
  120. chanpinId: '',
  121. dengjiId: '',
  122. curProcess: '',
  123. currentTabIndex: 1,
  124. existData: true,
  125. isShow: false,
  126. stickyHeight: 0, // 吸顶栏高度(px)
  127. currentStickyTitle: '', // 初始为空,不显示吸顶
  128. currentStickyIntro: '',
  129. currentDanyuanId:'',
  130. currentStickyDengjiId: '',
  131. titlePositions: [],
  132. scrollTop: 0,
  133. }
  134. },
  135. components: {
  136. CustomTabBar,
  137. danyuanInfoVue,
  138. ezyActiveVue
  139. },
  140. onLoad(options) {
  141. uni.hideTabBar()
  142. },
  143. onShow() {
  144. this.currentTabIndex = 1
  145. const cacheData = cacheManager.get('xuexi-shuxue');
  146. if (cacheData) {
  147. console.log('使用缓存数据');
  148. this.updateFromCache();
  149. } else {
  150. console.log('重新请求数据');
  151. const cacheDataAuth = cacheManager.get('auth');
  152. this.initFromOptions(cacheDataAuth);
  153. }
  154. },
  155. onHide() {
  156. console.log('学习页面隐藏')
  157. },
  158. onUnload() {
  159. // 页面卸载无需特殊处理
  160. },
  161. methods: {
  162. goTopBtn() {
  163. this.scrollTop = -1; // 先设一个无效值(确保变化)
  164. this.$nextTick(() => {
  165. this.scrollTop = 0; // 再滚到顶部
  166. });
  167. },
  168. moreBtn() {
  169. uni.switchTab({
  170. url: '/pages/chanpinXuanze/index'
  171. })
  172. },
  173. initFromOptions(options) {
  174. console.log('options', options);
  175. this.chanpinId = options.chanpinId;
  176. this.danyuanId = options.danyuanId;
  177. this.banbenId = options.banbenId;
  178. this.dengjiId = options.dengjiId;
  179. this.loadDataFromApi();
  180. },
  181. // 从缓存更新数据方法
  182. updateFromCache() {
  183. updateXuexiProcess()
  184. const cacheData = cacheManager.get('xuexi-shuxue');
  185. console.log('cacheData 从缓存更新数据方法', cacheData);
  186. if (cacheData) {
  187. this.banbenInfo = { ...cacheData };
  188. this.curProcess = cacheData.curProcess;
  189. this.danyuanList = [...(cacheData.danyuanList || [])];
  190. this.$nextTick(() => {
  191. this.updateTitlePositions();
  192. });
  193. }
  194. },
  195. loadDataFromApi() {
  196. this.banbenInfo = {}
  197. this.curProcess = ''
  198. this.danyuanList = []
  199. const req = {
  200. banbenId: this.banbenId
  201. }
  202. shuxueChanpinBanbenInfo(req).then(res => {
  203. if (res.code === 0) {
  204. this.banbenInfo = res.data;
  205. this.curProcess =res.data.curProcess * 100;
  206. this.danyuanList = res.data.danyuanList || [];
  207. if (!this.danyuanList.length) {
  208. this.existData = false
  209. }
  210. // 保存到缓存(新增了参数保存)
  211. const cacheData = {
  212. ...res.data,
  213. banbenId: this.banbenId,
  214. chanpinId: this.chanpinId,
  215. danyuanId: this.danyuanId,
  216. dengjiId: this.dengjiId
  217. };
  218. cacheManager.set('xuexi-shuxue', cacheData);
  219. // 更新全局auth信息
  220. cacheManager.updateObject('auth', {
  221. chanpinId: res.data.chanpinId,
  222. banbenId: this.banbenId,
  223. danyuanId: res.data.curDanyuanId,
  224. dengjiId: res.data.dengjiId
  225. });
  226. this.danyuanId = res.data.curDanyuanId
  227. this.dengjiId = res.data.dengjiId
  228. this.chanpinId = res.data.chanpinId
  229. // 数据加载完成后初始化观察器
  230. this.$nextTick(() => {
  231. this.updateTitlePositions();
  232. });
  233. }
  234. }).catch(res => {
  235. cacheManager.remove("xuexi-shuxue");
  236. toast("加载失败,请重试");
  237. });
  238. },
  239. updateStickyHeight() {
  240. const query = uni.createSelectorQuery().in(this);
  241. query.select('.item-fixed').boundingClientRect(res => {
  242. if (res) {
  243. this.stickyHeight = res.height; // 单位 px
  244. } else {
  245. this.stickyHeight = 100; // 默认 fallback(约 200rpx = 100px)
  246. }
  247. }).exec();
  248. },
  249. // 获取所有标题在 scroll-view 中的绝对位置
  250. updateTitlePositions() {
  251. this.titlePositions = [];
  252. this.danyuanTitleRefs = [];
  253. if (!this.danyuanList.length) return;
  254. const query = uni.createSelectorQuery().in(this);
  255. this.danyuanList.forEach(item => {
  256. query.select(`#title-${item.danyuanId}`).boundingClientRect();
  257. this.danyuanTitleRefs.push(item);
  258. });
  259. query.exec((rects) => {
  260. rects.forEach((rect, index) => {
  261. if (rect) {
  262. // 在 scroll-view 初始未滚动时,rect.top 就是内容中的绝对 top
  263. this.titlePositions.push({
  264. danyuanId: this.danyuanTitleRefs[index].danyuanId,
  265. name: this.danyuanTitleRefs[index].danyuanName,
  266. intro: this.danyuanTitleRefs[index].danyuanIntro,
  267. top: rect.top
  268. });
  269. }
  270. });
  271. this.titlePositions.sort((a, b) => a.top - b.top);
  272. // 👇 新增:更新吸顶栏高度
  273. this.updateStickyHeight();
  274. });
  275. },
  276. handleScroll(e) {
  277. const scrollTop = e.detail.scrollTop;
  278. const firstTitle = this.titlePositions[0];
  279. if (!firstTitle) {
  280. this.currentStickyTitle = '';
  281. return;
  282. }
  283. // 👇 关键修改:提前触发吸顶
  284. const triggerOffset = scrollTop + this.stickyHeightPx;
  285. // 如果还没滚到第一个标题的顶部(考虑吸顶高度),则隐藏
  286. if (triggerOffset < firstTitle.top) {
  287. this.currentStickyTitle = '';
  288. return;
  289. }
  290. // 找出最后一个 top <= triggerOffset 的标题(即刚进入吸顶区的单元)
  291. let matched = null;
  292. for (let i = this.titlePositions.length - 1; i >= 0; i--) {
  293. if (this.titlePositions[i].top <= triggerOffset) {
  294. matched = this.titlePositions[i];
  295. break;
  296. }
  297. }
  298. console.log('matched',matched);
  299. if (matched) {
  300. this.currentStickyTitle = matched.name;
  301. this.currentStickyIntro = matched.intro;
  302. this.currentDanyuanId = matched.danyuanId;
  303. this.currentStickyDengjiId = this.banbenInfo.dengjiId || 1;
  304. } else {
  305. this.currentStickyTitle = '';
  306. }
  307. },
  308. getJieAndDanyuan(data, jieId) {
  309. for (let danyuan of data.danyuanList) {
  310. for (let jie of danyuan.jieList) {
  311. if (jie.jieId == jieId) {
  312. return { danyuan, jie }
  313. }
  314. }
  315. }
  316. return null;
  317. },
  318. async saveAndNavigate(jieId, type, da, code) {
  319. if (code == 'jixu') {
  320. if (!this.banbenId || !this.danyuanId) {
  321. toast("banbenId或者danyuanId 丢失")
  322. return false
  323. }
  324. }
  325. let req = {
  326. "banbenId": this.banbenId,
  327. "danyuanId": da.danyuanId,
  328. "jieId": jieId
  329. }
  330. console.log('req',req);
  331. try {
  332. const res = await shuxueSave(req);
  333. if (res.code == 0 && res.data) {
  334. let curJieAndDanyuan = this.getJieAndDanyuan(this.banbenInfo, jieId);
  335. if (!curJieAndDanyuan) {
  336. toast("未找到课程信息");
  337. return false;
  338. }
  339. const cacheData = cacheManager.get('xuexi-shuxue') || {};
  340. cacheData.curDanyuanName = curJieAndDanyuan.danyuan.danyuanName;
  341. cacheData.curKechengName = curJieAndDanyuan.jie.jieIntro;
  342. cacheData.curJieId = jieId;
  343. cacheData.type = curJieAndDanyuan.jie.type;
  344. cacheManager.set('xuexi-shuxue', cacheData);
  345. if (type == 1) {
  346. uni.navigateTo({ url: `/pages/xinshuxue/lookShipin?jieId=${jieId}` })
  347. } else {
  348. uni.navigateTo({ url: `/pages/xinshuxue/unitTest?jieId=${jieId}` })
  349. }
  350. } else {
  351. toast("保存位置出错");
  352. return false;
  353. }
  354. } catch (error) {
  355. toast("保存失败");
  356. return false;
  357. }
  358. },
  359. handlePlay(da, code) {
  360. let jieId = code === 'jixu' ? da.curJieId : da.jieId;
  361. if (!jieId) {
  362. toast("无课程ID");
  363. return;
  364. }
  365. this.saveAndNavigate(jieId, da.type, da, code);
  366. },
  367. handleClickDanyuan(danyuanId) {
  368. if (!danyuanId) {
  369. toast("danyuanId丢失")
  370. return false
  371. }
  372. this.isShow = true;
  373. setTimeout(() => {
  374. // 更新为点击的动态单元Id [临时]
  375. this.$refs.dyRef.handleShow(danyuanId)
  376. }, 100)
  377. },
  378. handleBack() {
  379. uni.navigateTo({
  380. url: `/pages/chanpinXuanze/banben?dengjiId=` + this.dengjiId
  381. })
  382. },
  383. },
  384. // 计算吸顶栏下方的偏移(确保内容不被遮挡)
  385. computed: {
  386. stickyHeightPx() {
  387. // 175rpx ≈ 87.5px,取整 90px 足够安全
  388. return 90;
  389. },
  390. topOffset() {
  391. // 只有当吸顶栏显示时,才增加上边距避免内容被遮挡
  392. return this.currentStickyTitle ? '80rpx' : '0rpx';
  393. }
  394. }
  395. }
  396. </script>