index.vue 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. <template>
  2. <view class="ezy-xuexi-page3">
  3. <view class="icon-title-navBar-box">
  4. <text class="nav-bar-title">学习</text>
  5. </view>
  6. <!-- 滚动区域 -->
  7. <scroll-view v-if="existData" class="ezy-page-body xuexi-page-body" scroll-y :scroll-top="scrollTop"
  8. @scroll="handleScroll">
  9. <view class="xxjl-card-box-padding">
  10. <view class="xxjl-card-box">
  11. <!-- 显示内容 -->
  12. <view class="card-body-box">
  13. <img :src="banbenInfo.cover" />
  14. <view class="body-right">
  15. <view class="right-name">{{ banbenInfo.chanpinName }}</view>
  16. <view>等级:{{banbenInfo.dengjiName}}</view>
  17. <view>版本:{{banbenInfo.name}}</view>
  18. <view>单元:{{banbenInfo.curDanyuanName}}</view>
  19. <view>课程:{{banbenInfo.curKechengName}}</view>
  20. </view>
  21. </view>
  22. <view class="card-progress-box">
  23. <view class="xx-progress-box">
  24. <view>学习进度</view>
  25. <progress :percent="curProcess" class="xx-progress" stroke-width="20"
  26. backgroundColor="#3c7dfd" activeColor="#ffd11c" />
  27. </view>
  28. <ezyActiveVue class="ezy-btn-active jxxx-btn" @aclick="handlePlay(banbenInfo,'jixu')">
  29. </ezyActiveVue>
  30. </view>
  31. </view>
  32. </view>
  33. <view class="xx-item-list">
  34. <!-- <view class="xx-item-title">— 以下为当前等级课程目录 —</view> -->
  35. <view v-for="(danyuanItem, index) in danyuanList" :key="danyuanItem.danyuanId">
  36. <!-- 单元标题(带唯一ID,用于位置查询) -->
  37. <view class="xx-item-title" @click="handleClickDanyuan(danyuanItem.danyuanId)"
  38. :id="`title-${danyuanItem.danyuanId}`">
  39. — {{ danyuanItem.danyuanName }} {{ danyuanItem.danyuanIntro }} —
  40. </view>
  41. <!-- 节列表 -->
  42. <ezyActiveVue class="ezy-list-item-active xx-item-box" v-for="jieItem in danyuanItem.jieList"
  43. :key="jieItem.jieId" @aclick="handlePlay(jieItem,'play')">
  44. <view class="xx-item-status"
  45. :class="jieItem.wanchengFlag == 1 ? 'completed-status' : 'uncompleted-status'"></view>
  46. <img :src="jieItem.cover" />
  47. <view class="xx-text-box">
  48. <view>{{ jieItem.jieName }}</view>
  49. <view>{{ jieItem.jieIntro }}</view>
  50. </view>
  51. <view class="xx-item-btn"></view>
  52. </ezyActiveVue>
  53. </view>
  54. <view class="xx-item-title">本级别最后一单元啦~</view>
  55. <view class="xx-more-btn" @click="moreBtn"></view>
  56. </view>
  57. </scroll-view>
  58. <!-- 回到顶部 -->
  59. <view v-show="showGoTop" class="go-top-btn" @click="goTopBtn"></view>
  60. <!-- 无数据占位 -->
  61. <view v-if="!existData" class="ezy-page-body">
  62. <view class="ezy-no-sj">
  63. <icon></icon>
  64. <text>暂无数据</text>
  65. </view>
  66. </view>
  67. <!-- 弹窗组件 -->
  68. <danyuanInfoVue ref="dyRef" v-if="isShow" @close="isShow = false"></danyuanInfoVue>
  69. <!-- 底部 tabBar -->
  70. <custom-tab-bar :show="true" :current-index="currentTabIndex" />
  71. </view>
  72. </template>
  73. <script>
  74. import ezyActiveVue from "@/components/ezyActive/ezyActive.vue";
  75. import CustomTabBar from '@/components/custom-tabbar/index.vue';
  76. import cacheManager from "@/utils/cacheManager.js";
  77. import {
  78. shuxueChanpinBanbenInfo,
  79. shuxueSave
  80. } from "@/api/chanpinneirong.js"
  81. import {
  82. onLoad,
  83. onShow,
  84. onHide,
  85. onUnload
  86. } from "@dcloudio/uni-app"
  87. import danyuanInfoVue from '@/pages/xinshuxue/components/danyuanInfo.vue';
  88. import {
  89. toast
  90. } from '../../utils/common';
  91. import {
  92. updateXuexiProcess
  93. } from "./useNeirongShuxue"
  94. export default {
  95. data() {
  96. return {
  97. canExitApp: false,
  98. danyuanList: [],
  99. banbenInfo: {},
  100. banbenId: '',
  101. danyuanId: '',
  102. chanpinId: '',
  103. dengjiId: '',
  104. curProcess: '',
  105. currentTabIndex: 1,
  106. existData: true,
  107. isShow: false,
  108. stickyHeight: 0, // 吸顶栏高度(px)
  109. currentStickyTitle: '', // 初始为空,不显示吸顶
  110. currentStickyIntro: '',
  111. currentDanyuanId: '',
  112. currentStickyDengjiId: '',
  113. titlePositions: [],
  114. scrollTop: 0,
  115. showGoTop: false,
  116. scrollThreshold: 300,
  117. }
  118. },
  119. components: {
  120. CustomTabBar,
  121. danyuanInfoVue,
  122. ezyActiveVue
  123. },
  124. onLoad(options) {
  125. uni.hideTabBar()
  126. },
  127. onShow() {
  128. this.currentTabIndex = 1
  129. const cacheData = cacheManager.get('xuexi-shuxue');
  130. if (cacheData) {
  131. console.log('使用缓存数据');
  132. this.updateFromCache();
  133. } else {
  134. console.log('重新请求数据');
  135. const cacheDataAuth = cacheManager.get('auth');
  136. this.initFromOptions(cacheDataAuth);
  137. }
  138. },
  139. onHide() {
  140. console.log('学习页面隐藏')
  141. },
  142. onUnload() {
  143. // 页面卸载无需特殊处理
  144. },
  145. methods: {
  146. goTopBtn() {
  147. uni.pageScrollTo({ // 添加平滑滚动效果
  148. scrollTop: 0,
  149. duration: 300
  150. });
  151. this.scrollTop = 0;
  152. this.showGoTop = false; // 滚动到顶部后隐藏按钮
  153. },
  154. moreBtn() {
  155. uni.switchTab({
  156. url: '/pages/chanpinXuanze/index'
  157. })
  158. },
  159. initFromOptions(options) {
  160. //console.log('options', options);
  161. this.chanpinId = options.chanpinId;
  162. this.danyuanId = options.danyuanId;
  163. this.banbenId = options.banbenId;
  164. this.dengjiId = options.dengjiId;
  165. this.loadDataFromApi();
  166. },
  167. // 从缓存更新数据方法
  168. updateFromCache() {
  169. updateXuexiProcess()
  170. const cacheData = cacheManager.get('xuexi-shuxue');
  171. console.log('cacheData 从缓存更新数据方法', cacheData);
  172. if (cacheData) {
  173. this.banbenInfo = {
  174. ...cacheData
  175. };
  176. this.curProcess = cacheData.curProcess;
  177. this.danyuanList = [...(cacheData.danyuanList || [])];
  178. this.banbenId = cacheData.banbenId
  179. // this.$nextTick(() => {
  180. // this.updateTitlePositions();
  181. // });
  182. }
  183. },
  184. loadDataFromApi() {
  185. this.banbenInfo = {}
  186. this.curProcess = ''
  187. this.danyuanList = []
  188. const req = {
  189. banbenId: this.banbenId
  190. }
  191. shuxueChanpinBanbenInfo(req).then(res => {
  192. if (res.code === 0) {
  193. this.banbenInfo = res.data;
  194. this.banbenInfo.danyuanId = res.data.curDanyuanId;
  195. this.curProcess = res.data.curProcess * 100;
  196. this.danyuanList = res.data.danyuanList || [];
  197. if (this.danyuanList.length == 0) {
  198. this.existData = false
  199. } else {
  200. this.existData = true
  201. }
  202. // 保存到缓存(新增了参数保存)
  203. const cacheData = {
  204. ...res.data,
  205. banbenId: this.banbenId,
  206. chanpinId: this.chanpinId,
  207. danyuanId: this.danyuanId,
  208. dengjiId: this.dengjiId
  209. };
  210. cacheManager.set('xuexi-shuxue', cacheData);
  211. // 更新全局auth信息
  212. cacheManager.updateObject('auth', {
  213. chanpinId: res.data.chanpinId,
  214. banbenId: this.banbenId,
  215. danyuanId: res.data.curDanyuanId,
  216. dengjiId: res.data.dengjiId
  217. });
  218. this.danyuanId = res.data.curDanyuanId
  219. this.dengjiId = res.data.dengjiId
  220. this.chanpinId = res.data.chanpinId
  221. // 数据加载完成后初始化观察器
  222. // this.$nextTick(() => {
  223. // this.updateTitlePositions();
  224. // });
  225. }
  226. }).catch(res => {
  227. cacheManager.remove("xuexi-shuxue");
  228. toast("加载失败,请重试");
  229. });
  230. },
  231. handleScroll(e) {
  232. const scrollTop = e.detail.scrollTop;
  233. console.log('scrollTop', scrollTop);
  234. this.showGoTop = scrollTop > this.scrollThreshold;
  235. },
  236. getJieAndDanyuan(data, jieId) {
  237. for (let danyuan of data.danyuanList) {
  238. for (let jie of danyuan.jieList) {
  239. if (jie.jieId == jieId) {
  240. return {
  241. danyuan,
  242. jie
  243. }
  244. }
  245. }
  246. }
  247. return null;
  248. },
  249. async saveAndNavigate(jieId, type, da, code) {
  250. console.log('this.banbenId', this.banbenId);
  251. console.log('this.danyuanIddanyuanId', da.danyuanId);
  252. if (code == 'jixu') {
  253. if (!this.banbenId || !da.danyuanId) {
  254. toast("banbenId或者danyuanId 丢失")
  255. return false
  256. }
  257. }
  258. let req = {
  259. "banbenId": this.banbenId,
  260. "danyuanId": da.danyuanId,
  261. "jieId": jieId
  262. }
  263. console.log('req', req);
  264. try {
  265. const res = await shuxueSave(req);
  266. if (res.code == 0 && res.data) {
  267. let curJieAndDanyuan = this.getJieAndDanyuan(this.banbenInfo, jieId);
  268. if (!curJieAndDanyuan) {
  269. toast("未找到课程信息");
  270. return false;
  271. }
  272. const cacheData = cacheManager.get('xuexi-shuxue') || {};
  273. cacheData.curDanyuanName = curJieAndDanyuan.danyuan.danyuanName;
  274. cacheData.curKechengName = curJieAndDanyuan.jie.jieIntro;
  275. cacheData.danyuanId = curJieAndDanyuan.jie.danyuanId;
  276. cacheData.curJieId = jieId;
  277. cacheData.type = curJieAndDanyuan.jie.type;
  278. cacheManager.set('xuexi-shuxue', cacheData);
  279. if (type == 1) {
  280. uni.navigateTo({
  281. url: `/pages/xinshuxue/lookShipin?jieId=${jieId}`
  282. })
  283. } else {
  284. uni.navigateTo({
  285. url: `/pages/xinshuxue/unitTest?jieId=${jieId}`
  286. })
  287. }
  288. } else {
  289. toast("保存位置出错");
  290. return false;
  291. }
  292. } catch (error) {
  293. toast("保存失败");
  294. return false;
  295. }
  296. },
  297. handlePlay(da, code) {
  298. let jieId = code === 'jixu' ? da.curJieId : da.jieId;
  299. if (!jieId) {
  300. toast("无课程ID");
  301. return;
  302. }
  303. this.saveAndNavigate(jieId, da.type, da, code);
  304. },
  305. handleClickDanyuan(danyuanId) {
  306. if (!danyuanId) {
  307. toast("danyuanId丢失")
  308. return false
  309. }
  310. this.isShow = true;
  311. setTimeout(() => {
  312. // 更新为点击的动态单元Id [临时]
  313. this.$refs.dyRef.handleShow(danyuanId)
  314. }, 100)
  315. },
  316. handleBack() {
  317. uni.navigateTo({
  318. url: `/pages/chanpinXuanze/banben?dengjiId=` + this.dengjiId
  319. })
  320. },
  321. },
  322. // 计算吸顶栏下方的偏移(确保内容不被遮挡)
  323. computed: {
  324. }
  325. }
  326. </script>