index.vue 12 KB

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