index.vue 7.0 KB

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