index.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. console.log('cacheData',cacheData);
  175. this.banbenInfo = cacheData;
  176. this.curProcess = cacheData.curProcess;
  177. this.danyuanList = cacheData.danyuanList || [];
  178. this.banbenId = cacheData.banbenId || '';
  179. this.chanpinId = cacheData.chanpinId || '';
  180. this.danyuanId = cacheData.danyuanId || '';
  181. this.dengjiId = cacheData.dengjiId || '';
  182. },
  183. // 从参数初始化方法
  184. initFromOptions(options) {
  185. this.chanpinId = options.chanpinId;
  186. this.danyuanId = options.danyuanId;
  187. this.banbenId = options.banbenId;
  188. this.dengjiId = options.dengjiId;
  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>