index.vue 5.3 KB

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