index.vue 12 KB

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