index.vue 6.2 KB

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