index.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <template>
  2. <view class="grades-terms-page">
  3. <view class="icon-title-navBar-box">
  4. <!-- 返回按钮↓ -->
  5. <view class="nav-bar-icon" @click="handleBack"></view>
  6. </view>
  7. <view class="grade-item-box">
  8. <view :key="xueke.id" @click="handleSelectXueke(xueke)" v-for="xueke in xuekeData"
  9. :class="['grade-item',{active: xueke.id == activeXueke}]">{{xueke.lable}}</view>
  10. </view>
  11. <view class="subject-body">
  12. <view class="subject-content-box">
  13. <!-- 产品 -->
  14. <view v-for="item in xuekeList" :key="item.id" @click="handleSelectGrade(item)"
  15. :class="['subject-item', {active: item.id == activeProduct}]">
  16. <img :src="item.cover" class="subject-item-img" />
  17. <view class="subject-item-text">{{item.lable}}</view>
  18. </view>
  19. </view>
  20. </view>
  21. <view class="grade-line"></view>
  22. <button class="grade-confirm-btn" @click="handleConfirm"></button>
  23. </view>
  24. </template>
  25. <script setup>
  26. import {
  27. reactive,
  28. toRefs,
  29. computed
  30. } from "vue";
  31. import * as httpApi from "@/api/selectGradesTerms.js";
  32. import {
  33. onLoad
  34. } from "@dcloudio/uni-app";
  35. import {
  36. getUserIdentity,
  37. findRootNode,
  38. findTreeNode
  39. } from "@/utils/common.js"
  40. import cacheManager from "@/utils/cacheManager.js"
  41. import {
  42. getCommonTree,
  43. getIndexTree
  44. } from "../../api/selectGradesTerms";
  45. function useSelectGrade() {
  46. const userCode = getUserIdentity();
  47. const data = reactive({
  48. activeProduct: null, // 当前年级
  49. activeXueke: 1, // 当前学期
  50. activeTipFlag: null, // 当前学期
  51. xuekeData: [], // 学科总数据
  52. });
  53. onLoad(({
  54. productId,
  55. xuekeId,
  56. tipFlag
  57. }) => {
  58. if (userCode !== 'Visitor') {
  59. initUserProducts();
  60. } else {
  61. data.activeProduct = productId;
  62. data.activeXueke = xuekeId || 1; // 默认选中数学
  63. data.activeTipFlag = tipFlag || '0';
  64. initVisitProducts();
  65. }
  66. });
  67. // 初始化游客产品数据
  68. function initVisitProducts() {
  69. httpApi.getCommonTree().then(res => {
  70. data.xuekeData = res.data || [];
  71. })
  72. }
  73. function initUserProducts() {
  74. httpApi.getIndexTree().then(res => {
  75. data.xuekeData = res.data || [];
  76. const {
  77. levelId,
  78. } = cacheManager.get('auth');
  79. if (levelId) {
  80. // LevelId 翻找根节点学科,执行选中高亮操作
  81. const xuekeObj = findRootNode(data.xuekeData, levelId, 'levelId');
  82. const productObj = findTreeNode(data.xuekeData, levelId, 'children','levelId');
  83. data.activeProduct = productObj.id;
  84. data.activeXueke = xuekeObj.id;
  85. } else {
  86. data.activeProduct = null;
  87. data.activeXueke = 1
  88. }
  89. })
  90. }
  91. // 选择 年级+学期
  92. function handleConfirm() {
  93. if (!data.activeXueke) {
  94. uni.showToast({
  95. title: "请选择课程类别",
  96. duration: 2000,
  97. icon: 'error'
  98. });
  99. return;
  100. }
  101. if (!data.activeProduct) {
  102. uni.showToast({
  103. title: "请选择课程等级",
  104. duration: 2000,
  105. icon: 'error'
  106. });
  107. return;
  108. }
  109. goDAOToStudy();
  110. }
  111. // 跳转 岛 学习
  112. function goDAOToStudy() {
  113. const Product = data.xuekeData.find(item => item.id == data.activeXueke).children.find(item => item.id == data
  114. .activeProduct)
  115. const levelId = Product.levelId;
  116. const typeId = Product.typeId;
  117. const subjectId = Product.subjectId;
  118. if (userCode !== 'Visitor') {
  119. const auth = cacheManager.get('auth');
  120. cacheManager.updateObject('auth', {
  121. typeId: typeId,
  122. levelId: levelId,
  123. subjectId: subjectId,
  124. zhangId: data.activeProduct == auth.nianji && data.activeXueke == auth.cardId ? auth.zhangId :
  125. 0,
  126. currentZhang: 0
  127. })
  128. cacheManager.remove('daoPageCache')
  129. uni.redirectTo({
  130. url: `/pages/study/index`
  131. })
  132. } else {
  133. if (typeId == 1) {
  134. // 新岛
  135. uni.redirectTo({
  136. url: `/pages/study/index?levelId=${levelId}&typeId=${typeId}&subjectId=${subjectId}&tipFlag=${data.activeTipFlag}`
  137. })
  138. } else {
  139. // 旧岛
  140. uni.redirectTo({
  141. url: `/pages/study/index?levelId=${levelId}&typeId=${typeId}&subjectId=${subjectId}&zhangId=0&tipFlag=${data.activeTipFlag}`
  142. })
  143. }
  144. }
  145. }
  146. function handleBack() {
  147. if (userCode !== 'Visitor') {
  148. uni.redirectTo({
  149. url: `/pages/study/index`
  150. })
  151. } else {
  152. uni.redirectTo({
  153. url: '/pages/login/index'
  154. })
  155. }
  156. }
  157. return {
  158. ...toRefs(data),
  159. handleBack,
  160. // 方法
  161. handleConfirm, // 选择年级+学科
  162. };
  163. }
  164. const {
  165. activeProduct,
  166. activeXueke,
  167. xuekeData,
  168. handleConfirm,
  169. handleBack
  170. } = useSelectGrade()
  171. const xuekeList = computed(() => {
  172. if (!xuekeData.value.length) {
  173. return []
  174. }
  175. const d_id = activeXueke.value;
  176. return xuekeData.value.find(item => item.id == d_id).children
  177. })
  178. function handleSelectGrade(item) {
  179. activeProduct.value = item.id;
  180. }
  181. function handleSelectXueke(item) {
  182. activeXueke.value = item.id;
  183. activeProduct.value = null;
  184. }
  185. </script>