index.vue 5.6 KB

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