wordList.vue 12 KB

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