index.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <template>
  2. <view class="container">
  3. <view class="filter-section">
  4. <scroll-view scroll-x class="type-tabs">
  5. <view class="tab-row">
  6. <view
  7. v-for="item in typeList"
  8. :key="item.value"
  9. :class="['tab-item', currentType === item.value ? 'active' : '']"
  10. @click="onTypeChange(item.value)"
  11. >
  12. {{ item.label }}
  13. </view>
  14. </view>
  15. </scroll-view>
  16. <scroll-view scroll-x class="domain-tabs">
  17. <view class="tab-row">
  18. <view
  19. v-for="item in domainList"
  20. :key="item.value"
  21. :class="['tab-item small', currentDomain === item.value ? 'active' : '']"
  22. @click="onDomainChange(item.value)"
  23. >
  24. {{ item.label }}
  25. </view>
  26. </view>
  27. </scroll-view>
  28. </view>
  29. <scroll-view
  30. scroll-y
  31. class="product-list"
  32. @scrolltolower="onLoadMore"
  33. refresher-enabled
  34. :refresher-triggered="refreshing"
  35. @refresherrefresh="onRefresh"
  36. >
  37. <view v-if="loading && products.length === 0" class="loading-wrap">
  38. <text class="loading-text">加载中...</text>
  39. </view>
  40. <view v-else-if="products.length === 0" class="empty-wrap">
  41. <text class="empty-icon">📦</text>
  42. <text class="empty-text">暂无商品</text>
  43. </view>
  44. <view v-else class="grid">
  45. <view
  46. v-for="item in products"
  47. :key="item.id"
  48. class="product-card"
  49. @click="goDetail(item.id)"
  50. >
  51. <image
  52. class="cover"
  53. :src="item.coverImage || '/static/default-product.png'"
  54. mode="aspectFill"
  55. />
  56. <view class="card-body">
  57. <text class="product-name">{{ item.name }}</text>
  58. <view class="price-row">
  59. <text class="price">¥{{ item.price }}</text>
  60. <text v-if="item.memberPrice" class="member-price">会员¥{{ item.memberPrice }}</text>
  61. </view>
  62. <text class="vendor-name">{{ item.vendorName || '平台官方' }}</text>
  63. </view>
  64. </view>
  65. </view>
  66. <view v-if="loadingMore" class="loading-more">
  67. <text class="loading-text">加载中...</text>
  68. </view>
  69. <view v-if="noMore && products.length > 0" class="no-more">
  70. <text class="no-more-text">— 没有更多了 —</text>
  71. </view>
  72. </scroll-view>
  73. </view>
  74. </template>
  75. <script>
  76. import { productList } from '@/utils/api.js'
  77. export default {
  78. data() {
  79. return {
  80. typeList: [
  81. { label: '全部', value: '' },
  82. { label: '活动', value: 'activity' },
  83. { label: '课程', value: 'course' },
  84. { label: '实物', value: 'physical' },
  85. { label: '数字', value: 'digital' },
  86. { label: '服务', value: 'service' }
  87. ],
  88. domainList: [
  89. { label: '全部', value: '' },
  90. { label: '行动', value: 'action' },
  91. { label: '心智', value: 'mind' },
  92. { label: '身体', value: 'body' },
  93. { label: '智慧', value: 'wisdom' },
  94. { label: '财富', value: 'wealth' }
  95. ],
  96. currentType: '',
  97. currentDomain: '',
  98. products: [],
  99. page: 1,
  100. size: 20,
  101. loading: false,
  102. loadingMore: false,
  103. refreshing: false,
  104. noMore: false
  105. }
  106. },
  107. onLoad() {
  108. this.loadProducts()
  109. },
  110. methods: {
  111. onTypeChange(value) {
  112. this.currentType = value
  113. this.page = 1
  114. this.products = []
  115. this.noMore = false
  116. this.loadProducts()
  117. },
  118. onDomainChange(value) {
  119. this.currentDomain = value
  120. this.page = 1
  121. this.products = []
  122. this.noMore = false
  123. this.loadProducts()
  124. },
  125. loadProducts() {
  126. if (this.loading) return
  127. this.loading = true
  128. const params = {
  129. page: this.page,
  130. size: this.size
  131. }
  132. if (this.currentType) {
  133. params.productType = this.currentType
  134. }
  135. if (this.currentDomain) {
  136. params.domain = this.currentDomain
  137. }
  138. productList(params).then(res => {
  139. this.loading = false
  140. this.refreshing = false
  141. if (res.code === 200 && res.data) {
  142. const records = res.data.records || []
  143. if (this.page === 1) {
  144. this.products = records
  145. } else {
  146. this.products = this.products.concat(records)
  147. }
  148. const total = res.data.total || 0
  149. this.noMore = this.products.length >= total
  150. }
  151. }).catch(() => {
  152. this.loading = false
  153. this.refreshing = false
  154. })
  155. },
  156. onRefresh() {
  157. this.refreshing = true
  158. this.page = 1
  159. this.noMore = false
  160. this.loadProducts()
  161. },
  162. onLoadMore() {
  163. if (this.noMore || this.loadingMore) return
  164. this.page = this.page + 1
  165. this.loadingMore = true
  166. const params = {
  167. page: this.page,
  168. size: this.size
  169. }
  170. if (this.currentType) {
  171. params.productType = this.currentType
  172. }
  173. if (this.currentDomain) {
  174. params.domain = this.currentDomain
  175. }
  176. productList(params).then(res => {
  177. this.loadingMore = false
  178. if (res.code === 200 && res.data) {
  179. const records = res.data.records || []
  180. this.products = this.products.concat(records)
  181. const total = res.data.total || 0
  182. this.noMore = this.products.length >= total
  183. }
  184. }).catch(() => {
  185. this.loadingMore = false
  186. })
  187. },
  188. goDetail(id) {
  189. uni.navigateTo({
  190. url: '/pages/discover/product-detail/product-detail?id=' + id
  191. })
  192. }
  193. }
  194. }
  195. </script>
  196. <style scoped>
  197. .container {
  198. display: flex;
  199. flex-direction: column;
  200. height: 100vh;
  201. background: #f5f5f5;
  202. }
  203. .filter-section {
  204. background: #fff;
  205. padding: 20rpx 0 10rpx;
  206. border-bottom: 1rpx solid #eee;
  207. position: sticky;
  208. top: 0;
  209. z-index: 10;
  210. }
  211. .type-tabs {
  212. white-space: nowrap;
  213. margin-bottom: 16rpx;
  214. }
  215. .tab-row {
  216. display: flex;
  217. padding: 0 20rpx;
  218. }
  219. .tab-item {
  220. display: inline-block;
  221. padding: 12rpx 28rpx;
  222. font-size: 28rpx;
  223. color: #666;
  224. margin-right: 16rpx;
  225. border-radius: 40rpx;
  226. background: #f0f0f0;
  227. flex-shrink: 0;
  228. }
  229. .tab-item.small {
  230. padding: 8rpx 20rpx;
  231. font-size: 24rpx;
  232. margin-right: 12rpx;
  233. }
  234. .tab-item.active {
  235. background: #F97316;
  236. color: #fff;
  237. }
  238. .product-list {
  239. flex: 1;
  240. height: calc(100vh - 200rpx);
  241. }
  242. .grid {
  243. display: flex;
  244. flex-wrap: wrap;
  245. padding: 20rpx;
  246. justify-content: space-between;
  247. }
  248. .product-card {
  249. width: 49%;
  250. background: #fff;
  251. border-radius: 16rpx;
  252. overflow: hidden;
  253. margin-bottom: 20rpx;
  254. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.06);
  255. }
  256. .cover {
  257. width: 100%;
  258. height: 320rpx;
  259. background: #f0f0f0;
  260. }
  261. .card-body {
  262. padding: 16rpx;
  263. }
  264. .product-name {
  265. display: block;
  266. font-size: 28rpx;
  267. color: #333;
  268. lines: 2;
  269. overflow: hidden;
  270. text-overflow: ellipsis;
  271. display: -webkit-box;
  272. -webkit-line-clamp: 2;
  273. -webkit-box-orient: vertical;
  274. margin-bottom: 12rpx;
  275. }
  276. .price-row {
  277. display: flex;
  278. align-items: baseline;
  279. margin-bottom: 8rpx;
  280. }
  281. .price {
  282. font-size: 32rpx;
  283. color: #F97316;
  284. font-weight: bold;
  285. margin-right: 12rpx;
  286. }
  287. .member-price {
  288. font-size: 22rpx;
  289. color: #999;
  290. }
  291. .vendor-name {
  292. font-size: 22rpx;
  293. color: #999;
  294. }
  295. .loading-wrap,
  296. .empty-wrap {
  297. display: flex;
  298. flex-direction: column;
  299. align-items: center;
  300. padding-top: 200rpx;
  301. }
  302. .loading-text {
  303. font-size: 26rpx;
  304. color: #999;
  305. }
  306. .empty-icon {
  307. font-size: 100rpx;
  308. margin-bottom: 30rpx;
  309. }
  310. .empty-text {
  311. font-size: 28rpx;
  312. color: #999;
  313. }
  314. .loading-more,
  315. .no-more {
  316. text-align: center;
  317. padding: 30rpx;
  318. }
  319. .loading-text {
  320. font-size: 24rpx;
  321. color: #999;
  322. }
  323. .no-more-text {
  324. font-size: 24rpx;
  325. color: #ccc;
  326. }
  327. </style>