轻源码

  • QingYuanMa.com
  • 全球最大的互联网技术和资源下载平台
搜索
一起源码网 门户 微信小程序 查看主题

微信小程序实战商城系列《五》购物车

发布者: miyoko | 发布时间: 2018-6-7 20:46| 查看数: 5225| 评论数: 1|帖子模式

作者:michael_ouyang,来自授权地址 
自从认识某人后,我收获了两个成功。登录成功、付款成功,而且还拥有了自己的一辆车: 
购物车 
也发现了自己的不足之处: 
余额不足。

为大家介绍的就是购物车 
这里演示从商品列表中添加到购物车 
下面先做商品列表页。如下图:

布局分析:

首先一个list的主盒子,接着是item盒子,这是必须的。 
然后把item分成左侧的图片部分,和右侧的说明部分(item盒子使用横向弹性盒) 
右侧的说明部分又分上下2部分(右侧说明部分盒子使用纵向弹性盒) 
下面价钱购物车部分(下面价钱购物车部分也使用横向弹性盒,中间使用justify-content: space-between;填充空白)

index.wxml:

  1. <!--主盒子-->
  2. <view class="container">
  3. <!--head-->
  4. <view class="tit">
  5. <view class="title_val">商品列表</view>
  6. <view class="more">更多</view>
  7. </view>
  8. <!--list-->
  9. <view class="goodslist">
  10. <!--item-->
  11. <block wx:for="{{goodslist}}">
  12. <view class="goods">
  13. <!--左侧图片盒子-->
  14. <view>
  15. <image src="{{item.imgUrl}}" class="good-img" />
  16. </view>
  17. <!--右侧说明部分-->
  18. <view class="good-cont">
  19. <!--上--文字说明-->
  20. <view class="goods-navigator">
  21. <text class="good-name">{{item.name}}</text>
  22. </view>
  23. <!--下--价格部分-->
  24. <view class="good-price">
  25. <text>¥{{item.price}}</text>
  26. <image id="{{item.id}}" class="cart" src="/images/new_73.jpg" bindtap="addcart" />
  27. </view>
  28. </view>
  29. </view>
  30. </block>
  31. </view>
  32. </view>

index.wxss:

  1. /**index.wxss**/
  2. page{
  3. height: 100%;
  4. }
  5. .container{
  6. background: #f5f5f5;
  7. }
  8. .tit{
  9. display: flex;
  10. flex-direction: row;
  11. justify-content: space-between;
  12. height: 30px;
  13. position: relative;
  14. }
  15. .tit::before{
  16. content:'';
  17. background: #ffcc00;
  18. width:5px;
  19. height: 100%;
  20. position: absolute;
  21. left: 0;
  22. top: 0;
  23. }
  24. .title_val{
  25. padding: 0 15px;
  26. font-size: 14px;
  27. color: #555;
  28. line-height: 30px;
  29. }
  30. .more{
  31. font-size: 12px;
  32. line-height: 30px;
  33. color: #999;
  34. padding: 0 5px 0 0 ;
  35. }
  36. .goodslist{
  37. background: #fff;
  38. display: flex;
  39. flex-direction: column;
  40. }
  41. .goods{
  42. display: flex;
  43. flex-direction: row;
  44. border-bottom: 1px solid #ddd;
  45. }
  46. .good-img{
  47. padding: 5px;
  48. width: 80px;
  49. height: 80px;
  50. }
  51. .good-cont{
  52. display: flex;
  53. flex: 1;
  54. flex-direction: column;
  55. font-size: 14px;
  56. }
  57. .goods-navigator{
  58. display: flex;
  59. flex: 1;
  60. flex-direction: column;
  61. justify-content: center;
  62. }
  63. .good-name{
  64. display: flex;
  65. flex: 1;
  66. flex-direction: column;
  67. color: #555;
  68. justify-content: center;
  69. }
  70. .good-price{
  71. display: flex;
  72. flex: 1;
  73. flex-direction: row;
  74. justify-content: space-between;
  75. color:#e4393c;
  76. font-weight: 600;
  77. }
  78. .cart{
  79. width: 40px;
  80. height: 40px;
  81. padding-right: 10px;
  82. }

