商品详情1

master
fjc 7 months ago
parent 5f902c23ec
commit cbb34a5085

@ -1,75 +1,92 @@
const { getGoodsDetails,addGoodsCart } = require("../../api/index.js") const { getGoodsDetails, addGoodsCart } = require("../../api/index.js");
Page({ Page({
/** /**
* 页面的初始数据 * 页面的初始数据
*/ */
data: { data: {
goodsDetails:{} goodsDetails: {} // 存储商品详情数据
}, },
/** /**
* 生命周期函数--监听页面加载 * 生命周期函数--监听页面加载
* @param {Object} options - 页面加载时传递的参数
*/ */
onLoad(options) { onLoad(options) {
// 提示用户在获取数据 // 显示加载提示
wx.showLoading({ wx.showLoading({
title: '等待数据加载...', title: '等待数据加载...',
}) });
getGoodsDetails({id:options.id}).then(res =>{
wx.hideLoading() // 获取商品详情数据
if(res.data.status === 200){ getGoodsDetails({ id: options.id }).then(res => {
wx.hideLoading(); // 隐藏加载提示
if (res.data.status === 200) {
// 设置商品详情数据
this.setData({ this.setData({
goodsDetails:res.data.data[0] goodsDetails: res.data.data[0]
}) });
}else{ } else {
// 显示数据获取失败的提示
wx.showToast({ wx.showToast({
title: '数据获取失败', title: '数据获取失败',
icon:"success" icon: "none" // 修改为 "none",因为 "success" 不适合表示错误
}) });
} }
}) });
}, },
/** /**
* 客服 * 客服点击事件
*/ */
onClickKF(){}, onClickKF() {
// 客服功能实现
},
/** /**
* 购物车 * 购物车点击事件
*/ */
onClickCart(){ onClickCart() {
// 跳转到购物车页面
wx.switchTab({ wx.switchTab({
url: '/pages/cart/cart', url: '/pages/cart/cart',
}) });
}, },
/** /**
* 加入购物车 * 加入购物车点击事件
*/ */
onClickAddCart(){ onClickAddCart() {
// 添加商品到购物车
addGoodsCart({ addGoodsCart({
title:this.data.goodsDetails.title, title: this.data.goodsDetails.title, // 商品标题
price:this.data.goodsDetails.price, price: this.data.goodsDetails.price, // 商品价格
image:this.data.goodsDetails.topimage, image: this.data.goodsDetails.topimage, // 商品图片
currentID:this.data.goodsDetails.id currentID: this.data.goodsDetails.id // 商品ID
}).then(res =>{ }).then(res => {
if(res.data.status === 200){ if (res.data.status === 200) {
// 显示添加成功提示
wx.showToast({ wx.showToast({
title: res.data.msg, title: res.data.msg,
}) });
}else{ } else {
// 显示添加失败提示
wx.showToast({ wx.showToast({
title: res.data.msg, title: res.data.msg,
}) });
} }
}) });
}, },
/** /**
* 立即购买 * 立即购买点击事件
* @param {Object} e - 事件对象
*/ */
onClickBuy(e){ onClickBuy(e) {
// 跳转到购买页面并传递商品ID
wx.navigateTo({ wx.navigateTo({
url: '/pages/buy/buy?id='+e.currentTarget.dataset.id, url: '/pages/buy/buy?id=' + e.currentTarget.dataset.id,
}) });
} }
}) });

@ -1,22 +1,41 @@
<!-- 商品详情页面的根容器 -->
<view class="details"> <view class="details">
<!-- 商品顶部图片,使用宽度自适应模式 -->
<image class="topimage" mode="widthFix" src="{{ goodsDetails.topimage }}"></image> <image class="topimage" mode="widthFix" src="{{ goodsDetails.topimage }}"></image>
<!-- 商品基本信息容器 -->
<view class="content"> <view class="content">
<!-- 商品价格显示 -->
<view class="price"> <view class="price">
¥<text>{{ goodsDetails.price }}</text>.00 ¥<text>{{ goodsDetails.price }}</text>.00
</view> </view>
<!-- 商品标题显示 -->
<view class="title"> <view class="title">
<text>{{ goodsDetails.title }}</text> <text>{{ goodsDetails.title }}</text>
</view> </view>
</view> </view>
<!-- 商品详细介绍图片,使用宽度自适应模式 -->
<view class="introduce"> <view class="introduce">
<image mode="widthFix" src="{{ goodsDetails.details }}"></image> <image mode="widthFix" src="{{ goodsDetails.details }}"></image>
</view> </view>
<!-- 商品操作栏容器 -->
<view class="cart"> <view class="cart">
<!-- Vant 商品操作栏组件 -->
<van-goods-action> <van-goods-action>
<!-- 客服图标按钮,点击时触发 onClickKF 事件 -->
<van-goods-action-icon icon="chat-o" text="客服" bind:click="onClickKF" /> <van-goods-action-icon icon="chat-o" text="客服" bind:click="onClickKF" />
<!-- 购物车图标按钮,点击时触发 onClickCart 事件 -->
<van-goods-action-icon icon="cart-o" text="购物车" bind:click="onClickCart" /> <van-goods-action-icon icon="cart-o" text="购物车" bind:click="onClickCart" />
<!-- 加入购物车按钮,点击时触发 onClickAddCart 事件 -->
<van-goods-action-button text="加入购物车" type="warning" bind:click="onClickAddCart" /> <van-goods-action-button text="加入购物车" type="warning" bind:click="onClickAddCart" />
<!-- 立即购买按钮,点击时触发 onClickBuy 事件,并传递商品 ID -->
<van-goods-action-button data-id="{{ goodsDetails.id }}" text="立即购买" bind:click="onClickBuy" /> <van-goods-action-button data-id="{{ goodsDetails.id }}" text="立即购买" bind:click="onClickBuy" />
</van-goods-action> </van-goods-action>
</view> </view>
</view> </view>

@ -1,32 +1,39 @@
.details{ /* 商品详情页面的整体样式 */
background: #fff; .details {
background: #fff; /* 设置背景颜色为白色 */
} }
.details .topimage{ /* 商品顶部图片的样式 */
width: 100%; .details .topimage {
width: 100%; /* 图片宽度占满父容器 */
} }
.details .content{ /* 商品基本信息容器的样式 */
margin: 10px; .details .content {
margin: 10px; /* 设置外边距为 10px */
} }
/* 商品价格的样式 */
.details .content .price { .details .content .price {
color: #f2270c; color: #f2270c; /* 设置价格文字颜色为红色 */
} }
.details .content .price text{ /* 商品价格中的文本样式 */
color: #f2270c; .details .content .price text {
display: inline-block; color: #f2270c; /* 设置价格文字颜色为红色 */
font-family: JDZH-Regular; display: inline-block; /* 使文本块级显示,方便设置其他样式 */
font-size: 20px; font-family: JDZH-Regular; /* 设置字体为 JDZH-Regular */
line-height: 30px; font-size: 20px; /* 设置字体大小为 20px */
line-height: 30px; /* 设置行高为 30px */
} }
.details .content .title{ /* 商品标题的样式 */
font-size: 15px; .details .content .title {
font-weight: 700; font-size: 15px; /* 设置字体大小为 15px */
font-weight: 700; /* 设置字体加粗 */
} }
.details .introduce image{ /* 商品详细介绍图片的样式 */
width: 100%; .details .introduce image {
} width: 100%; /* 图片宽度占满父容器 */
}

Loading…
Cancel
Save