index.vue 5.7 KB

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