index.vue 9.2 KB

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