index.uvue 6.1 KB

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