index.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. v-if="item.id == data.activeId" :key="item.id">
  12. </mainCardVue>
  13. </template>
  14. </view>
  15. </view>
  16. <view class="word-view-bottom">
  17. <view class="collect-btn" v-if="userCode != 'Visitor'" @click="handleShouCang">
  18. <icon :class="{active: data.collectFlag}"></icon><view>收藏</view>
  19. </view>
  20. <view class="bottom-btn-box">
  21. <view class="word-view-btn" @click="prevWord" v-if="!isFirst">上一词</view>
  22. <view class="word-view-btn" @click="nextWord" v-if="!isLast">下一词</view>
  23. <view class="word-view-btn" v-if="isLast" @click="handleComplete">完成</view>
  24. </view>
  25. </view>
  26. </view>
  27. </template>
  28. <script setup>
  29. import mainCardVue from "./components/mainCard.vue";
  30. import {
  31. ref,
  32. reactive,
  33. computed
  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. const userCode = getUserIdentity();
  43. function courseBjFun() {
  44. return 'static/images/course/course-cjdc-bj.png'
  45. }
  46. function chunkArray(arr, chunkSize) {
  47. const result = [];
  48. for (let i = 0; i < arr.length; i += chunkSize) {
  49. result.push(arr.slice(i, i + chunkSize));
  50. }
  51. return result;
  52. }
  53. const data = reactive({
  54. jieId: null, // 节/单元ID
  55. activeId: null, // 当前单词
  56. wordList: [], // 单词列表
  57. arrayList: [], // 整合数据
  58. activeWord: null, // 单词详情数据
  59. collectFlag: 0, // 收藏状态
  60. subjectId: null, // 学科ID
  61. levelId: null, // 等级
  62. typeId: null, // 类型
  63. tipFlag: null, // 提示
  64. zhangId: null, // 章ID
  65. })
  66. onLoad(({
  67. jieId,
  68. wordId,
  69. subjectId,
  70. levelId,
  71. typeId,
  72. tipFlag,
  73. zhangId
  74. }) => {
  75. data.jieId = jieId;
  76. data.activeId = wordId;
  77. data.subjectId = subjectId;
  78. data.levelId = levelId;
  79. data.typeId = typeId;
  80. data.tipFlag = tipFlag;
  81. data.zhangId = zhangId;
  82. // 获取单词列表数据
  83. initWordInfo();
  84. })
  85. // 是否是最后一题
  86. const isLast = computed(() => {
  87. if (!data.wordList.length) {
  88. return false
  89. }
  90. return data.activeId == data.wordList[data.wordList.length - 1].id;
  91. })
  92. const isFirst = computed(() => {
  93. if (!data.wordList.length) {
  94. return false
  95. }
  96. return data.activeId == data.wordList[0].id;
  97. })
  98. const activeWords = computed(() => {
  99. if (!data.activeId) {
  100. return null
  101. }
  102. return data.arrayList.find(item => item.some(cit => cit.id == data.activeId))
  103. })
  104. function nextWord() {
  105. const index = data.wordList.findIndex(item => item.id == data.activeId);
  106. if (index < data.wordList.length - 1) {
  107. uni.redirectTo({
  108. url: `/pages/newEnglish/index?jieId=${data.jieId}&wordId=${data.wordList[index + 1].id }`
  109. })
  110. }
  111. }
  112. function prevWord() {
  113. const index = data.wordList.findIndex(item => item.id == data.activeId);
  114. if (index > 0) {
  115. uni.redirectTo({
  116. url: `/pages/newEnglish/index?jieId=${data.jieId}&wordId=${data.wordList[index - 1].id }`
  117. })
  118. }
  119. }
  120. function handleBack() {
  121. // 返回单词列表
  122. if (userCode !== 'Visitor') {
  123. uni.redirectTo({
  124. url: `/pages/wordList/wordList?jieId=${data.jieId}`
  125. })
  126. } else {
  127. const youkePageData = JSON.stringify({
  128. subjectId: data.subjectId,
  129. levelId: data.levelId,
  130. typeId: data.typeId,
  131. tipFlag: data.tipFlag,
  132. youkeZhangId: data.zhangId,
  133. jieId: data.jieId
  134. })
  135. uni.redirectTo({
  136. url: `/pages/wordList/wordList?youkePageData=${youkePageData}`
  137. })
  138. }
  139. }
  140. function handleComplete() {
  141. // 返回岛
  142. if (userCode !== 'Visitor') {
  143. uni.redirectTo({
  144. url: `/pages/study/index`
  145. })
  146. } else {
  147. uni.redirectTo({
  148. url: `/pages/study/index?levelId=${data.levelId}&typeId=${data.typeId}&subjectId=${data.subjectId}&tipFlag=${data.tipFlag}&youkeZhangId=${data.zhangId}&jieId=${data.jieId}`
  149. })
  150. }
  151. }
  152. function handleShouCang() {
  153. httpApi.getWordShouCang({
  154. wordId: data.activeId
  155. }).then(res => {
  156. if (res.data) {
  157. uni.showToast({
  158. title: '收藏成功'
  159. })
  160. data.collectFlag = 1;
  161. } else {
  162. uni.showToast({
  163. title: '收藏取消'
  164. })
  165. data.collectFlag = 0;
  166. }
  167. })
  168. }
  169. function initWordInfo() {
  170. if (userCode !== 'Visitor') {
  171. httpApi.getWordInfo({
  172. jieId: data.jieId,
  173. wordId: data.activeId
  174. }).then(res => {
  175. data.activeWord = res.data;
  176. data.wordList = res.data.danciList; // 单词组
  177. data.collectFlag = res.data.collectFlag; // 收藏状态
  178. if (data.wordList.length) {
  179. data.arrayList = chunkArray(data.wordList, 5); // 将1维单词数组转换为2维数组
  180. }
  181. })
  182. } else {
  183. httpApi.getCommonWordInfo({
  184. jieId: data.jieId,
  185. wordId: data.activeId
  186. }).then(res => {
  187. data.activeWord = res.data;
  188. data.wordList = res.data.danciList; // 单词组
  189. data.collectFlag = res.data.collectFlag; // 收藏状态
  190. if (data.wordList.length) {
  191. data.arrayList = chunkArray(data.wordList, 5); // 将1维单词数组转换为2维数组
  192. }
  193. })
  194. }
  195. }
  196. </script>
  197. <style>
  198. </style>