index.vue 6.2 KB

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