四零语境前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2641 lines
97 KiB

1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
  1. <template>
  2. <view class="book-container">
  3. <!-- 条件编译 -->
  4. <!-- #ifndef H5 -->
  5. <uv-status-bar></uv-status-bar>
  6. <!-- 自定义顶部导航栏 -->
  7. <view class="custom-navbar" :class="{ 'navbar-hidden': !showNavbar }">
  8. <uv-status-bar></uv-status-bar>
  9. <view class="navbar-content">
  10. <view class="navbar-left" @click="goBack">
  11. <uv-icon name="arrow-left" size="20" color="#262626"></uv-icon>
  12. </view>
  13. <view class="navbar-title">{{ currentPageTitle }}</view>
  14. </view>
  15. </view>
  16. <!-- #endif -->
  17. <!-- Swiper内容区域 -->
  18. <swiper class="content-swiper" :current="currentPage - 1" @change="onSwiperChange">
  19. <swiper-item v-for="(page, index) in bookPages" :key="index" class="swiper-item">
  20. <scroll-view scroll-y :scroll-top="scrollTops[index] || 0" :scroll-with-animation="true"
  21. style="height: 100vh;"
  22. :id="`scroll-container-${index}`"
  23. class="scroll-container" @scroll="onScroll" @touchstart="onTouchStart"
  24. @touchmove="onTouchMove" @touchend="onTouchEnd">
  25. <view class="content-area" @click="toggleNavbar">
  26. <view class="title">{{ currentPageTitle }}</view>
  27. <!-- 会员限制页面 -->
  28. <view v-if="!isMember && pagePay[index] === 'Y' && userInfo.freeUser != 'Y'" class="member-content">
  29. <text class="member-title">{{ pageTitles[index] }}</text>
  30. <view class="member-button" @click.stop="unlockBook">
  31. <text class="member-button-text">升级会员解锁</text>
  32. </view>
  33. </view>
  34. <!-- 图片卡片页面 -->
  35. <view class="card-content" v-else-if="pageTypes[index] === '1'">
  36. <view class="card-line">
  37. <image :src="configParamContent('highlight_icon')" class="card-line-image"
  38. mode="aspectFill" />
  39. <text class="card-line-text">划线重点</text>
  40. </view>
  41. <view v-for="(item, itemIndex) in page" :key="itemIndex" class="text-content">
  42. <image class="card-image"
  43. :id="`image-${itemIndex}`"
  44. v-if="item && item.type === 'image'" :src="item.imageUrl"
  45. mode="widthFix"></image>
  46. <view :class="{
  47. 'lead-text': isCardTextHighlighted(page, itemIndex),
  48. 'introduction-text' : item.isLead,
  49. }"
  50. :id="`text-${itemIndex}`"
  51. v-else-if="item && item.type === 'text' && item.content"
  52. @click.stop="handleTextClick(item.content, item, index)">
  53. <text v-for="(segment, segmentIndex) in processChineseText(item.content)"
  54. :key="segmentIndex"
  55. :class="['chinese-segment', { 'clickable-keyword': segment.isKeyword }]"
  56. @click.stop="segment.isKeyword ? handleChineseKeywordClick(segment.keywordData) : handleTextClick(item.content, item, index)"
  57. user-select :style="item.style" :id="`text-segment-${segmentIndex}`">{{
  58. segment.text }}</text>
  59. </view>
  60. </view>
  61. </view>
  62. <view v-else>
  63. <view v-for="(item, itemIndex) in page" :key="itemIndex">
  64. <!-- 文本页面 -->
  65. <view v-if="item && item.type === 'text' && item.content" class="text-content">
  66. <view :class="{
  67. 'lead-text': isTextHighlighted(page, itemIndex),
  68. 'introduction-text' : item.isLead,
  69. }"
  70. @click.stop="handleTextClick(item.content, item, index)"
  71. :ref="`textRef_${index}_${itemIndex}`" :id="`text-${itemIndex}`">
  72. <text class="content-text clickable-text"
  73. :style="item.style" user-select>
  74. {{ item.content }}
  75. </text>
  76. </view>
  77. </view>
  78. <!-- 图片页面 -->
  79. <view v-else-if="item.type === 'image'" class="image-container"
  80. :ref="`imageRef_${index}_${itemIndex}`" :id="`image-${itemIndex}`">
  81. <image class="content-image" :src="item.imageUrl" mode="widthFix"></image>
  82. </view>
  83. <!-- 视频页面 -->
  84. <view v-else-if="item.type === 'video'" class="video-content" :id="`video-${itemIndex}`" @click.stop>
  85. <!-- 视频加载状态 -->
  86. <view v-if="videoLoading" class="video-loading">
  87. <text class="loading-text">视频加载中...</text>
  88. </view>
  89. <!-- 视频播放器 -->
  90. <video v-else :src="item.url" class="video-player" controls :poster="item.coverUrl"
  91. @loadstart="onVideoLoadStart" @loadeddata="onVideoLoadStart"
  92. @error="onVideoError"></video>
  93. </view>
  94. </view>
  95. </view>
  96. </view>
  97. </scroll-view>
  98. </swiper-item>
  99. </swiper>
  100. <!-- 自定义底部控制栏 -->
  101. <CustomTabbar :show-navbar="showNavbar" :current-page="currentPage" :course-id="courseId" :voice-id="voiceId"
  102. :book-pages="bookPages" :is-text-page="isTextPage" :should-load-audio="shouldLoadAudio"
  103. :is-member="isMember" :current-page-requires-member="currentPageRequiresMember" :page-pay="pagePay"
  104. :is-word-audio-playing="isWordAudioPlaying" @toggle-course-popup="toggleCoursePopup"
  105. @toggle-sound="toggleSound" @go-to-page="goToPage" @previous-page="previousPage" @next-page="nextPage"
  106. @audio-state-change="onAudioStateChange" @highlight-change="onHighlightChange"
  107. @scroll-to-text="onScrollToText" @voice-change-complete="onVoiceChangeComplete"
  108. @voice-change-error="onVoiceChangeError" @page-data-needed="onPageDataNeeded" @loop-mode-change="onLoopModeChange" ref="customTabbar" />
  109. <!-- 课程选择弹出窗 -->
  110. <CoursePopup :style="{ zIndex: 10000 }" :course-list="courseList" :current-course="currentCourse"
  111. :is-reversed="isReversed" @toggle-sort="toggleSort" @select-course="selectCourse" ref="coursePopup" />
  112. <!-- 释义弹出窗 -->
  113. <MeaningPopup :style="{ zIndex: 10000 }" :current-word-meaning="currentWordMeaning"
  114. @close-meaning-popup="closeMeaningPopup" @repeat-word-audio="repeatWordAudio" ref="meaningPopup" />
  115. <!-- 悬浮按钮组件 -->
  116. <FloatingButtons
  117. :is-last-page="isLastPage"
  118. :has-next-course="hasNextCourse"
  119. @next-course="goToNextCourse"
  120. @back-to-start="backToStart"
  121. />
  122. </view>
  123. </template>
  124. <script>
  125. import AudioControls from './AudioControls.vue'
  126. import CustomTabbar from './components/CustomTabbar.vue'
  127. import CoursePopup from './components/CoursePopup.vue'
  128. import MeaningPopup from './components/MeaningPopup.vue'
  129. import FloatingButtons from './components/FloatingButtons.vue'
  130. import audioManager from '@/utils/audioManager.js'
  131. export default {
  132. components: {
  133. AudioControls,
  134. CustomTabbar,
  135. CoursePopup,
  136. MeaningPopup,
  137. FloatingButtons
  138. },
  139. data() {
  140. return {
  141. isMember: false,
  142. memberId: '',
  143. voiceId: null,
  144. courseId: '',
  145. showNavbar: true,
  146. currentPage: 1, // 当前页面索引
  147. currentCourse: 1, // 当前课程索引
  148. audioLoopMode: 'sequential', // 音频循环模式:顺序/单句/单页
  149. currentWordMeaning: null, // 当前显示的单词释义
  150. isReversed: false, // 是否倒序显示
  151. // 文本高亮相关 - 由AudioControls组件管理,这里只保留必要的接口
  152. currentHighlightIndex: -1, // 当前高亮的文本索引,用于模板渲染
  153. wordAudioCache: {}, // 單詞語音緩存
  154. // 注意:音频实例现在由audioManager统一管理,不再在组件中维护
  155. isWordAudioPlaying: false, // 是否有单词音频正在播放
  156. // 音频状态相关 - 这些状态现在由AudioControls组件管理
  157. // 保留这些属性用于与AudioControls组件的数据同步
  158. isAudioLoading: false, // 音频是否正在加载
  159. hasAudioData: false, // 是否有音频数据
  160. audioLoadFailed: false, // 音频加载是否失败
  161. // 视频状态相关
  162. videoLoading: false, // 视频是否正在加载
  163. // 滚动相关
  164. scrollTops: [], // 每个页面的scroll-view滚动位置数组
  165. scrollDebounceTimer: null, // 滚动防抖定时器
  166. isScrolling: false, // 是否正在滚动中
  167. // 手动滚动检测相关
  168. isUserTouching: false, // 用户是否正在触摸屏幕
  169. touchStartTime: 0, // 触摸开始时间
  170. touchStartY: 0, // 触摸开始Y坐标
  171. userScrollTimer: null, // 用户滚动检测定时器
  172. lastUserScrollTime: 0, // 最后一次用户滚动时间
  173. courseIdList: [],
  174. bookTitle: '',
  175. courseList: [
  176. ],
  177. // 二维数组 代表每个页面
  178. bookPages: [
  179. ],
  180. // 存储每个页面的标题
  181. pageTitles: [],
  182. // 存储每个页面的type信息
  183. pageTypes: [],
  184. // 存储每个页面的单词释义数据
  185. pageWords: [],
  186. // 存储每个页面的付费状态
  187. pagePay: [],
  188. elementPositionsCache: {}, // 缓存每个页面的元素位置信息
  189. }
  190. },
  191. onShow() {
  192. if (uni.getStorageSync('token')) {
  193. this.$store.dispatch('getUserInfo');
  194. }
  195. },
  196. computed: {
  197. displayCourseList() {
  198. return this.isReversed ? [...this.courseList].reverse() : this.courseList;
  199. },
  200. // 从Vuex获取音色列表
  201. voiceList() {
  202. return this.$store.state.voiceList;
  203. },
  204. // 从Vuex获取默认音色ID
  205. defaultVoiceId() {
  206. return this.$store.state.defaultVoiceId;
  207. },
  208. // 判断当前页面是否为文字类型
  209. isTextPage() {
  210. // 如果是卡片页面(type为'1'),不显示音频控制栏
  211. if (this.currentPageType === '1') {
  212. return true;
  213. }
  214. const currentPageData = this.bookPages[this.currentPage - 1];
  215. // currentPageData是一个数组 其中的一个元素的type是text就会返回true
  216. return currentPageData && currentPageData.some(item => item.type === 'text');
  217. },
  218. // 判断当前页面是否需要加载音频(包括文本页面和卡片页面)
  219. shouldLoadAudio() {
  220. // 文本页面需要加载音频
  221. if (this.isTextPage) {
  222. return true;
  223. }
  224. // 卡片页面(type为'1')也需要加载音频以支持点击播放
  225. if (this.currentPageType === '1') {
  226. return true;
  227. }
  228. return false;
  229. },
  230. // 动态页面标题
  231. currentPageTitle() {
  232. return this.pageTitles[this.currentPage - 1] || this.bookTitle;
  233. },
  234. // 当前页面类型
  235. currentPageType() {
  236. return this.pageTypes[this.currentPage - 1] || '';
  237. },
  238. // 当前页面的单词释义数据
  239. currentPageWords() {
  240. return this.pageWords[this.currentPage - 1] || [];
  241. },
  242. // 当前页面是否需要会员
  243. currentPageRequiresMember() {
  244. // 免费用户不受会员限制
  245. if (this.userInfo && this.userInfo.freeUser === 'Y') {
  246. return false;
  247. }
  248. return this.pagePay[this.currentPage - 1] === 'Y';
  249. },
  250. // 判断是否为当前课程的最后一页
  251. isLastPage() {
  252. return this.currentPage === this.bookPages.length;
  253. },
  254. // 判断是否有下一课
  255. hasNextCourse() {
  256. if (!this.courseList || this.courseList.length === 0) return false;
  257. // 使用 courseId 而不是 currentCourse,因为 courseId 是当前正在学习的课程ID
  258. const currentCourseIndex = this.courseList.findIndex(course => course.id == this.courseId);
  259. return currentCourseIndex >= 0 && currentCourseIndex < this.courseList.length - 1;
  260. }
  261. },
  262. // watch: {
  263. // scrollTops: {
  264. // handler(newVal, oldVal) {
  265. // console.log('📊 scrollTops变化:', {
  266. // currentPage: this.currentPage,
  267. // newScrollTops: newVal,
  268. // currentPageScrollTop: newVal[this.currentPage - 1]
  269. // });
  270. // },
  271. // deep: true
  272. // }
  273. // },
  274. methods: {
  275. // 触摸开始事件 - 检测用户开始触摸
  276. onTouchStart(e) {
  277. this.isUserTouching = true;
  278. this.touchStartTime = Date.now();
  279. this.touchStartY = e.touches[0].pageY;
  280. // 清除之前的用户滚动定时器
  281. if (this.userScrollTimer) {
  282. clearTimeout(this.userScrollTimer);
  283. this.userScrollTimer = null;
  284. }
  285. console.log('👆 用户开始触摸屏幕');
  286. },
  287. // 触摸移动事件 - 检测用户滚动操作
  288. onTouchMove(e) {
  289. if (!this.isUserTouching) return;
  290. const currentY = e.touches[0].pageY;
  291. const deltaY = Math.abs(currentY - this.touchStartY);
  292. // 如果移动距离超过阈值,认为是滚动操作
  293. if (deltaY > 10) {
  294. // 记录用户滚动时间
  295. this.lastUserScrollTime = Date.now();
  296. // 如果当前正在自动滚动,立即停止
  297. if (this.isScrolling) {
  298. console.log('🛑 检测到用户手动滚动,停止自动滚动');
  299. this.isScrolling = false;
  300. // 清除滚动防抖定时器
  301. if (this.scrollDebounceTimer) {
  302. clearTimeout(this.scrollDebounceTimer);
  303. this.scrollDebounceTimer = null;
  304. }
  305. }
  306. }
  307. },
  308. // 触摸结束事件 - 用户停止触摸
  309. onTouchEnd(e) {
  310. this.isUserTouching = false;
  311. // 设置一个短暂的延迟,在用户停止触摸后的一段时间内仍然阻止自动滚动
  312. // 这样可以避免用户刚停止滚动就立即触发自动滚动
  313. this.userScrollTimer = setTimeout(() => {
  314. console.log('✋ 用户滚动操作结束,允许自动滚动');
  315. this.userScrollTimer = null;
  316. }, 500); // 减少到500ms,提高响应性
  317. console.log('👆 用户停止触摸屏幕');
  318. },
  319. // 检查是否应该阻止自动滚动
  320. shouldPreventAutoScroll() {
  321. // 降低敏感度:只有在用户正在触摸且最近有滚动行为时才阻止
  322. const now = Date.now();
  323. const recentUserScroll = this.userScrollTimer !== null && (now - this.lastUserScrollTime) < 1000;
  324. return this.isUserTouching && recentUserScroll;
  325. },
  326. // 处理scroll-view滚动事件
  327. onScroll(e) {
  328. // 更新当前页面的滚动位置
  329. const scrollTop = e.detail.scrollTop;
  330. const currentPageIndex = this.currentPage - 1;
  331. const previousScrollTop = this.scrollTops[currentPageIndex] || 0;
  332. // 只有当滚动位置发生显著变化时才更新
  333. if (Math.abs(previousScrollTop - scrollTop) > 5) {
  334. // 检测是否为手动滚动(如果正在自动滚动中,但滚动位置与预期不符,则认为是手动滚动)
  335. if (this.isScrolling) {
  336. // 提高手动滚动检测阈值,减少误判
  337. const scrollDifference = Math.abs(previousScrollTop - scrollTop);
  338. if (scrollDifference > 80) { // 从50提高到80,减少误判
  339. console.log('🖐️ 检测到手动滚动,中断自动滚动状态');
  340. this.isScrolling = false;
  341. this.lastUserScrollTime = Date.now(); // 记录手动滚动时间
  342. }
  343. }
  344. this.$set(this.scrollTops, currentPageIndex, scrollTop);
  345. }
  346. },
  347. // 视频事件处理方法
  348. onVideoLoadStart() {
  349. this.videoLoading = true;
  350. },
  351. onVideoCanPlay() {
  352. this.videoLoading = false;
  353. },
  354. onVideoError() {
  355. this.videoLoading = false;
  356. uni.showToast({
  357. title: '视频加载失败',
  358. icon: 'none',
  359. duration: 2000
  360. });
  361. },
  362. // 獲取用戶會員信息 判斷是否和傳參傳過來的會員id相同
  363. async getMemberInfo() {
  364. // 检查是否为免费用户
  365. if (this.userInfo && this.userInfo.freeUser === 'Y') {
  366. this.isMember = true; // 免费用户享有会员权限
  367. return;
  368. }
  369. const memberRes = await this.$api.member.getUserMemberInfo()
  370. if (memberRes.code === 200) {
  371. this.isMember = memberRes.result.map(item => item.memberId).includes(this.memberId)
  372. }
  373. },
  374. // 处理AudioControls组件的事件
  375. onAudioStateChange(audioState) {
  376. // 更新高亮状态
  377. this.currentHighlightIndex = audioState.currentHighlightIndex;
  378. // 更新音频加载状态(用于控制UI显示)
  379. if (audioState.hasOwnProperty('isLoading')) {
  380. this.isAudioLoading = audioState.isLoading;
  381. }
  382. // 更新音频数据状态
  383. if (audioState.hasOwnProperty('hasAudioData')) {
  384. this.hasAudioData = audioState.hasAudioData;
  385. }
  386. // 更新音频加载失败状态
  387. if (audioState.hasOwnProperty('audioLoadFailed')) {
  388. this.audioLoadFailed = audioState.audioLoadFailed;
  389. }
  390. },
  391. // 处理页面数据需要重新加载的事件
  392. async onPageDataNeeded(pageNumber) {
  393. console.log('收到页面数据需要重新加载的请求,页面:', pageNumber);
  394. // 如果页面数据不存在或为空,重新获取
  395. if (!this.bookPages || this.bookPages.length === 0 || !this.bookPages[pageNumber - 1]) {
  396. console.log('页面数据不存在,重新获取页面数据');
  397. try {
  398. await this.getBookPages();
  399. console.log('页面数据重新获取完成');
  400. // 页面数据更新后,AudioControls组件的bookPages监听器会自动触发音频获取
  401. // 无需手动调用getCurrentPageAudio,避免重复调用
  402. } catch (error) {
  403. console.error('重新获取页面数据失败:', error);
  404. }
  405. } else {
  406. console.log('页面数据已存在,无需重新获取');
  407. }
  408. },
  409. // 接收循环模式变化事件
  410. onLoopModeChange(mode) {
  411. this.audioLoopMode = mode;
  412. const labels = { sequential: '顺序', sentence: '单句', page: '单页' };
  413. const label = labels[mode] || '顺序';
  414. // 可选:提示当前播放模式
  415. uni.showToast({ title: `播放模式:${label}`, icon: 'none', duration: 800 });
  416. },
  417. // 处理音色切换完成事件
  418. onVoiceChangeComplete(data) {
  419. // 可以在这里添加一些UI反馈,比如显示切换成功的提示
  420. if (data.hasAudioData) {
  421. } else {
  422. }
  423. // 如果启用了预加载所有页面
  424. if (data.preloadAllPages) {
  425. // 可以显示一个提示,告诉用户正在后台加载
  426. uni.showToast({
  427. title: '正在加载新音色...',
  428. icon: 'loading',
  429. duration: 2000
  430. });
  431. }
  432. },
  433. // 处理音色切换错误事件
  434. onVoiceChangeError(error) {
  435. console.error('音色切换失败:', error);
  436. // 可以在这里显示错误提示给用户
  437. uni.showToast({
  438. title: '音色切换失败,请重试',
  439. icon: 'none',
  440. duration: 2000
  441. });
  442. },
  443. // 处理文本点击事件
  444. handleTextClick(textContent, item, pageIndex) {
  445. if (this.isWordAudioPlaying) {
  446. this.isWordAudioPlaying = false;
  447. }
  448. // 检查是否点击的是当前页面
  449. if (pageIndex !== undefined && pageIndex !== this.currentPage - 1) {
  450. console.warn('⚠️ 点击的不是当前页面,忽略点击事件');
  451. return;
  452. }
  453. // 验证参数有效性
  454. if (!item) {
  455. console.error('❌ handleTextClick: item参数为空');
  456. uni.showToast({
  457. title: '数据错误,请刷新页面',
  458. icon: 'none'
  459. });
  460. return;
  461. }
  462. // 如果textContent为undefined,尝试从item中获取
  463. if (!textContent && item && item.content) {
  464. textContent = item.content;
  465. }
  466. // 最终验证textContent
  467. if (!textContent || typeof textContent !== 'string' || textContent.trim() === '') {
  468. console.error('❌ handleTextClick: 无效的文本内容', textContent);
  469. uni.showToast({
  470. title: '文本内容无效',
  471. icon: 'none'
  472. });
  473. return;
  474. }
  475. if (!this.$refs.customTabbar) {
  476. console.error('❌ customTabbar引用不存在');
  477. uni.showToast({
  478. title: '音频控制组件未准备好',
  479. icon: 'none'
  480. });
  481. return;
  482. }
  483. // console.log(' audioControls存在:', !!this.$refs.customTabbar.$refs.audioControls);
  484. if (!this.$refs.customTabbar.$refs.audioControls) {
  485. console.error('❌ audioControls引用不存在');
  486. uni.showToast({
  487. title: '音频控制组件未准备好',
  488. icon: 'none'
  489. });
  490. return;
  491. }
  492. // 检查当前页面是否为文本页面或卡片页面
  493. // 卡片页面(type为'1')现在也支持整句音频播放
  494. // 特别针对划线重点页面的调试
  495. if (this.currentPageType === '1') {
  496. }
  497. if (!this.isTextPage && this.currentPageType !== '1') {
  498. console.warn('⚠️ 当前页面不是文本页面或卡片页面');
  499. uni.showToast({
  500. title: '当前页面不支持音频播放',
  501. icon: 'none'
  502. });
  503. return;
  504. }
  505. // 获取音频控制组件实例
  506. const audioControls = this.$refs.customTabbar.$refs.audioControls;
  507. // 检查音频是否正在加载中
  508. if (audioControls.isAudioLoading) {
  509. uni.showToast({
  510. title: '音频正在加载中,请稍后再试',
  511. icon: 'loading',
  512. duration: 1500
  513. });
  514. // 等待音频加载完成后自动播放
  515. const checkAndPlay = () => {
  516. if (!audioControls.isAudioLoading && audioControls.currentPageAudios.length > 0) {
  517. const success = audioControls.playSpecificAudio(textContent);
  518. if (!success) {
  519. console.error('❌ 音频加载完成后播放失败');
  520. }
  521. } else if (!audioControls.isAudioLoading) {
  522. console.error('❌ 音频加载完成但没有音频数据');
  523. uni.showToast({
  524. title: '当前页面没有音频内容',
  525. icon: 'none'
  526. });
  527. } else {
  528. // 继续等待
  529. setTimeout(checkAndPlay, 500);
  530. }
  531. };
  532. // 延迟检查,给音频加载一些时间
  533. setTimeout(checkAndPlay, 500);
  534. return;
  535. }
  536. // 检查是否有音频数据
  537. if (!audioControls.currentPageAudios || audioControls.currentPageAudios.length === 0) {
  538. console.warn('⚠️ 当前页面没有音频数据,尝试重新加载');
  539. uni.showToast({
  540. title: '正在重新加载音频...',
  541. icon: 'loading'
  542. });
  543. // 尝试重新加载音频
  544. audioControls.getCurrentPageAudio();
  545. // 等待重新加载完成后播放
  546. const retryPlay = () => {
  547. if (!audioControls.isAudioLoading && audioControls.currentPageAudios.length > 0) {
  548. const success = audioControls.playSpecificAudio(textContent);
  549. if (!success) {
  550. console.error('❌ 音频重新加载后播放失败');
  551. }
  552. } else if (!audioControls.isAudioLoading) {
  553. console.error('❌ 音频重新加载失败');
  554. uni.showToast({
  555. title: '音频加载失败,请检查网络连接',
  556. icon: 'none'
  557. });
  558. } else {
  559. // 继续等待
  560. setTimeout(retryPlay, 500);
  561. }
  562. };
  563. setTimeout(retryPlay, 1500);
  564. return;
  565. }
  566. const success = audioControls.playSpecificAudio(textContent);
  567. if (success) {
  568. // console.log('✅ 成功播放指定音频段落');
  569. } else {
  570. console.error('❌ 播放指定音频段落失败');
  571. }
  572. },
  573. onHighlightChange(highlightData) {
  574. // 兼容旧格式(直接传递索引)和新格式(传递对象)
  575. if (typeof highlightData === 'number') {
  576. // 旧格式:直接是索引
  577. this.currentHighlightIndex = highlightData;
  578. } else if (typeof highlightData === 'object' && highlightData !== null) {
  579. // 新格式:包含详细信息的对象
  580. this.currentHighlightIndex = highlightData.highlightIndex;
  581. // 可以在这里处理分段音频的额外信息
  582. if (highlightData.isSegmented) {
  583. // console.log('分段音频高亮:', {
  584. // highlightIndex: highlightData.highlightIndex,
  585. // segmentIndex: highlightData.segmentIndex,
  586. // startIndex: highlightData.startIndex,
  587. // endIndex: highlightData.endIndex,
  588. // currentText: highlightData.currentText
  589. // });
  590. }
  591. } else {
  592. // 清除高亮
  593. this.currentHighlightIndex = -1;
  594. }
  595. },
  596. // 处理滚动到高亮文本
  597. onScrollToText(scrollData) {
  598. console.log('📍 收到滚动请求:', scrollData);
  599. // 检查是否应该阻止自动滚动(用户正在手动操作)
  600. if (this.shouldPreventAutoScroll()) {
  601. // console.log('🚫 用户正在手动滚动,跳过自动滚动到文本');
  602. return;
  603. }
  604. // 防抖处理:如果正在滚动中,清除之前的定时器
  605. if (this.scrollDebounceTimer) {
  606. clearTimeout(this.scrollDebounceTimer);
  607. }
  608. this.scrollDebounceTimer = setTimeout(() => {
  609. // 再次检查是否应该阻止自动滚动
  610. if (this.shouldPreventAutoScroll()) {
  611. // console.log('🚫 防抖延迟后检测到用户手动滚动,跳过自动滚动到文本');
  612. return;
  613. }
  614. this.performScrollToText(scrollData);
  615. }, 50); // 减少防抖延迟,提高响应性
  616. },
  617. // 执行滚动到高亮文本的具体逻辑
  618. performScrollToText(scrollData) {
  619. // 最终检查:如果用户正在手动操作,直接返回
  620. if (this.shouldPreventAutoScroll()) {
  621. // console.log('🚫 执行滚动前检测到用户手动操作,取消自动滚动');
  622. return;
  623. }
  624. // 确保在任何情况下都能重置滚动状态
  625. const resetScrollingState = () => {
  626. this.isScrolling = false;
  627. console.log('🔄 滚动状态已重置');
  628. };
  629. // 设置安全超时,确保状态不会永久卡住
  630. const safetyTimeout = setTimeout(() => {
  631. if (this.isScrolling) {
  632. console.warn('⚠️ 滚动状态安全超时,强制重置');
  633. resetScrollingState();
  634. }
  635. }, 2000); // 2秒安全超时
  636. if (!scrollData || typeof scrollData.highlightIndex !== 'number' || scrollData.highlightIndex < 0) {
  637. console.warn('滚动数据无效:', scrollData);
  638. clearTimeout(safetyTimeout);
  639. return;
  640. }
  641. // 确保在当前页面
  642. if (scrollData.currentPage && scrollData.currentPage !== this.currentPage) {
  643. console.warn('页面不匹配,跳过滚动:', {
  644. scrollDataPage: scrollData.currentPage,
  645. currentPage: this.currentPage
  646. });
  647. clearTimeout(safetyTimeout);
  648. return;
  649. }
  650. // 如果正在滚动中,跳过本次滚动
  651. if (this.isScrolling) {
  652. console.warn('正在滚动中,跳过本次滚动');
  653. clearTimeout(safetyTimeout);
  654. return;
  655. }
  656. // 构建元素选择器
  657. let selector = '';
  658. if (scrollData.isSegmented && typeof scrollData.segmentIndex === 'number') {
  659. // 分段音频:使用分段索引
  660. selector = `#text-segment-${scrollData.segmentIndex}`;
  661. } else {
  662. // 普通音频:需要找到对应的文本元素
  663. // originalTextIndex是指向原始页面数据中的索引,需要映射到实际的DOM元素
  664. const targetItemIndex = this.findTextItemIndex(scrollData.highlightIndex);
  665. if (targetItemIndex !== -1) {
  666. selector = `#text-${targetItemIndex}`;
  667. } else {
  668. console.warn('无法找到对应的文本元素索引:', scrollData.highlightIndex);
  669. selector = `#text-${scrollData.highlightIndex}`; // 备用方案
  670. }
  671. }
  672. //console.log('开始滚动到文本:', { selector, scrollData });
  673. // 标记正在滚动
  674. this.isScrolling = true;
  675. // 等待DOM更新后再查找元素
  676. this.$nextTick(async () => {
  677. try {
  678. // 获取所有元素的真实位置信息
  679. const elementPositions = await this.getAllElementPositions();
  680. console.log('📏 获取到的元素位置信息:', elementPositions);
  681. // 计算精确的滚动位置
  682. const preciseScrollTop = await this.calculatePreciseScrollPosition(scrollData, elementPositions);
  683. if (preciseScrollTop !== null) {
  684. // 检查是否需要滚动(避免不必要的滚动)
  685. const currentScroll = this.scrollTops[this.currentPage - 1] || 0;
  686. const scrollDifference = Math.abs(preciseScrollTop - currentScroll);
  687. // 提高滚动阈值,减少不必要的微小滚动
  688. if (scrollDifference > 30) { // 从20提高到30
  689. this.$set(this.scrollTops, this.currentPage - 1, preciseScrollTop);
  690. console.log('✅ 使用精确位置滚动:', {
  691. selector,
  692. preciseScrollTop,
  693. currentPage: this.currentPage,
  694. scrollDifference
  695. });
  696. // 滚动完成后重置状态
  697. setTimeout(() => {
  698. resetScrollingState();
  699. }, 200);
  700. } else {
  701. resetScrollingState();
  702. console.log('📍 目标已在最佳可视位置,无需滚动');
  703. }
  704. } else {
  705. // 精确计算失败,使用原有的查询方法作为备用
  706. console.error('🔄 精确计算失败,使用备用查询方法');
  707. //this.fallbackScrollToText(selector, resetScrollingState, safetyTimeout);
  708. }
  709. } catch (error) {
  710. console.error('❌ 精确滚动计算失败:', error);
  711. // 使用原有的查询方法作为备用
  712. //this.fallbackScrollToText(selector, resetScrollingState, safetyTimeout);
  713. }
  714. });
  715. },
  716. // 备用滚动方法(原有的查询方式)
  717. fallbackScrollToText(selector, resetScrollingState, safetyTimeout) {
  718. // 使用uni.createSelectorQuery获取元素位置
  719. const query = uni.createSelectorQuery().in(this);
  720. // 获取scroll-view容器的位置信息
  721. query.select('.scroll-container').boundingClientRect();
  722. // 获取目标元素的位置信息
  723. query.select(selector).boundingClientRect();
  724. query.exec((res) => {
  725. // 清除安全超时
  726. clearTimeout(safetyTimeout);
  727. const scrollViewRect = res[0];
  728. const targetRect = res[1];
  729. console.log('查询结果:', {
  730. scrollViewRect: scrollViewRect ? '找到' : '未找到',
  731. targetRect: targetRect ? '找到' : '未找到',
  732. selector
  733. });
  734. if (scrollViewRect && targetRect) {
  735. // 计算目标元素相对于scroll-view的位置
  736. const currentScrollTop = this.scrollTops[this.currentPage - 1] || 0;
  737. const targetOffsetTop = targetRect.top - scrollViewRect.top + currentScrollTop;
  738. // 计算滚动位置,让目标元素在屏幕上方1/4处(更好的阅读体验)
  739. const screenHeight = uni.getSystemInfoSync().windowHeight;
  740. const targetScrollTop = targetOffsetTop - screenHeight / 4;
  741. // 更新scroll-view的滚动位置
  742. const finalScrollTop = Math.max(0, targetScrollTop);
  743. // 检查是否需要滚动(避免不必要的滚动)
  744. const currentScroll = this.scrollTops[this.currentPage - 1] || 0;
  745. const scrollDifference = Math.abs(finalScrollTop - currentScroll);
  746. // 提高滚动阈值,减少不必要的微小滚动
  747. if (scrollDifference > 30) { // 从20提高到30,与精确滚动保持一致
  748. this.$set(this.scrollTops, this.currentPage - 1, finalScrollTop);
  749. // 滚动完成后重置状态
  750. setTimeout(() => {
  751. resetScrollingState();
  752. }, 200); // 减少等待时间
  753. console.log('✅ 滚动到高亮文本:', {
  754. selector,
  755. targetOffsetTop,
  756. finalScrollTop,
  757. currentPage: this.currentPage,
  758. scrollDifference
  759. });
  760. } else {
  761. // 不需要滚动,立即重置状态
  762. resetScrollingState();
  763. console.log('📍 目标已在最佳可视位置,无需滚动');
  764. }
  765. } else {
  766. console.error('❌ 未找到目标元素或scroll-view:', {
  767. selector,
  768. scrollViewFound: !!scrollViewRect,
  769. targetFound: !!targetRect,
  770. currentPage: this.currentPage,
  771. highlightIndex: scrollData.highlightIndex
  772. });
  773. // 尝试备用方案:直接滚动到页面顶部附近
  774. if (!targetRect) {
  775. console.log('🔄 尝试备用滚动方案');
  776. // 改进备用方案:基于highlightIndex计算更准确的位置
  777. const estimatedPosition = this.calculateEstimatedScrollPosition(scrollData.highlightIndex);
  778. this.$set(this.scrollTops, this.currentPage - 1, estimatedPosition);
  779. setTimeout(() => {
  780. resetScrollingState();
  781. }, 200);
  782. } else {
  783. // 立即重置状态
  784. resetScrollingState();
  785. }
  786. }
  787. });
  788. },
  789. // 查找文本元素在页面中的实际索引
  790. findTextItemIndex(originalTextIndex) {
  791. const currentPageData = this.bookPages[this.currentPage - 1];
  792. if (!currentPageData || !Array.isArray(currentPageData)) {
  793. return -1;
  794. }
  795. let textCount = 0;
  796. for (let i = 0; i < currentPageData.length; i++) {
  797. const item = currentPageData[i];
  798. if (item && item.type === 'text' && item.content) {
  799. if (textCount === originalTextIndex) {
  800. return i; // 返回在页面数组中的实际索引
  801. }
  802. textCount++;
  803. }
  804. }
  805. return -1; // 未找到
  806. },
  807. // 获取所有页面元素的位置信息
  808. /*
  809. async getAllElementPositions() {
  810. return new Promise((resolve) => {
  811. // 检查缓存是否存在
  812. // if (this.elementPositionsCache[this.currentPage - 1]) {
  813. // resolve(this.elementPositionsCache[this.currentPage - 1]);
  814. // return;
  815. // }
  816. const currentPageData = this.bookPages[this.currentPage - 1];
  817. if (!currentPageData || !Array.isArray(currentPageData)) {
  818. resolve([]);
  819. return;
  820. }
  821. const query = uni.createSelectorQuery().in(this);
  822. const elementPositions = [];
  823. // 获取scroll-container的位置作为基准
  824. query.select('.scroll-container').boundingClientRect();
  825. // 为每个元素添加查询
  826. currentPageData.forEach((item, index) => {
  827. if (item && (item.type === 'text' || item.type === 'image' || item.type === 'video')) {
  828. if (item.type === 'text') {
  829. query.select(`#text-${index}`).boundingClientRect();
  830. } else if (item.type === 'image') {
  831. query.select(`#image-${index}`).boundingClientRect();
  832. } else if (item.type === 'video') {
  833. query.select(`#video-${index}`).boundingClientRect();
  834. }
  835. }
  836. });
  837. query.exec((res) => {
  838. const containerRect = res[0];
  839. console.log('getAllElementPositions res:', res, this.scrollTops[this.currentPage - 1]);
  840. if (!containerRect) {
  841. resolve([]);
  842. return;
  843. }
  844. // 处理查询结果
  845. let resultIndex = 1; // 跳过第一个容器结果
  846. let constTop = 0
  847. let constTopType = {
  848. 'text': 10,
  849. 'image': 10,
  850. 'video': 10,
  851. }
  852. currentPageData.forEach((item, index) => {
  853. if (item && (item.type === 'text' || item.type === 'image' || item.type === 'video')) {
  854. const elementRect = res[resultIndex];
  855. if (elementRect) {
  856. const currentScrollTop = this.scrollTops[this.currentPage - 1] || 0;
  857. const top = constTop;
  858. // const top = (elementRect.top - containerRect.top) + currentScrollTop;
  859. elementPositions.push({
  860. id: elementRect.id,
  861. index,
  862. type: item.type,
  863. top,
  864. height: elementRect.height,
  865. bottom: top + elementRect.height
  866. });
  867. constTop += elementRect.height + constTopType[item.type];
  868. }
  869. resultIndex++;
  870. }
  871. });
  872. resolve(elementPositions);
  873. });
  874. }).then((elementPositions) => {
  875. //进行缓存
  876. if (elementPositions.length > 0) {
  877. this.elementPositionsCache[this.currentPage - 1] = elementPositions;
  878. }
  879. return elementPositions;
  880. });
  881. },
  882. */
  883. // 使用浏览器 DOM API 获取所有页面元素的位置信息(H5)
  884. async getAllElementPositions() {
  885. // const cached = this.elementPositionsCache[this.currentPage - 1];
  886. // if (cached && Array.isArray(cached)) {
  887. // return cached;
  888. // }
  889. const currentPageData = this.bookPages[this.currentPage - 1];
  890. if (!currentPageData || !Array.isArray(currentPageData)) {
  891. return [];
  892. }
  893. // 确保 DOM 已更新
  894. await this.$nextTick();
  895. const rootEl = this.$el || document;
  896. let currentPage = this.currentPage - 1;
  897. let containerId = `scroll-container-${currentPage}`;
  898. const containerEl = (rootEl.querySelector ? rootEl.querySelector(`#${containerId}`) : null) || document.querySelector('.scroll-container');
  899. if (!containerEl || typeof containerEl.getBoundingClientRect !== 'function') {
  900. console.warn('[getAllElementPositions] 未找到滚动容器或不支持 DOM API');
  901. return [];
  902. }
  903. const containerRect = containerEl.getBoundingClientRect();
  904. // console.log('document.getElementById containerEl', containerEl);
  905. const currentScrollTop = this.scrollTops[this.currentPage] || 0;
  906. const elementPositions = [];
  907. let constTop = 0
  908. let constTopType = {
  909. 'text': 10,
  910. 'image': 10,
  911. 'video': 10,
  912. }
  913. currentPageData.forEach((item, index) => {
  914. if (!item || !item.type) return;
  915. let el = document.querySelector(`#${containerId} #${item.type}-${index}`);
  916. // console.log('document.getElementById', `${item.type}-${index}`, el.clientHeight, el);
  917. if (el && typeof el.getBoundingClientRect === 'function') {
  918. const rect = el.getBoundingClientRect();
  919. const top = constTop;
  920. elementPositions.push({
  921. id: el.id || '',
  922. index,
  923. type: item.type,
  924. top,
  925. height: rect.height,
  926. bottom: top + rect.height
  927. });
  928. constTop += rect.height + constTopType[item.type];
  929. }
  930. });
  931. if (elementPositions.length > 0) {
  932. this.elementPositionsCache[this.currentPage - 1] = elementPositions;
  933. }
  934. return elementPositions;
  935. },
  936. awaitSleep(time) {
  937. return new Promise((resolve) => {
  938. setTimeout(() => {
  939. resolve();
  940. }, time);
  941. });
  942. },
  943. // 检查元素是否在可视范围内
  944. isElementInViewport(elementPosition, currentScrollTop) {
  945. const screenHeight = uni.getSystemInfoSync().windowHeight;
  946. const viewportTop = currentScrollTop;
  947. const viewportBottom = currentScrollTop + screenHeight;
  948. // 元素的顶部和底部位置
  949. const elementTop = elementPosition.top;
  950. const elementBottom = elementPosition.bottom;
  951. // 检查元素是否完全或部分在可视范围内
  952. const isVisible = elementBottom > viewportTop && elementTop < viewportBottom;
  953. // 计算元素在可视范围内的比例
  954. const visibleTop = Math.max(elementTop, viewportTop);
  955. const visibleBottom = Math.min(elementBottom, viewportBottom);
  956. const visibleHeight = Math.max(0, visibleBottom - visibleTop);
  957. const visibilityRatio = visibleHeight / elementPosition.height;
  958. return {
  959. isVisible,
  960. visibilityRatio,
  961. elementTop,
  962. elementBottom,
  963. viewportTop,
  964. viewportBottom
  965. };
  966. },
  967. // 计算最佳滚动位置
  968. calculateOptimalScrollPosition(targetElement, currentScrollTop) {
  969. const screenHeight = uni.getSystemInfoSync().windowHeight;
  970. // 检查当前元素的可见性
  971. const visibility = this.isElementInViewport(targetElement, currentScrollTop);
  972. // 如果元素已经完全可见且在合适位置,不需要滚动
  973. if (visibility.isVisible && visibility.visibilityRatio > 0.8) {
  974. // 检查元素是否在屏幕的合适位置(上方1/3到2/3之间)
  975. const elementCenter = (targetElement.top + targetElement.bottom) / 2;
  976. const relativePosition = (elementCenter - currentScrollTop) / screenHeight;
  977. if (relativePosition >= 0.2 && relativePosition <= 0.7) {
  978. console.log('📍 元素已在最佳可视位置,无需滚动');
  979. return null; // 不需要滚动
  980. }
  981. }
  982. // 计算目标滚动位置:让元素显示在屏幕上方1/3处(更舒适的阅读位置)
  983. const optimalOffsetRatio = 0.3; // 30%的位置,比1/4更舒适
  984. const offsetFromTop = screenHeight * optimalOffsetRatio;
  985. // 考虑元素高度,确保不会被截断
  986. const elementHeight = targetElement.height;
  987. const adjustedOffset = Math.min(offsetFromTop, screenHeight * 0.1); // 最小10%偏移
  988. const targetScrollTop = Math.max(0, targetElement.top - adjustedOffset);
  989. console.log('🎯 计算最佳滚动位置:', {
  990. currentVisibility: visibility,
  991. elementHeight,
  992. optimalOffsetRatio,
  993. adjustedOffset,
  994. targetScrollTop
  995. });
  996. return targetScrollTop;
  997. },
  998. // 计算精确的滚动位置
  999. async calculatePreciseScrollPosition(scrollData, elementPositions) {
  1000. if (!elementPositions || elementPositions.length === 0) {
  1001. return null;
  1002. }
  1003. let targetElementIndex = -1;
  1004. // if (scrollData.segmentIndex !== undefined) {
  1005. // // 分段音频情况
  1006. // targetElementIndex = scrollData.segmentIndex;
  1007. if (scrollData.highlightIndex !== undefined) {
  1008. // 普通音频情况,需要找到对应的文本元素
  1009. targetElementIndex = this.findTextItemIndex(scrollData.highlightIndex);
  1010. }
  1011. if (targetElementIndex === -1) {
  1012. return null;
  1013. }
  1014. // 查找目标元素的位置信息
  1015. const targetElement = elementPositions.find(pos => pos.index === targetElementIndex && pos.type === 'text');
  1016. if (!targetElement) {
  1017. console.error('未找到目标元素位置信息:', elementPositions, targetElementIndex);
  1018. return null;
  1019. }
  1020. // 获取当前滚动位置
  1021. const currentScrollTop = this.scrollTops[this.currentPage - 1] || 0;
  1022. // 使用优化的滚动位置计算
  1023. const targetScrollTop = this.calculateOptimalScrollPosition(targetElement, currentScrollTop);
  1024. console.log('🎯 精确滚动位置计算:', {
  1025. targetElementIndex,
  1026. targetElement,
  1027. currentScrollTop,
  1028. targetScrollTop
  1029. });
  1030. return targetScrollTop;
  1031. },
  1032. // 计算估算的滚动位置(备用方案)
  1033. calculateEstimatedScrollPosition(highlightIndex) {
  1034. const currentPageData = this.bookPages[this.currentPage - 1];
  1035. if (!currentPageData || !Array.isArray(currentPageData)) {
  1036. return highlightIndex * 80; // 基础估算
  1037. }
  1038. // 基于页面内容计算更准确的位置
  1039. let estimatedHeight = 0;
  1040. let textCount = 0;
  1041. for (let i = 0; i < currentPageData.length && textCount <= highlightIndex; i++) {
  1042. const item = currentPageData[i];
  1043. if (item && item.type === 'text' && item.content) {
  1044. if (textCount === highlightIndex) {
  1045. break;
  1046. }
  1047. // 根据内容长度估算高度
  1048. const contentLength = item.content.length;
  1049. estimatedHeight += Math.max(60, contentLength * 1.2); // 基础高度 + 内容长度因子
  1050. textCount++;
  1051. } else if (item && item.type === 'image') {
  1052. estimatedHeight += 200; // 图片估算高度
  1053. } else if (item && item.type === 'video') {
  1054. estimatedHeight += 300; // 视频估算高度
  1055. }
  1056. }
  1057. return Math.max(0, estimatedHeight - 100); // 留一些上边距
  1058. },
  1059. // 获取音色列表 拿第一个做默认的音色id
  1060. async getVoiceList() {
  1061. // 优先从Vuex获取音色列表
  1062. if (this.voiceList && this.voiceList.length > 0) {
  1063. console.log('从Vuex获取音色列表:', this.voiceList);
  1064. this.voiceId = this.defaultVoiceId || Number(this.voiceList[0].voiceType);
  1065. console.log('使用Vuex中的默认音色ID:', this.voiceId);
  1066. // 同步默认音色设置到audioManager
  1067. audioManager.setGlobalVoiceId(this.voiceId);
  1068. return;
  1069. }
  1070. // 如果Vuex中没有数据,则从API获取(兜底方案)
  1071. console.log('Vuex中无音色数据,从API获取...');
  1072. try {
  1073. const voiceRes = await this.$api.music.list()
  1074. if (voiceRes.code === 200) {
  1075. console.log('音色列表API返回:', voiceRes.result);
  1076. // 更新Vuex中的音色列表
  1077. this.$store.commit('setVoiceList', voiceRes.result);
  1078. this.voiceId = Number(voiceRes.result[0].voiceType)
  1079. console.log('获取默认音色ID:', this.voiceId, '类型:', typeof this.voiceId);
  1080. // 同步默认音色设置到audioManager
  1081. audioManager.setGlobalVoiceId(this.voiceId);
  1082. } else {
  1083. console.error('获取音色列表失败:', voiceRes);
  1084. }
  1085. } catch (error) {
  1086. console.error('获取音色列表异常:', error);
  1087. }
  1088. },
  1089. toggleNavbar() {
  1090. this.showNavbar = !this.showNavbar
  1091. },
  1092. goBack() {
  1093. uni.navigateBack()
  1094. },
  1095. toggleCoursePopup() {
  1096. if (this.$refs.coursePopup) {
  1097. this.$refs.coursePopup.open()
  1098. }
  1099. // console.log('123123123');
  1100. },
  1101. toggleSort() {
  1102. this.isReversed = !this.isReversed
  1103. },
  1104. selectCourse(courseId) {
  1105. this.currentCourse = courseId
  1106. this.courseId = courseId // 同时更新 courseId
  1107. // 这里可以添加切换课程的逻辑
  1108. // console.log('选择课程:', courseId)
  1109. this.getCourseList(courseId)
  1110. },
  1111. showWordMeaning() {
  1112. if (this.$refs.meaningPopup) {
  1113. this.$refs.meaningPopup.open()
  1114. }
  1115. },
  1116. closeMeaningPopup() {
  1117. this.currentWordMeaning = null;
  1118. // 重置音频播放状态,确保后续句子点击能正常播放
  1119. if (this.isWordAudioPlaying) {
  1120. this.isWordAudioPlaying = false;
  1121. // 如果有正在播放的音频,停止它
  1122. if (this.currentWordAudio) {
  1123. try {
  1124. this.currentWordAudio.pause();
  1125. this.currentWordAudio.destroy();
  1126. } catch (error) {
  1127. }
  1128. this.currentWordAudio = null;
  1129. }
  1130. }
  1131. },
  1132. // 将英文句子分割成单词数组
  1133. splitEnglishSentence(sentence) {
  1134. // 使用正则表达式分割句子,保留标点符号
  1135. const tokens = sentence.match(/\b\w+\b|[^\w\s]/g) || [];
  1136. return tokens.map((token, index) => ({
  1137. text: token,
  1138. index: index,
  1139. isWord: /\b\w+\b/.test(token), // 判断是否为单词
  1140. hasDefinition: false // 是否有释义,稍后会设置
  1141. }));
  1142. },
  1143. // 查找单词释义
  1144. findWordDefinition(word) {
  1145. const currentPageWords = this.pageWords[this.currentPage - 1] || [];
  1146. // 不区分大小写匹配
  1147. return currentPageWords.find(wordData =>
  1148. wordData.word.toLowerCase() === word.toLowerCase()
  1149. );
  1150. },
  1151. // 处理中文文本,标记重点词汇
  1152. processChineseText(text) {
  1153. const currentPageWords = this.pageWords[this.currentPage - 1] || [];
  1154. if (!text || currentPageWords.length === 0) {
  1155. return [{ text: text, isKeyword: false, keywordData: null }];
  1156. }
  1157. // 创建一个数组来存储处理后的文本片段
  1158. const segments = [];
  1159. let currentIndex = 0;
  1160. // 按照重点词汇的长度排序,优先匹配较长的词汇
  1161. const sortedWords = [...currentPageWords].sort((a, b) => b.word.length - a.word.length);
  1162. while (currentIndex < text.length) {
  1163. let matched = false;
  1164. // 尝试匹配重点词汇
  1165. for (const wordData of sortedWords) {
  1166. const keyword = wordData.word;
  1167. if (text.substr(currentIndex, keyword.length) === keyword) {
  1168. // 找到匹配的重点词汇
  1169. segments.push({
  1170. text: keyword,
  1171. isKeyword: true,
  1172. keywordData: wordData
  1173. });
  1174. currentIndex += keyword.length;
  1175. matched = true;
  1176. break;
  1177. }
  1178. }
  1179. if (!matched) {
  1180. // 没有匹配到重点词汇,添加单个字符
  1181. segments.push({
  1182. text: text[currentIndex],
  1183. isKeyword: false,
  1184. keywordData: null
  1185. });
  1186. currentIndex++;
  1187. }
  1188. }
  1189. // 合并相邻的非重点词汇片段
  1190. const mergedSegments = [];
  1191. let currentSegment = null;
  1192. for (const segment of segments) {
  1193. if (segment.isKeyword) {
  1194. // 如果当前有未完成的非重点词汇片段,先添加它
  1195. if (currentSegment) {
  1196. mergedSegments.push(currentSegment);
  1197. currentSegment = null;
  1198. }
  1199. // 添加重点词汇
  1200. mergedSegments.push(segment);
  1201. } else {
  1202. // 非重点词汇,合并到当前片段
  1203. if (currentSegment) {
  1204. currentSegment.text += segment.text;
  1205. } else {
  1206. currentSegment = { ...segment };
  1207. }
  1208. }
  1209. }
  1210. // 添加最后的非重点词汇片段
  1211. if (currentSegment) {
  1212. mergedSegments.push(currentSegment);
  1213. }
  1214. return mergedSegments;
  1215. },
  1216. // 初始化audioManager事件监听
  1217. initAudioManagerListeners() {
  1218. // 监听音频播放状态变化
  1219. audioManager.on('play', (data) => {
  1220. if (data?.audioType === 'word') {
  1221. this.isWordAudioPlaying = true;
  1222. }
  1223. });
  1224. audioManager.on('pause', (data) => {
  1225. if (data?.audioType === 'word') {
  1226. this.isWordAudioPlaying = false;
  1227. }
  1228. });
  1229. audioManager.on('ended', (data) => {
  1230. if (data?.audioType === 'word') {
  1231. this.isWordAudioPlaying = false;
  1232. }
  1233. });
  1234. audioManager.on('error', (data) => {
  1235. if (data?.audioType === 'word') {
  1236. this.isWordAudioPlaying = false;
  1237. uni.showToast({
  1238. title: '語音播放失敗',
  1239. icon: 'none'
  1240. });
  1241. }
  1242. });
  1243. },
  1244. async playWordAudio(word) {
  1245. try {
  1246. console.log('🎵 开始播放单词音频:', word);
  1247. // 🎯 使用audioManager的全局音色设置
  1248. const globalVoiceId = audioManager.getGlobalVoiceId();
  1249. const voiceIdToUse = globalVoiceId || this.voiceId;
  1250. if (!voiceIdToUse || voiceIdToUse === '' || voiceIdToUse === null || voiceIdToUse === undefined) {
  1251. console.warn('⚠️ 音色ID未设置,无法播放音频');
  1252. uni.showToast({
  1253. title: '音色未加载,请稍后重试',
  1254. icon: 'none',
  1255. duration: 2000
  1256. });
  1257. return;
  1258. }
  1259. console.log('🎵 使用音色ID:', voiceIdToUse, '播放文本:', word);
  1260. // 調用語音轉換API
  1261. const audioRes = await this.$api.music.textToVoice({
  1262. text: word,
  1263. voiceType: voiceIdToUse
  1264. });
  1265. console.log('🎵 API响应:', audioRes);
  1266. // 檢查響應並播放音頻
  1267. if (audioRes && audioRes.result && audioRes.result.url) {
  1268. console.log('✅ 获取到音频URL:', audioRes.result.url);
  1269. // 使用audioManager播放音频,应用全局语速设置
  1270. await audioManager.playAudio(audioRes.result.url, 'word', {
  1271. playbackRate: audioManager.getGlobalPlaybackRate()
  1272. });
  1273. } else {
  1274. console.error('❌ API响应无效:', audioRes);
  1275. uni.showToast({
  1276. title: '語音播放失敗',
  1277. icon: 'none'
  1278. });
  1279. }
  1280. } catch (error) {
  1281. console.error('❌ 播放单词语音异常:', error);
  1282. uni.showToast({
  1283. title: '語音播放失敗',
  1284. icon: 'none'
  1285. });
  1286. }
  1287. },
  1288. // 重複播放單詞語音(用於釋義彈窗中的揚聲器圖標)
  1289. repeatWordAudio() {
  1290. if (this.currentWordMeaning && this.currentWordMeaning.word) {
  1291. // 将单词和解释合并后播放音频
  1292. const combinedText = `${this.currentWordMeaning.word}${this.currentWordMeaning.meaning || ''}`;
  1293. this.playWordAudio(combinedText);
  1294. } else {
  1295. console.warn('沒有當前單詞可以播放');
  1296. }
  1297. },
  1298. // 处理单词点击事件
  1299. handleWordClick(word) {
  1300. const definition = this.findWordDefinition(word);
  1301. if (definition) {
  1302. this.currentWordMeaning = {
  1303. word: definition.word,
  1304. phonetic: definition.soundmark || '',
  1305. partOfSpeech: '', // 可以根据需要添加词性
  1306. meaning: definition.paraphrase || '',
  1307. knowledgeGain: definition.knowledge || '',
  1308. image: definition.image || ''
  1309. };
  1310. // 将单词和解释合并后播放音频
  1311. const combinedText = `${word}${definition.paraphrase || ''}`;
  1312. this.playWordAudio(combinedText);
  1313. this.showWordMeaning();
  1314. } else {
  1315. // 如果没有释义,只播放单词
  1316. if (word) {
  1317. this.playWordAudio(word);
  1318. } else {
  1319. }
  1320. }
  1321. },
  1322. // 处理中文重点词汇点击事件
  1323. handleChineseKeywordClick(keywordData) {
  1324. if (keywordData) {
  1325. this.currentWordMeaning = {
  1326. word: keywordData.word,
  1327. phonetic: keywordData.soundmark || '',
  1328. partOfSpeech: '', // 可以根据需要添加词性
  1329. meaning: keywordData.paraphrase || '',
  1330. knowledgeGain: keywordData.knowledge || '',
  1331. image: keywordData.image || ''
  1332. };
  1333. // 将词汇和解释合并后播放音频
  1334. const combinedText = `${keywordData.word}${keywordData.paraphrase || ''}`;
  1335. this.playWordAudio(combinedText);
  1336. this.showWordMeaning();
  1337. } else {
  1338. }
  1339. },
  1340. // 计算音频总时长
  1341. async calculateTotalDuration() {
  1342. let totalDuration = 0;
  1343. for (let i = 0; i < this.currentPageAudios.length; i++) {
  1344. const audio = this.currentPageAudios[i];
  1345. // 優先使用API返回的時長信息
  1346. if (audio.duration && audio.duration > 0) {
  1347. totalDuration += audio.duration;
  1348. continue;
  1349. }
  1350. // 如果沒有API時長信息,嘗試獲取音頻時長
  1351. try {
  1352. const duration = await this.getAudioDuration(audio.url);
  1353. audio.duration = duration;
  1354. totalDuration += duration;
  1355. } catch (error) {
  1356. console.error('获取音频时长失败:', error);
  1357. // 如果无法获取时长,根據文字長度估算(更精確的估算)
  1358. const textLength = audio.text.length;
  1359. // 假設每分鐘可以讀150-200個字符,這裡用180作為平均值
  1360. const estimatedDuration = Math.max(2, textLength / 3); // 每3個字符約1秒
  1361. audio.duration = estimatedDuration;
  1362. totalDuration += estimatedDuration;
  1363. console.log(`估算音頻時長 ${i + 1}:`, estimatedDuration, '秒 (文字長度:', textLength, ')');
  1364. }
  1365. }
  1366. this.totalTime = totalDuration;
  1367. },
  1368. // 获取音频时长
  1369. getAudioDuration(audioUrl) {
  1370. return new Promise((resolve, reject) => {
  1371. const audio = uni.createInnerAudioContext();
  1372. audio.src = audioUrl;
  1373. let resolved = false;
  1374. // 监听音频加载完成事件
  1375. audio.onCanplay(() => {
  1376. if (!resolved && audio.duration && audio.duration > 0) {
  1377. resolved = true;
  1378. resolve(audio.duration);
  1379. audio.destroy();
  1380. }
  1381. });
  1382. // 监听音频元数据加载完成事件
  1383. audio.onLoadedmetadata = () => {
  1384. if (!resolved && audio.duration && audio.duration > 0) {
  1385. resolved = true;
  1386. resolve(audio.duration);
  1387. audio.destroy();
  1388. }
  1389. };
  1390. // 监听音频时长更新事件
  1391. audio.onDurationChange = () => {
  1392. if (!resolved && audio.duration && audio.duration > 0) {
  1393. resolved = true;
  1394. resolve(audio.duration);
  1395. audio.destroy();
  1396. }
  1397. };
  1398. // 如果以上方法都無法獲取時長,嘗試播放一小段來獲取時長
  1399. audio.onPlay(() => {
  1400. if (!resolved) {
  1401. setTimeout(() => {
  1402. if (!resolved && audio.duration && audio.duration > 0) {
  1403. resolved = true;
  1404. resolve(audio.duration);
  1405. audio.destroy();
  1406. }
  1407. }, 100); // 播放100ms後檢查時長
  1408. }
  1409. });
  1410. audio.onError((error) => {
  1411. console.error('音频加载失败:', error);
  1412. if (!resolved) {
  1413. resolved = true;
  1414. reject(error);
  1415. audio.destroy();
  1416. }
  1417. });
  1418. // 設置較長的超時時間,並在超時前嘗試播放
  1419. setTimeout(() => {
  1420. if (!resolved) {
  1421. audio.play();
  1422. }
  1423. }, 1000);
  1424. // 最終超時處理
  1425. setTimeout(() => {
  1426. if (!resolved) {
  1427. console.warn('獲取音頻時長超時,使用默認值');
  1428. resolved = true;
  1429. reject(new Error('获取音频时长超时'));
  1430. audio.destroy();
  1431. }
  1432. }, 5000);
  1433. });
  1434. },
  1435. // 音频控制方法 - 这些方法现在由AudioControls组件处理
  1436. // 保留一些简单的接口方法用于与AudioControls组件通信
  1437. // 判断当前文本是否应该高亮 - 这个方法需要保留,因为它用于模板渲染
  1438. isTextHighlighted(page, index) {
  1439. // 只有当前页面且是文本类型才可能高亮
  1440. if (page !== this.bookPages[this.currentPage - 1]) return false;
  1441. // 计算当前页面中text类型元素的索引
  1442. let textIndex = 0;
  1443. for (let i = 0; i <= index; i++) {
  1444. if (page[i].type === 'text') {
  1445. if (i === index) {
  1446. const shouldHighlight = textIndex === this.currentHighlightIndex;
  1447. if (shouldHighlight) {
  1448. }
  1449. return shouldHighlight;
  1450. }
  1451. textIndex++;
  1452. }
  1453. }
  1454. return false;
  1455. },
  1456. // 判断划线重点卡片中的文本是否应该高亮
  1457. isCardTextHighlighted(page, index) {
  1458. // 只有当前页面且是文本类型才可能高亮
  1459. if (page !== this.bookPages[this.currentPage - 1]) return false;
  1460. // 计算当前页面中text类型元素的索引
  1461. let textIndex = 0;
  1462. for (let i = 0; i <= index; i++) {
  1463. if (page[i].type === 'text') {
  1464. if (i === index) {
  1465. return textIndex === this.currentHighlightIndex;
  1466. }
  1467. textIndex++;
  1468. }
  1469. }
  1470. return false;
  1471. },
  1472. async previousPage() {
  1473. if (this.currentPage > 1) {
  1474. this.currentPage--;
  1475. // 获取对应页面的数据(如果还没有获取过)
  1476. if (this.courseIdList[this.currentPage - 1] && this.bookPages[this.currentPage - 1].length === 0) {
  1477. await this.getBookPages(this.courseIdList[this.currentPage - 1]);
  1478. }
  1479. }
  1480. },
  1481. async nextPage() {
  1482. if (this.currentPage < this.bookPages.length) {
  1483. this.currentPage++;
  1484. // 获取对应页面的数据(如果还没有获取过)
  1485. if (this.courseIdList[this.currentPage - 1] && this.bookPages[this.currentPage - 1].length === 0) {
  1486. await this.getBookPages(this.courseIdList[this.currentPage - 1]);
  1487. }
  1488. }
  1489. },
  1490. toggleSound() {
  1491. // 检查是否正在加载音频,如果是则阻止音色切换
  1492. if (this.isAudioLoading) {
  1493. uni.showToast({
  1494. title: '音频加载中,请稍后再试',
  1495. icon: 'none',
  1496. duration: 2000
  1497. });
  1498. return;
  1499. }
  1500. // 检查AudioControls组件是否正在加载音频
  1501. if (this.$refs.customTabbar && this.$refs.customTabbar.$refs.audioControls && this.$refs.customTabbar.$refs.audioControls.isAudioLoading) {
  1502. uni.showToast({
  1503. title: '音频加载中,请稍后再试',
  1504. icon: 'none',
  1505. duration: 2000
  1506. });
  1507. return;
  1508. }
  1509. console.log('音色切换')
  1510. uni.navigateTo({
  1511. url: '/subPages/home/music?voiceId=' + this.voiceId
  1512. })
  1513. },
  1514. unlockBook() {
  1515. console.log('解锁全书')
  1516. // 这里可以跳转到会员页面或者调用解锁接口
  1517. uni.navigateTo({
  1518. url: '/subPages/member/recharge'
  1519. })
  1520. },
  1521. // 跳转到下一课
  1522. async goToNextCourse() {
  1523. if (!this.hasNextCourse) {
  1524. uni.showToast({
  1525. title: '已经是最后一课了',
  1526. icon: 'none',
  1527. duration: 2000
  1528. });
  1529. return;
  1530. }
  1531. try {
  1532. // 找到当前课程在课程列表中的索引
  1533. const currentCourseIndex = this.courseList.findIndex(course => course.id == this.courseId);
  1534. if (currentCourseIndex >= 0 && currentCourseIndex < this.courseList.length - 1) {
  1535. // 获取下一课的ID
  1536. const nextCourse = this.courseList[currentCourseIndex + 1];
  1537. console.log('跳转到下一课:', nextCourse);
  1538. // 切换到下一课
  1539. await this.selectCourse(nextCourse.id);
  1540. uni.showToast({
  1541. title: `已切换到第${currentCourseIndex + 2}`,
  1542. icon: 'success',
  1543. duration: 2000
  1544. });
  1545. }
  1546. } catch (error) {
  1547. console.error('跳转下一课失败:', error);
  1548. uni.showToast({
  1549. title: '跳转失败,请重试',
  1550. icon: 'none',
  1551. duration: 2000
  1552. });
  1553. }
  1554. },
  1555. // 回到开始(当前课程的第一页)
  1556. async backToStart() {
  1557. try {
  1558. // 回到当前课程的第一页
  1559. this.currentPage = 1;
  1560. console.log('回到开始,跳转到第一页');
  1561. // 获取第一页的数据(如果还没有获取过)
  1562. if (this.courseIdList[0] && this.bookPages[0].length === 0) {
  1563. await this.getBookPages(this.courseIdList[0]);
  1564. }
  1565. uni.showToast({
  1566. title: '已回到第一页',
  1567. icon: 'success',
  1568. duration: 2000
  1569. });
  1570. } catch (error) {
  1571. console.error('回到开始失败:', error);
  1572. uni.showToast({
  1573. title: '操作失败,请重试',
  1574. icon: 'none',
  1575. duration: 2000
  1576. });
  1577. }
  1578. },
  1579. async goToPage(page) {
  1580. this.currentPage = page
  1581. console.log('跳转到页面:', page)
  1582. // 获取对应页面的数据(如果还没有获取过)
  1583. if (this.courseIdList[this.currentPage - 1] && this.bookPages[this.currentPage - 1].length === 0) {
  1584. await this.getBookPages(this.courseIdList[this.currentPage - 1]);
  1585. }
  1586. },
  1587. async onSwiperChange(e) {
  1588. this.currentPage = e.detail.current + 1
  1589. // 获取对应页面的数据(如果还没有获取过)
  1590. if (this.courseIdList[this.currentPage - 1] && this.bookPages[this.currentPage - 1].length === 0) {
  1591. await this.getBookPages(this.courseIdList[this.currentPage - 1]);
  1592. }
  1593. },
  1594. async getCourseList(id) {
  1595. const res = await this.$api.book.coursePage({
  1596. id: id
  1597. })
  1598. if (res.code === 200) {
  1599. // 课程切换时,先清理音频控制组件的所有数据
  1600. if (this.$refs.customTabbar && this.$refs.customTabbar.$refs.audioControls) {
  1601. this.$refs.customTabbar.$refs.audioControls.resetForCourseChange();
  1602. }
  1603. // 清空当前页面相关数据
  1604. this.currentPage = 1; // 重置到第一页
  1605. this.currentCourse = 1; // 重置当前课程索引
  1606. this.currentWordMeaning = null; // 清空单词释义
  1607. this.currentWordAudio = null; // 清空单词音频
  1608. this.currentHighlightIndex = -1; // 清空高亮索引
  1609. // 清理单词音频缓存
  1610. this.clearWordAudioCache();
  1611. // 重新初始化课程数据
  1612. this.courseIdList = res.result.map(item => item.id)
  1613. // 初始化二维数组 换一种方式
  1614. this.bookPages = this.courseIdList.map(() => [])
  1615. // 初始化标题数组
  1616. this.pageTitles = this.courseIdList.map(() => '')
  1617. // 初始化页面类型数组
  1618. this.pageTypes = this.courseIdList.map(() => '')
  1619. // 初始化页面单词数组
  1620. this.pageWords = this.courseIdList.map(() => [])
  1621. // 初始化滚动位置数组
  1622. this.scrollTops = this.courseIdList.map(() => 0)
  1623. // 初始化第一页
  1624. if (this.courseIdList.length > 0) {
  1625. await this.getBookPages(this.courseIdList[0])
  1626. // 课程切换后,确保音频控件能正确加载新课程的音频
  1627. // 使用$nextTick确保DOM和数据都已更新
  1628. this.$nextTick(async () => {
  1629. if (this.$refs.customTabbar && this.$refs.customTabbar.$refs.audioControls) {
  1630. try {
  1631. // 直接调用getCurrentPageAudio方法,更可靠
  1632. await this.$refs.customTabbar.$refs.audioControls.getCurrentPageAudio();
  1633. } catch (error) {
  1634. console.error('课程切换后音频加载失败:', error);
  1635. }
  1636. }
  1637. this.getAllElementPositions()
  1638. });
  1639. // 预加载后续几页的内容(异步执行,不阻塞当前页面显示)
  1640. this.preloadNextPages()
  1641. }
  1642. }
  1643. },
  1644. async getBookPages(id) {
  1645. const res = await this.$api.book.coursesPageDetail({
  1646. id: id
  1647. })
  1648. if (res.code === 200) {
  1649. // 使用$set确保响应式更新
  1650. const rawPageData = JSON.parse(res.result.content)
  1651. console.log('获取到的原始页面数据:', rawPageData)
  1652. // 过滤掉无效的数据项
  1653. const filteredPageData = rawPageData.filter(item => {
  1654. return item && typeof item === 'object' && (item.type || item.content)
  1655. })
  1656. console.log('过滤后的页面数据:', filteredPageData)
  1657. // 确保当前页面存在
  1658. if (this.currentPage - 1 < this.bookPages.length) {
  1659. this.$set(this.bookPages, this.currentPage - 1, filteredPageData)
  1660. // 保存页面标题
  1661. this.$set(this.pageTitles, this.currentPage - 1, res.result.title || '')
  1662. // 保存页面类型
  1663. this.$set(this.pageTypes, this.currentPage - 1, res.result.type || '')
  1664. // 保存页面单词释义数据
  1665. this.$set(this.pageWords, this.currentPage - 1, res.result.words || [])
  1666. // 保存页面付费状态
  1667. this.$set(this.pagePay, this.currentPage - 1, res.result.pay || 'N')
  1668. }
  1669. }
  1670. },
  1671. // 获取课程列表
  1672. async getCoursePageList(bookId) {
  1673. const res = await this.$api.book.course({
  1674. id: bookId
  1675. })
  1676. if (res.code === 200) {
  1677. this.courseList = res.result.records
  1678. // 打上序列号
  1679. this.courseList = this.courseList.map((item, index) => ({
  1680. ...item,
  1681. index,
  1682. }))
  1683. }
  1684. },
  1685. // 清理音频缓存
  1686. clearAudioCache() {
  1687. this.audioCache = {};
  1688. },
  1689. // 清理單詞語音緩存
  1690. clearWordAudioCache() {
  1691. this.wordAudioCache = {};
  1692. },
  1693. // 限制缓存大小,保留最近访问的页面
  1694. limitCacheSize(maxSize = 10) {
  1695. const cacheKeys = Object.keys(this.audioCache);
  1696. if (cacheKeys.length > maxSize) {
  1697. // 删除最旧的缓存项
  1698. const keysToDelete = cacheKeys.slice(0, cacheKeys.length - maxSize);
  1699. keysToDelete.forEach(key => {
  1700. delete this.audioCache[key];
  1701. });
  1702. }
  1703. },
  1704. // 预加载后续页面内容
  1705. async preloadNextPages() {
  1706. try {
  1707. // 优化策略:只预加载接下来的2-3页内容,避免过多请求
  1708. const preloadCount = Math.min(3, this.courseIdList.length - 1); // 预加载3页或剩余页数
  1709. // 串行预加载,避免并发请求过多
  1710. for (let i = 1; i <= preloadCount; i++) {
  1711. if (i < this.courseIdList.length && this.bookPages[i].length === 0) {
  1712. try {
  1713. await this.preloadSinglePage(this.courseIdList[i], i);
  1714. // 每页之间间隔400ms,提高预加载效率
  1715. if (i < preloadCount) {
  1716. await new Promise(resolve => setTimeout(resolve, 0));
  1717. }
  1718. } catch (error) {
  1719. console.error(`预加载第${i + 1}页失败:`, error);
  1720. // 继续预加载下一页
  1721. }
  1722. }
  1723. }
  1724. // 延迟800ms后再通知AudioControls组件开始预加载音频,提高响应速度
  1725. setTimeout(() => {
  1726. if (this.$refs.customTabbar && this.$refs.customTabbar.$refs.audioControls) {
  1727. this.$refs.customTabbar.$refs.audioControls.startPreloadAudio();
  1728. }
  1729. }, 800);
  1730. } catch (error) {
  1731. console.error('预加载页面内容失败:', error);
  1732. }
  1733. },
  1734. // 预加载单个页面
  1735. async preloadSinglePage(courseId, pageIndex) {
  1736. try {
  1737. const res = await this.$api.book.coursesPageDetail({
  1738. id: courseId
  1739. });
  1740. if (res.code === 200) {
  1741. const rawPageData = JSON.parse(res.result.content);
  1742. const filteredPageData = rawPageData.filter(item => {
  1743. return item && typeof item === 'object' && (item.type || item.content);
  1744. });
  1745. // 使用$set确保响应式更新
  1746. this.$set(this.bookPages, pageIndex, filteredPageData);
  1747. this.$set(this.pageTitles, pageIndex, res.result.title || '');
  1748. this.$set(this.pageTypes, pageIndex, res.result.type || '');
  1749. this.$set(this.pageWords, pageIndex, res.result.words || []);
  1750. this.$set(this.pagePay, pageIndex, res.result.pay || 'N');
  1751. }
  1752. } catch (error) {
  1753. console.error(`预加载第${pageIndex + 1}页失败:`, error);
  1754. throw error;
  1755. }
  1756. },
  1757. // 自動加載第一頁音頻並播放
  1758. async autoLoadAndPlayFirstPage() {
  1759. try {
  1760. // 確保當前是第一頁且需要加載音頻
  1761. if (this.currentPage === 1 && this.shouldLoadAudio) {
  1762. // 加載音頻
  1763. await this.getCurrentPageAudio();
  1764. // 檢查是否成功加載音頻
  1765. if (this.currentPageAudios && this.currentPageAudios.length > 0) {
  1766. // getCurrentPageAudio方法已經處理了第一個音頻的播放,這裡不需要再次調用playAudio
  1767. } else {
  1768. }
  1769. } else {
  1770. }
  1771. } catch (error) {
  1772. console.error('自動加載和播放音頻失敗:', error);
  1773. }
  1774. },
  1775. },
  1776. async onLoad(args) {
  1777. this.$scrollTo('imageRef')
  1778. // 初始化audioManager事件监听
  1779. this.initAudioManagerListeners();
  1780. // 监听音色切换事件,传递给AudioControls组件处理
  1781. uni.$on('selectVoice', async (voiceId) => {
  1782. if (this.voiceId === voiceId) {
  1783. return;
  1784. }
  1785. // 检查是否正在加载音频,如果是则阻止音色切换
  1786. if (this.isAudioLoading || (this.$refs.customTabbar && this.$refs.customTabbar.$refs.audioControls && this.$refs.customTabbar.$refs.audioControls.isAudioLoading)) {
  1787. uni.showToast({
  1788. title: '音频加载中,请稍后再试',
  1789. icon: 'none',
  1790. duration: 2000
  1791. });
  1792. return;
  1793. }
  1794. // 更新本地音色ID
  1795. this.voiceId = voiceId;
  1796. // 同步音色设置到audioManager
  1797. audioManager.setGlobalVoiceId(voiceId);
  1798. // 清理單詞語音資源
  1799. this.clearWordAudioCache();
  1800. // 停止当前播放的音频(现在由audioManager统一管理)
  1801. audioManager.stopCurrentAudio();
  1802. // 通知AudioControls组件处理音色切换
  1803. if (this.$refs.customTabbar && this.$refs.customTabbar.$refs.audioControls) {
  1804. try {
  1805. // 传入选项:preloadAllPages: true 表示要预加载所有页面的音频
  1806. await this.$refs.customTabbar.$refs.audioControls.handleVoiceChange(voiceId, {
  1807. preloadAllPages: true
  1808. });
  1809. } catch (error) {
  1810. console.error('音色切换处理失败:', error);
  1811. }
  1812. }
  1813. })
  1814. this.courseId = args.courseId
  1815. this.currentCourse = args.courseId // 同时设置 currentCourse
  1816. this.memberId = args.memberId
  1817. // 先获取点进来的课程的页面列表
  1818. await Promise.all([this.getVoiceList(), this.getMemberInfo(), this.getCourseList(this.courseId), this.getCoursePageList(args.bookId)])
  1819. // 页面加载完成后,通知AudioControls组件自动加载第一页音频
  1820. this.$nextTick(() => {
  1821. if (this.$refs.customTabbar && this.$refs.customTabbar.$refs.audioControls) {
  1822. this.$refs.customTabbar.$refs.audioControls.autoLoadAndPlayFirstPage();
  1823. }
  1824. });
  1825. },
  1826. // 页面卸载时清理资源
  1827. onUnload() {
  1828. uni.$off('selectVoice')
  1829. // 0. 清理滚动防抖定时器
  1830. if (this.scrollDebounceTimer) {
  1831. clearTimeout(this.scrollDebounceTimer);
  1832. this.scrollDebounceTimer = null;
  1833. }
  1834. // 1. 清理单词语音资源
  1835. if (this.currentWordAudio) {
  1836. try {
  1837. this.currentWordAudio.destroy();
  1838. } catch (error) {
  1839. console.error('销毁单词音频实例失败:', error);
  1840. }
  1841. this.currentWordAudio = null;
  1842. }
  1843. // 2. 清理单词语音缓存
  1844. this.clearWordAudioCache();
  1845. // 3. 停止单词音频播放状态
  1846. this.isWordAudioPlaying = false;
  1847. // 4. 通知AudioControls组件清理资源
  1848. if (this.$refs.customTabbar && this.$refs.customTabbar.$refs.audioControls) {
  1849. this.$refs.customTabbar.$refs.audioControls.destroyAudio();
  1850. }
  1851. // 5. 清理全局音频实例(防止遗漏)
  1852. try {
  1853. // 获取所有可能的音频上下文并销毁
  1854. if (typeof wx !== 'undefined' && wx.getBackgroundAudioManager) {
  1855. const bgAudio = wx.getBackgroundAudioManager();
  1856. if (bgAudio) {
  1857. bgAudio.stop();
  1858. }
  1859. }
  1860. } catch (error) {
  1861. console.error('清理背景音频失败:', error);
  1862. }
  1863. },
  1864. // 页面隐藏时暂停音频
  1865. onHide() {
  1866. // 1. 暂停单词音频
  1867. if (this.currentWordAudio && this.isWordAudioPlaying) {
  1868. try {
  1869. this.currentWordAudio.pause();
  1870. this.isWordAudioPlaying = false;
  1871. } catch (error) {
  1872. console.error('暂停单词音频失败:', error);
  1873. }
  1874. }
  1875. // 2. 通知AudioControls组件暂停音频
  1876. if (this.$refs.customTabbar && this.$refs.customTabbar.$refs.audioControls) {
  1877. this.$refs.customTabbar.$refs.audioControls.pauseOnHide();
  1878. }
  1879. }
  1880. }
  1881. </script>
  1882. <style lang="scss" scoped>
  1883. .book-container {
  1884. width: 100%;
  1885. min-height: 100vh;
  1886. background-color: #F8F8F8;
  1887. position: relative;
  1888. overflow: hidden;
  1889. }
  1890. .custom-navbar {
  1891. position: fixed;
  1892. top: 0;
  1893. left: 0;
  1894. right: 0;
  1895. background-color: #F8F8F8;
  1896. z-index: 1000;
  1897. transition: transform 0.3s ease;
  1898. &.navbar-hidden {
  1899. transform: translateY(-100%);
  1900. }
  1901. }
  1902. .navbar-content {
  1903. display: flex;
  1904. align-items: center;
  1905. justify-content: space-between;
  1906. padding: 20rpx 32rpx;
  1907. // padding-top: calc(20rpx + var(--status-bar-height, 0));
  1908. height: 60rpx;
  1909. }
  1910. .navbar-left,
  1911. .navbar-right {
  1912. width: 80rpx;
  1913. display: flex;
  1914. align-items: center;
  1915. }
  1916. .navbar-right {
  1917. justify-content: flex-end;
  1918. flex: 1;
  1919. }
  1920. .navbar-title {
  1921. transform: translateX(-50rpx);
  1922. flex: 1;
  1923. text-align: center;
  1924. font-family: PingFang SC;
  1925. font-weight: 500;
  1926. font-size: 32rpx;
  1927. color: #262626;
  1928. line-height: 48rpx;
  1929. }
  1930. .content-swiper {
  1931. flex: 1;
  1932. // min-height: calc(100vh - 100rpx);
  1933. // margin-top: 100rpx;
  1934. height: 100vh;
  1935. }
  1936. .swiper-item {
  1937. min-height: 100vh;
  1938. // background-color: red;
  1939. }
  1940. .content-area {
  1941. flex: 1;
  1942. padding: 30rpx 40rpx 100rpx;
  1943. /* #ifndef H5 */
  1944. padding: 100rpx 40rpx;
  1945. /* #endif */
  1946. // padding-top: ;
  1947. // background: linear-gradient(180deg, #DEFFFF 0%, #FBFEFF 22.65%, #F0FBFF 100%);
  1948. min-height: 100%;
  1949. box-sizing: border-box;
  1950. overflow-y: auto;
  1951. .title {
  1952. font-family: PingFang SC;
  1953. font-weight: 500;
  1954. font-size: 34rpx;
  1955. text-align: center;
  1956. color: #181818;
  1957. line-height: 48rpx;
  1958. margin-bottom: 32rpx;
  1959. }
  1960. .image-container {
  1961. width: 100%;
  1962. display: flex;
  1963. justify-content: center;
  1964. align-items: center;
  1965. margin: 30rpx 0;
  1966. /* 平板设备适配 */
  1967. @media screen and (min-width: 768px) {
  1968. margin: 40rpx 0;
  1969. }
  1970. }
  1971. .content-image {
  1972. width: 100%;
  1973. height: auto;
  1974. //max-width: 600rpx;
  1975. /* 限制最大宽度,避免在大屏设备上过大 */
  1976. display: block;
  1977. border-radius: 12rpx;
  1978. /* 添加圆角,提升视觉效果 */
  1979. box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1);
  1980. /* 添加阴影,增强层次感 */
  1981. /* 平板设备适配 */
  1982. // @media screen and (min-width: 768px) {
  1983. // max-width: 500rpx;
  1984. // }
  1985. }
  1986. .video-content {
  1987. width: 100%;
  1988. height: auto;
  1989. margin: 30rpx auto;
  1990. position: relative;
  1991. .video-player {
  1992. // height: 100%;
  1993. width: 100%;
  1994. height: 60vw;
  1995. // margin: 0 auto;
  1996. // height: auto;
  1997. }
  1998. .video-loading {
  1999. position: absolute;
  2000. top: 50%;
  2001. left: 50%;
  2002. transform: translate(-50%, -50%);
  2003. color: #666;
  2004. font-size: 28rpx;
  2005. }
  2006. }
  2007. }
  2008. .card-content {
  2009. background: linear-gradient(180deg, #DEFFFF 0%, #FBFEFF 22.65%, #F0FBFF 100%);
  2010. display: flex;
  2011. flex-direction: column;
  2012. gap: 32rpx;
  2013. min-height: 1172rpx;
  2014. margin-top: 20rpx;
  2015. border-radius: 32rpx;
  2016. // height: 100%;
  2017. padding: 20rpx;
  2018. padding-bottom: 100rpx;
  2019. // margin: 0
  2020. border: 1px solid #FFFFFF;
  2021. box-sizing: border-box;
  2022. .card-line {
  2023. display: flex;
  2024. align-items: center;
  2025. // margin-bottom: 20rpx;
  2026. padding: 20rpx;
  2027. padding-bottom: 0;
  2028. }
  2029. .card-line-image {
  2030. width: 48rpx;
  2031. height: 48rpx;
  2032. margin-right: 16rpx;
  2033. }
  2034. .card-line-text {
  2035. font-family: PingFang SC;
  2036. font-weight: 600;
  2037. font-size: 30rpx;
  2038. line-height: 48rpx;
  2039. color: #3B3D3D;
  2040. }
  2041. .card-image {
  2042. // width: 590rpx;
  2043. width: 100%;
  2044. //height: 268rpx;
  2045. border-radius: 24rpx;
  2046. margin: 30rpx auto;
  2047. // margin-bottom: 20rpx;
  2048. }
  2049. // .english-text {
  2050. // display: block;
  2051. // font-family: PingFang SC;
  2052. // font-weight: 600;
  2053. // font-size: 32rpx;
  2054. // line-height: 48rpx;
  2055. // color: #3B3D3D;
  2056. // // margin-bottom: 16rpx;
  2057. // }
  2058. // .english-text-container {
  2059. // display: flex;
  2060. // flex-wrap: wrap;
  2061. // align-items: baseline;
  2062. // }
  2063. // .english-token {
  2064. // font-family: PingFang SC;
  2065. // font-weight: 600;
  2066. // font-size: 32rpx;
  2067. // line-height: 48rpx;
  2068. // color: #3B3D3D;
  2069. // margin-right: 10rpx;
  2070. // }
  2071. .clickable-word {
  2072. background: $primary-color;
  2073. text-decoration: underline;
  2074. cursor: pointer;
  2075. transition: all 0.2s ease;
  2076. padding: 0 20rpx;
  2077. }
  2078. .clickable-word:hover {
  2079. background-color: rgba(0, 122, 255, 0.1);
  2080. border-radius: 4rpx;
  2081. }
  2082. .chinese-segment {
  2083. font-family: PingFang SC;
  2084. font-weight: 400;
  2085. font-size: 28rpx;
  2086. line-height: 48rpx;
  2087. color: #3B3D3D;
  2088. }
  2089. .clickable-keyword {
  2090. background: $primary-color;
  2091. text-decoration: underline;
  2092. cursor: pointer;
  2093. color: #fff !important;
  2094. transition: all 0.2s ease;
  2095. border-radius: 4rpx;
  2096. padding: 4rpx;
  2097. }
  2098. .clickable-keyword:hover {
  2099. background-color: rgba(0, 122, 255, 0.1);
  2100. }
  2101. .chinese-text {
  2102. display: block;
  2103. font-family: PingFang SC;
  2104. font-weight: 400;
  2105. font-size: 28rpx;
  2106. line-height: 48rpx;
  2107. color: #4F4F4F;
  2108. }
  2109. }
  2110. /* 会员限制页面样式 */
  2111. .member-content {
  2112. display: flex;
  2113. flex-direction: column;
  2114. align-items: center;
  2115. justify-content: center;
  2116. height: 90%;
  2117. background-color: #F8F8F8;
  2118. padding: 40rpx;
  2119. margin: -40rpx;
  2120. box-sizing: border-box;
  2121. }
  2122. .member-title {
  2123. font-family: PingFang SC;
  2124. font-weight: 500;
  2125. font-size: 40rpx;
  2126. line-height: 1;
  2127. color: #6f6f6f;
  2128. text-align: center;
  2129. margin-bottom: 48rpx;
  2130. }
  2131. .member-button {
  2132. width: 670rpx;
  2133. height: 72rpx;
  2134. background: #06DADC;
  2135. border-radius: 200rpx;
  2136. display: flex;
  2137. align-items: center;
  2138. justify-content: center;
  2139. }
  2140. .member-button-text {
  2141. font-family: PingFang SC;
  2142. font-weight: 400;
  2143. font-size: 30rpx;
  2144. color: #FFFFFF;
  2145. }
  2146. // .video-content {
  2147. // width: 100%;
  2148. // height: auto;
  2149. // // margin: 200rpx -40rpx 0;
  2150. // // height: 500rpx;
  2151. // background-color: #FFFFFF;
  2152. // // padding: 40rpx;
  2153. // border-radius: 24rpx;
  2154. // display: flex;
  2155. // align-items: center;
  2156. // justify-content: center;
  2157. // .video-player{
  2158. // width: 100%;
  2159. // margin: 0 auto;
  2160. // height: auto;
  2161. // }
  2162. // }
  2163. .text-content {
  2164. // background-color: #F6F6F6;
  2165. box-sizing: border-box;
  2166. &>view {
  2167. padding: 20rpx;
  2168. }
  2169. }
  2170. .content-text {
  2171. font-family: PingFang SC;
  2172. // font-weight: 400;
  2173. font-size: 28rpx;
  2174. color: #3B3D3D;
  2175. line-height: 48rpx;
  2176. letter-spacing: 0;
  2177. text-align: justify;
  2178. word-break: break-all;
  2179. transition: all 0.3s ease;
  2180. }
  2181. .clickable-text {
  2182. cursor: pointer;
  2183. &:active {
  2184. background-color: rgba(6, 218, 220, 0.1);
  2185. border-radius: 4rpx;
  2186. }
  2187. }
  2188. .lead-text {
  2189. background: #06dadc12;
  2190. // background: #fffbe6;#06dadc
  2191. /* 柔和的提示背景 */
  2192. // border: 1px solid #ffe58f;
  2193. border-radius: 8px;
  2194. // padding: 10rpx 20rpx;
  2195. /* 添加平滑过渡动画 */
  2196. transition: all 0.3s ease;
  2197. }
  2198. .introduction-text {
  2199. background: #fffbe6;
  2200. border: 1px solid #ffe58f;
  2201. border-radius: 8px;
  2202. padding: 10rpx 20rpx;
  2203. }
  2204. .text-highlight {
  2205. background-color: rgba(255, 248, 220, 0.8);
  2206. /* 温暖的米黄色,对眼睛友好 */
  2207. border-left: 4rpx solid #ffd700;
  2208. /* 左侧金色边框作为朗读指示 */
  2209. padding: 4rpx 8rpx;
  2210. border-radius: 6rpx;
  2211. box-shadow: 0 2rpx 6rpx rgba(255, 215, 0, 0.15);
  2212. /* 柔和的阴影 */
  2213. /* 添加平滑过渡动画 */
  2214. transition: all 0.3s ease;
  2215. }
  2216. </style>