index.vue 7.0 KB

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