index.vue 6.2 KB

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