feat:"完成页面接口的对接"
This commit is contained in:
@@ -90,8 +90,8 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="uts">
|
||||
import { getBanners, getHotCases } from '@/api/index.uts'
|
||||
import { SERVICE_TYPES } from '@/utils/config.uts'
|
||||
import { getBanners, getHotCases, getActiveServices } from '@/api/index.uts'
|
||||
import { getServiceTypeName } from '@/utils/config.uts'
|
||||
|
||||
// 轮播图类型
|
||||
type BannerItem = {
|
||||
@@ -146,30 +146,46 @@
|
||||
{ icon: '💰', title: '价格透明', desc: '无隐形消费' }
|
||||
])
|
||||
|
||||
// 初始化服务类型
|
||||
const initServiceTypes = () => {
|
||||
serviceTypes.value = SERVICE_TYPES.map((item) : ServiceType => {
|
||||
return {
|
||||
id: item.id,
|
||||
name: item.name,
|
||||
icon: item.icon
|
||||
} as ServiceType
|
||||
})
|
||||
// 获取服务类型
|
||||
const fetchServiceTypes = async () => {
|
||||
try {
|
||||
const res = await getActiveServices()
|
||||
if (res.code === 0 && res.data != null) {
|
||||
const data = res.data as UTSJSONObject
|
||||
const list = data['list'] as UTSJSONObject[] || []
|
||||
serviceTypes.value = list.map((item) : ServiceType => {
|
||||
return {
|
||||
id: String(item['id']),
|
||||
name: item['name'] as string,
|
||||
icon: item['icon'] as string || '/static/icons/default.png'
|
||||
} as ServiceType
|
||||
})
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('获取服务类型失败', e)
|
||||
// 失败时使用默认数据
|
||||
serviceTypes.value = [
|
||||
{ id: 'repair', name: '局部修复', icon: '/static/icons/repair.png' },
|
||||
{ id: 'refurbish', name: '整体翻新', icon: '/static/icons/refurbish.png' }
|
||||
] as ServiceType[]
|
||||
}
|
||||
}
|
||||
|
||||
// 获取轮播图
|
||||
const fetchBanners = async () => {
|
||||
try {
|
||||
const res = await getBanners()
|
||||
const data = res.data as UTSJSONObject[]
|
||||
bannerList.value = data.map((item) : BannerItem => {
|
||||
return {
|
||||
id: item['id'] as string,
|
||||
image: item['image'] as string,
|
||||
title: item['title'] as string,
|
||||
link: item['link'] as string
|
||||
} as BannerItem
|
||||
})
|
||||
if (res.code === 0 && res.data != null) {
|
||||
const data = res.data as UTSJSONObject[]
|
||||
bannerList.value = data.map((item) : BannerItem => {
|
||||
return {
|
||||
id: item['id'] as string,
|
||||
image: item['image'] as string,
|
||||
title: item['title'] as string,
|
||||
link: item['link'] as string
|
||||
} as BannerItem
|
||||
})
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('获取轮播图失败', e)
|
||||
}
|
||||
@@ -179,23 +195,31 @@
|
||||
const fetchHotCases = async () => {
|
||||
try {
|
||||
const res = await getHotCases()
|
||||
const data = res.data as UTSJSONObject
|
||||
// 适应后端返回格式:{ items: [], total: 0 }
|
||||
const list = data['items'] as UTSJSONObject[] || []
|
||||
hotCases.value = list.map((item) : CaseItem => {
|
||||
return {
|
||||
id: item['id'] as string,
|
||||
title: item['title'] as string,
|
||||
category: item['category'] as string,
|
||||
categoryName: item['categoryName'] as string,
|
||||
coverImage: item['coverImage'] as string,
|
||||
material: item['material'] as string,
|
||||
duration: item['duration'] as string,
|
||||
price: item['price'] as string,
|
||||
views: item['views'] as number,
|
||||
likes: item['likes'] as number
|
||||
} as CaseItem
|
||||
})
|
||||
if (res.code == 0 && res.data != null) {
|
||||
const data = res.data as UTSJSONObject
|
||||
// 适应后端返回格式:{ list: [], total: 0 }
|
||||
const list = data['list'] as UTSJSONObject[] || []
|
||||
hotCases.value = list.map((item) : CaseItem => {
|
||||
// 优先使用images,其次afterImages,最后beforeImages
|
||||
const images = item['images'] as string[] || []
|
||||
const afterImages = item['afterImages'] as string[] || []
|
||||
const beforeImages = item['beforeImages'] as string[] || []
|
||||
const coverImage = images.length > 0 ? images[0] : (afterImages.length > 0 ? afterImages[0] : (beforeImages.length > 0 ? beforeImages[0] : ''))
|
||||
|
||||
return {
|
||||
id: String(item['id']),
|
||||
title: item['title'] as string,
|
||||
category: item['serviceType'] as string || '',
|
||||
categoryName: getServiceTypeName(item['serviceType'] as string),
|
||||
coverImage: coverImage,
|
||||
material: item['materials'] as string || '暂无',
|
||||
duration: (item['duration'] as number || 0) + '天',
|
||||
price: item['price'] != null ? '¥' + item['price'] : '面议',
|
||||
views: item['views'] as number || 0,
|
||||
likes: item['likes'] as number || 0
|
||||
} as CaseItem
|
||||
})
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('获取热门案例失败', e)
|
||||
}
|
||||
@@ -247,15 +271,10 @@
|
||||
const testAPI = async () => {
|
||||
try {
|
||||
console.log('开始API对接测试...')
|
||||
|
||||
// 测试轮播图(使用Mock数据)
|
||||
const bannerRes = await getBanners()
|
||||
console.log('轮播图API响应:', bannerRes)
|
||||
|
||||
// 测试案例列表
|
||||
const caseRes = await getCaseList()
|
||||
console.log('案例列表API响应:', caseRes)
|
||||
|
||||
uni.showToast({
|
||||
title: 'API测试完成,请查看控制台',
|
||||
icon: 'success'
|
||||
@@ -270,7 +289,7 @@
|
||||
}
|
||||
|
||||
onLoad(() => {
|
||||
initServiceTypes()
|
||||
fetchServiceTypes()
|
||||
fetchBanners()
|
||||
fetchHotCases()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user