index.uvue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <script setup lang='ts'>
  2. import Back from '@/components/back.uvue'
  3. import Loading from '@/components/loading.uvue'
  4. import { ref, onMounted, nextTick } from 'vue'
  5. import { type SubjectKnowledgeCardResult, getSubjectKnowledgeCard } from '@/services/subject/card'
  6. import { fetchSubjectConfigInfo } from '@/services/subject/info'
  7. import type { SubjectCatalogResult } from '@/services/subject/catalog'
  8. import { config } from '@/config'
  9. import { dict } from '@/.cool/store'
  10. const activeCategory = ref<string>()
  11. const activeTab = ref<string>('knowledge')
  12. const isLoading = ref(true)
  13. const categories = ref<SubjectCatalogResult[]>([])
  14. const cards = ref<SubjectKnowledgeCardResult[]>([])
  15. const cardsScrollView = ref<any>(null)
  16. const categoryRefs = ref<any>()
  17. async function getDataList() {
  18. const id = dict.getValueByLabelMapByType('index_subject_id')['物理']
  19. const res = await getSubjectKnowledgeCard({ subjectId: id })
  20. cards.value = res.userCardList || []
  21. categories.value = res.catalogList || []
  22. }
  23. function handleSelect(categoryId: string) {
  24. activeCategory.value = categoryId
  25. //找到第一个元素上categoryId属性与categoryId相同的元素
  26. const element = categoryRefs.value.find((item: any) => {
  27. return item.dataset.category === categoryId
  28. })
  29. if (element) {
  30. const rect = element.getBoundingClientRect()
  31. console.log(rect)
  32. if (cardsScrollView.value) {
  33. cardsScrollView.value.scrollTo({
  34. top: rect.top - 70, // 减去顶部偏移
  35. animated: true
  36. })
  37. }
  38. }
  39. }
  40. function onScroll(e: any) {
  41. const scrollTop = e.detail.scrollTop
  42. // 遍历所有分类,检查哪个分类在可视区域
  43. for (const category of categories.value) {
  44. const element = categoryRefs.value.find((item: any) => {
  45. return item.dataset.category === category.id
  46. })
  47. if (element) {
  48. const rect = element.getBoundingClientRect()
  49. // 检查元素是否在可视区域内
  50. if (rect.top <= 150 && rect.bottom >= 150) {
  51. activeCategory.value = category.id
  52. break
  53. }
  54. }
  55. }
  56. }
  57. onMounted(async () => {
  58. await getDataList()
  59. isLoading.value = false
  60. nextTick(() => {
  61. handleSelect(categories.value[0].id as string)
  62. })
  63. })
  64. </script>
  65. <template>
  66. <Loading v-show="isLoading" />
  67. <cl-page v-show="!isLoading">
  68. <Back />
  69. <!-- 顶部标题栏 -->
  70. <view class="content">
  71. <!-- 左侧导航菜单 -->
  72. <scroll-view direction="vertical" :show-scrollbar="false" class="sidebar">
  73. <view v-for="category in categories" :key="category.id" class="sidebar-item"
  74. :class="{ active: activeCategory === category.id }" @tap="handleSelect(category.id as string)">
  75. <cl-image :src="config.baseUrl + category?.fileList?.[0]?.url" mode="heightFix" class="!h-[28px]"></cl-image>
  76. <text class="sidebar-text">{{ category.name }}</text>
  77. </view>
  78. </scroll-view>
  79. <!-- 右侧卡片网格 -->
  80. <view class="cards-container">
  81. <view class="header">
  82. <view class="title-tabs">
  83. <view class="tab" :class="{ active: activeTab === 'knowledge' }" @tap="activeTab = 'knowledge'">知识卡
  84. </view>
  85. <view class="tab" :class="{ active: activeTab === 'honor' }" @tap="activeTab = 'honor'">荣誉</view>
  86. </view>
  87. </view>
  88. <scroll-view direction="vertical" :show-scrollbar="false" class="cards" ref="cardsScrollView"
  89. @scroll="onScroll">
  90. <view class="grid grid-cols-5 gap-4">
  91. <view class="card" v-for="card in cards" :key="card.id" :data-category="card.catalogId" ref="categoryRefs">
  92. <image v-if="card.userCardId" :src="config.baseUrl + card.iconPath" class="w-full h-full" />
  93. <image v-else src="https://oss.xiaoxiongcode.com/static/home/21.png" class="w-full h-full" />
  94. <view class="card-title">{{ card.cardName }}</view>
  95. </view>
  96. </view>
  97. </scroll-view>
  98. </view>
  99. </view>
  100. </cl-page>
  101. </template>
  102. <style lang="scss" scoped>
  103. .header {
  104. @apply absolute left-1/2 top-5;
  105. transform: translateX(-50%);
  106. text-align: center;
  107. .title-tabs {
  108. @apply flex flex-row items-center justify-center rounded-full;
  109. background-color: #9AD2FA;
  110. .tab {
  111. @apply rounded-full;
  112. padding: 5px 10px;
  113. font-size: 16px;
  114. transition: all 0.3s ease;
  115. &.active {
  116. background-color: white;
  117. color: #1E88E5;
  118. font-weight: bold;
  119. }
  120. }
  121. }
  122. }
  123. .content {
  124. display: flex;
  125. flex-direction: row;
  126. .sidebar {
  127. width: 200px;
  128. background-color: #116FE9;
  129. padding: 20px;
  130. padding-top: 70px;
  131. height: 100vh;
  132. .sidebar-item {
  133. @apply text-white rounded-full py-1 cursor-pointer flex flex-row items-center;
  134. margin-bottom: 8px;
  135. transition: all 0.3s ease;
  136. font-size: 12px;
  137. width: 100%;
  138. &.active {
  139. background-color: rgba(255, 255, 255, 1);
  140. color: #1E88E5;
  141. font-weight: bold;
  142. }
  143. .sidebar-icon {
  144. font-size: 28px;
  145. margin-right: 2px;
  146. }
  147. }
  148. }
  149. .cards-container {
  150. @apply relative;
  151. flex: 1;
  152. width: 100%;
  153. padding: 20px;
  154. padding-top: 70px;
  155. background: linear-gradient(0deg, #2EB2FD, #0B85F4);
  156. .cards {
  157. height: calc(100vh - 90px);
  158. }
  159. .category-section {
  160. margin-bottom: 40px;
  161. .category-title {
  162. @apply text-white font-bold text-xl mb-4;
  163. }
  164. }
  165. .card {
  166. // background-color: rgba(255, 255, 255, 0.9);
  167. // border-radius: 16px;
  168. // padding: 16px;
  169. display: flex;
  170. flex-direction: column;
  171. align-items: center;
  172. // box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  173. aspect-ratio: 3/4;
  174. .card-content {
  175. width: 100%;
  176. height: 120px;
  177. background-color: #E3F2FD;
  178. border-radius: 12px;
  179. display: flex;
  180. align-items: center;
  181. justify-content: center;
  182. margin-bottom: 12px;
  183. aspect-ratio: 3/4;
  184. .question-mark {
  185. font-size: 60px;
  186. font-weight: bold;
  187. color: #1E88E5;
  188. }
  189. }
  190. .card-title {
  191. @apply text-center font-bold absolute left-0 w-full text-[#0E3E87] text-[1.5vw];
  192. bottom: 13%;
  193. }
  194. }
  195. }
  196. }
  197. </style>