index.vue 6.9 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. <ezyActiveVue class="ezy-btn-active word-view-btn" @click="prevWord" v-if="!isFirst&&isLearnRecord!=0">上一词</ezyActiveVue>
  18. <view v-else></view>
  19. <ezyActiveVue class="ezy-btn-active word-view-btn" @click="nextWord" v-if="!isLast&&isLearnRecord!=0">下一词</ezyActiveVue>
  20. <ezyActiveVue class="ezy-btn-active word-view-btn" v-if="isLast&&isLearnRecord!=0" @click="handleComplete">完成</ezyActiveVue>
  21. </view>
  22. </view>
  23. </view>
  24. </template>
  25. <script setup>
  26. import mainCardVue from "./components/mainCard.vue";
  27. import {
  28. ref,
  29. reactive,
  30. computed,
  31. nextTick,
  32. onUnmounted
  33. } from "vue";
  34. import {
  35. onLoad
  36. } from "@dcloudio/uni-app";
  37. import * as httpApi from "@/api/chaojidanci.js"
  38. import {
  39. useAudioCache,
  40. audioPlayer
  41. } from "./components/useAudio.js"
  42. import cacheManager from "@/utils/cacheManager";
  43. import ezyActiveVue from "@/components/ezyActive/ezyActive.vue";
  44. const {
  45. cacheAudio,
  46. clearAudioCache
  47. } = useAudioCache();
  48. const AudioP = new audioPlayer();
  49. const isLearnRecord = ref(null)
  50. const isLearnStatus = ref(null)
  51. const isAudioPlaying = ref(false) // 音频播放状态
  52. // 监听音频播放事件
  53. uni.$on('danci-audio-play', updateAudioPlayingTrue)
  54. uni.$on('danci-audio-ended', updateAudioPlayingFalse)
  55. onUnmounted(() => {
  56. uni.$off('danci-audio-play', updateAudioPlayingTrue)
  57. uni.$off('danci-audio-ended', updateAudioPlayingFalse)
  58. })
  59. function updateAudioPlayingFalse() {
  60. isAudioPlaying.value = false
  61. }
  62. function updateAudioPlayingTrue() {
  63. isAudioPlaying.value = true
  64. }
  65. function handleSwiperChange(index) {
  66. return false
  67. // 客户暂时不要
  68. if (isAudioPlaying.value) return; // 如果正在播放,不允许切换
  69. // 无论切换到哪个页面,都播放单词的主发音(yinpin)
  70. if (data.activeWord && data.activeWord.yinpin) {
  71. handlePlayAudio({
  72. url: data.activeWord.yinpin,
  73. code: 'swiper-auto-play'
  74. });
  75. }
  76. }
  77. function courseBjFun() {
  78. return 'static/images/course/course-cjdc-bj.png'
  79. }
  80. function chunkArray(arr, chunkSize) {
  81. const result = [];
  82. for (let i = 0; i < arr.length; i += chunkSize) {
  83. result.push(arr.slice(i, i + chunkSize));
  84. }
  85. return result;
  86. }
  87. const data = reactive({
  88. danyuanId: null, // 节/单元ID
  89. activeId: null, // 当前单词
  90. wordList: [], // 单词列表
  91. arrayList: [], // 整合数据
  92. activeWord: null, // 单词详情数据
  93. collectFlag: 0, // 收藏状态
  94. subjectId: null, // 学科ID
  95. levelId: null, // 等级
  96. typeId: null, // 类型
  97. tipFlag: null, // 提示
  98. youkeZhangId: null, // 章ID
  99. isLearnStatus: null, // 学习记录状态
  100. })
  101. onLoad(({
  102. danyuanId,
  103. wordId,
  104. subjectId,
  105. levelId,
  106. typeId,
  107. tipFlag,
  108. isLearnStatus,
  109. }) => {
  110. data.danyuanId = danyuanId;
  111. isLearnRecord.value = danyuanId;
  112. data.activeId = wordId;
  113. data.isLearnStatus = isLearnStatus;
  114. data.subjectId = subjectId;
  115. data.levelId = levelId;
  116. data.typeId = typeId;
  117. data.tipFlag = tipFlag;
  118. // 获取单词列表数据
  119. initWordInfo();
  120. // 清理过期文件
  121. clearAudioCache();
  122. })
  123. // 是否是最后一题
  124. const isLast = computed(() => {
  125. if (!data.wordList.length) {
  126. return false
  127. }
  128. return data.activeId == data.wordList[data.wordList.length - 1].id;
  129. })
  130. const isFirst = computed(() => {
  131. if (!data.wordList.length) {
  132. return false
  133. }
  134. return data.activeId == data.wordList[0].id;
  135. })
  136. const activeWords = computed(() => {
  137. if (!data.activeId) {
  138. return null
  139. }
  140. return data.arrayList.find(item => item.some(cit => cit.id == data.activeId))
  141. })
  142. function nextWord() {
  143. const index = data.wordList.findIndex(item => item.id == data.activeId);
  144. if (index < data.wordList.length - 1) {
  145. uni.redirectTo({
  146. url: `/pages/chaojidanci/newEnglish/index?danyuanId=${data.danyuanId}&wordId=${data.wordList[index + 1].id}`
  147. })
  148. }
  149. }
  150. function prevWord() {
  151. const index = data.wordList.findIndex(item => item.id == data.activeId);
  152. if (index > 0) {
  153. uni.redirectTo({
  154. url: `/pages/chaojidanci/newEnglish/index?danyuanId=${data.danyuanId}&wordId=${data.wordList[index - 1].id}`
  155. })
  156. }
  157. }
  158. function handleBack() {
  159. // 返回单词列表
  160. uni.navigateBack()
  161. }
  162. function handleComplete() {
  163. // 返回岛
  164. uni.switchTab({
  165. url: `/pages/chanpinneirong/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>