index.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <template>
  2. <view class="word-view-page">
  3. <view class="icon-title-navBar-box">
  4. <view @click="handleBack" class="nav-bar-icon"></view>
  5. <text class="nav-bar-title">学习</text>
  6. </view>
  7. <view class="word-view-border">
  8. <template v-for="item in data.wordList">
  9. <mainCardVue :active-word="data.activeWord" :pageData="data" :active-words="activeWords"
  10. @play-audio="handlePlayAudio" @goXiangjie="goXiangjie" @swiper-change="handleSwiperChange"
  11. :is-playing="isAudioPlaying" v-if="item.id == data.activeId" :key="item.id">
  12. </mainCardVue>
  13. </template>
  14. </view>
  15. <view class="word-view-bottom">
  16. <view class="bottom-btn-box">
  17. <view class="word-view-btn" @click="prevWord" v-if="!isFirst&&isLearnRecord!=0">上一词</view>
  18. <view class="word-view-btn" @click="nextWord" v-if="!isLast&&isLearnRecord!=0">下一词</view>
  19. <view class="word-view-btn" v-if="isLast&&isLearnRecord!=0" @click="handleComplete">完成</view>
  20. </view>
  21. </view>
  22. </view>
  23. </template>
  24. <script setup>
  25. import mainCardVue from "./components/mainCard.vue";
  26. import {
  27. ref,
  28. reactive,
  29. computed,
  30. nextTick,
  31. onUnmounted
  32. } from "vue";
  33. import {
  34. onLoad
  35. } from "@dcloudio/uni-app";
  36. import * as httpApi from "@/api/chaojidanci.js"
  37. import {
  38. useAudioCache,
  39. audioPlayer
  40. } from "./components/useAudio.js"
  41. import cacheManager from "@/utils/cacheManager";
  42. const {
  43. cacheAudio,
  44. clearAudioCache
  45. } = useAudioCache();
  46. const AudioP = new audioPlayer();
  47. const isLearnRecord = ref(null)
  48. const isLearnStatus = ref(null)
  49. const isAudioPlaying = ref(false) // 音频播放状态
  50. // 监听音频播放事件
  51. uni.$on('danci-audio-play', updateAudioPlayingTrue)
  52. uni.$on('danci-audio-ended', updateAudioPlayingFalse)
  53. onUnmounted(() => {
  54. uni.$off('danci-audio-play', updateAudioPlayingTrue)
  55. uni.$off('danci-audio-ended', updateAudioPlayingFalse)
  56. })
  57. function updateAudioPlayingFalse() {
  58. isAudioPlaying.value = false
  59. }
  60. function updateAudioPlayingTrue() {
  61. isAudioPlaying.value = true
  62. }
  63. function handleSwiperChange(index) {
  64. return false
  65. // 客户暂时不要
  66. if (isAudioPlaying.value) return; // 如果正在播放,不允许切换
  67. // 无论切换到哪个页面,都播放单词的主发音(yinpin)
  68. if (data.activeWord && data.activeWord.yinpin) {
  69. handlePlayAudio({
  70. url: data.activeWord.yinpin,
  71. code: 'swiper-auto-play'
  72. });
  73. }
  74. }
  75. function courseBjFun() {
  76. return 'static/images/course/course-cjdc-bj.png'
  77. }
  78. function chunkArray(arr, chunkSize) {
  79. const result = [];
  80. for (let i = 0; i < arr.length; i += chunkSize) {
  81. result.push(arr.slice(i, i + chunkSize));
  82. }
  83. return result;
  84. }
  85. const data = reactive({
  86. danyuanId: null, // 节/单元ID
  87. activeId: null, // 当前单词
  88. wordList: [], // 单词列表
  89. arrayList: [], // 整合数据
  90. activeWord: null, // 单词详情数据
  91. collectFlag: 0, // 收藏状态
  92. subjectId: null, // 学科ID
  93. levelId: null, // 等级
  94. typeId: null, // 类型
  95. tipFlag: null, // 提示
  96. youkeZhangId: null, // 章ID
  97. isLearnStatus: null, // 学习记录状态
  98. })
  99. onLoad(({
  100. danyuanId,
  101. wordId,
  102. subjectId,
  103. levelId,
  104. typeId,
  105. tipFlag,
  106. isLearnStatus,
  107. }) => {
  108. data.danyuanId = danyuanId;
  109. isLearnRecord.value = danyuanId;
  110. data.activeId = wordId;
  111. data.isLearnStatus = isLearnStatus;
  112. data.subjectId = subjectId;
  113. data.levelId = levelId;
  114. data.typeId = typeId;
  115. data.tipFlag = tipFlag;
  116. // 获取单词列表数据
  117. initWordInfo();
  118. // 清理过期文件
  119. clearAudioCache();
  120. })
  121. // 是否是最后一题
  122. const isLast = computed(() => {
  123. if (!data.wordList.length) {
  124. return false
  125. }
  126. return data.activeId == data.wordList[data.wordList.length - 1].id;
  127. })
  128. const isFirst = computed(() => {
  129. if (!data.wordList.length) {
  130. return false
  131. }
  132. return data.activeId == data.wordList[0].id;
  133. })
  134. const activeWords = computed(() => {
  135. if (!data.activeId) {
  136. return null
  137. }
  138. return data.arrayList.find(item => item.some(cit => cit.id == data.activeId))
  139. })
  140. function nextWord() {
  141. const index = data.wordList.findIndex(item => item.id == data.activeId);
  142. if (index < data.wordList.length - 1) {
  143. uni.redirectTo({
  144. url: `/pages/chaojidanci/newEnglish/index?danyuanId=${data.danyuanId}&wordId=${data.wordList[index + 1].id}`
  145. })
  146. }
  147. }
  148. function prevWord() {
  149. const index = data.wordList.findIndex(item => item.id == data.activeId);
  150. if (index > 0) {
  151. uni.redirectTo({
  152. url: `/pages/chaojidanci/newEnglish/index?danyuanId=${data.danyuanId}&wordId=${data.wordList[index - 1].id}`
  153. })
  154. }
  155. }
  156. function handleBack() {
  157. // 返回单词列表
  158. uni.redirectTo({
  159. url: `/pages/wordList/wordList?danyuanId=${data.danyuanId}`
  160. })
  161. }
  162. function handleComplete() {
  163. // 返回岛
  164. uni.redirectTo({
  165. url: `/pages/study/index`
  166. })
  167. }
  168. function initWordInfo() {
  169. httpApi.getWordInfo({
  170. danyuanId: data.danyuanId,
  171. wordId: data.activeId
  172. }).then(res => {
  173. data.activeWord = res.data;
  174. data.wordList = res.data.danciList; // 单词组
  175. data.collectFlag = res.data.collectFlag; // 收藏状态
  176. if (data.wordList.length) {
  177. data.arrayList = chunkArray(data.wordList, 5); // 将1维单词数组转换为2维数组
  178. }
  179. handleCacheAudio()
  180. })
  181. }
  182. // 新增自动播放功能
  183. const autoPlayAudio = async () => {
  184. // 等待数据加载完成
  185. await nextTick();
  186. // 检查是否有单词发音数据
  187. if (data.activeWord && data.activeWord.yinpin) {
  188. // 延迟500ms确保音频已缓存
  189. setTimeout(() => {
  190. handlePlayAudio({
  191. url: data.activeWord.yinpin,
  192. code: 'auto-play' // 特殊标记,区分自动播放
  193. });
  194. }, 500);
  195. }
  196. };
  197. function goXiangjie() {
  198. uni.redirectTo({
  199. url: `/pages/chaojidanci/newEnglish/components/xiangjie?levelId=${data.levelId}&typeId=${data.typeId}&subjectId=${data.subjectId}&tipFlag=${data.tipFlag}&wordId=${data.activeId}&youkeZhangId=${data.youkeZhangId}&danyuanId=${data.danyuanId}`
  200. })
  201. }
  202. function handleCacheAudio() {
  203. const arr = []
  204. // yinpin
  205. arr.push(data.activeWord.yinpin);
  206. // yinjie
  207. data.activeWord.yinjie.forEach(item => {
  208. arr.push(item.yinpin)
  209. })
  210. // pindu
  211. data.activeWord.pindu.forEach(item => {
  212. arr.push(item.yinpin)
  213. })
  214. // 缓存音频
  215. arr.map(item => cacheAudio(item));
  216. // console.log('arr',arr)
  217. }
  218. async function handlePlayAudio({
  219. url,
  220. code
  221. }) {
  222. console.log('播放', url)
  223. console.log('播放', code)
  224. const cachedPath = await cacheAudio(url);
  225. if (cachedPath && cachedPath.includes('.mp3')) {
  226. // console.log('地址:', cachedPath)
  227. AudioP.play(cachedPath, code);
  228. } else {
  229. uni.showToast({
  230. title: '音频加载失败',
  231. icon: 'none'
  232. });
  233. }
  234. }
  235. </script>
  236. <style>
  237. </style>