wordList.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <template>
  2. <view class="word-list-page">
  3. <view class="icon-title-navBar-box">
  4. <view @click="goBack" class="nav-bar-icon"></view>
  5. <text class="nav-bar-title">{{ listData.title || "" }}</text>
  6. </view>
  7. <view class="ezy-page-body word-body-box">
  8. <view
  9. class="word-tab-box"
  10. v-if="listData.danyuanNumberList && listData.danyuanNumberList.length > 0"
  11. >
  12. <!-- disabled-->
  13. <icon class="word-jt jt-left" :class="{ disabled: listData.activeIndex === 0 }"/>
  14. <view class="tab-item-list" id="tab-item-list">
  15. <!-- active -->
  16. <ezyActiveVue
  17. :id="'item-' + item.danyuanId"
  18. class="ezy-btn-active tab-item"
  19. v-for="(item, index) in listData.danyuanNumberList"
  20. :key="item.danyuanId"
  21. :class="{ active: listData.activeIndex == index }"
  22. v-show="
  23. isTargetInSameGroup(
  24. listData.danyuanNumberList,
  25. listData.danyuanNumberList[listData.activeIndex],
  26. item.danyuanId,
  27. )
  28. "
  29. @click="handleTabClick(index)"
  30. >
  31. Unit{{ item.number }}
  32. </ezyActiveVue>
  33. </view>
  34. <!-- disabled-->
  35. <icon class="word-jt jt-right"
  36. :class="{ disabled: listData.activeIndex === listData.danyuanNumberList.length - 1 }"/>
  37. </view>
  38. <swiper
  39. class="word-list-swiper-box"
  40. :indicator-dots="false"
  41. :autoplay="false"
  42. :circular="false"
  43. :current="listData.activeIndex"
  44. @change="handleSwiperChange"
  45. :disable-touch="!swiperData.isAllowed"
  46. >
  47. <swiper-item
  48. class="word-list-swiper-item"
  49. v-for="citem in unitList"
  50. :key="citem.danyuanId"
  51. @touchstart="handleTouchStart"
  52. @touchend="handleTouchEnd"
  53. >
  54. <view
  55. class="word-list-body"
  56. v-if="citem.wordList && citem.wordList.length > 0"
  57. >
  58. <!-- num -->
  59. <view class="word-num-box">
  60. <icon></icon
  61. >
  62. <text
  63. >{{ citem.studyCount || 0 }}/{{ citem.count || 0 }}词
  64. </text
  65. >
  66. </view>
  67. <!-- 单词 -->
  68. <view
  69. class="word-list-item"
  70. v-for="(item, index) in citem.wordList"
  71. :key="index"
  72. @click="toWord(item)"
  73. :class="{ active: item.wcFlag == 1 }"
  74. >
  75. <view class="item-word">
  76. <view class="word-text">
  77. <text
  78. v-for="(word, wordIndex) in item.chaifen"
  79. :key="wordIndex"
  80. class="word-color"
  81. >
  82. {{ word }}
  83. </text>
  84. </view>
  85. <view class="phonetic-alphabet">{{
  86. item.yinbiao || ""
  87. }}
  88. </view>
  89. </view>
  90. <view class="item-explain">
  91. <view class="item-explain-content">
  92. <view
  93. class="explain-text"
  94. v-for="(meaning, meaningIndex) in item.jianyi"
  95. :key="meaningIndex"
  96. >{{ meaning }}
  97. </view
  98. >
  99. </view>
  100. </view>
  101. <view class="item-arrow">
  102. <icon></icon>
  103. </view>
  104. </view>
  105. </view>
  106. <!-- 没有单词 -->
  107. <view class="no-word-box" v-else> 暂无单词</view>
  108. </swiper-item>
  109. </swiper>
  110. </view>
  111. <tip-small-dialog ref="goPayDialogRef" @confirm-btn="goPayPage" :content="tipContent"></tip-small-dialog>
  112. </view>
  113. </template>
  114. <script setup>
  115. import {reactive, ref, nextTick, computed, getCurrentInstance} from "vue";
  116. import {
  117. toast,
  118. getDataFromStr,
  119. useActiveDomIntoView,
  120. isTargetInSameGroup,
  121. } from "@/utils/common";
  122. import {onLoad} from "@dcloudio/uni-app";
  123. import {getWordZhangList} from "@/api/chaojidanci.js";
  124. import cacheManager from "@/utils/cacheManager.js";
  125. import {useSwiper} from "@/utils/useSwiper";
  126. import tipSmallDialog from "@/components/dialog/tipSmallDialog.vue";
  127. import ezyActiveVue from "@/components/ezyActive/ezyActive.vue";
  128. const goPayDialogRef = ref(null);
  129. const listData = reactive({
  130. danyuanNumberList: [], //节名称
  131. title: "", // 版本+年级+学期
  132. wordList: [], // 单词列表,默认值设为空数组
  133. activeIndex: 0,
  134. });
  135. let danyuanId = ref(null);
  136. let routerOpt = ref(false);
  137. let wordLeft = ref(0);
  138. const unitList = ref([]);
  139. const info = reactive({
  140. levelId: null,
  141. zhangId: null,
  142. });
  143. const tipContent = '是否前往开通付费?';
  144. const {swiperData, handleTouchStart, handleTouchEnd, userCode} = useSwiper(
  145. listData,
  146. (userCode) => {
  147. },
  148. );
  149. onLoad((options) => {
  150. routerOpt = options;
  151. // 非游客
  152. danyuanId.value = routerOpt.danyuanId;
  153. getWordListData();
  154. });
  155. // Swiper 切换
  156. function handleSwiperChange(e) {
  157. listData.activeIndex = e.detail.current;
  158. if (!swiperData.isAllowed) {
  159. // 不满足条件时回退到原索引
  160. nextTick(() => {
  161. listData.activeIndex = 0; // 强制回退
  162. });
  163. swiperData.isAllowed = false; // 重置状态
  164. }
  165. // findWordLeft(listData.activeIndex);
  166. centerActiveTab();
  167. updataShuju(listData.danyuanNumberList[listData.activeIndex]);
  168. }
  169. // 处理tab点击
  170. function handleTabClick(index) {
  171. listData.activeIndex = index;
  172. centerActiveTab();
  173. updataShuju(listData.danyuanNumberList[index]);
  174. }
  175. // 使激活的tab居中显示
  176. function centerActiveTab() {
  177. nextTick(() => {
  178. const query = uni.createSelectorQuery().in(getCurrentInstance());
  179. query.select('#tab-item-list').boundingClientRect();
  180. query.select('.tab-item.active').boundingClientRect();
  181. query.exec((res) => {
  182. if (res && res[0] && res[1]) {
  183. const containerWidth = res[0].width;
  184. const activeLeft = res[1].left - res[0].left;
  185. const activeWidth = res[1].width;
  186. const scrollLeft = activeLeft - (containerWidth / 2) + (activeWidth / 2);
  187. // 边界处理
  188. const finalScrollLeft = Math.max(0, scrollLeft);
  189. // 使用 uni-app 的 API 来设置滚动位置
  190. uni.createSelectorQuery().in(getCurrentInstance())
  191. .select('#tab-item-list')
  192. .fields({ node: true }, (result) => {
  193. if (result && result.node) {
  194. const node = result.node;
  195. node.scrollLeft = finalScrollLeft;
  196. }
  197. })
  198. .exec();
  199. }
  200. })
  201. });
  202. }
  203. // 返回
  204. function goBack() {
  205. uni.redirectTo({
  206. url: `/pages/study/index`,
  207. });
  208. }
  209. // 修改缓存zid 用于定位岛
  210. function updataCache(data) {
  211. cacheManager.updateObject("zhangInfo", {
  212. curZid: data,
  213. });
  214. }
  215. //tab click
  216. function handleTitleClick(item) {
  217. updataShuju(item);
  218. }
  219. // 修改数据 &&定位岛
  220. function updataShuju(data) {
  221. // 非游客
  222. danyuanId.value = data.danyuanId;
  223. // 修改缓存zid 用于定位岛
  224. updataCache(data.zhangZid);
  225. getWordListData();
  226. }
  227. // 返回节index
  228. function findIndexByDanyuanId(list, danyuanId) {
  229. const findIndex = list.findIndex((item) => item.danyuanId == danyuanId);
  230. // unit滚动到指定位置
  231. findWordLeft(findIndex);
  232. return findIndex;
  233. }
  234. // 获取滚动距离
  235. function findWordLeft(data) {
  236. // useActiveDomIntoView(".word-title-box", ".title-item.active");
  237. // 替换为 uni-app 兼容的滚动方法
  238. centerActiveTab();
  239. }
  240. function getWordListData() {
  241. const opt = {
  242. danyuanId: 244, // routerOpt.danyuanId
  243. banbenId: 39, // routerOpt.banbenId
  244. };
  245. getWordZhangList(opt)
  246. .then((res) => {
  247. if (res.code === 0) {
  248. listData.title = res.data.title;
  249. unitList.value = res.data.danyuanWordsList;
  250. listData.danyuanNumberList = res.data.danyuanNumberList;
  251. unitList.value.forEach((item) => {
  252. if (item.danyuanId == danyuanId.value) {
  253. listData.activeIndex = findIndexByDanyuanId(
  254. listData.danyuanNumberList,
  255. danyuanId.value
  256. );
  257. }
  258. });
  259. }
  260. })
  261. .catch((err) => {
  262. console.log('err', err)
  263. toast("获取单词列表数据失败");
  264. });
  265. }
  266. // 单词
  267. function toWord(data) {
  268. uni.redirectTo({
  269. url: `/pages/chaojidanci/newEnglish/index?danyuanId=${danyuanId.value}&wordId=${data.id}`,
  270. });
  271. }
  272. // 去支付
  273. function goPayPage() {
  274. let zhangInfoLocal = cacheManager.get("zhangInfo");
  275. if (!zhangInfoLocal.cardId) {
  276. toast("cardId 丢失请重新选择学科LevelId");
  277. return false;
  278. }
  279. uni.redirectTo({
  280. url:
  281. "/pages/mall/mallPage?cardId=" +
  282. zhangInfoLocal.cardId +
  283. "&from=daoPage" +
  284. "&subjectId=" +
  285. zhangInfoLocal.subjectId,
  286. });
  287. }
  288. </script>