index.vue 6.5 KB

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