您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

微信小程序商城开发之动态API实现商品的详情页的代码(上)

2025/4/20 15:44:14发布15次查看
本篇文章给大家带来的内容是关于微信小程序商城开发之动态api实现商品的详情页的代码(上) ,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
看效果
开发计划1、实现商品详情页面布局(这篇实现3个模块,头部商品图片轮播、商品价格和商品描述、商品详情展示)
2、根据用户点击不同的商品请求api动态加载数据
根据商品id获取商品详情api数据模型访问:https://100boot.cn/ 选择微商城案例,如下图所示:
下方还有详细的数据模型可以查看哦!
home.js 增加跳转商品详情事件上一篇还记得我们做了商品点击的事件采集吗?那么再加上跳转商品详情页功能,如下图所示:
detail.wxml<swiper indicator-dots="{{indicatordots}}" autoplay="{{autoplay}}" interval="{{interval}}" duration="{{duration}}"> <block wx:for="{{goods.imgurls}}"> <swiper-item> <image src="{{item}}" src="{{item}}" bindtap="previewimage" mode="widthfix"></image> </swiper-item> </block></swiper><!--商品价格和商品描述--><view><view class="product-name-wrap"> {{goods.title}} </view> <view class="product-price-wrap"> <view> <p class="product-price-new">¥{{goods.price}}</p> <p class="product-price-old">原价¥{{goods.privilegeprice}}</p> </view> </view></view> <view class="details"> <scroll-view scroll-y="true"> <text>商品详情</text> <block wx:for-items="{{goods.detailimg}}" wx:key="name"> <image class="image_detail" src="{{item}}" mode="widthfix"/> </block> <view class="temp"></view> </scroll-view> </view>
detail.wxsspage { display: flex; flex-direction: column; height: 100%;}/* 直接设置swiper属性 */swiper { /* height: 500rpx; */ height: 750rpx;}swiper-item image { width: 100%; height: 100%;}/**商品价格**/.product-price-wrap{ display: flex; justify-content:space-between;/**两边对齐**/ flex-direction: row; flex-wrap: wrap; margin:5px 5px; /* border:1rpx solid red; */} .product-price-wrap .product-price-new{ color: red; font-size: 40rpx; margin: 10rpx;}.product-price-wrap .product-price-old{ color: #888; text-decoration: line-through; padding-left: 5px; font-size: 12px; line-height:30px; font-weight:300;}.product-name-wrap{ margin: 0px 10px; font-size: 14px; color: #404040;}.details{ padding: 0 5px 0 5px; }.detail { display: flex; flex-direction: column; margin-top: 15rpx; margin-bottom: 0rpx; }.detail .title { font-size: 40rpx; margin: 10rpx; color: black; text-align: justify; height: 100rpx;}.detail .price { color: red; font-size: 40rpx; margin: 10rpx;}.line_flag { width: 80rpx; height: 1rpx; display: inline-block; margin: 20rpx auto; background-color: gainsboro; text-align: center;}.line { width: 100%; height: 2rpx; display: inline-block; margin: 20rpx 0rpx; background-color: gainsboro; text-align: center;}.detail-nav { display: flex; flex-direction: row; align-items: center; float: left; background-color: #fff; position: fixed; bottom: 0; right: 0; z-index: 1; width: 100%; height: 100rpx;}.button-green { background-color: #4caf50; /* green */}.button-red { background-color: #f44336; /* 红色 */}.button-addcar { background-color: #f44336; /* 红色 */ width: 100%;}.image_detail { width: 100%; /* height: 750rpx; */}.detail-nav image { width: 70rpx; height: 50rpx; margin: 20rpx 40rpx;}.line_nav { width: 5rpx; height: 100%; background-color: gainsboro;}/* 占位 */.temp { height: 100rpx;}
detail.jsconst ajax = require('../../utils/ajax.js');const utils = require('../../utils/util.js');var imgurls = []; var detailimg = [];var goodsid = null;var goods = null;page({ /** * 页面的初始数据 */ data: { islike: true, showdialog: false, goods:null, indicatordots: true, //是否显示面板指示点 autoplay: true, //是否自动切换 interval: 3000, //自动切换时间间隔,3s duration: 1000, // 滑动动画时长1s }, //预览图片 previewimage: function (e) { var current = e.target.dataset.src; wx.previewimage({ current: current, // 当前显示图片的http链接 urls: this.data.imgurls // 需要预览的图片http链接列表 }) }, /** * 生命周期函数--监听页面加载 */ onload: function (options) { var that = this; goodsid = options.goodsid; console.log('goodsid:' + goodsid); //加载商品详情 that.goodsinfoshow(); }, goodsinfoshow: function (success) { var that = this; ajax.request({ method: 'get', url: 'goods/getgoodsinfo?key=' + utils.key+'&goodsid=' + goodsid, success: data => { var goodsitem = data.result; for (var i = 0; i < goodsitem.shopgoodsimagelist.length; i++) { imgurls[i] = goodsitem.shopgoodsimagelist[i].imgurl; } var details = goodsitem.details.split(";"); for (var j = 0; j < details.length; j++) { detailimg[j] = details[j]; } goods = { imgurls: imgurls, title: goodsitem.name, price: goodsitem.price, privilegeprice: goodsitem.privilegeprice, detailimg: detailimg, imgurl: goodsitem.imgurl, buyrate: goodsitem.buyrate, goodsid: goodsid, count:1, totalmoney: goodsitem.price, } that.setdata({ goods : goods }) console.log(goods.title) } }) },})
相关推荐:
微信小程序商城开发之https框架的搭建以及顶部和底部导航的实现
微信小程序商城开发之商城首页福利场不限下拉刷新动态api数据的代码实现
以上就是微信小程序商城开发之动态api实现商品的详情页的代码(上)的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product