index.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <template>
  2. <view class="ezy-xuexi-page">
  3. <view class="icon-title-navBar-box">
  4. <view @click="handleBack" class="nav-bar-icon"></view>
  5. <text class="nav-bar-title">学习</text>
  6. </view>
  7. <view class="ezy-page-body xuexi-page-body">
  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 class="xx-scroll-body">
  30. <view class="xx-item-list">
  31. <view class="xx-item-title">— 以下为当前等级课程目录 —</view>
  32. <view v-for="danyuanItem in danyuanList" :key="danyuanItem.danyuanId">
  33. <view class="item-dy-box" @click="handleClickDanyuan">
  34. <view class="dy-left-box">L{{danyuanItem.danyuanId}}</view>
  35. <view class="dy-right-box">
  36. <view class="dy-name">{{danyuanItem.danyuanName}}</view>
  37. <view>{{danyuanItem.danyuanIntro}}</view>
  38. </view>
  39. </view>
  40. <view class="xx-item-box" v-for="jieItem in danyuanItem.jieList" :key="jieItem.jieId"
  41. @click="handlePlay(jieItem,'play')">
  42. <view class="xx-item-status"
  43. :class="jieItem.wanchengFlag == 1 ? 'completed-status' : 'uncompleted-status'"> </view>
  44. <img :src="jieItem.cover" />
  45. <view class="xx-text-box">
  46. <view>{{ jieItem.jieName }}</view>
  47. <view>{{ jieItem.jieIntro }}</view>
  48. </view>
  49. <view class="xx-item-btn"></view>
  50. </view>
  51. </view>
  52. </view>
  53. </view>
  54. </view>
  55. <danyuanInfoVue ref="dyRef" v-if="isShow" @close="isShow= false"></danyuanInfoVue>
  56. <custom-tab-bar :show="true" :current-index="currentTabIndex" />
  57. <!-- 回到顶部 -->
  58. <view class="go-top-btn" v-if="false"></view>
  59. </view>
  60. </template>
  61. <script>
  62. import CustomTabBar from '@/components/custom-tabbar/index.vue';
  63. import cacheManager from "@/utils/cacheManager.js";
  64. import {
  65. shuxueChanpinBanbenInfo,
  66. shuxueSave
  67. } from "@/api/chanpinneirong.js"
  68. import {
  69. onLoad,
  70. onShow
  71. } from "@dcloudio/uni-app"
  72. import danyuanInfoVue from '@/pages/xinshuxue/components/danyuanInfo.vue';
  73. import {
  74. toast
  75. } from '../../utils/common';
  76. export default {
  77. data() {
  78. return {
  79. canExitApp: false,
  80. danyuanList: [],
  81. currentUnitName: "",
  82. isShow: true,
  83. banbenInfo: {},
  84. banbenId: '',
  85. danyuanId: '',
  86. chanpinId: '',
  87. dengjiId: '',
  88. curProcess: '',
  89. currentTabIndex: 1,
  90. cacheManagerLocal: null,
  91. hasCache: false, // 是否有缓存的标志
  92. needRefresh: false // 是否需要刷新数据的标志
  93. }
  94. },
  95. components: {
  96. CustomTabBar,
  97. danyuanInfoVue
  98. },
  99. onShow() {
  100. console.log('学习页面显示,使用缓存数据')
  101. console.log('this.needRefresh',this.needRefresh)
  102. console.log('this.hasCache',this.hasCache)
  103. this.currentTabIndex = 1
  104. // 如果从其他页面返回需要刷新数据
  105. if (this.needRefresh) {
  106. this.needRefresh = false;
  107. this.updateFromCache();
  108. }
  109. // 如果是第一次进入,已经有缓存数据了,直接使用缓存
  110. if (this.hasCache) {
  111. this.updateFromCache();
  112. }
  113. },
  114. onHide() {
  115. console.log('学习页面隐藏,保存滚动位置')
  116. this.needRefresh = true; // 标记离开页面,返回时需要刷新
  117. },
  118. onLoad(options) {
  119. uni.hideTabBar()
  120. const cacheData = cacheManager.get('xuexi-shuxue');
  121. // cacheData && this.shouldUseCache(options, cacheData)
  122. if (cacheData) {
  123. // 使用缓存数据
  124. console.log('使用缓存数据');
  125. this.hasCache = true;
  126. this.initFromCache(cacheData);
  127. } else {
  128. // 没有缓存或参数不同,重新请求
  129. console.log('重新请求数据');
  130. this.hasCache = false;
  131. const cacheDataAuth = cacheManager.get('auth');
  132. this.initFromOptions(cacheDataAuth);
  133. this.loadDataFromApi();
  134. }
  135. },
  136. // 返回处理
  137. onBackPress(options) {
  138. if (options.from === 'backbutton') {
  139. // 返回键处理
  140. return this.handleHarmonyOSBack()
  141. }
  142. return false
  143. }),
  144. methods: {
  145. handleHarmonyOSBack() {
  146. // 双击返回键退出应用
  147. if (!this.canExitApp) {
  148. this.canExitApp = true
  149. // 显示提示
  150. uni.showToast({
  151. title: '再按一次退出应用',
  152. icon: 'none',
  153. duration: 2000
  154. })
  155. // 2秒后重置状态
  156. setTimeout(() => {
  157. this.canExitApp = false
  158. }, 2000)
  159. return true // 拦截返回事件
  160. } else {
  161. // 退出应用
  162. this.exitApp()
  163. return true
  164. }
  165. }
  166. exitApp() {
  167. // #ifdef APP-PLUS || APP-PLUS-NVUE
  168. // 系统退出应用
  169. plus.runtime.quit()
  170. // #endif
  171. },
  172. // 从缓存初始化方法
  173. initFromCache(cacheData) {
  174. this.banbenInfo = cacheData;
  175. this.curProcess = cacheData.curProcess;
  176. this.danyuanList = cacheData.danyuanList || [];
  177. this.banbenId = cacheData.banbenId || '';
  178. this.chanpinId = cacheData.chanpinId || '';
  179. this.danyuanId = cacheData.danyuanId || '';
  180. this.dengjiId = cacheData.dengjiId || '';
  181. },
  182. // 从参数初始化方法
  183. initFromOptions(options) {
  184. this.chanpinId = options.chanpinId;
  185. this.danyuanId = options.danyuanId;
  186. this.banbenId = options.banbenId;
  187. this.dengjiId = options.dengjiId;
  188. },
  189. // 从缓存更新数据方法
  190. updateFromCache() {
  191. const cacheData = cacheManager.get('xuexi-shuxue');
  192. console.log('cacheData 从缓存更新数据方法',cacheData);
  193. if (cacheData) {
  194. this.banbenInfo = {
  195. ...cacheData
  196. };
  197. this.curProcess = cacheData.curProcess;
  198. this.danyuanList = [...(cacheData.danyuanList || [])];
  199. }
  200. },
  201. shouldUseCache(options, cacheData) {
  202. if (options.banbenId && cacheData.banbenId === options.banbenId) {
  203. return true;
  204. }
  205. return false;
  206. },
  207. // 改成了
  208. loadDataFromApi() {
  209. const req = {
  210. banbenId: this.banbenId
  211. }
  212. shuxueChanpinBanbenInfo(req).then(res => {
  213. if (res.code === 0) {
  214. this.banbenInfo = res.data;
  215. this.curProcess = res.data.curProcess;
  216. this.danyuanList = res.data.danyuanList || [];
  217. // 保存到缓存(新增了参数保存)
  218. const cacheData = {
  219. ...res.data,
  220. banbenId: this.banbenId,
  221. chanpinId: this.chanpinId,
  222. danyuanId: this.danyuanId,
  223. dengjiId: this.dengjiId
  224. };
  225. cacheManager.set('xuexi-shuxue', cacheData);
  226. this.hasCache = true;
  227. // 更新全局auth信息
  228. cacheManager.updateObject('auth', {
  229. chanpinId: this.chanpinId,
  230. banbenId: this.banbenId,
  231. danyuanId: this.danyuanId,
  232. dengjiId: this.dengjiId
  233. });
  234. }
  235. }).catch(res => {
  236. cacheManager.remove("xuexi-shuxue");
  237. this.hasCache = false;
  238. toast("加载失败,请重试");
  239. });
  240. },
  241. getJieAndDanyuan(data, jieId) {
  242. // 循环单元
  243. for (let danyuan of data.danyuanList) {
  244. // 循环节
  245. for (let jie of danyuan.jieList) {
  246. if (jie.jieId == jieId) {
  247. return {
  248. danyuan: danyuan,
  249. jie: jie
  250. }
  251. }
  252. }
  253. }
  254. return null;
  255. },
  256. // 统一保存和跳转方法
  257. async saveAndNavigate(jieId, type) {
  258. let req = {
  259. "banbenId": this.banbenId,
  260. "danyuanId": this.danyuanId,
  261. "jieId": jieId
  262. }
  263. try {
  264. const res = await shuxueSave(req);
  265. console.log('res', res);
  266. if (res.code == 0 && res.data) {
  267. let curJieAndDanyuan = this.getJieAndDanyuan(this.banbenInfo, jieId);
  268. console.log('curJieAndDanyuan', curJieAndDanyuan);
  269. if (!curJieAndDanyuan) {
  270. toast("未找到课程信息");
  271. return false;
  272. }
  273. // 更新缓存
  274. const cacheData = cacheManager.get('xuexi-shuxue') || {};
  275. cacheData.curDanyuanName = curJieAndDanyuan.danyuan.danyuanName;
  276. cacheData.curKechengName = curJieAndDanyuan.danyuan.danyuanIntro;
  277. cacheData.curJieId = jieId;
  278. cacheData.type = curJieAndDanyuan.jie.type;
  279. cacheManager.set('xuexi-shuxue', cacheData);
  280. // 跳转到学习页面
  281. if (type == 1) {
  282. uni.navigateTo({
  283. url: `/pages/xinshuxue/lookShipin?jieId=${jieId}`
  284. })
  285. } else {
  286. uni.navigateTo({
  287. url: `/pages/xinshuxue/unitTest?jieId=${jieId}`
  288. })
  289. }
  290. } else {
  291. toast("保存位置出错");
  292. return false;
  293. }
  294. } catch (error) {
  295. toast("保存失败");
  296. return false;
  297. }
  298. },
  299. handlePlay(da, code) {
  300. console.log('da', da);
  301. let jieId = null;
  302. if (code == 'jixu') {
  303. if (!da.curJieId) {
  304. toast("无课程ID");
  305. return;
  306. }
  307. jieId = da.curJieId;
  308. } else {
  309. jieId = da.jieId;
  310. }
  311. this.saveAndNavigate(jieId, da.type);
  312. },
  313. handleClickDanyuan() {
  314. this.isShow = true;
  315. setTimeout(() => {
  316. this.$refs.dyRef.handleShow(this.danyuanId)
  317. }, 100)
  318. },
  319. handleBack() {
  320. uni.navigateTo({
  321. url: `/pages/chanpinXuanze/banben?dengjiId=` + this.dengjiId
  322. })
  323. },
  324. }
  325. }
  326. </script>