index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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.$nextTick(() => {
  196. this.updateTitlePositions();
  197. });
  198. }
  199. },
  200. loadDataFromApi() {
  201. this.banbenInfo = {}
  202. this.curProcess = ''
  203. this.danyuanList = []
  204. const req = {
  205. banbenId: this.banbenId
  206. }
  207. shuxueChanpinBanbenInfo(req).then(res => {
  208. if (res.code === 0) {
  209. this.banbenInfo = res.data;
  210. this.curProcess =res.data.curProcess * 100;
  211. this.danyuanList = res.data.danyuanList || [];
  212. if (this.danyuanList.length ==0) {
  213. this.existData = false
  214. }else{
  215. this.existData = true
  216. }
  217. // 保存到缓存(新增了参数保存)
  218. const cacheData = {
  219. ...res.data,
  220. banbenId: this.banbenId,
  221. chanpinId: this.chanpinId,
  222. danyuanId: this.danyuanId,
  223. dengjiId: this.dengjiId
  224. };
  225. cacheManager.set('xuexi-shuxue', cacheData);
  226. // 更新全局auth信息
  227. cacheManager.updateObject('auth', {
  228. chanpinId: res.data.chanpinId,
  229. banbenId: this.banbenId,
  230. danyuanId: res.data.curDanyuanId,
  231. dengjiId: res.data.dengjiId
  232. });
  233. this.danyuanId = res.data.curDanyuanId
  234. this.dengjiId = res.data.dengjiId
  235. this.chanpinId = res.data.chanpinId
  236. // 数据加载完成后初始化观察器
  237. this.$nextTick(() => {
  238. this.updateTitlePositions();
  239. });
  240. }
  241. }).catch(res => {
  242. cacheManager.remove("xuexi-shuxue");
  243. toast("加载失败,请重试");
  244. });
  245. },
  246. updateStickyHeight() {
  247. const query = uni.createSelectorQuery().in(this);
  248. query.select('.item-fixed').boundingClientRect(res => {
  249. if (res) {
  250. this.stickyHeight = res.height; // 单位 px
  251. } else {
  252. this.stickyHeight = 100; // 默认 fallback(约 200rpx = 100px)
  253. }
  254. }).exec();
  255. },
  256. // 获取所有标题在 scroll-view 中的绝对位置
  257. updateTitlePositions() {
  258. this.titlePositions = [];
  259. this.danyuanTitleRefs = [];
  260. if (!this.danyuanList.length) return;
  261. const query = uni.createSelectorQuery().in(this);
  262. this.danyuanList.forEach(item => {
  263. query.select(`#title-${item.danyuanId}`).boundingClientRect();
  264. this.danyuanTitleRefs.push(item);
  265. });
  266. query.exec((rects) => {
  267. rects.forEach((rect, index) => {
  268. if (rect) {
  269. // 在 scroll-view 初始未滚动时,rect.top 就是内容中的绝对 top
  270. this.titlePositions.push({
  271. danyuanId: this.danyuanTitleRefs[index].danyuanId,
  272. name: this.danyuanTitleRefs[index].danyuanName,
  273. intro: this.danyuanTitleRefs[index].danyuanIntro,
  274. top: rect.top
  275. });
  276. }
  277. });
  278. this.titlePositions.sort((a, b) => a.top - b.top);
  279. // 👇 新增:更新吸顶栏高度
  280. this.updateStickyHeight();
  281. });
  282. },
  283. handleScroll(e) {
  284. const scrollTop = e.detail.scrollTop;
  285. const firstTitle = this.titlePositions[0];
  286. if (!firstTitle) {
  287. this.currentStickyTitle = '';
  288. return;
  289. }
  290. // 👇 关键修改:提前触发吸顶
  291. const triggerOffset = scrollTop + this.stickyHeightPx;
  292. // 如果还没滚到第一个标题的顶部(考虑吸顶高度),则隐藏
  293. if (triggerOffset < firstTitle.top) {
  294. this.currentStickyTitle = '';
  295. return;
  296. }
  297. // 找出最后一个 top <= triggerOffset 的标题(即刚进入吸顶区的单元)
  298. let matched = null;
  299. for (let i = this.titlePositions.length - 1; i >= 0; i--) {
  300. if (this.titlePositions[i].top <= triggerOffset) {
  301. matched = this.titlePositions[i];
  302. break;
  303. }
  304. }
  305. console.log('matched',matched);
  306. if (matched) {
  307. this.currentStickyTitle = matched.name;
  308. this.currentStickyIntro = matched.intro;
  309. this.currentDanyuanId = matched.danyuanId;
  310. this.currentStickyDengjiId = this.banbenInfo.dengjiId || 1;
  311. } else {
  312. this.currentStickyTitle = '';
  313. }
  314. },
  315. getJieAndDanyuan(data, jieId) {
  316. for (let danyuan of data.danyuanList) {
  317. for (let jie of danyuan.jieList) {
  318. if (jie.jieId == jieId) {
  319. return { danyuan, jie }
  320. }
  321. }
  322. }
  323. return null;
  324. },
  325. async saveAndNavigate(jieId, type, da, code) {
  326. if (code == 'jixu') {
  327. if (!this.banbenId || !this.danyuanId) {
  328. toast("banbenId或者danyuanId 丢失")
  329. return false
  330. }
  331. }
  332. let req = {
  333. "banbenId": this.banbenId,
  334. "danyuanId": da.danyuanId,
  335. "jieId": jieId
  336. }
  337. console.log('req',req);
  338. try {
  339. const res = await shuxueSave(req);
  340. if (res.code == 0 && res.data) {
  341. let curJieAndDanyuan = this.getJieAndDanyuan(this.banbenInfo, jieId);
  342. if (!curJieAndDanyuan) {
  343. toast("未找到课程信息");
  344. return false;
  345. }
  346. const cacheData = cacheManager.get('xuexi-shuxue') || {};
  347. cacheData.curDanyuanName = curJieAndDanyuan.danyuan.danyuanName;
  348. cacheData.curKechengName = curJieAndDanyuan.jie.jieIntro;
  349. cacheData.curJieId = jieId;
  350. cacheData.type = curJieAndDanyuan.jie.type;
  351. cacheManager.set('xuexi-shuxue', cacheData);
  352. if (type == 1) {
  353. uni.navigateTo({ url: `/pages/xinshuxue/lookShipin?jieId=${jieId}` })
  354. } else {
  355. uni.navigateTo({ url: `/pages/xinshuxue/unitTest?jieId=${jieId}` })
  356. }
  357. } else {
  358. toast("保存位置出错");
  359. return false;
  360. }
  361. } catch (error) {
  362. toast("保存失败");
  363. return false;
  364. }
  365. },
  366. handlePlay(da, code) {
  367. let jieId = code === 'jixu' ? da.curJieId : da.jieId;
  368. if (!jieId) {
  369. toast("无课程ID");
  370. return;
  371. }
  372. this.saveAndNavigate(jieId, da.type, da, code);
  373. },
  374. handleClickDanyuan(danyuanId) {
  375. if (!danyuanId) {
  376. toast("danyuanId丢失")
  377. return false
  378. }
  379. this.isShow = true;
  380. setTimeout(() => {
  381. // 更新为点击的动态单元Id [临时]
  382. this.$refs.dyRef.handleShow(danyuanId)
  383. }, 100)
  384. },
  385. handleBack() {
  386. uni.navigateTo({
  387. url: `/pages/chanpinXuanze/banben?dengjiId=` + this.dengjiId
  388. })
  389. },
  390. },
  391. // 计算吸顶栏下方的偏移(确保内容不被遮挡)
  392. computed: {
  393. stickyHeightPx() {
  394. // 175rpx ≈ 87.5px,取整 90px 足够安全
  395. return 90;
  396. },
  397. topOffset() {
  398. // 只有当吸顶栏显示时,才增加上边距避免内容被遮挡
  399. return this.currentStickyTitle ? '80rpx' : '0rpx';
  400. }
  401. }
  402. }
  403. </script>