index.vue 5.0 KB

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