微信小程序商城开发中商城首页福利场不限下拉刷新动态 API 数据代码实现方法

why 282 2024-08-06

本篇文章给大家带来的内容是关于微信小程序商城开发之商城首页福利场不限下拉刷新动态API数据的代码实现 ,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

效果展示

话不多说,直接看实现效果:

福利专场商品无限下拉展示.gif

电商首页回顾

image.png

功能模块.jpg

如上图所示,电商首页只剩下第五个模块-福利专场,福利专场是商品列表集合 我们将采用无限下来刷新的方式来实现改业务。

福利专场数据模型

image.png

福利专场API.png

福利专场API详情可以通过访问 https://100boot- 选择案例查看。

home.wxml

福利专场商品列表布局

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

<!--福利专场 start-->

  <view class="welfare-container">

  <view class="category-title">

    <text class="title">福利专场</text>

    <view class="line_name">

      <text>每天早10晚8准时上新</text>

    </view>

  </view> 

  <scroll-view scroll-y="true">

      <view class="welfares">

         <block wx:for-items="{{newGoods}}" wx:key="id">

          <view class="welfares-good" catchtap="catchTapCategory" data-name="{{item.name}}" data-goodsid="{{item.id}}">

            <view>

              <image src="{{item.imgUrl}}" class="welfares-image" mode="widthFix"/>

            </view>

            <view class="product-name">

              {{item.name}}            </view>

            <view class="product-price-wrap">

              <p class="product-price-new">¥{{item.price}}</p> 

              <p class="product-price-old">¥{{item.privilegePrice}}</p>

              <p class="discount">{{item.discount}}折</p> 

            </view>

             

          </view>

        </block>

      </view>

  </scroll-view>  </view>   <!--福利专场 end-->

福利专场商品列表下拉加载更多布局

1

2

3

4

<view class="weui-loadmore" hidden="{{hidden}}">

   <view class="weui-loading"></view>

   <view class="weui-loadmore__tips">努力加载中</view>

 </view>

home.wxss

福利专场商品列表css样式

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

/**=========福利专场============**/

.welfares{

  display: flex;

  justify-content: left;

  flex-direction: row;

  flex-wrap: wrap;

  margin:10px 5px;

}

.welfares-good{

  /* height: 500rpx;  */

  width: 47%;

  margin:0px auto;

  margin-bottom: 20px;

  position: relative;

  display: inline-block;

  font-size: 24rpx;

  /* border: 1px #eee solid;  */

}

.welfares-image{

  width: 100%;

  display: inline-block;

  border: 1px #eee solid;

}

.welfares-good .product-name{

  color: #000;

  /* height: 28px; */

  text-align:left;

   margin: 0px 5px;

   margin-bottom: 5px;

}

.product-price-wrap{

  height: 40rpx;

}

  

.product-price-wrap .product-price-new{

  color: #e80080;

  margin-left:5px;

  font-weight:900;

  font-size: 30rpx;

}

.product-price-wrap .product-price-old{

  color: #888;

  text-decoration: line-through;

  padding-left: 2px;

}

.product-price-wrap .discount{

  position: absolute;

  right: 5px;

  background-color: #000;

  color: #fff;

}

福利专场商品列表下拉加载更多样式

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

/*  加载更多   */

.weui-loading {

  margin: 0 5px;

  width: 20px;

  height: 20px;

  display: inline-block;

  vertical-align: middle;

  -webkit-animation: weuiLoading 1s steps(12, end) infinite;

  animation: weuiLoading 1s steps(12, end) infinite;

  background: transparent url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxMjAiIGhlaWdodD0iMTIwIiB2aWV3Qm94PSIwIDAgMTAwIDEwMCI+PHBhdGggZmlsbD0ibm9uZSIgZD0iTTAgMGgxMDB2MTAwSDB6Ii8+PHJlY3Qgd2lkdGg9IjciIGhlaWdodD0iMjAiIHg9IjQ2LjUiIHk9IjQwIiBmaWxsPSIjRTlFOUU5IiByeD0iNSIgcnk9IjUiIHRyYW5zZm9ybT0idHJhbnNsYXRlKDAgLTMwKSIvPjxyZWN0IHdpZHRoPSI3IiBoZWlnaHQ9IjIwIiB4PSI0Ni41IiB5PSI0MCIgZmlsbD0iIzk4OTY5NyIgcng9IjUiIHJ5PSI1Ii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A2NSkiLz48cmVjdCB3aWR0aD0iNyIgaGVpZ2h0PSIyMCIgeD0iNDYuNSIgeT0iNDAiIGZpbGw9IiNEMkQyRDIiIHJ4PSI1IiByeT0iNSIgdHJhbnNmb3JtPSJyb3RhdGUoLTkwIDM1IDY1KSIvPjxyZWN0IHdpZHRoPSI3IiBoZWlnaHQ9IjIwIiB4PSI0Ni41IiB5PSI0MCIgZmlsbD0iI0RBREFEQSIgcng9IjUiIHJ5PSI1IiB0cmFuc2Zvcm09InJvdGF0ZSgtNjAgMjQuMDIgNjUpIi8+PHJlY3Qgd2lkdGg9IjciIGhlaWdodD0iMjAiIHg9IjQ2LjUiIHk9IjQwIiBmaWxsPSIjRTJFMkUyIiByeD0iNSIgcnk9IjUiIHRyYW5zZm9ybT0icm90YXRlKC0zMCAtNS45OCA2NSkiLz48L3N2Zz4=) no-repeat;

  background-size: 100%;

}

