index.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. data.activeProduct = productObj.id;
  86. data.activeXueke = xuekeObj.id;
  87. } else {
  88. data.activeProduct = null;
  89. data.activeXueke = 1
  90. }
  91. setTimeout(() => {
  92. // 滚动到某个元素显示
  93. data.scrollTop = `s_${data.activeProduct}`
  94. }, 100)
  95. })
  96. }
  97. // 选择 年级+学期
  98. function handleConfirm() {
  99. if (!data.activeXueke) {
  100. uni.showToast({
  101. title: "请选择课程类别",
  102. duration: 2000,
  103. icon: 'error'
  104. });
  105. return;
  106. }
  107. if (!data.activeProduct) {
  108. uni.showToast({
  109. title: "请选择课程等级",
  110. duration: 2000,
  111. icon: 'error'
  112. });
  113. return;
  114. }
  115. goDAOToStudy();
  116. }
  117. // 跳转 岛 学习
  118. function goDAOToStudy() {
  119. const Product = data.xuekeData.find(item => item.id == data.activeXueke).children.find(item => item.id == data
  120. .activeProduct)
  121. const levelId = Product.levelId;
  122. const typeId = Product.typeId;
  123. const subjectId = Product.subjectId;
  124. if (userCode !== 'Visitor') {
  125. cacheManager.updateObject('auth', {
  126. typeId: typeId,
  127. levelId: levelId,
  128. subjectId: subjectId,
  129. })
  130. // 通知岛重新调用接口
  131. cacheManager.remove('zhangInfo')
  132. if (subjectId == 2 && typeId == 1) {
  133. // 新版英语跳转版本章选择页面
  134. uni.redirectTo({
  135. url: `/pages/selectVersion/selectVersion`
  136. })
  137. } else {
  138. uni.redirectTo({
  139. url: `/pages/study/index`
  140. })
  141. }
  142. } else {
  143. if (typeId == 1) {
  144. if (subjectId == 2) {
  145. // 新版英语 需要选择上下册
  146. uni.redirectTo({
  147. url: `/pages/selectVersion/selectVersion?levelId=${levelId}&typeId=${typeId}&subjectId=${subjectId}&tipFlag=${data.activeTipFlag}`
  148. })
  149. return;
  150. }
  151. // 新岛
  152. uni.redirectTo({
  153. url: `/pages/study/index?levelId=${levelId}&typeId=${typeId}&subjectId=${subjectId}&tipFlag=${data.activeTipFlag}`
  154. })
  155. } else {
  156. // 旧岛
  157. uni.redirectTo({
  158. url: `/pages/study/index?levelId=${levelId}&typeId=${typeId}&subjectId=${subjectId}&zhangId=0&tipFlag=${data.activeTipFlag}`
  159. })
  160. }
  161. }
  162. }
  163. function handleBack() {
  164. if (userCode !== 'Visitor') {
  165. const auth = cacheManager.get('auth');
  166. if (!data.activeProduct && !auth.levelId) {
  167. uni.showToast({
  168. title:'请选择内容'
  169. })
  170. return;
  171. }
  172. if (auth.subjectId == 2 && auth.typeId == 1) {
  173. // 新版英语
  174. uni.redirectTo({
  175. url: `/pages/selectVersion/selectVersion`
  176. })
  177. return;
  178. }
  179. uni.redirectTo({
  180. url: `/pages/study/index`
  181. })
  182. } else {
  183. uni.redirectTo({
  184. url: '/pages/login/index'
  185. })
  186. }
  187. }
  188. return {
  189. ...toRefs(data),
  190. handleBack,
  191. // 方法
  192. handleConfirm, // 选择年级+学科
  193. };
  194. }
  195. const {
  196. activeProduct,
  197. activeXueke,
  198. xuekeData,
  199. handleConfirm,
  200. handleBack,
  201. scrollTop
  202. } = useSelectGrade()
  203. const xuekeList = computed(() => {
  204. if (!xuekeData.value.length) {
  205. return []
  206. }
  207. const d_id = activeXueke.value;
  208. return xuekeData.value.find(item => item.id == d_id).children
  209. })
  210. function handleSelectGrade(item) {
  211. activeProduct.value = item.id;
  212. }
  213. function handleSelectXueke(item) {
  214. activeXueke.value = item.id;
  215. activeProduct.value = null;
  216. }
  217. </script>