index.vue 9.8 KB

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