index.vue 11 KB

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