index.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. console.log('userCode',userCode);
  51. const data = reactive({
  52. activeProduct: null, // 当前年级
  53. activeXueke: 1, // 当前学期
  54. activeTipFlag: null, // 当前学期
  55. xuekeData: [], // 学科总数据
  56. scrollTop: '',
  57. });
  58. function handleScroll() {
  59. }
  60. onLoad(({
  61. productId,
  62. xuekeId,
  63. tipFlag
  64. }) => {
  65. if (userCode !== 'Visitor') {
  66. initUserProducts();
  67. } else {
  68. data.activeProduct = productId;
  69. data.activeXueke = xuekeId || 1; // 默认选中数学
  70. data.activeTipFlag = tipFlag || '0';
  71. initVisitProducts();
  72. }
  73. });
  74. // 初始化游客产品数据
  75. function initVisitProducts() {
  76. httpApi.getCommonTree().then(res => {
  77. data.xuekeData = res.data || [];
  78. })
  79. }
  80. function initUserProducts() {
  81. httpApi.getIndexTree().then(res => {
  82. data.xuekeData = res.data || [];
  83. const {
  84. levelId,
  85. } = cacheManager.get('auth');
  86. if (levelId) {
  87. // LevelId 翻找根节点学科,执行选中高亮操作
  88. const xuekeObj = findRootNode(data.xuekeData, levelId, 'levelId');
  89. const productObj = findTreeNode(data.xuekeData, levelId, 'children','levelId');
  90. data.activeProduct = productObj.id;
  91. data.activeXueke = xuekeObj.id;
  92. } else {
  93. data.activeProduct = null;
  94. data.activeXueke = 1
  95. }
  96. nextTick(() => {
  97. // 滚动到某个元素显示
  98. data.scrollTop = `s_${data.activeProduct}`
  99. })
  100. })
  101. }
  102. // 选择 年级+学期
  103. function handleConfirm() {
  104. if (!data.activeXueke) {
  105. uni.showToast({
  106. title: "请选择课程类别",
  107. duration: 2000,
  108. icon: 'error'
  109. });
  110. return;
  111. }
  112. if (!data.activeProduct) {
  113. uni.showToast({
  114. title: "请选择课程等级",
  115. duration: 2000,
  116. icon: 'error'
  117. });
  118. return;
  119. }
  120. goDAOToStudy();
  121. }
  122. // 跳转 岛 学习
  123. function goDAOToStudy() {
  124. const Product = data.xuekeData.find(item => item.id == data.activeXueke).children.find(item => item.id == data
  125. .activeProduct)
  126. const levelId = Product.levelId;
  127. const typeId = Product.typeId;
  128. const subjectId = Product.subjectId;
  129. if (userCode !== 'Visitor') {
  130. const auth = cacheManager.get('auth');
  131. cacheManager.updateObject('auth', {
  132. typeId: typeId,
  133. levelId: levelId,
  134. subjectId: subjectId,
  135. zhangId: data.activeProduct == auth.levelId && data.activeXueke == auth.cardId ? auth.zhangId :
  136. 0,
  137. currentZhang: 0
  138. })
  139. // 通知岛重新调用接口
  140. cacheManager.remove('daoPageCache')
  141. uni.redirectTo({
  142. url: `/pages/study/index`
  143. })
  144. } else {
  145. if (typeId == 1) {
  146. // 新岛
  147. uni.redirectTo({
  148. url: `/pages/study/index?levelId=${levelId}&typeId=${typeId}&subjectId=${subjectId}&tipFlag=${data.activeTipFlag}`
  149. })
  150. } else {
  151. // 旧岛
  152. uni.redirectTo({
  153. url: `/pages/study/index?levelId=${levelId}&typeId=${typeId}&subjectId=${subjectId}&zhangId=0&tipFlag=${data.activeTipFlag}`
  154. })
  155. }
  156. }
  157. }
  158. function handleBack() {
  159. if (userCode !== 'Visitor') {
  160. if (!data.activeProduct) {
  161. return;
  162. }
  163. uni.redirectTo({
  164. url: `/pages/study/index`
  165. })
  166. } else {
  167. uni.redirectTo({
  168. url: '/pages/login/index'
  169. })
  170. }
  171. }
  172. return {
  173. ...toRefs(data),
  174. handleBack,
  175. // 方法
  176. handleConfirm, // 选择年级+学科
  177. };
  178. }
  179. const {
  180. activeProduct,
  181. activeXueke,
  182. xuekeData,
  183. handleConfirm,
  184. handleBack,
  185. scrollTop
  186. } = useSelectGrade()
  187. const xuekeList = computed(() => {
  188. if (!xuekeData.value.length) {
  189. return []
  190. }
  191. const d_id = activeXueke.value;
  192. return xuekeData.value.find(item => item.id == d_id).children
  193. })
  194. function handleSelectGrade(item) {
  195. activeProduct.value = item.id;
  196. }
  197. function handleSelectXueke(item) {
  198. activeXueke.value = item.id;
  199. activeProduct.value = null;
  200. }
  201. </script>