index.vue 11 KB

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