index.vue 10 KB

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