index.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <template>
  2. <view class="word-view-page" :style="{backgroundImage: 'url(' + courseBjFun() + ')'}">
  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. <template v-for="item in data.wordList">
  8. <mainCardVue :active-word="data.activeWord" :pageData="data" :active-words="activeWords"
  9. @play-audio="handlePlayAudio" @goXiangjie="goXiangjie" v-if="item.id == data.activeId"
  10. :key="item.id">
  11. </mainCardVue>
  12. </template>
  13. <view class="word-view-bottom">
  14. <view class="collect-btn" v-if="userCode != 'Visitor'" @click="handleShouCang">
  15. <icon :class="{active: data.collectFlag}"></icon>
  16. <view>收藏</view>
  17. </view>
  18. <view class="bottom-btn-box">
  19. <view class="word-view-btn" @click="prevWord" v-if="!isFirst&&isLearnRecord!=0">上一词</view>
  20. <view class="word-view-btn" @click="nextWord" v-if="!isLast&&isLearnRecord!=0">下一词</view>
  21. <view class="word-view-btn" v-if="isLast&&isLearnRecord!=0" @click="handleComplete">完成</view>
  22. </view>
  23. </view>
  24. <audioRightWrongVue></audioRightWrongVue>
  25. </view>
  26. </template>
  27. <script setup>
  28. import mainCardVue from "./components/mainCard.vue";
  29. import {
  30. ref,
  31. reactive,
  32. computed,
  33. nextTick
  34. } from "vue";
  35. import {
  36. onLoad
  37. } from "@dcloudio/uni-app";
  38. import * as httpApi from "@/api/word.js"
  39. import {
  40. getUserIdentity,
  41. } from "@/utils/common.js"
  42. import {
  43. useAudioCache,
  44. audioPlayer
  45. } from "./components/useAudio.js"
  46. import {
  47. getWordCancel
  48. } from "../../api/word";
  49. import audioRightWrongVue from "./components/audioRightWrong.vue";
  50. const userCode = getUserIdentity();
  51. const {
  52. cacheAudio,
  53. clearAudioCache
  54. } = useAudioCache();
  55. const AudioP = new audioPlayer();
  56. const isLearnRecord = ref(null)
  57. const isLearnStatus = ref(null)
  58. function courseBjFun() {
  59. return 'static/images/course/course-cjdc-bj.png'
  60. }
  61. function chunkArray(arr, chunkSize) {
  62. const result = [];
  63. for (let i = 0; i < arr.length; i += chunkSize) {
  64. result.push(arr.slice(i, i + chunkSize));
  65. }
  66. return result;
  67. }
  68. const data = reactive({
  69. jieId: null, // 节/单元ID
  70. activeId: null, // 当前单词
  71. wordList: [], // 单词列表
  72. arrayList: [], // 整合数据
  73. activeWord: null, // 单词详情数据
  74. collectFlag: 0, // 收藏状态
  75. subjectId: null, // 学科ID
  76. levelId: null, // 等级
  77. typeId: null, // 类型
  78. tipFlag: null, // 提示
  79. youkeZhangId: null, // 章ID
  80. isLearnStatus: null, // 学习记录状态
  81. })
  82. onLoad(({
  83. jieId,
  84. wordId,
  85. subjectId,
  86. levelId,
  87. typeId,
  88. tipFlag,
  89. isLearnStatus,
  90. youkeZhangId
  91. }) => {
  92. data.jieId = jieId;
  93. isLearnRecord.value = jieId;
  94. data.activeId = wordId;
  95. data.isLearnStatus = isLearnStatus;
  96. data.subjectId = subjectId;
  97. data.levelId = levelId;
  98. data.typeId = typeId;
  99. data.tipFlag = tipFlag;
  100. data.youkeZhangId = youkeZhangId;
  101. // 获取单词列表数据
  102. initWordInfo();
  103. // 清理过期文件
  104. clearAudioCache();
  105. })
  106. // 是否是最后一题
  107. const isLast = computed(() => {
  108. if (!data.wordList.length) {
  109. return false
  110. }
  111. return data.activeId == data.wordList[data.wordList.length - 1].id;
  112. })
  113. const isFirst = computed(() => {
  114. if (!data.wordList.length) {
  115. return false
  116. }
  117. return data.activeId == data.wordList[0].id;
  118. })
  119. const activeWords = computed(() => {
  120. if (!data.activeId) {
  121. return null
  122. }
  123. return data.arrayList.find(item => item.some(cit => cit.id == data.activeId))
  124. })
  125. function nextWord() {
  126. const index = data.wordList.findIndex(item => item.id == data.activeId);
  127. if (index < data.wordList.length - 1) {
  128. uni.redirectTo({
  129. url: `/pages/newEnglish/index?jieId=${data.jieId}&wordId=${data.wordList[index + 1].id }`
  130. })
  131. }
  132. }
  133. function prevWord() {
  134. const index = data.wordList.findIndex(item => item.id == data.activeId);
  135. if (index > 0) {
  136. uni.redirectTo({
  137. url: `/pages/newEnglish/index?jieId=${data.jieId}&wordId=${data.wordList[index - 1].id }`
  138. })
  139. }
  140. }
  141. function handleBack() {
  142. // 返回单词列表
  143. if (userCode !== 'Visitor') {
  144. if (data.jieId == 0) {
  145. uni.redirectTo({
  146. url: `/pages/my/learnRecord?jieId=${data.jieId}&isLearnStatus=${data.isLearnStatus}`
  147. })
  148. } else {
  149. uni.redirectTo({
  150. url: `/pages/wordList/wordList?jieId=${data.jieId}`
  151. })
  152. }
  153. } else {
  154. const youkePageData = JSON.stringify({
  155. subjectId: data.subjectId,
  156. levelId: data.levelId,
  157. typeId: data.typeId,
  158. tipFlag: data.tipFlag,
  159. youkeZhangId: data.youkeZhangId,
  160. jieId: data.jieId
  161. })
  162. uni.redirectTo({
  163. url: `/pages/wordList/wordList?youkePageData=${youkePageData}`
  164. })
  165. }
  166. }
  167. function handleComplete() {
  168. // 返回岛
  169. if (userCode !== 'Visitor') {
  170. uni.redirectTo({
  171. url: `/pages/study/index`
  172. })
  173. } else {
  174. uni.redirectTo({
  175. url: `/pages/study/index?levelId=${data.levelId}&typeId=${data.typeId}&subjectId=${data.subjectId}&tipFlag=${data.tipFlag}&youkeZhangId=${data.youkeZhangId}&jieId=${data.jieId}`
  176. })
  177. }
  178. }
  179. function handleShouCang() {
  180. if (data.collectFlag == 1) {
  181. httpApi.getWordCancel({
  182. wordId: data.activeId
  183. }).then(res => {
  184. uni.showToast({
  185. title: '操作成功'
  186. })
  187. data.collectFlag = 0;
  188. })
  189. } else {
  190. httpApi.getWordShouCang({
  191. wordId: data.activeId
  192. }).then(res => {
  193. uni.showToast({
  194. title: '操作成功'
  195. })
  196. data.collectFlag = 1;
  197. })
  198. }
  199. }
  200. function initWordInfo() {
  201. if (userCode !== 'Visitor') {
  202. httpApi.getWordInfo({
  203. jieId: data.jieId,
  204. wordId: data.activeId
  205. }).then(res => {
  206. data.activeWord = res.data;
  207. data.wordList = res.data.danciList; // 单词组
  208. data.collectFlag = res.data.collectFlag; // 收藏状态
  209. if (data.wordList.length) {
  210. data.arrayList = chunkArray(data.wordList, 5); // 将1维单词数组转换为2维数组
  211. }
  212. // console.log('res.data',res.data)
  213. handleCacheAudio()
  214. //autoPlayAudio()
  215. })
  216. } else {
  217. httpApi.getCommonWordInfo({
  218. jieId: data.jieId,
  219. wordId: data.activeId
  220. }).then(res => {
  221. data.activeWord = res.data;
  222. data.wordList = res.data.danciList; // 单词组
  223. data.collectFlag = res.data.collectFlag; // 收藏状态
  224. if (data.wordList.length) {
  225. data.arrayList = chunkArray(data.wordList, 5); // 将1维单词数组转换为2维数组
  226. }
  227. handleCacheAudio()
  228. //autoPlayAudio()
  229. })
  230. }
  231. }
  232. // 新增自动播放功能
  233. const autoPlayAudio = async () => {
  234. // 等待数据加载完成
  235. await nextTick();
  236. // 检查是否有单词发音数据
  237. if (data.activeWord && data.activeWord.yinpin) {
  238. // 延迟500ms确保音频已缓存
  239. setTimeout(() => {
  240. handlePlayAudio({
  241. url: data.activeWord.yinpin,
  242. code: 'auto-play' // 特殊标记,区分自动播放
  243. });
  244. }, 500);
  245. }
  246. };
  247. function goXiangjie() {
  248. uni.redirectTo({
  249. url: `/pages/newEnglish/components/xiangjie?levelId=${data.levelId}&typeId=${data.typeId}&subjectId=${data.subjectId}&tipFlag=${data.tipFlag}&wordId=${data.activeId}&youkeZhangId=${data.youkeZhangId}&jieId=${data.jieId}`
  250. })
  251. }
  252. function handleCacheAudio() {
  253. const arr = []
  254. // yinpin
  255. arr.push(data.activeWord.yinpin);
  256. // yinjie
  257. data.activeWord.yinjie.forEach(item => {
  258. arr.push(item.yinpin)
  259. })
  260. // pindu
  261. data.activeWord.pindu.forEach(item => {
  262. arr.push(item.yinpin)
  263. })
  264. // 缓存音频
  265. arr.map(item => cacheAudio(item));
  266. // console.log('arr',arr)
  267. }
  268. async function handlePlayAudio({
  269. url,
  270. code
  271. }) {
  272. console.log('播放', url)
  273. console.log('播放', code)
  274. const cachedPath = await cacheAudio(url);
  275. if (cachedPath && cachedPath.includes('.mp3')) {
  276. // console.log('地址:', cachedPath)
  277. AudioP.play(cachedPath, code);
  278. } else {
  279. uni.showToast({
  280. title: '音频加载失败',
  281. icon: 'none'
  282. });
  283. }
  284. }
  285. </script>
  286. <style>
  287. </style>