index.vue 6.3 KB

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