.weui-loadmore {

  width: 65%;

  margin: 1.5em auto;

  line-height: 1.6em;

  font-size: 12px;

  text-align: center;

}

.weui-loadmore__tips {

  display: inline-block;

  vertical-align: middle;

  color: #888;

}

home.js

福利专场商品列表初始化

1

2

3

4

5

6

7

#初始化数据接上篇

var sectionData = [];

var ifLoadMore = null;

var page = 1;//默认第一页

data: {

    hidden:false,

  },

页面初始化加载福利专场商品列表函数

1

2

3

4

5

6

7

8

9

#接上篇

/**

  * 生命周期函数--监听页面加载

  */

  onLoad:

  function (options) {    

  //加载福利专场

    that.newGoodsShow();

  },

福利专场商品列表数据模型处理

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

newGoodsShow: function (success) {

    var that = this;

    ajax.request({

      method: &#39;GET&#39;,

      url: &#39;goods/getHotGoodsList?key=&#39;+ utils.key+&#39;&page=&#39; + page+&#39;&size=10&#39;,

      success: data => {

        var newGoodsData = data.result.list;

        page += 1;

        if (ifLoadMore) {

          //加载更多

          if (newGoodsData.length > 0){

            console.log(newGoodsData)

            //日期以及title长度处理

            for (var i in newGoodsData) {

              //商品名称长度处理

              var name = newGoodsData[i].name;

              if (name.length > 26) {

                newGoodsData[i].name = name.substring(0, 23) + &#39;...&#39;;

              }

            }

            sectionData[&#39;newGoods&#39;] = sectionData[&#39;newGoods&#39;].concat(newGoodsData);

  

          }else{

              ifLoadMore = false;

              this.setData({

                hidden:true

              })

              wx.showToast({

                title: &#39;暂无更多内容!&#39;,

                icon: &#39;loading&#39;,

                duration: 2000

              })

          }

           

        }else{

          if (ifLoadMore == null){

            ifLoadMore = true;

  

            //日期以及title长度处理

            for (var i in newGoodsData) {

              //商品名称长度处理

              var name = newGoodsData[i].name;

              if (name.length > 26) {

                newGoodsData[i].name = name.substring(0, 23) + &#39;...&#39;;

              }

            }

            sectionData[&#39;newGoods&#39;] = newGoodsData;//刷新

          }

           

        }

        that.setData({

          newGoods: sectionData[&#39;newGoods&#39;],

          // isHideLoadMore: true

        });

        wx.stopPullDownRefresh();//结束动画

      }

    })

  },

福利专场商品列表上拉触底事件

1

2

3

4

5

6

7

8

9

10

11

12

/**

 * 页面上拉触底事件的处理函数

 */

onReachBottom: function () {

  console.log("上拉");

  var that = this;

  console.log(&#39;加载更多&#39;);

  if (ifLoadMore != null){

    that.newGoodsShow();

 

  }

},

商品点击查看详情事件采集

image.png

商品点击查看详情事件采集.png

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

catchTapCategory: function (e) {

    var that = this;

    var goodsId = e.currentTarget.dataset.goodsid;

    console.log(&#39;goodsId:&#39; + goodsId);

    //新增商品用户点击数量

    that.goodsClickShow(goodsId);

    //跳转商品详情

    // wx.navigateTo({ url: &#39;../detail/detail?goodsId=&#39; + goodsId })

  },

  goodsClickShow(goodsId) {

    console.log(&#39;增加商品用户点击数量&#39;);

    var that = this;

    ajax.request({

      method: &#39;GET&#39;,

      url: &#39;goods/addGoodsClickRate?key=&#39; + utils.key + &#39;&goodsId=&#39; + goodsId,

      success: data => {

        console.log("用户点击统计返回结果:" + data.message)

      }

    })

  },

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:微信小程序商城开发中借助动态 API 构建商品详情页代码(上篇)
下一篇:微信小程序商城开发中商城首页轮播图、商品分类导航与新品特卖的代码实现
相关文章

 发表评论

暂时没有评论,来抢沙发吧~