index.vue 6.2 KB

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