index.vue 12 KB

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