index.vue 12 KB

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