index.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. if (subjectId == 2 && typeId == 1) {
  148. // 新版英语跳转版本章选择页面
  149. uni.redirectTo({
  150. url: `/pages/selectVersion/selectVersion`
  151. })
  152. } else {
  153. uni.redirectTo({
  154. url: `/pages/study/index`
  155. })
  156. }
  157. } else {
  158. if (typeId == 1) {
  159. if (subjectId == 2) {
  160. // 新版英语 需要选择上下册
  161. uni.redirectTo({
  162. url: `/pages/selectVersion/selectVersion?levelId=${levelId}&typeId=${typeId}&subjectId=${subjectId}&tipFlag=${data.activeTipFlag}`
  163. })
  164. return;
  165. }
  166. // 新岛
  167. uni.redirectTo({
  168. url: `/pages/study/index?levelId=${levelId}&typeId=${typeId}&subjectId=${subjectId}&tipFlag=${data.activeTipFlag}`
  169. })
  170. } else {
  171. // 旧岛
  172. uni.redirectTo({
  173. url: `/pages/study/index?levelId=${levelId}&typeId=${typeId}&subjectId=${subjectId}&zhangId=0&tipFlag=${data.activeTipFlag}`
  174. })
  175. }
  176. }
  177. }
  178. function handleBack() {
  179. if (userCode !== 'Visitor') {
  180. if (!data.activeProduct) {
  181. return;
  182. }
  183. const auth = cacheManager.get('auth');
  184. if (auth.subjectId == 2 && auth.typeId == 1) {
  185. // 新版英语
  186. uni.redirectTo({
  187. url: `/pages/selectVersion/selectVersion`
  188. })
  189. return;
  190. }
  191. uni.redirectTo({
  192. url: `/pages/study/index`
  193. })
  194. } else {
  195. uni.redirectTo({
  196. url: '/pages/login/index'
  197. })
  198. }
  199. }
  200. return {
  201. ...toRefs(data),
  202. handleBack,
  203. // 方法
  204. handleConfirm, // 选择年级+学科
  205. };
  206. }
  207. const {
  208. activeProduct,
  209. activeXueke,
  210. xuekeData,
  211. handleConfirm,
  212. handleBack,
  213. scrollTop
  214. } = useSelectGrade()
  215. const xuekeList = computed(() => {
  216. if (!xuekeData.value.length) {
  217. return []
  218. }
  219. const d_id = activeXueke.value;
  220. return xuekeData.value.find(item => item.id == d_id).children
  221. })
  222. function handleSelectGrade(item) {
  223. activeProduct.value = item.id;
  224. }
  225. function handleSelectXueke(item) {
  226. activeXueke.value = item.id;
  227. activeProduct.value = null;
  228. }
  229. </script>