這篇文章帶給大家的內容是關於微信小程式商城開發之動態API實現商品的詳情頁的程式碼(上) ,有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。
1、實作商品詳情頁面佈局(這篇實現3個模組,頭部商品圖片輪播、商品價格和商品描述、商品詳情展示)
2、根據用戶點擊不同的商品請求API動態加載數據
存取:https://100boot.cn/ 選擇微商城案例,如下圖所示:
下方還有詳細的資料模型可以查看!
上一篇還記得我們做了商品點擊查看詳情的事件收集嗎?那麼再加上跳轉商品詳情頁功能,如下圖:
<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>
page { 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; }
const 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實現商品的詳情頁的程式碼(上)的詳細內容。更多資訊請關注PHP中文網其他相關文章!