wordList.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. {{ item.intro }}
  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. <ezyActiveVue
  72. class="ezy-list-item-active 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. </ezyActiveVue>
  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. import {yingyuSave2} from "../../../api/chanpinneirong";
  134. const goPayDialogRef = ref(null);
  135. const listData = reactive({
  136. danyuanNumberList: [], //节名称
  137. title: "", // 版本+年级+学期
  138. wordList: [], // 单词列表,默认值设为空数组
  139. activeIndex: 0,
  140. });
  141. let danyuanId = ref(null);
  142. let routerOpt = ref(false);
  143. let wordLeft = ref(0);
  144. const unitList = ref([]);
  145. const info = reactive({
  146. levelId: null,
  147. zhangId: null,
  148. });
  149. const {
  150. isShowTishi,
  151. updateDate,
  152. } = useShowTishi()
  153. const {swiperData, handleTouchStart, handleTouchEnd, userCode} = useSwiper(
  154. listData,
  155. (userCode) => {
  156. },
  157. );
  158. // 计算属性,控制 swiper 是否禁用触摸
  159. const isSwiperDisabled = computed(() => {
  160. // 检查下一个项是否被锁定
  161. const nextIndex = listData.activeIndex + 1;
  162. if (nextIndex < listData.danyuanNumberList.length) {
  163. const nextItem = listData.danyuanNumberList[nextIndex];
  164. if (nextItem.lock) {
  165. return true;
  166. }
  167. }
  168. return !swiperData.isAllowed;
  169. });
  170. // 记录swiper触摸开始位置
  171. let startX = 0;
  172. // 计算属性,判断是否在第一组
  173. const isFirstGroup = computed(() => {
  174. const chunkSize = 4;
  175. const currentGroup = Math.floor(listData.activeIndex / chunkSize);
  176. return currentGroup === 0;
  177. });
  178. // 计算属性,判断是否在最后一组
  179. const isLastGroup = computed(() => {
  180. const chunkSize = 4;
  181. const currentGroup = Math.floor(listData.activeIndex / chunkSize);
  182. const totalGroups = Math.ceil(listData.danyuanNumberList.length / chunkSize);
  183. return currentGroup >= totalGroups - 1;
  184. });
  185. // 处理左侧箭头点击
  186. function handleLeftClick() {
  187. const chunkSize = 4;
  188. const currentGroup = Math.floor(listData.activeIndex / chunkSize);
  189. if (currentGroup > 0) {
  190. const prevGroupStartIndex = (currentGroup - 1) * chunkSize;
  191. const item = listData.danyuanNumberList[prevGroupStartIndex];
  192. if (item.lock) {
  193. // 弹出弹窗让用户确认是否去购买
  194. goPayDialogRef.value.handleShow();
  195. return;
  196. }
  197. listData.activeIndex = prevGroupStartIndex;
  198. centerActiveTab();
  199. updataShuju(listData.danyuanNumberList[prevGroupStartIndex]);
  200. }
  201. }
  202. // 处理右侧箭头点击
  203. function handleRightClick() {
  204. const chunkSize = 4;
  205. const currentGroup = Math.floor(listData.activeIndex / chunkSize);
  206. const totalGroups = Math.ceil(listData.danyuanNumberList.length / chunkSize);
  207. if (currentGroup < totalGroups - 1) {
  208. const nextGroupStartIndex = (currentGroup + 1) * chunkSize;
  209. const item = listData.danyuanNumberList[nextGroupStartIndex];
  210. if (item.lock) {
  211. // 弹出弹窗让用户确认是否去购买
  212. goPayDialogRef.value.handleShow();
  213. return;
  214. }
  215. listData.activeIndex = nextGroupStartIndex;
  216. centerActiveTab();
  217. updataShuju(listData.danyuanNumberList[nextGroupStartIndex]);
  218. }
  219. }
  220. // 处理swiper触摸开始
  221. function handleSwiperTouchStart(e) {
  222. startX = e.touches[0].clientX;
  223. }
  224. // 处理swiper触摸移动
  225. function handleSwiperTouchMove(e) {
  226. const currentX = e.touches[0].clientX;
  227. const diffX = currentX - startX;
  228. // 向左滑动
  229. if (diffX < -50) {
  230. // 检查下一个项是否被锁定
  231. const nextIndex = listData.activeIndex + 1;
  232. if (nextIndex < listData.danyuanNumberList.length) {
  233. const nextItem = listData.danyuanNumberList[nextIndex];
  234. if (nextItem.lock) {
  235. // 阻止滑动
  236. e.preventDefault();
  237. // 弹出弹窗
  238. goPayDialogRef.value.handleShow();
  239. }
  240. }
  241. }
  242. }
  243. onLoad((options) => {
  244. routerOpt.value = options;
  245. // 非游客
  246. danyuanId.value = routerOpt.value.danyuanId;
  247. });
  248. onShow(() => {
  249. getWordListData();
  250. })
  251. // Swiper 切换
  252. function handleSwiperChange(e) {
  253. // 只有当 swiper 的变化是由用户手动滑动引起的,才调用 updataShuju 函数
  254. // 避免由数据更新引起的 swiper 变化触发二次请求
  255. if (e.detail.source === 'touch') {
  256. const currentIndex = e.detail.current;
  257. const item = listData.danyuanNumberList[currentIndex];
  258. if (item.lock) {
  259. // 弹出弹窗让用户确认是否去购买
  260. goPayDialogRef.value.handleShow();
  261. // 回退到原索引
  262. nextTick(() => {
  263. listData.activeIndex = e.detail.current - 1 >= 0 ? e.detail.current - 1 : 0;
  264. });
  265. return;
  266. return;
  267. }
  268. listData.activeIndex = e.detail.current;
  269. if (!swiperData.isAllowed) {
  270. // 不满足条件时回退到原索引
  271. nextTick(() => {
  272. listData.activeIndex = 0; // 强制回退
  273. });
  274. swiperData.isAllowed = false; // 重置状态
  275. }
  276. // findWordLeft(listData.activeIndex);
  277. centerActiveTab();
  278. updataShuju(listData.danyuanNumberList[listData.activeIndex]);
  279. }
  280. }
  281. // 处理tab点击
  282. function handleTabClick(index) {
  283. const item = listData.danyuanNumberList[index];
  284. if (item.lock) {
  285. // 弹出弹窗让用户确认是否去购买
  286. goPayDialogRef.value.handleShow();
  287. return;
  288. }
  289. listData.activeIndex = index;
  290. centerActiveTab();
  291. updataShuju(listData.danyuanNumberList[index]);
  292. }
  293. // 使激活的tab居中显示
  294. function centerActiveTab() {
  295. nextTick(() => {
  296. const query = uni.createSelectorQuery().in(getCurrentInstance());
  297. query.select('#tab-item-list').boundingClientRect();
  298. query.select('.tab-item.active').boundingClientRect();
  299. query.exec((res) => {
  300. if (res && res[0] && res[1]) {
  301. const containerWidth = res[0].width;
  302. const activeLeft = res[1].left - res[0].left;
  303. const activeWidth = res[1].width;
  304. const scrollLeft = activeLeft - (containerWidth / 2) + (activeWidth / 2);
  305. // 边界处理
  306. const finalScrollLeft = Math.max(0, scrollLeft);
  307. // 使用 uni-app 的 API 来设置滚动位置
  308. uni.createSelectorQuery().in(getCurrentInstance())
  309. .select('#tab-item-list')
  310. .fields({ node: true }, (result) => {
  311. if (result && result.node) {
  312. const node = result.node;
  313. node.scrollLeft = finalScrollLeft;
  314. }
  315. })
  316. .exec();
  317. }
  318. })
  319. });
  320. }
  321. // 返回
  322. function goBack() {
  323. // uni.redirectTo({
  324. // url: `/pages/study/index`,
  325. // });
  326. uni.navigateBack()
  327. }
  328. // 修改缓存zid 用于定位岛
  329. async function updataCache(da) {
  330. console.log('da', da)
  331. let req = {
  332. "banbenId": routerOpt.value.banbenId,
  333. "danyuanId": danyuanId.value
  334. }
  335. const res = await yingyuSave2(req);
  336. if (res.code == 0) {
  337. const cacheData = cacheManager.get('contentInfo') || {};
  338. cacheData.curDanyuanName = da.danyuanName;
  339. cacheData.danyuanId = da.danyuanId;
  340. cacheData.curDanyuanId = da.danyuanId;
  341. cacheManager.updateObject('contentInfo', cacheData);
  342. } else {
  343. return false;
  344. }
  345. }
  346. //tab click
  347. function handleTitleClick(item) {
  348. updataShuju(item);
  349. }
  350. // 修改数据 &&定位岛
  351. function updataShuju(data) {
  352. // 非游客
  353. danyuanId.value = data.danyuanId;
  354. // 修改缓存zid 用于定位岛
  355. updataCache(data);
  356. getWordListData();
  357. }
  358. // 返回节index
  359. function findIndexByDanyuanId(list, danyuanId) {
  360. const findIndex = list.findIndex((item) => item.danyuanId == danyuanId);
  361. // unit滚动到指定位置
  362. findWordLeft(findIndex);
  363. return findIndex;
  364. }
  365. // 获取滚动距离
  366. function findWordLeft(data) {
  367. // useActiveDomIntoView(".word-title-box", ".title-item.active");
  368. // 替换为 uni-app 兼容的滚动方法
  369. centerActiveTab();
  370. }
  371. function getWordListData() {
  372. const opt = {
  373. danyuanId: danyuanId.value, // routerOpt.danyuanId
  374. banbenId: routerOpt.value.banbenId, // routerOpt.banbenId
  375. };
  376. getWordZhangList(opt)
  377. .then((res) => {
  378. if (res.code === 0) {
  379. listData.title = res.data.title;
  380. unitList.value = res.data.danyuanWordsList;
  381. listData.danyuanNumberList = res.data.danyuanNumberList;
  382. unitList.value.forEach((item) => {
  383. if (item.danyuanId == danyuanId.value) {
  384. listData.activeIndex = findIndexByDanyuanId(
  385. listData.danyuanNumberList,
  386. danyuanId.value
  387. );
  388. }
  389. });
  390. }
  391. })
  392. .catch((err) => {
  393. console.log('err', err)
  394. toast("获取单词列表数据失败");
  395. });
  396. }
  397. // 单词
  398. function toWord(data) {
  399. uni.navigateTo({
  400. url: `/pages/chaojidanci/newEnglish/index?danyuanId=${danyuanId.value}&wordId=${data.id}`,
  401. });
  402. }
  403. // 去支付
  404. function goPayPage() {
  405. uni.navigateTo({
  406. url: "/pages/chanpinShop/cp3/dingdan"
  407. });
  408. }
  409. </script>