Home.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <template>
  2. <div class="home">
  3. <div class="search-bar">
  4. <el-input
  5. v-model="keyword"
  6. placeholder="搜索闲置物品"
  7. :prefix-icon="Search"
  8. size="large"
  9. clearable
  10. @keyup.enter="onSearch"
  11. />
  12. </div>
  13. <el-tabs v-model="activeCategory" class="category-tabs" @tab-change="onCategoryChange">
  14. <el-tab-pane label="全部" name="" />
  15. <el-tab-pane v-for="cat in categories" :key="cat.id" :label="cat.name" :name="String(cat.id)" />
  16. </el-tabs>
  17. <el-row :gutter="12" class="item-grid">
  18. <el-col
  19. v-for="item in itemList"
  20. :key="item.id"
  21. :xs="24"
  22. :sm="12"
  23. :md="8"
  24. :lg="6"
  25. class="item-col"
  26. >
  27. <el-card :body-style="{ padding: '0px' }" shadow="hover" class="item-card" @click="goDetail(item.id)">
  28. <el-image
  29. :src="item.images?.[0]"
  30. fit="cover"
  31. class="item-image"
  32. >
  33. <template #error>
  34. <div class="image-placeholder">
  35. <el-icon :size="32"><Picture /></el-icon>
  36. </div>
  37. </template>
  38. </el-image>
  39. <div class="item-info">
  40. <h4 class="item-title">{{ item.title }}</h4>
  41. <div class="item-meta">
  42. <span class="price">¥{{ item.price }}</span>
  43. <span class="seller">{{ item.sellerNickname }}</span>
  44. </div>
  45. </div>
  46. </el-card>
  47. </el-col>
  48. </el-row>
  49. <div v-if="loading" class="loading-skeleton">
  50. <el-skeleton :rows="3" animated />
  51. </div>
  52. <div v-if="!loading && itemList.length === 0" class="empty-state">
  53. <el-empty description="暂无物品" />
  54. </div>
  55. <el-pagination
  56. v-if="total > pageSize"
  57. v-model:current-page="pageNum"
  58. :total="total"
  59. :page-size="pageSize"
  60. layout="prev, pager, next"
  61. class="pagination"
  62. @current-change="loadItems"
  63. />
  64. </div>
  65. </template>
  66. <script setup lang="ts">
  67. import { ref, onMounted } from 'vue'
  68. import { useRouter } from 'vue-router'
  69. import { Search, Picture } from '@element-plus/icons-vue'
  70. import { getItems } from '../api/endpoints/items'
  71. import { getCategories } from '../api/endpoints/categories'
  72. import type { ItemVO, Category } from '../api/models'
  73. const router = useRouter()
  74. const keyword = ref('')
  75. const activeCategory = ref('')
  76. const itemList = ref<ItemVO[]>([])
  77. const categories = ref<Category[]>([])
  78. const total = ref(0)
  79. const pageNum = ref(1)
  80. const pageSize = 10
  81. const loading = ref(false)
  82. const loadItems = async () => {
  83. loading.value = true
  84. try {
  85. const categoryId = activeCategory.value ? Number(activeCategory.value) : undefined
  86. const api = getItems()
  87. const params = { pageNum: pageNum.value, pageSize }
  88. const res = categoryId
  89. ? await api.byCategory(categoryId, params)
  90. : await api.list(params)
  91. if (res.code === 200) {
  92. itemList.value = res.data?.list ?? []
  93. total.value = res.data?.total ?? 0
  94. }
  95. } finally {
  96. loading.value = false
  97. }
  98. }
  99. const onSearch = () => {
  100. if (keyword.value.trim()) {
  101. router.push({ path: '/search', query: { keyword: keyword.value.trim() } })
  102. }
  103. }
  104. const onCategoryChange = () => {
  105. pageNum.value = 1
  106. loadItems()
  107. }
  108. const goDetail = (id?: number) => {
  109. if (id) router.push(`/item/${id}`)
  110. }
  111. onMounted(async () => {
  112. await loadItems()
  113. try {
  114. const res = await getCategories().list1()
  115. if (res.code === 200) {
  116. categories.value = res.data ?? []
  117. }
  118. } catch {
  119. // categories optional
  120. }
  121. })
  122. </script>
  123. <style scoped>
  124. .home {
  125. max-width: 1200px;
  126. margin: 0 auto;
  127. padding: 20px;
  128. }
  129. .search-bar {
  130. margin-bottom: 20px;
  131. }
  132. .category-tabs {
  133. margin-bottom: 20px;
  134. }
  135. .item-grid {
  136. margin-bottom: 20px;
  137. }
  138. .item-col {
  139. margin-bottom: 12px;
  140. }
  141. .item-card {
  142. cursor: pointer;
  143. border-radius: 8px;
  144. overflow: hidden;
  145. transition: transform 0.2s;
  146. }
  147. .item-card:hover {
  148. transform: translateY(-2px);
  149. }
  150. .item-image {
  151. width: 100%;
  152. height: 180px;
  153. display: block;
  154. }
  155. .image-placeholder {
  156. height: 180px;
  157. display: flex;
  158. align-items: center;
  159. justify-content: center;
  160. background: #f5f7fa;
  161. color: #c0c4cc;
  162. }
  163. .item-info {
  164. padding: 12px;
  165. }
  166. .item-title {
  167. margin: 0 0 8px;
  168. font-size: 14px;
  169. font-weight: 500;
  170. overflow: hidden;
  171. text-overflow: ellipsis;
  172. white-space: nowrap;
  173. }
  174. .item-meta {
  175. display: flex;
  176. justify-content: space-between;
  177. align-items: center;
  178. }
  179. .price {
  180. color: #f56c6c;
  181. font-size: 16px;
  182. font-weight: 600;
  183. }
  184. .seller {
  185. font-size: 12px;
  186. color: #909399;
  187. }
  188. .loading-skeleton {
  189. padding: 20px;
  190. }
  191. .empty-state {
  192. padding: 40px 0;
  193. }
  194. .pagination {
  195. display: flex;
  196. justify-content: center;
  197. padding: 20px 0;
  198. }
  199. </style>