zuichu1.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. <template>
  2. <view class="ezy-xuexi-page">
  3. <view class="icon-title-navBar-box">
  4. <text class="nav-bar-title">学习</text>
  5. </view>
  6. <view v-if="existData" class="ezy-page-body xuexi-page-body">
  7. <view class="xxjl-card-box-padding">
  8. <view class="xxjl-card-box">
  9. <!-- 显示内容 -->
  10. <view class="card-body-box">
  11. <img :src="banbenInfo.cover" />
  12. <view class="body-right">
  13. <view class="right-name">{{ banbenInfo.chanpinName }}</view>
  14. <view>等级:{{banbenInfo.dengjiName}}</view>
  15. <view>版本:{{banbenInfo.name}}</view>
  16. <view>单元:{{banbenInfo.curDanyuanName}}</view>
  17. <view>课程:{{banbenInfo.curKechengName}}</view>
  18. </view>
  19. </view>
  20. <view class="card-progress-box">
  21. <view class="xx-progress-box">
  22. <view>学习进度</view>
  23. <progress :percent="curProcess" class="xx-progress" stroke-width="20"
  24. backgroundColor="#3c7dfd" activeColor="#ffd11c" />
  25. </view>
  26. <ezyActiveVue class="ezy-btn-active jxxx-btn" @aclick="handlePlay(banbenInfo,'jixu')"></ezyActiveVue>
  27. </view>
  28. </view>
  29. </view>
  30. <view class="xx-item-list">
  31. <view class="xx-item-title">— 以下为当前等级课程目录 —</view>
  32. <view v-for="(danyuanItem,index) in danyuanList" :key="danyuanItem.danyuanId">
  33. <!-- 第一单元 -->
  34. <ezyActiveVue v-if="index ==0" class="ezy-list-item-active item-dy-box item-fixed" @aclick="handleClickDanyuan(danyuanItem)" :id="`unit-title-${danyuanItem.danyuanId}`">
  35. <view class="dy-left-box">L{{banbenInfo.dengjiId}}</view>
  36. <view class="dy-right-box">
  37. <view class="right-content">
  38. <view class="dy-name">{{danyuanItem.danyuanName}}</view>
  39. <view>{{danyuanItem.danyuanIntro}}</view>
  40. </view>
  41. </view>
  42. </ezyActiveVue>
  43. <!-- 其他单元 -->
  44. <view v-if="index !=0" class="xx-item-title" :id="`unit-title-${danyuanItem.danyuanId}`">— {{danyuanItem.danyuanName}}
  45. {{danyuanItem.danyuanIntro}} —
  46. </view>
  47. <ezyActiveVue class="ezy-list-item-active xx-item-box" v-for="jieItem in danyuanItem.jieList" :key="jieItem.jieId"
  48. @aclick="handlePlay(jieItem,'play')">
  49. <view class="xx-item-status"
  50. :class="jieItem.wanchengFlag == 1 ? 'completed-status' : 'uncompleted-status'">
  51. </view>
  52. <img :src="jieItem.cover" />
  53. <view class="xx-text-box">
  54. <view>{{ jieItem.jieName }}</view>
  55. <view>{{ jieItem.jieIntro }}</view>
  56. </view>
  57. <view class="xx-item-btn"></view>
  58. </ezyActiveVue>
  59. </view>
  60. <view class="xx-item-title">本级别最后一单元啦~</view>
  61. <view class="xx-more-btn" @click="moreBtn"></view>
  62. </view>
  63. <!-- 回到顶部 -->
  64. <view class="go-top-btn" v-if="false"></view>
  65. </view>
  66. <view v-if="!existData">
  67. </view>
  68. <danyuanInfoVue ref="dyRef" v-if="isShow" @close="isShow= false"></danyuanInfoVue>
  69. <custom-tab-bar :show="true" :current-index="currentTabIndex" />
  70. </view>
  71. </template>
  72. <script>
  73. import ezyActiveVue from "@/components/ezyActive/ezyActive.vue";
  74. import CustomTabBar from '@/components/custom-tabbar/index.vue';
  75. import cacheManager from "@/utils/cacheManager.js";
  76. import {
  77. shuxueChanpinBanbenInfo,
  78. shuxueSave
  79. } from "@/api/chanpinneirong.js"
  80. import {
  81. onLoad,
  82. onShow,
  83. onHide,
  84. onUnload
  85. } from "@dcloudio/uni-app"
  86. import danyuanInfoVue from '@/pages/xinshuxue/components/danyuanInfo.vue';
  87. import {
  88. toast
  89. } from '../../utils/common';
  90. import {updateXuexiProcess} from "./useNeirongShuxue"
  91. export default {
  92. data() {
  93. return {
  94. canExitApp: false,
  95. danyuanList: [],
  96. currentUnitName: "",
  97. isShow: true,
  98. banbenInfo: {},
  99. banbenId: '',
  100. danyuanId: '',
  101. chanpinId: '',
  102. dengjiId: '',
  103. curProcess: '',
  104. currentTabIndex: 1,
  105. cacheManagerLocal: null,
  106. hasCache: false, // 是否有缓存的标志
  107. needRefresh: false, // 是否需要刷新数据的标志
  108. existData: true,
  109. currentVisibleUnit: '', // 当前可见的单元名称
  110. observer: null, // IntersectionObserver实例
  111. lastToastUnitId: null ,// 上一次toast的单元ID,防止重复触发
  112. isFixed:false,
  113. }
  114. },
  115. components: {
  116. CustomTabBar,
  117. danyuanInfoVue,
  118. ezyActiveVue
  119. },
  120. onShow() {
  121. this.currentTabIndex = 1
  122. const cacheData = cacheManager.get('xuexi-shuxue');
  123. if (cacheData) {
  124. console.log('使用缓存数据');
  125. this.updateFromCache();
  126. } else {
  127. console.log('重新请求数据');
  128. this.hasCache = false;
  129. const cacheDataAuth = cacheManager.get('auth');
  130. this.initFromOptions(cacheDataAuth);
  131. }
  132. // 数据加载完成后初始化观察器
  133. this.$nextTick(() => {
  134. setTimeout(() => {
  135. this.initObserver();
  136. }, 300);
  137. });
  138. },
  139. onHide() {
  140. console.log('学习页面隐藏')
  141. this.needRefresh = true;
  142. // 销毁观察器
  143. if (this.observer) {
  144. this.observer.disconnect();
  145. this.observer = null;
  146. }
  147. },
  148. onUnload() {
  149. // 页面卸载时销毁观察器
  150. if (this.observer) {
  151. this.observer.disconnect();
  152. this.observer = null;
  153. }
  154. },
  155. onLoad(options) {
  156. uni.hideTabBar()
  157. },
  158. methods: {
  159. moreBtn(){
  160. uni.switchTab({
  161. url: '/pages/chanpinXuanze/index'
  162. })
  163. },
  164. // 初始化IntersectionObserver
  165. initObserver() {
  166. if (this.observer) {
  167. this.observer.disconnect();
  168. this.observer = null;
  169. }
  170. // 监听每个单元标题的位置
  171. this.observer = uni.createIntersectionObserver(this).relativeToViewport({
  172. top: 0, // 监听进入屏幕顶部的标题
  173. bottom: 0
  174. });
  175. this.danyuanList.forEach((item, index) => {
  176. const titleId = `unit-title-${item.danyuanId}`;
  177. this.observer.observe(`#${titleId}`, (res) => {
  178. if (res.intersectionRatio > 0) {
  179. // 标题在屏幕顶部区域内
  180. if (this.lastToastUnitId !== item.danyuanId) {
  181. this.lastToastUnitId = item.danyuanId;
  182. console.log(`显示${item.danyuanName}`);
  183. console.log('item',item);
  184. if(item.danyuanName !="第一单元"){
  185. this.isFixed = true
  186. }else{
  187. this.isFixed = false
  188. }
  189. // uni.showToast({
  190. // title: `${item.danyuanName}`,
  191. // icon: 'none',
  192. // duration: 1500
  193. // });
  194. }
  195. }
  196. });
  197. });
  198. // 页面初始时显示第一个单元
  199. if (this.danyuanList.length > 0) {
  200. this.lastToastUnitId = this.danyuanList[0].danyuanId;
  201. }
  202. },
  203. // 从参数初始化方法
  204. initFromOptions(options) {
  205. console.log('options', options);
  206. this.chanpinId = options.chanpinId;
  207. this.danyuanId = options.danyuanId;
  208. this.banbenId = options.banbenId;
  209. this.dengjiId = options.dengjiId;
  210. this.loadDataFromApi();
  211. },
  212. // 从缓存更新数据方法
  213. updateFromCache() {
  214. updateXuexiProcess()
  215. const cacheData = cacheManager.get('xuexi-shuxue');
  216. console.log('cacheData 从缓存更新数据方法', cacheData);
  217. if (cacheData) {
  218. this.banbenInfo = {
  219. ...cacheData
  220. };
  221. this.curProcess = cacheData.curProcess;
  222. this.danyuanList = [...(cacheData.danyuanList || [])];
  223. // 数据更新后重新初始化观察器
  224. this.$nextTick(() => {
  225. setTimeout(() => {
  226. this.initObserver();
  227. }, 300);
  228. });
  229. }
  230. },
  231. shouldUseCache(options, cacheData) {
  232. if (options.banbenId && cacheData.banbenId === options.banbenId) {
  233. return true;
  234. }
  235. return false;
  236. },
  237. // 改成了
  238. loadDataFromApi() {
  239. this.banbenInfo = {}
  240. this.curProcess = ''
  241. this.danyuanList = []
  242. const req = {
  243. banbenId: this.banbenId
  244. }
  245. shuxueChanpinBanbenInfo(req).then(res => {
  246. if (res.code === 0) {
  247. this.banbenInfo = res.data;
  248. this.curProcess =res.data.curProcess * 100;
  249. this.danyuanList = res.data.danyuanList || [];
  250. if (!this.danyuanList) {
  251. this.existData = false
  252. }
  253. // 保存到缓存(新增了参数保存)
  254. const cacheData = {
  255. ...res.data,
  256. banbenId: this.banbenId,
  257. chanpinId: this.chanpinId,
  258. danyuanId: this.danyuanId,
  259. dengjiId: this.dengjiId
  260. };
  261. cacheManager.set('xuexi-shuxue', cacheData);
  262. this.hasCache = true;
  263. // 更新全局auth信息
  264. cacheManager.updateObject('auth', {
  265. chanpinId: res.data.chanpinId,
  266. banbenId: this.banbenId,
  267. danyuanId: res.data.curDanyuanId,
  268. dengjiId: res.data.dengjiId
  269. });
  270. this.danyuanId = res.data.curDanyuanId
  271. this.dengjiId = res.data.dengjiId
  272. this.chanpinId = res.data.chanpinId
  273. // 数据加载完成后初始化观察器
  274. this.$nextTick(() => {
  275. setTimeout(() => {
  276. this.initObserver();
  277. }, 300);
  278. });
  279. }
  280. }).catch(res => {
  281. cacheManager.remove("xuexi-shuxue");
  282. this.hasCache = false;
  283. toast("加载失败,请重试");
  284. });
  285. },
  286. getJieAndDanyuan(data, jieId) {
  287. // 循环单元
  288. for (let danyuan of data.danyuanList) {
  289. // 循环节
  290. for (let jie of danyuan.jieList) {
  291. if (jie.jieId == jieId) {
  292. return {
  293. danyuan: danyuan,
  294. jie: jie
  295. }
  296. }
  297. }
  298. }
  299. return null;
  300. },
  301. // 统一保存和跳转方法
  302. async saveAndNavigate(jieId, type,da, code) {
  303. if (code == 'jixu') { // 追加参数判断 非继续 [临时]
  304. if (!this.banbenId || !this.danyuanId) {
  305. toast("banbenId或者danyuanId 丢失")
  306. return false
  307. }
  308. }
  309. let req = {
  310. "banbenId": this.banbenId,
  311. "danyuanId": da.danyuanId, // 修改当时版本ID为数据内版本Id [临时]
  312. "jieId": jieId
  313. }
  314. try {
  315. const res = await shuxueSave(req);
  316. console.log('res', res);
  317. if (res.code == 0 && res.data) {
  318. let curJieAndDanyuan = this.getJieAndDanyuan(this.banbenInfo, jieId);
  319. console.log('curJieAndDanyuan', curJieAndDanyuan);
  320. if (!curJieAndDanyuan) {
  321. toast("未找到课程信息");
  322. return false;
  323. }
  324. // 更新缓存
  325. const cacheData = cacheManager.get('xuexi-shuxue') || {};
  326. cacheData.curDanyuanName = curJieAndDanyuan.danyuan.danyuanName;
  327. cacheData.curKechengName = curJieAndDanyuan.jie.jieIntro;
  328. cacheData.curJieId = jieId;
  329. cacheData.type = curJieAndDanyuan.jie.type;
  330. cacheManager.set('xuexi-shuxue', cacheData);
  331. // 跳转到学习页面
  332. if (type == 1) {
  333. uni.navigateTo({
  334. url: `/pages/xinshuxue/lookShipin?jieId=${jieId}`
  335. })
  336. } else {
  337. uni.navigateTo({
  338. url: `/pages/xinshuxue/unitTest?jieId=${jieId}`
  339. })
  340. }
  341. } else {
  342. toast("保存位置出错");
  343. return false;
  344. }
  345. } catch (error) {
  346. toast("保存失败");
  347. return false;
  348. }
  349. },
  350. handlePlay(da, code) {
  351. console.log('da', da);
  352. let jieId = null;
  353. if (code == 'jixu') {
  354. if (!da.curJieId) {
  355. toast("无课程ID");
  356. return;
  357. }
  358. jieId = da.curJieId;
  359. } else {
  360. jieId = da.jieId;
  361. }
  362. // [临时]
  363. this.saveAndNavigate(jieId, da.type, da, code);
  364. },
  365. handleClickDanyuan(item) {
  366. // 更新为点击的动态单元Id [临时]
  367. if (!item.danyuanId) {
  368. toast("this.danyuanId丢失")
  369. return false
  370. }
  371. this.isShow = true;
  372. setTimeout(() => {
  373. // 更新为点击的动态单元Id [临时]
  374. this.$refs.dyRef.handleShow(item.danyuanId)
  375. }, 100)
  376. },
  377. handleBack() {
  378. uni.navigateTo({
  379. url: `/pages/chanpinXuanze/banben?dengjiId=` + this.dengjiId
  380. })
  381. },
  382. }
  383. }
  384. </script>