index.js: 
数据部分,一般情况都是访问接口获取数据的,这里并没有使用网络访问,为了简化demo,直接把一组数据放在data对象中。同学们可以根据其数据结构自己编写后台接口

  1. Page({
  2. data: {
  3. goodslist: [
  4. {
  5. id:"001",
  6. imgUrl:"",
  7. name:"女装T恤中长款大码摆裙春夏新款",
  8. price:"65.00"
  9. },
  10. {
  11. id:"002",
  12. imgUrl:"",
  13. name:"火亮春秋季 男青年修身款圆领男士T恤",
  14. price:"68.00"
  15. },
  16. {
  17. id:"003",
  18. imgUrl:"",
  19. name:"新款立体挂脖t恤女短袖大码宽松条纹V领上衣显瘦休闲春夏",
  20. price:"86.00"
  21. },
  22. {
  23. id:"004",
  24. imgUrl:"",
  25. name:"男运动上衣春季上新品 上衣流行装青年",
  26. price:"119.00"
  27. },
  28. {
  29. id:"005",
  30. imgUrl:"",
  31. name:"时尚字母三角露胸t恤女装亮丝大码宽松不规则春夏潮",
  32. price:"69.00"
  33. },
  34. {
  35. id:"006",
  36. imgUrl:"",
  37. name:"新款立体挂脖t恤短袖大码宽松条纹V领上衣显瘦休闲春夏",
  38. price:"86.00"
  39. },
  40. {
  41. id:"007",
  42. imgUrl:"",
  43. name:"时尚字母三角露胸t恤女装亮丝大码宽松不规则春夏潮",
  44. price:"119.00"
  45. },
  46. {
  47. id:"008",
  48. imgUrl:"",
  49. name:"男运动上衣春季上新品 上衣流行装青年",
  50. price:"69.00"
  51. },
  52. ]
  53. },
  54. // 加入购物车
  55. addcart:function(e){
  56. this.setData({
  57. toastHidden:false
  58. });
  59. // 遍历列表 与 购物车列表
  60. for (var i in this.data.goodslist){
  61. // 列表中某一项item的id == 点击事件传递过来的id。则是被点击的项
  62. if(this.data.goodslist[i].id == e.target.id){
  63. // 给goodsList数组的当前项添加count元素,值为1,用于记录添加到购物车的数量
  64. this.data.goodslist[i].count = 1;
  65. // 获取购物车的缓存数组(没有数据,则赋予一个空数组)
  66. var arr = wx.getStorageSync('cart') || [];
  67. // 如果购物车有数据
  68. if(arr.length>0){
  69. // 遍历购物车数组
  70. for(var j in arr){
  71. // 判断购物车内的item的id,和事件传递过来的id,是否相等
  72. if(arr[j].id == e.target.id){
  73. // 相等的话,给count+1(即再次添加入购物车,数量+1)
  74. arr[j].count = arr[j].count + 1;
  75. // 最后,把购物车数据,存放入缓存(此处不用再给购物车数组push元素进去,因为这个是购物车有的,直接更新当前数组即可)
  76. try {
  77. wx.setStorageSync('cart', arr)
  78. } catch (e) {
  79. console.log(e)
  80. }
  81. // 返回(在if内使用return,跳出循环节约运算,节约性能)
  82. return;
  83. }
  84. }
  85. // 遍历完购物车后,没有对应的item项,把goodslist的当前项放入购物车数组
  86. arr.push(this.data.goodslist[i]);
  87. }
  88. // 购物车没有数据,把item项push放入当前数据(第一次存放时)
  89. else{
  90. arr.push(this.data.goodslist[i]);
  91. }
  92. // 最后,把购物车数据,存放入缓存
  93. try {
  94. wx.setStorageSync('cart', arr)
  95. // 返回(在if内使用return,跳出循环节约运算,节约性能)
  96. return;
  97. } catch (e) {
  98. console.log(e)
  99. }
  100. }
  101. }
  102. }
  103. })

编写购物车部分,如下图所示: 

布局分析:

首先一个list的主盒子,接着是item盒子,这是必须的。 
然后把item分成左侧的图片部分,和右侧的说明部分(item盒子使用横向弹性盒) 
右侧的说明部分又分上下2部分(右侧说明部分盒子使用纵向弹性盒) 
下面价钱、购物加减、购物车部分(使用纵向弹性盒) 
最下面的购物加减、购物车部分(使用横向弹性盒,中间使用justify-content: space-between;填充空白)

