index.uvue 6.1 KB

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