index.vue 9.3 KB

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