index.vue 10 KB

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