wordList.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. @touchstart="handleSwiperTouchStart"
  46. @touchmove="handleSwiperTouchMove"
  47. :disable-touch="isSwiperDisabled"
  48. >
  49. <swiper-item
  50. class="word-list-swiper-item"
  51. v-for="citem in unitList"
  52. :key="citem.danyuanId"
  53. @touchstart="handleTouchStart"
  54. @touchend="handleTouchEnd"
  55. >
  56. <view
  57. class="word-list-body"
  58. v-if="citem.wordList && citem.wordList.length > 0"
  59. >
  60. <!-- num -->
  61. <view class="word-num-box">
  62. <icon></icon
  63. >
  64. <text
  65. >{{ citem.studyCount || 0 }}/{{ citem.count || 0 }}词
  66. </text
  67. >
  68. </view>
  69. <!-- 单词 -->
  70. <view
  71. class="word-list-item"
  72. v-for="(item, index) in citem.wordList"
  73. :key="index"
  74. @click="toWord(item)"
  75. :class="{ active: item.wcFlag == 1 }"
  76. >
  77. <view class="item-word">
  78. <view class="word-text">
  79. <text
  80. v-for="(word, wordIndex) in item.chaifen"
  81. :key="wordIndex"
  82. class="word-color"
  83. >
  84. {{ word }}
  85. </text>
  86. </view>
  87. <view class="phonetic-alphabet">{{
  88. item.yinbiao || ""
  89. }}
  90. </view>
  91. </view>
  92. <view class="item-explain">
  93. <view class="item-explain-content">
  94. <view
  95. class="explain-text"
  96. v-for="(meaning, meaningIndex) in item.jianyi"
  97. :key="meaningIndex"
  98. >{{ meaning }}
  99. </view
  100. >
  101. </view>
  102. </view>
  103. <view class="item-arrow">
  104. <icon></icon>
  105. </view>
  106. </view>
  107. </view>
  108. <!-- 没有单词 -->
  109. <view class="no-word-box" v-else> 暂无单词</view>
  110. </swiper-item>
  111. </swiper>
  112. </view>
  113. <tip-small-dialog ref="goPayDialogRef" @confirm-btn="goPayPage" content="需要购买当前课程才能学习" qrBtnName="前往购买"></tip-small-dialog>
  114. </view>
  115. </template>
  116. <script setup>
  117. import {reactive, ref, nextTick, computed, getCurrentInstance} from "vue";
  118. import {
  119. toast,
  120. getDataFromStr,
  121. useActiveDomIntoView,
  122. isTargetInSameGroup,
  123. } from "@/utils/common";
  124. import {onLoad} from "@dcloudio/uni-app";
  125. import {getWordZhangList} from "@/api/chaojidanci.js";
  126. import cacheManager from "@/utils/cacheManager.js";
  127. import {useSwiper} from "@/utils/useSwiper";
  128. import tipSmallDialog from "@/components/dialog/tipSmallDialog.vue";
  129. import ezyActiveVue from "@/components/ezyActive/ezyActive.vue";
  130. const goPayDialogRef = ref(null);
  131. const listData = reactive({
  132. danyuanNumberList: [], //节名称
  133. title: "", // 版本+年级+学期
  134. wordList: [], // 单词列表,默认值设为空数组
  135. activeIndex: 0,
  136. });
  137. let danyuanId = ref(null);
  138. let routerOpt = ref(false);
  139. let wordLeft = ref(0);
  140. const unitList = ref([]);
  141. const info = reactive({
  142. levelId: null,
  143. zhangId: null,
  144. });
  145. const {swiperData, handleTouchStart, handleTouchEnd, userCode} = useSwiper(
  146. listData,
  147. (userCode) => {
  148. },
  149. );
  150. // 计算属性,控制 swiper 是否禁用触摸
  151. const isSwiperDisabled = computed(() => {
  152. // 检查下一个项是否被锁定
  153. const nextIndex = listData.activeIndex + 1;
  154. if (nextIndex < listData.danyuanNumberList.length) {
  155. const nextItem = listData.danyuanNumberList[nextIndex];
  156. if (nextItem.lock) {
  157. return true;
  158. }
  159. }
  160. return !swiperData.isAllowed;
  161. });
  162. // 记录swiper触摸开始位置
  163. let startX = 0;
  164. // 处理swiper触摸开始
  165. function handleSwiperTouchStart(e) {
  166. startX = e.touches[0].clientX;
  167. }
  168. // 处理swiper触摸移动
  169. function handleSwiperTouchMove(e) {
  170. const currentX = e.touches[0].clientX;
  171. const diffX = currentX - startX;
  172. // 向左滑动
  173. if (diffX < -50) {
  174. // 检查下一个项是否被锁定
  175. const nextIndex = listData.activeIndex + 1;
  176. if (nextIndex < listData.danyuanNumberList.length) {
  177. const nextItem = listData.danyuanNumberList[nextIndex];
  178. if (nextItem.lock) {
  179. // 阻止滑动
  180. e.preventDefault();
  181. // 弹出弹窗
  182. goPayDialogRef.value.handleShow();
  183. }
  184. }
  185. }
  186. }
  187. onLoad((options) => {
  188. routerOpt = options;
  189. // 非游客
  190. danyuanId.value = routerOpt.danyuanId;
  191. getWordListData();
  192. });
  193. // Swiper 切换
  194. function handleSwiperChange(e) {
  195. // 只有当 swiper 的变化是由用户手动滑动引起的,才调用 updataShuju 函数
  196. // 避免由数据更新引起的 swiper 变化触发二次请求
  197. if (e.detail.source === 'touch') {
  198. const currentIndex = e.detail.current;
  199. const item = listData.danyuanNumberList[currentIndex];
  200. if (item.lock) {
  201. // 弹出弹窗让用户确认是否去购买
  202. goPayDialogRef.value.handleShow();
  203. // 回退到原索引
  204. nextTick(() => {
  205. listData.activeIndex = e.detail.current - 1 >= 0 ? e.detail.current - 1 : 0;
  206. });
  207. return;
  208. return;
  209. }
  210. listData.activeIndex = e.detail.current;
  211. if (!swiperData.isAllowed) {
  212. // 不满足条件时回退到原索引
  213. nextTick(() => {
  214. listData.activeIndex = 0; // 强制回退
  215. });
  216. swiperData.isAllowed = false; // 重置状态
  217. }
  218. // findWordLeft(listData.activeIndex);
  219. centerActiveTab();
  220. updataShuju(listData.danyuanNumberList[listData.activeIndex]);
  221. }
  222. }
  223. // 处理tab点击
  224. function handleTabClick(index) {
  225. const item = listData.danyuanNumberList[index];
  226. if (item.lock) {
  227. // 弹出弹窗让用户确认是否去购买
  228. goPayDialogRef.value.handleShow();
  229. return;
  230. }
  231. listData.activeIndex = index;
  232. centerActiveTab();
  233. updataShuju(listData.danyuanNumberList[index]);
  234. }
  235. // 使激活的tab居中显示
  236. function centerActiveTab() {
  237. nextTick(() => {
  238. const query = uni.createSelectorQuery().in(getCurrentInstance());
  239. query.select('#tab-item-list').boundingClientRect();
  240. query.select('.tab-item.active').boundingClientRect();
  241. query.exec((res) => {
  242. if (res && res[0] && res[1]) {
  243. const containerWidth = res[0].width;
  244. const activeLeft = res[1].left - res[0].left;
  245. const activeWidth = res[1].width;
  246. const scrollLeft = activeLeft - (containerWidth / 2) + (activeWidth / 2);
  247. // 边界处理
  248. const finalScrollLeft = Math.max(0, scrollLeft);
  249. // 使用 uni-app 的 API 来设置滚动位置
  250. uni.createSelectorQuery().in(getCurrentInstance())
  251. .select('#tab-item-list')
  252. .fields({ node: true }, (result) => {
  253. if (result && result.node) {
  254. const node = result.node;
  255. node.scrollLeft = finalScrollLeft;
  256. }
  257. })
  258. .exec();
  259. }
  260. })
  261. });
  262. }
  263. // 返回
  264. function goBack() {
  265. // uni.redirectTo({
  266. // url: `/pages/study/index`,
  267. // });
  268. uni.navigateBack()
  269. }
  270. // 修改缓存zid 用于定位岛
  271. function updataCache(data) {
  272. cacheManager.updateObject("zhangInfo", {
  273. curZid: data,
  274. });
  275. }
  276. //tab click
  277. function handleTitleClick(item) {
  278. updataShuju(item);
  279. }
  280. // 修改数据 &&定位岛
  281. function updataShuju(data) {
  282. // 非游客
  283. danyuanId.value = data.danyuanId;
  284. // 修改缓存zid 用于定位岛
  285. updataCache(data.zhangZid);
  286. getWordListData();
  287. }
  288. // 返回节index
  289. function findIndexByDanyuanId(list, danyuanId) {
  290. const findIndex = list.findIndex((item) => item.danyuanId == danyuanId);
  291. // unit滚动到指定位置
  292. findWordLeft(findIndex);
  293. return findIndex;
  294. }
  295. // 获取滚动距离
  296. function findWordLeft(data) {
  297. // useActiveDomIntoView(".word-title-box", ".title-item.active");
  298. // 替换为 uni-app 兼容的滚动方法
  299. centerActiveTab();
  300. }
  301. function getWordListData() {
  302. const opt = {
  303. danyuanId: 244, // routerOpt.danyuanId
  304. banbenId: 39, // routerOpt.banbenId
  305. };
  306. getWordZhangList(opt)
  307. .then((res) => {
  308. if (res.code === 0) {
  309. listData.title = res.data.title;
  310. unitList.value = res.data.danyuanWordsList;
  311. listData.danyuanNumberList = res.data.danyuanNumberList;
  312. unitList.value.forEach((item) => {
  313. if (item.danyuanId == danyuanId.value) {
  314. listData.activeIndex = findIndexByDanyuanId(
  315. listData.danyuanNumberList,
  316. danyuanId.value
  317. );
  318. }
  319. });
  320. }
  321. })
  322. .catch((err) => {
  323. console.log('err', err)
  324. toast("获取单词列表数据失败");
  325. });
  326. }
  327. // 单词
  328. function toWord(data) {
  329. uni.navigateTo({
  330. url: `/pages/chaojidanci/newEnglish/index?danyuanId=${danyuanId.value}&wordId=${data.id}`,
  331. });
  332. }
  333. // 去支付
  334. function goPayPage() {
  335. uni.navigateTo({
  336. url: "/pages/chanpinShop/cp4/dingdan"
  337. });
  338. }
  339. </script>