cart.wxml:

  1. <!--要是够车内没有数据,就行显示没有数据-->
  2. <view class="cart" hidden="{{iscart}}">
  3. <image src="/images/cart.png"/>
  4. <view>购物车什么都没有,赶快去购物吧</view>
  5. </view>
  6. <!--要是有数据,就显示数据-->
  7. <view class="cartList" hidden="{{!iscart}}">
  8. <!--header-->
  9. <view class="baoyou"></view>
  10. <!--list item-->
  11. <block wx:for="{{cart}}">
  12. <view class="goods">
  13. <!--左侧图片-->
  14. <view>
  15. <image src="{{item.imgUrl}}" class="good-img"/>
  16. </view>
  17. <!--右侧说明部分-->
  18. <view class="good-cont">
  19. <!--文字说明-->
  20. <view class="goods-navigator">
  21. <text class="good-name">{{item.name}}</text>
  22. </view>
  23. <!--价钱和购物加减的父盒子-->
  24. <view class="good-price">
  25. <text class="price">¥{{item.price}}</text>
  26. <view class="btn-box">
  27. <view class="btn">
  28. <button id="del{{index}}" type="default" size="mini" bindtap="delCount">-</button>
  29. <input value="{{item.count}}"/>
  30. <button id="add{{index}}" type="default" size="mini" bindtap="addCount">+</button>
  31. </view>
  32. <image id="img{{index}}" src="/images/del2.png" bindtap="delGoods"/>
  33. </view>
  34. </view>
  35. </view>
  36. </view>
  37. </block>
  38. <!--footer-->
  39. <view class="total">
  40. <view class="total_text">合计:<text>¥{{total}}</text></view>
  41. <button class="total_js" size="mini">去结算({{goodsCount}})</button>
  42. </view>
  43. </view>

cart.wxss:

  1. page {
  2. background: #f2ebe3;
  3. }
  4. .cart {
  5. padding: 100px 0 0 0;
  6. display: flex;
  7. justify-content: center;
  8. flex-direction: column;
  9. align-items: center;
  10. color: #999;
  11. }
  12. .cart image {
  13. width: 66px;
  14. height: 66px;
  15. margin-bottom: 20px;
  16. }
  17. .baoyou {
  18. font-size: 18px;
  19. color: #db2929;
  20. padding: 10px;
  21. }
  22. .goods {
  23. background: #fff;
  24. border-top: 1px solid #ddd;
  25. margin-bottom: 10px;
  26. padding: 10px 10px 0 10px;
  27. display: flex;
  28. }
  29. .goods image {
  30. width: 80px;
  31. height: 80px;
  32. border: 1px solid #ddd;
  33. }
  34. .goods .good-cont {
  35. display: flex;
  36. flex: 1;
  37. flex-direction: column;
  38. color: #555;
  39. font-size: 14px;
  40. padding: 5px;
  41. height: 100px;
  42. }
  43. .goods .good-cont .goods-navigator {
  44. display: flex;
  45. flex: 2;
  46. }
  47. .goods .good-cont .good-price {
  48. display: flex;
  49. flex-direction: column;
  50. flex: 3;
  51. }
  52. .goods .good-cont .good-price .price {
  53. font-size: 16px;
  54. color: #ec5151;
  55. }
  56. .goods .good-cont .good-price .btn-box {
  57. display: flex;
  58. flex-direction: row;
  59. justify-content: space-between;
  60. }
  61. .goods .good-cont .good-price .btn-box image {
  62. width: 23px;
  63. height: 23px;
  64. border: 0;
  65. margin: 0;
  66. }
  67. .goods .good-cont .good-price .btn {
  68. display: flex;
  69. flex-direction: row;
  70. }
  71. .goods .good-cont .good-price .btn input {
  72. margin: 0;
  73. width: 40px;
  74. text-align: center;
  75. border: 1px solid #eee;
  76. font-size: 16px;
  77. height: 28px;
  78. }
  79. .goods .good-cont .good-price .btn button {
  80. margin: 0;
  81. }
  82. .total {
  83. height: 40px;
  84. display: flex;
  85. flex-direction: row;
  86. justify-content: space-between;
  87. padding: 0 20px;
  88. }
  89. .total .total_text {
  90. display: flex;
  91. color: #777;
  92. }
  93. .total .total_text text {
  94. color: #ec5151;
  95. }
  96. .total .total_js {
  97. color: #fff;
  98. background: #ec5151;
  99. height: 30px;
  100. margin: 0;
  101. }

