feat: 首页猜你喜欢

This commit is contained in:
jqtmviyu 2025-04-21 13:22:18 +08:00
parent 96d1273364
commit ce5572de06
5 changed files with 193 additions and 5 deletions

153
src/components/JbcGuess.vue Normal file
View File

@ -0,0 +1,153 @@
<template>
<!-- 猜你喜欢 -->
<view class="caption">
<text class="text">猜你喜欢</text>
</view>
<view class="guess">
<navigator
class="guess-item"
v-for="item in guessList"
:key="item.id"
:url="`/pages/goods/goods?id=${item.id}`"
>
<image class="image" mode="aspectFill" :src="item.picture"></image>
<view class="name"> {{ item.name }} </view>
<view class="price">
<text class="small">¥</text>
<text>{{ item.price }}</text>
</view>
</navigator>
</view>
<view class="loading-text">
{{ finish ? '没有更多数据~' : '正在加载...' }}
</view>
</template>
<script setup lang="ts">
import type { GuessItem } from '@/types/home'
import { getGuessLike } from '@/services/home'
import { onBeforeMount, ref, defineExpose } from 'vue'
import type { PageParams } from '@/types/global'
//
const pageParams: Required<PageParams> = {
page: 1,
pageSize: 10,
}
//
const guessList = ref<GuessItem[]>([])
//
const finish = ref(false)
const getGuessList = async () => {
// 退
if (finish.value === true) {
return uni.showToast({ icon: 'none', title: '没有更多数据~' })
}
const res = await getGuessLike(pageParams)
//
guessList.value.push(...res.result.items)
//
if (res.result.page < res.result.pages) {
pageParams.page++
} else {
finish.value = true
}
}
//
const resetData = () => {
pageParams.page = 1
guessList.value = []
finish.value = false
}
//
onBeforeMount(() => {
getGuessList()
})
//
defineExpose({
resetData,
getMore: getGuessList,
})
</script>
<style lang="scss">
:host {
display: block;
}
/* 分类标题 */
.caption {
display: flex;
justify-content: center;
line-height: 1;
padding: 36rpx 0 40rpx;
font-size: 32rpx;
color: #262626;
.text {
display: flex;
justify-content: center;
align-items: center;
padding: 0 28rpx 0 30rpx;
&::before,
&::after {
content: '';
width: 20rpx;
height: 20rpx;
background-image: url(@/static/images/bubble.png);
background-size: contain;
margin: 0 10rpx;
}
}
}
/* 猜你喜欢 */
.guess {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
padding: 0 20rpx;
.guess-item {
width: calc(375rpx - 60rpx);
padding: 24rpx 20rpx 20rpx;
margin-bottom: 20rpx;
border-radius: 10rpx;
overflow: hidden;
background-color: #fff;
}
.image {
width: 304rpx;
height: 304rpx;
}
.name {
height: 75rpx;
margin: 10rpx 0;
font-size: 26rpx;
color: #262626;
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
}
.price {
line-height: 1;
padding-top: 4rpx;
color: #cf4444;
font-size: 26rpx;
}
.small {
font-size: 80%;
}
}
//
.loading-text {
text-align: center;
font-size: 28rpx;
color: #666;
padding: 20rpx 0;
}
</style>

View File

@ -45,6 +45,7 @@ defineProps<{
font-size: 32rpx;
color: #262626;
position: relative;
.title-desc {
font-size: 24rpx;
color: #7f7f7f;
@ -59,20 +60,25 @@ defineProps<{
height: 254rpx;
border-right: 1rpx solid #eee;
border-top: 1rpx solid #eee;
.title {
justify-content: start;
}
&:nth-child(2n) {
border-right: 0 none;
}
&:nth-child(-n + 2) {
border-top: 0 none;
}
.image {
width: 150rpx;
height: 150rpx;
}
}
.cards {
flex: 1;
padding: 15rpx 20rpx;

View File

@ -3,13 +3,15 @@
<!-- 自定义导航栏 -->
<CustomNavbar />
<!-- 滚动容器 -->
<scroll-view scroll-y class="scrool-view" refresher-enabled>
<scroll-view scroll-y enable-back-to-top class="scrool-view" @scrolltolower="onScrollToLower">
<!-- 轮播图 -->
<JbcSwiper :list="bannerList" />
<!-- 分类 -->
<CategoryPanel :list="categoryList" />
<!-- 热门 -->
<HotPanel :list="hotList" />
<!-- 猜你喜欢 -->
<JbcGuess ref="guessRef" />
</scroll-view>
</view>
</template>
@ -22,6 +24,7 @@ import { getCategory, getHomeBanner, getHomeHot } from '@/services/home'
import { onLoad } from '@dcloudio/uni-app'
import CategoryPanel from './components/CategoryPanel.vue'
import HotPanel from './components/HotPanel.vue'
import type { JbcGuessInstance } from '@/types/components'
//
const bannerList = ref<BannerItem[]>([])
@ -42,8 +45,17 @@ const hotList = ref<HotItem[]>([])
const getHotList = async () => {
const res = await getHomeHot()
hotList.value = res.result
console.log('hotList: ', hotList.value)
}
//
const guessRef = ref<JbcGuessInstance>()
//
const onScrollToLower = () => {
guessRef.value?.getMore()
}
//
onLoad(() => {
getBannerList()
getCategoryList()
@ -59,12 +71,12 @@ page {
}
.viewport {
height: 100%;
display: flex;
flex-direction: column;
height: 100%;
}
.scroll-view {
.scrool-view {
flex: 1;
overflow: hidden;
}

View File

@ -1,4 +1,5 @@
import type { BannerItem, CategoryItem, HotItem } from '@/types/home'
import type { PageParams, PageResult } from '@/types/global'
import type { BannerItem, CategoryItem, GuessItem, HotItem } from '@/types/home'
import { http } from '@/utils/http'
/**
@ -37,3 +38,16 @@ export const getHomeHot = () => {
method: 'GET',
})
}
/**
*
* @param data -
* @returns
*/
export const getGuessLike = (data?: PageParams) => {
return http<PageResult<GuessItem>>({
url: '/home/goods/guessLike',
method: 'GET',
data,
})
}

View File

@ -1,10 +1,13 @@
import JbcSwiper from '@/components/JbcSwiper.vue'
import JbcGuess from '@/components/JbcGuess.vue'
declare module 'vue' {
export interface GlobalComponents {
JbcSwiper: typeof JbcSwiper
JbcGuess: typeof JbcGuess
}
}
// 组件实例类型
export type JbcSwiperInstance = InstanceType<typeof JbcSwiper>
export type JbcGuessInstance = InstanceType<typeof JbcGuess>