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. <scroll-view scroll-y>
  8. <view class="xxjl-card-box-padding">
  9. <view class="xxjl-card-box">
  10. <!-- 显示内容 -->
  11. <view class="card-body-box">
  12. <img :src="banbenInfo.cover" />
  13. <view class="body-right">
  14. <view class="right-name">{{ banbenInfo.chanpinName }}</view>
  15. <view>等级:{{banbenInfo.dengjiName}}</view>
  16. <view>版本:{{banbenInfo.name}}</view>
  17. <view>单元:{{banbenInfo.curDanyuanName}}</view>
  18. <view>课程:{{banbenInfo.curKechengName}}</view>
  19. </view>
  20. </view>
  21. <view class="card-progress-box">
  22. <view class="xx-progress-box">
  23. <view>学习进度</view>
  24. <progress :percent="curProcess" class="xx-progress" stroke-width="20"
  25. backgroundColor="#3c7dfd" activeColor="#ffd11c" />
  26. </view>
  27. <view class="jxxx-btn" @click="handlePlay(banbenInfo,'jixu')"></view>
  28. </view>
  29. </view>
  30. </view>
  31. <view class="xx-item-list">
  32. <view class="xx-item-title">— 以下为当前等级课程目录 —</view>
  33. <view v-for="(danyuanItem,index) in danyuanList" :key="danyuanItem.danyuanId">
  34. <!-- 第一单元 -->
  35. <view v-if="index ==0" class="item-dy-box" :class="{ 'item-fixed': isFixed }" @click="handleClickDanyuan" :id="`unit-title-${danyuanItem.danyuanId}`">
  36. <view class="dy-left-box">L{{banbenInfo.dengjiId}}</view>
  37. <view class="dy-right-box">
  38. <view class="right-content">
  39. <view class="dy-name">{{danyuanItem.danyuanName}}</view>
  40. <view>{{danyuanItem.danyuanIntro}}</view>
  41. </view>
  42. </view>
  43. </view>
  44. <!-- 其他单元 -->
  45. <view v-if="index !=0" class="xx-item-title" :id="`unit-title-${danyuanItem.danyuanId}`">— {{danyuanItem.danyuanName}}
  46. {{danyuanItem.danyuanIntro}} —
  47. </view>
  48. <view class="xx-item-box" v-for="jieItem in danyuanItem.jieList" :key="jieItem.jieId"
  49. @click="handlePlay(jieItem,'play')">
  50. <view class="xx-item-status"
  51. :class="jieItem.wanchengFlag == 1 ? 'completed-status' : 'uncompleted-status'">
  52. </view>
  53. <img :src="jieItem.cover" />
  54. <view class="xx-text-box">
  55. <view>{{ jieItem.jieName }}</view>
  56. <view>{{ jieItem.jieIntro }}</view>
  57. </view>
  58. <view class="xx-item-btn"></view>
  59. </view>
  60. </view>
  61. </view>
  62. <!-- 回到顶部 -->
  63. <view class="go-top-btn" v-if="false"></view>
  64. </scroll-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 CustomTabBar from '@/components/custom-tabbar/index.vue';
  74. import cacheManager from "@/utils/cacheManager.js";
  75. import {
  76. shuxueChanpinBanbenInfo,
  77. shuxueSave
  78. } from "@/api/chanpinneirong.js"
  79. import {
  80. onLoad,
  81. onShow,
  82. onHide,
  83. onUnload
  84. } from "@dcloudio/uni-app"
  85. import danyuanInfoVue from '@/pages/xinshuxue/components/danyuanInfo.vue';
  86. import {
  87. toast
  88. } from '../../utils/common';
  89. import {updateXuexiProcess} from "./useNeirongShuxue"
  90. export default {
  91. data() {
  92. return {
  93. canExitApp: false,
  94. danyuanList: [],
  95. currentUnitName: "",
  96. isShow: true,
  97. banbenInfo: {},
  98. banbenId: '',
  99. danyuanId: '',
  100. chanpinId: '',
  101. dengjiId: '',
  102. curProcess: '',
  103. currentTabIndex: 1,
  104. cacheManagerLocal: null,
  105. hasCache: false, // 是否有缓存的标志
  106. needRefresh: false, // 是否需要刷新数据的标志
  107. existData: true,
  108. currentVisibleUnit: '', // 当前可见的单元名称
  109. observer: null, // IntersectionObserver实例
  110. lastToastUnitId: null ,// 上一次toast的单元ID,防止重复触发
  111. isFixed:false,
  112. }
  113. },
  114. components: {
  115. CustomTabBar,
  116. danyuanInfoVue
  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: this.chanpinId,
  259. banbenId: this.banbenId,
  260. danyuanId: this.danyuanId,
  261. dengjiId: this.dengjiId
  262. });
  263. // 数据加载完成后初始化观察器
  264. this.$nextTick(() => {
  265. setTimeout(() => {
  266. this.initObserver();
  267. }, 300);
  268. });
  269. }
  270. }).catch(res => {
  271. cacheManager.remove("xuexi-shuxue");
  272. this.hasCache = false;
  273. toast("加载失败,请重试");
  274. });
  275. },
  276. getJieAndDanyuan(data, jieId) {
  277. // 循环单元
  278. for (let danyuan of data.danyuanList) {
  279. // 循环节
  280. for (let jie of danyuan.jieList) {
  281. if (jie.jieId == jieId) {
  282. return {
  283. danyuan: danyuan,
  284. jie: jie
  285. }
  286. }
  287. }
  288. }
  289. return null;
  290. },
  291. // 统一保存和跳转方法
  292. async saveAndNavigate(jieId, type) {
  293. if (!this.banbenId || !this.danyuanId) {
  294. toast("banbenId或者danyuanId 丢失")
  295. return false
  296. }
  297. let req = {
  298. "banbenId": this.banbenId,
  299. "danyuanId": this.danyuanId,
  300. "jieId": jieId
  301. }
  302. try {
  303. const res = await shuxueSave(req);
  304. console.log('res', res);
  305. if (res.code == 0 && res.data) {
  306. let curJieAndDanyuan = this.getJieAndDanyuan(this.banbenInfo, jieId);
  307. console.log('curJieAndDanyuan', curJieAndDanyuan);
  308. if (!curJieAndDanyuan) {
  309. toast("未找到课程信息");
  310. return false;
  311. }
  312. // 更新缓存
  313. const cacheData = cacheManager.get('xuexi-shuxue') || {};
  314. cacheData.curDanyuanName = curJieAndDanyuan.danyuan.danyuanName;
  315. cacheData.curKechengName = curJieAndDanyuan.danyuan.danyuanIntro;
  316. cacheData.curJieId = jieId;
  317. cacheData.type = curJieAndDanyuan.jie.type;
  318. cacheManager.set('xuexi-shuxue', cacheData);
  319. // 跳转到学习页面
  320. if (type == 1) {
  321. uni.navigateTo({
  322. url: `/pages/xinshuxue/lookShipin?jieId=${jieId}`
  323. })
  324. } else {
  325. uni.navigateTo({
  326. url: `/pages/xinshuxue/unitTest?jieId=${jieId}`
  327. })
  328. }
  329. } else {
  330. toast("保存位置出错");
  331. return false;
  332. }
  333. } catch (error) {
  334. toast("保存失败");
  335. return false;
  336. }
  337. },
  338. handlePlay(da, code) {
  339. console.log('da', da);
  340. let jieId = null;
  341. if (code == 'jixu') {
  342. if (!da.curJieId) {
  343. toast("无课程ID");
  344. return;
  345. }
  346. jieId = da.curJieId;
  347. } else {
  348. jieId = da.jieId;
  349. }
  350. this.saveAndNavigate(jieId, da.type);
  351. },
  352. handleClickDanyuan() {
  353. if (!this.danyuanId) {
  354. toast("this.danyuanId丢失")
  355. return false
  356. }
  357. this.isShow = true;
  358. setTimeout(() => {
  359. this.$refs.dyRef.handleShow(this.danyuanId)
  360. }, 100)
  361. },
  362. handleBack() {
  363. uni.navigateTo({
  364. url: `/pages/chanpinXuanze/banben?dengjiId=` + this.dengjiId
  365. })
  366. },
  367. }
  368. }
  369. </script>