cart.js:

  1. Page({
  2. data: {
  3. iscart: false,
  4. cart: [], //数据
  5. count: 1,
  6. total: 0, //总金额
  7. goodsCount: 0 //数量
  8. },
  9. onShow: function () {
  10. // 获取产品展示页保存的缓存数据(购物车的缓存数组,没有数据,则赋予一个空数组)
  11. var arr = wx.getStorageSync('cart') || [];
  12. // 有数据的话,就遍历数据,计算总金额 和 总数量
  13. if (arr.length > 0) {
  14. for (var i in arr) {
  15. this.data.total += Number(arr[i].price);
  16. this.data.goodsCount += Number(arr[i].count);
  17. }
  18. // 更新数据
  19. this.setData({
  20. iscart: true,
  21. cart: arr,
  22. total: this.data.total,
  23. goodsCount: this.data.goodsCount
  24. });
  25. }
  26. },
  27. /* 减数 */
  28. delCount: function (e) {
  29. console.log(e)
  30. // 获取购物车该商品的数量
  31. // [获取设置在该btn的id,即list的index值]
  32. if (this.data.cart[e.target.id.substring(3)].count <= 1) {
  33. return;
  34. }
  35. // 商品总数量-1
  36. this.data.goodsCount -= 1;
  37. // 总价钱 减去 对应项的价钱单价
  38. this.data.total -= Number(this.data.cart[e.target.id.substring(3)].price);
  39. // 购物车主体数据对应的项的数量-1 并赋给主体数据对应的项内
  40. this.data.cart[e.target.id.substring(3)].count = --this.data.cart[e.target.id.substring(3)].count;
  41. // 更新data数据对象
  42. this.setData({
  43. cart: this.data.cart,
  44. total: this.data.total,
  45. goodsCount: this.data.goodsCount
  46. })
  47. // 主体数据重新赋入缓存内
  48. try {
  49. wx.setStorageSync('cart', this.data.cart)
  50. } catch (e) {
  51. console.log(e)
  52. }
  53. },
  54. /* 加数 */
  55. addCount: function (e) {
  56. // 商品总数量+1
  57. this.data.goodsCount += 1;
  58. // 总价钱 加上 对应项的价钱单价
  59. this.data.total += Number(this.data.cart[e.target.id.substring(3)].price);
  60. // 购物车主体数据对应的项的数量+1 并赋给主体数据对应的项内
  61. this.data.cart[e.target.id.substring(3)].count = ++this.data.cart[e.target.id.substring(3)].count;
  62. // 更新data数据对象
  63. this.setData({
  64. cart: this.data.cart,
  65. total: this.data.total,
  66. goodsCount: this.data.goodsCount
  67. })
  68. // 主体数据重新赋入缓存内
  69. try {
  70. wx.setStorageSync('cart', this.data.cart)
  71. } catch (e) {
  72. console.log(e)
  73. }
  74. },
  75. /* 删除item */
  76. delGoods: function (e) {
  77. // 商品总数量 减去 对应删除项的数量
  78. this.data.goodsCount = this.data.goodsCount - this.data.cart[e.target.id.substring(3)].count;
  79. // 总价钱 减去 对应删除项的单价*数量
  80. this.data.total -= this.data.cart[e.target.id.substring(3)].price * this.data.cart[e.target.id.substring(3)].count;
  81. // 主体数据的数组移除该项
  82. this.data.cart.splice(e.target.id.substring(3), 1);
  83. // 更新data数据对象
  84. this.setData({
  85. cart: this.data.cart,
  86. total: this.data.total,
  87. goodsCount: this.data.goodsCount
  88. })
  89. // 主体数据重新赋入缓存内
  90. try {
  91. wx.setStorageSync('cart', this.data.cart)
  92. } catch (e) {
  93. console.log(e)
  94. }
  95. }
  96. })

运行结果: 

demo:购物车.zip

最新评论

自挽 发表于 2022-6-23 19:24
音乐编程软件有哪些

浏览过的版块

轻源码让程序更轻更快

QingYuanMa.com

工作时间 周一至周六 8:00-17:30

侵权处理

客服QQ点击咨询

关注抖音号

定期抽VIP

Copyright © 2016-2021 https://www.171739.xyz/ 滇ICP备13200218号

快速回复 返回顶部 返回列表