index.vue 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. <template>
  2. <view class="container">
  3. <!-- 当前单元标题(吸顶效果) -->
  4. <view class="sticky-title" v-if="currentUnitName">
  5. {{ showStickyTitle ? currentUnitName : "学习" }}
  6. </view>
  7. <!-- 返回顶部按钮 -->
  8. <view class="back-top" v-if="showBackTop" @click="scrollToTop">
  9. <text>↑</text>
  10. </view>
  11. <scroll-view scroll-y class="scroll-view" :scroll-top="scrollTop" @scroll="onScroll">
  12. <view class="subject-info" v-if="subjectDetail">
  13. <text class="course-name">{{ subjectDetail.chanpinName }}</text>
  14. <text class="course-name">等级:{{ subjectDetail.dengjiName }}</text>
  15. <text class="course-name">版本:{{ subjectDetail.dengjiName }}</text>
  16. <text class="course-name">单元:{{ subjectDetail.curDanyuanName }}</text>
  17. <progress :percent="20" class="exam-progress-box" stroke-width="10" backgroundColor="#3c7dfd"
  18. activeColor="#ffd11c" />
  19. <text class="course-name">继续学习</text>
  20. </view>
  21. <!-- 单元列表 -->
  22. <view v-for="(unit, index) in danyuanList" :key="unit.danyuanId" :id="'unit-' + index" class="unit-item">
  23. <!-- 单元标题 -->
  24. <view class="unit-title">
  25. <text class="unit-name">{{ unit.danyuanName }}</text>
  26. <text class="unit-intro">{{ unit.danyuanIntro }}</text>
  27. </view>
  28. <text class="unit-name">{{ unit.danyuanName }}</text>
  29. <!-- 节列表 -->
  30. <view v-for="section in unit.jieList" :key="section.jieId" class="section-item">
  31. <view class="status" :class="section.wanchengFlag === 1 ? 'completed' : 'uncompleted'">
  32. {{ section.wanchengFlag === 1 ? '已完成' : '未开始' }}
  33. </view>
  34. <view class="section-left">
  35. <image class="section-cover" :src="section.cover" mode="aspectFill"></image>
  36. <view class="section-info">
  37. <text class="section-name">{{ section.jieName }}</text>
  38. <text class="section-desc">{{ section.jieIntro }}</text>
  39. </view>
  40. </view>
  41. <view class="section-right">
  42. <text class="section-number">第{{ section.number }}节</text>
  43. <text>播放按钮</text>
  44. </view>
  45. </view>
  46. </view>
  47. <!-- 底部占位 -->
  48. <view style="height: 100px; text-align: center;">查看更多内容</view>
  49. </scroll-view>
  50. <CustomTabBar :currentTabNumber="1"></CustomTabBar>
  51. </view>
  52. </template>
  53. <script>
  54. import CustomTabBar from '@/components/custom-tabbar/custom-tabbar.vue';
  55. import {
  56. shuxueChanpinBanben
  57. } from "@/api/chanpinneirong.js"
  58. export default {
  59. data() {
  60. return {
  61. danyuanList: [],
  62. currentUnitName: "",
  63. subjectDetail: null,
  64. showBackTop: false,
  65. scrollTop: 0,
  66. pageCacheKey: 'learn_page_scroll_cache',
  67. hasRestoredScroll: false,
  68. showStickyTitle: false
  69. }
  70. },
  71. components: {
  72. CustomTabBar
  73. },
  74. onShow() {
  75. console.log('学习页面显示,尝试恢复滚动位置')
  76. // 如果没有恢复过,从缓存恢复
  77. if (!this.hasRestoredScroll) {
  78. this.restoreScrollPosition()
  79. }
  80. },
  81. // 新增:页面隐藏时保存滚动位置
  82. onHide() {
  83. console.log('学习页面隐藏,保存滚动位置')
  84. this.saveScrollPosition()
  85. },
  86. onLoad() {
  87. this.loadDataFromApi()
  88. },
  89. onReady() {
  90. // 页面渲染完成后执行
  91. setTimeout(() => {
  92. this.getUnitPositions();
  93. }, 300);
  94. },
  95. methods: {
  96. loadDataFromApi() {
  97. const req = {
  98. banbenId: 7
  99. }
  100. shuxueChanpinBanben(req).then(res => {
  101. this.subjectDetail = res.data
  102. this.danyuanList = res.data.danyuanList || []
  103. // 初始化当前单元
  104. if (this.danyuanList.length > 0) {
  105. this.currentUnitName = this.danyuanList[0].danyuanName
  106. }
  107. })
  108. },
  109. // 保存滚动位置到缓存
  110. saveScrollPosition() {
  111. if (this.scrollTop > 0) {
  112. const scrollData = {
  113. scrollTop: this.scrollTop,
  114. currentUnit: this.currentUnitName,
  115. timestamp: Date.now()
  116. }
  117. uni.setStorageSync(this.pageCacheKey, scrollData)
  118. console.log('保存滚动位置到缓存:', scrollData.scrollTop, '当前单元:', scrollData.currentUnit)
  119. }
  120. },
  121. // 从缓存恢复滚动位置
  122. restoreScrollPosition() {
  123. try {
  124. const saved = uni.getStorageSync(this.pageCacheKey)
  125. if (saved && saved.scrollTop > 0) {
  126. this.showStickyTitle = true
  127. // 延迟执行滚动
  128. setTimeout(() => {
  129. this.scrollTop = saved.scrollTop
  130. // 恢复当前单元名称
  131. if (saved.currentUnit) {
  132. this.currentUnitName = saved.currentUnit
  133. }
  134. this.hasRestoredScroll = true
  135. // 再次延迟确保滚动生效
  136. // setTimeout(() => {
  137. // this.scrollTop = saved.scrollTop + 0.01
  138. // }, 50)
  139. }, 100)
  140. } else {
  141. this.showStickyTitle = false
  142. }
  143. } catch (e) {
  144. console.error('读取缓存失败:', e)
  145. }
  146. },
  147. // 获取每个单元的位置信息
  148. getUnitPositions() {
  149. const query = uni.createSelectorQuery().in(this);
  150. this.danyuanList.forEach((unit, index) => {
  151. query.select('#unit-' + index).boundingClientRect();
  152. });
  153. query.exec((res) => {
  154. console.log('单元位置信息:', res);
  155. });
  156. },
  157. // 滚动事件处理
  158. onScroll(e) {
  159. const scrollTop = e.detail.scrollTop;
  160. // 更新滚动位置
  161. this.scrollTop = scrollTop;
  162. // 实时保存到缓存
  163. const saveData = {
  164. scrollTop: scrollTop,
  165. currentUnit: this.currentUnitName,
  166. timestamp: Date.now()
  167. }
  168. uni.setStorageSync(this.pageCacheKey, saveData)
  169. // 显示/隐藏返回顶部按钮
  170. this.showBackTop = scrollTop > 400;
  171. // 监听滚动,更新当前显示的单元
  172. this.updateCurrentUnit(scrollTop);
  173. },
  174. // 更新当前显示的单元
  175. updateCurrentUnit(scrollTop) {
  176. const query = uni.createSelectorQuery().in(this);
  177. for (let i = 0; i < this.danyuanList.length; i++) {
  178. query.select('#unit-' + i).boundingClientRect();
  179. }
  180. query.exec((res) => {
  181. // 添加一个偏移量,让切换更平滑
  182. const offset = 80;
  183. for (let i = 0; i < res.length; i++) {
  184. const rect = res[i];
  185. if (rect) {
  186. // 计算单元在页面中的实际位置(考虑滚动)
  187. const unitTop = rect.top + scrollTop;
  188. if (scrollTop + offset >= unitTop) {
  189. if (i == res.length - 1 || scrollTop + offset < (res[i + 1].top + scrollTop)) {
  190. // 每次滚动都设置吸顶标题显示状态
  191. this.showStickyTitle = i >= 1
  192. if (this.currentUnitName !== this.danyuanList[i].danyuanName) {
  193. this.currentUnitName = this.danyuanList[i].danyuanName;
  194. // 单元变化时立即保存
  195. const saveData = {
  196. scrollTop: this.scrollTop,
  197. currentUnit: this.currentUnitName,
  198. timestamp: Date.now()
  199. }
  200. uni.setStorageSync(this.pageCacheKey, saveData)
  201. }
  202. break;
  203. }
  204. }
  205. }
  206. }
  207. });
  208. },
  209. // 返回顶部
  210. scrollToTop() {
  211. // 清除缓存
  212. uni.removeStorageSync(this.pageCacheKey)
  213. this.hasRestoredScroll = false
  214. this.showStickyTitle = false
  215. // 回到顶部
  216. this.scrollTop = this.scrollTop + 1; // 先改变值触发重新渲染
  217. this.$nextTick(() => {
  218. this.scrollTop = 0;
  219. this.currentUnitName = this.danyuanList[0].danyuanName;
  220. });
  221. }
  222. }
  223. }
  224. </script>
  225. <style scoped>
  226. .container {
  227. height: 100vh;
  228. background-color: #f5f5f5;
  229. }
  230. /* 吸顶标题 */
  231. .sticky-title {
  232. position: fixed;
  233. top: 0;
  234. left: 0;
  235. right: 0;
  236. z-index: 100;
  237. background-color: #1890ff;
  238. color: white;
  239. padding: 12px 16px;
  240. font-size: 16px;
  241. font-weight: bold;
  242. text-align: center;
  243. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  244. }
  245. /* 返回顶部按钮 */
  246. .back-top {
  247. position: fixed;
  248. right: 16px;
  249. bottom: 100px;
  250. z-index: 99;
  251. width: 44px;
  252. height: 44px;
  253. background-color: #1890ff;
  254. border-radius: 50%;
  255. display: flex;
  256. align-items: center;
  257. justify-content: center;
  258. color: white;
  259. font-size: 20px;
  260. box-shadow: 0 2px 8px rgba(24, 144, 255, 0.4);
  261. }
  262. .back-top:active {
  263. background-color: #096dd9;
  264. transform: scale(0.95);
  265. }
  266. /* 滚动区域 */
  267. .scroll-view {
  268. height: 100%;
  269. padding-top: 50px;
  270. /* 给吸顶标题留出空间 */
  271. box-sizing: border-box;
  272. }
  273. /* 单元样式 */
  274. .unit-item {
  275. margin: 12px;
  276. background: white;
  277. border-radius: 8px;
  278. overflow: hidden;
  279. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
  280. }
  281. .unit-title {
  282. padding: 16px;
  283. background: linear-gradient(135deg, #1890ff, #36cfc9);
  284. color: white;
  285. }
  286. .unit-name {
  287. font-size: 18px;
  288. font-weight: bold;
  289. display: block;
  290. margin-bottom: 4px;
  291. }
  292. .unit-intro {
  293. font-size: 14px;
  294. opacity: 0.9;
  295. }
  296. /* 节样式 */
  297. .section-item {
  298. padding: 12px 16px;
  299. border-bottom: 1px solid #f0f0f0;
  300. display: flex;
  301. justify-content: space-between;
  302. align-items: center;
  303. }
  304. .section-item:last-child {
  305. border-bottom: none;
  306. }
  307. .section-left {
  308. display: flex;
  309. align-items: center;
  310. flex: 1;
  311. }
  312. .section-cover {
  313. width: 60px;
  314. height: 60px;
  315. border-radius: 6px;
  316. margin-right: 12px;
  317. }
  318. .section-info {
  319. flex: 1;
  320. }
  321. .section-name {
  322. font-size: 16px;
  323. color: #333;
  324. display: block;
  325. margin-bottom: 4px;
  326. font-weight: 500;
  327. }
  328. .section-desc {
  329. font-size: 13px;
  330. color: #666;
  331. }
  332. .section-right {
  333. display: flex;
  334. flex-direction: column;
  335. align-items: flex-end;
  336. margin-left: 12px;
  337. }
  338. .section-number {
  339. font-size: 12px;
  340. color: #999;
  341. margin-bottom: 4px;
  342. }
  343. .status {
  344. font-size: 12px;
  345. padding: 2px 8px;
  346. border-radius: 10px;
  347. }
  348. .status.completed {
  349. background-color: #f6ffed;
  350. color: #52c41a;
  351. border: 1px solid #b7eb8f;
  352. }
  353. .status.uncompleted {
  354. background-color: #fff7e6;
  355. color: #fa8c16;
  356. border: 1px solid #ffd591;
  357. }
  358. </style>