当前位置:   article > 正文

微信小程序实现商品加入购物车案例_cartlist.findindex

cartlist.findindex

思考:购物车中的数据保存在哪里?用哪种数据结构进行保存?

小程序中可能有多个页面需要对购物车中的数据进行操作,因此我们想到把数据存到全局中。可以使用wx.setStorageSync()储存,用wx.getStorageSync()进行获取,以数组格式方便对数据进行操作。
一、商品加入购物车

单件商品信息存在{}中,在加入购物车的时候还需要加入两个字段为num代表商品数量,checked代表是否选中(购物车中可以选中商品进行支付),加入后要重新设置购物车的状态

  1. doPlusNum(e) {
  2. // 选中的商品信息
  3. let productInfo = e.currentTarget.dataset.item
  4. // 先获取缓存中的商品信息
  5. let cart = wx.getStorageSync('cart') || []
  6. // 判断当前商品是否第一次添加
  7. let index = cart.findIndex(v => v.id === productInfo.id)
  8. if(index === -1) {
  9. // 第一次添加则把商品信息及初始化的数量和选中状态一起存入
  10. cart.push({...productInfo,num: 1,checked: true})
  11. } else {
  12. // 前面添加过的话只需要更改商品中的数量即可
  13. cart[index].num = cart[index].num + 1
  14. }
  15. // 把更改后的购物车数据重新存入缓存
  16. wx.setStorageSync('cart', cart)
  17. this.setData({cartList: cart})
  18. wx.showToast({
  19. title: '商品已放入购物车',
  20. icon: 'none'
  21. })
  22. // 加入购物车给购物车加一个抖动的动画
  23. this.cartWwing()
  24. // 设置购物车状态(勾选、全选、总数、总价)
  25. this.setCart()
  26. },
二、商品移出购物车

在移出购物车的时候需要判断购物车中对应商品的状态,有多件商品则只需更改数量,只有一件商品则直接移除商品信息,最后要重新设置购物车的状态

  1. doMinusNum(e) {
  2. let that = this
  3. let productInfo = e.currentTarget.dataset.item
  4. let cart = wx.getStorageSync('cart') || []
  5. // 找到缓存中对应的商品
  6. let index = cart.findIndex(v => v.id === productInfo.id)
  7. // 商品数量大于1则直接减去数量,然后设置购物车状态
  8. if(cart[index].num > 1) {
  9. cart[index].num--;
  10. this.setCart(cart)
  11. } else if(cart[index].num == 1) {
  12. // 商品数量为1则给出弹窗提示
  13. cart[index].num = 0
  14. wx.showModal({
  15. content: '确定不要了吗?',
  16. success(res) {
  17. if(res.confirm) {
  18. // 确定移出则删除对应商品信息后设置购物车状态
  19. cart.splice(index,1)
  20. } else if(res.cancel) {
  21. // 取消后商品数量不做改变
  22. cart[index].num = 1
  23. }
  24. that.setCart(cart)
  25. }
  26. })
  27. }
  28. },
三、购物车底部工具栏及勾选、全选、总数、总价实现
1、设置购物车状态

计算商品总价时一般四舍五入保留两位,用到了getRoundeNumber方法

  1. setCart(cart) {
  2. cart = cart ? cart : wx.getStorageSync('cart') || []
  3. if(cart.length === 0) {
  4. this.setData({hideModal: true})
  5. }
  6. let allChecked = true,totalNum = 0,totalPrice = 0
  7. cart.forEach(v => {
  8. if(v.checked) {
  9. // 计算已经勾选商品的总价及总数
  10. totalPrice += getRoundeNumber(v.price * v.num) * 1
  11. totalNum += v.num
  12. } else {
  13. // 购物车中存在商品且没有商品被勾选,则全选按钮取消勾选
  14. allChecked = false
  15. }
  16. })
  17. // 购物车中不存在商品,则全选按钮取消勾选
  18. allChecked = cart.length != 0 ? allChecked : false
  19. wx.setStorageSync('cart', cart)
  20. this.setData({
  21. allChecked,
  22. totalNum,
  23. totalPrice,
  24. cartList: cart
  25. })
  26. this.handleList()
  27. },

附:getRoundeNumber方法如下

  1. const getRoundeNumber = num => {
  2. if (!Number.prototype._toFixed) {
  3. Number.prototype._toFixed = Number.prototype.toFixed
  4. }
  5. Number.prototype.toFixed = function(n) {
  6. return (this + 1e-14)._toFixed(n)
  7. }
  8. return Number(num).toFixed(2)
  9. }

2、勾选

  1. handleCheck(e) {
  2. let { id } = e.currentTarget.dataset
  3. let cartList = JSON.parse(JSON.stringify(this.data.cartList))
  4. let index = cartList.findIndex(v => v.id === id)
  5. cartList[index].checked = !cartList[index].checked
  6. // 设置购物车状态
  7. this.setCart(cartList)
  8. },

3、全选

  1. handleAllCheck() {
  2. let { cartList,allChecked } = this.data
  3. allChecked = !allChecked
  4. cartList.forEach(v => v.checked = allChecked)
  5. // 设置购物车状态
  6. this.setCart(cartList)
  7. },

4、清空购物车

  1. handleClearCart() {
  2. let that = this
  3. wx.showModal({
  4. content:'确定不要了吗?',
  5. success(res) {
  6. if(res.confirm) {
  7. that.setCart([])
  8. } else if(res.cancel) {
  9. console.log('用户点击取消');
  10. }
  11. }
  12. })
  13. },
5、已勾选商品支付成功后清除购物车中对应的数据
  1. let newCart = wx.getStorageSync('cart').filter(v => !v.checked)
  2. this.setCart(newCart)

6、当后台管理更新商品信息时购物车中的数据要对应更新
购物车中的数据是保存在本地缓存中的,如果在后台管理修改商品的信息或者删除商品,但是小程序中的缓存没有
及时更新,那么在下单的时候就会出现问题,所以需要在进入商城页面的时候就调用获取所有商品的接口跟购物车缓存中的数据进行对比,然后及时更新。
 

  1. getAllProduct() {
  2. let data = {}
  3. ...
  4. goodsMallFindAll(data).then(res => {
  5. if(res.data.code === 1) {
  6. let allProduct = res.data.data
  7. let cart = wx.getStorageSync('cart') || []
  8. let allProductId = allProduct.map(e => e.id)
  9. //过滤掉不存在的商品
  10. cart = cart.filter(e => allProductId.includes(e.id))
  11. //商品的数据进行更新
  12. cart = cart.map(ele => {
  13. allProduct.map(ele2 => {
  14. if(ele.id == ele2.id) {
  15. ele = Object.assign(ele, ele2);
  16. }
  17. })
  18. return ele
  19. })
  20. wx.setStorageSync('cart', cart)
  21. this.setCart()
  22. } else {
  23. wx.showToast({
  24. title: res.data.msg,
  25. icon: 'none'
  26. })
  27. }
  28. })
  29. },
四、附上整体代码

(1)wxml文件如下:

  1. <!-- 商品菜单及列表 -->
  2. <view class="cates">
  3. <!-- 左侧菜单 -->
  4. <scroll-view scroll-y class="left_menu">
  5. <view class="menu_item title">商品列表</view>
  6. <view class="menu_item {{index == currentIndex ? 'active' : ''}}" wx:for="{{menuList}}" wx:key="index" bindtap="handleMenuItemChange" data-index="{{index}}" data-id="{{item.id}}">{{item.name}}
  7. </view>
  8. </scroll-view>
  9. <!-- 右侧列表 -->
  10. <scroll-view scroll-y class="right_content" scroll-top="{{scrollTop}}">
  11. <view class="product-item" wx:for="{{productList}}" wx:key="index" bindtap="goDetail" data-item="{{item}}">
  12. <image class="image" src="{{item.images}}"></image>
  13. <view class="info">
  14. <view class="name">{{item.name}}</view>
  15. <view class="remark">{{item.remark}}</view>
  16. <view>
  17. <view class="price">¥{{item.price}}</view>
  18. <view wx:if="{{item.storeCount && item.storeCount != null}}" class="inventory">还剩{{item.storeCount}}件</view>
  19. </view>
  20. </view>
  21. <view class="stepperBox" catchtap="preventBubbling">
  22. <van-stepper show-minus="{{false}}" input-width="0" bind:plus="doPlusNum" data-item="{{item}}"></van-stepper>
  23. <view wx:if="{{item.num}}" class="num">{{item.num}}</view>
  24. </view>
  25. </view>
  26. </scroll-view>
  27. </view>
  28. <!-- 底部固定购物车 -->
  29. <view class="cart">
  30. <view class="cart_img_view" bindtap="handleCart">
  31. <image animation="{{ani}}" src="/public/image/icon_cart.png" class="cart_img"></image>
  32. <view class="cart_num" wx:if="{{totalNum > 0}}">
  33. {{totalNum}}
  34. </view>
  35. </view>
  36. <view class="cart_price">¥{{totalPrice}}</view>
  37. <view class="cart_text" bindtap="placeTheOrder">去支付</view>
  38. </view>
  39. <!-- 购物车展示 -->
  40. <modal hideModal="{{hideModal}}">
  41. <view class="cartBox">
  42. <view class="top">
  43. <view class="selectAll">
  44. <checkbox-group bindchange="handleAllCheck">
  45. <checkbox color="#fff" checked="{{allChecked}}"></checkbox>
  46. </checkbox-group>
  47. <view>已选购商品({{totalNum}}件)</view>
  48. </view>
  49. <view class="clearCart" bindtap="handleClearCart">
  50. <image src="/public/image/icon_del.png"></image>
  51. <view>清空</view>
  52. </view>
  53. </view>
  54. <view class="bottom">
  55. <view wx:for="{{cartList}}" wx:key="index" class="cart-item">
  56. <view class="cart-item-left">
  57. <checkbox-group bindchange="checkboxChange" data-id="{{item.id}}">
  58. <checkbox color="#fff" checked="{{item.checked}}" value="{{item.id}}"></checkbox>
  59. </checkbox-group>
  60. <view class="cart-item-left-content">
  61. <image></image>
  62. <view class="info">
  63. <view class="name">{{item.name}}</view>
  64. <view class="remark">{{item.remark}}</view>
  65. <view class="price">¥{{item.price}}</view>
  66. </view>
  67. </view>
  68. </view>
  69. <view class="cart-item-right">
  70. <van-stepper async-change min="0" show-minus="{{item.num == 0 ? false : true}}" input-width="{{item.num == 0 ? 0 : 32}}" value="{{item.num}}" disable-input bind:plus="doPlusNum" bind:minus="doMinusNum" data-item="{{item}}"></van-stepper>
  71. </view>
  72. </view>
  73. </view>
  74. </view>
  75. </modal>

(2)scss文件如下:

在小程序中直接使用scss语法是不支持的,需要进行一系列操作,具体的可参考微信开发者工具中使用scss一文。

  1. .cates {
  2. display: flex;
  3. height: calc(100vh - 390rpx);
  4. .left_menu {
  5. background-color: #eeeeee;
  6. width: 187rpx;
  7. .menu_item {
  8. display: flex;
  9. justify-content: center;
  10. align-items: center;
  11. font-size: 30rpx;
  12. height: 80rpx;
  13. }
  14. .active {
  15. font-weight: bolder;
  16. color: var(--themeColor);
  17. background-color: #fff;
  18. }
  19. .title {
  20. color: #1A1A1A;
  21. font-size: 28rpx;
  22. font-weight: bold;
  23. background-color: none;
  24. }
  25. }
  26. .right_content {
  27. width: calc(100% - 187rpx);
  28. padding: 0 20rpx;
  29. box-sizing: border-box;
  30. .product-item {
  31. display: flex;
  32. align-items: center;
  33. gap: 30rpx;
  34. height: 210rpx;
  35. box-sizing: border-box;
  36. position: relative;
  37. border-bottom: 1rpx solid #eeeeee;
  38. padding: 35rpx 0;
  39. .image {
  40. width: 140rpx;
  41. height: 140rpx;
  42. border-radius: 100%;
  43. border: 1rpx solid var(--themeColor);
  44. }
  45. .info {
  46. height: 100%;
  47. display: flex;
  48. flex-direction: column;
  49. justify-content: space-between;
  50. .name {
  51. font-weight: bold;
  52. font-size: 28rpx;
  53. }
  54. .remark {
  55. color: #767676;
  56. font-size: 24rpx;
  57. }
  58. .price {
  59. display: inline-block;
  60. color: #B08657;
  61. font-size: 28rpx;
  62. }
  63. .inventory {
  64. display: inline-block;
  65. font-size: 24rpx;
  66. color: #c5c5c5;
  67. margin-left: 20rpx;
  68. }
  69. }
  70. .van-stepper {
  71. position: absolute;
  72. right: 10rpx;
  73. bottom: 10rpx;
  74. .van-stepper__input {
  75. display: none;
  76. }
  77. }
  78. .num {
  79. position: absolute;
  80. right: 0rpx;
  81. bottom: 45rpx;
  82. width: 35rpx;
  83. height: 35rpx;
  84. border-radius: 100%;
  85. display: flex;
  86. align-items: center;
  87. justify-content: center;
  88. background-color: #c3a07a;
  89. color: #fff;
  90. font-size: 16rpx;
  91. }
  92. }
  93. }
  94. }
  95. .cart {
  96. position: fixed;
  97. bottom: 40rpx;
  98. left: 50%;
  99. transform: translate(-50%);
  100. z-index: 9999;
  101. width: 710rpx;
  102. height: 140rpx;
  103. background-color: #fff;
  104. border-radius: 92rpx;
  105. display: flex;
  106. align-items: center;
  107. z-index: 99;
  108. .cart_img_view {
  109. display: flex;
  110. justify-content:center;
  111. align-items:Center;
  112. position: relative;
  113. width: 120rpx;
  114. height: 120rpx;
  115. border-radius: 100%;
  116. background-color: var(--themeColor);
  117. margin-left: 22rpx;
  118. .cart_img {
  119. width: 64rpx;
  120. height: 58rpx;
  121. }
  122. .cart_num {
  123. position: absolute;
  124. width: 40rpx;
  125. height: 40rpx;
  126. top: -10rpx;
  127. right: -20rpx;
  128. background-color: #c1a077;
  129. padding: 2.5rpx;
  130. border-radius: 100%;
  131. display: flex;
  132. justify-content:center;
  133. align-items:Center;
  134. color: #fff;
  135. font-size: 25rpx;
  136. border: 1rpx solid #fff;
  137. }
  138. }
  139. .cart_price {
  140. margin-left: 40rpx;
  141. color: #3D3D3D;
  142. font-size: 36rpx;
  143. font-weight: 500;
  144. }
  145. .cart_text {
  146. position: absolute;
  147. right: 0;
  148. top: 0;
  149. width: 190rpx;
  150. height: 100%;
  151. border-radius: 0rpx 92rpx 92rpx 0rpx;
  152. background-color: var(--themeColor);
  153. font-size: 28rpx;
  154. color: white;
  155. display: flex;
  156. justify-content:center;
  157. align-items:Center;
  158. }
  159. }
  160. .popup-content-class {
  161. padding: 0 !important;
  162. }
  163. .cartBox {
  164. position: fixed;
  165. bottom: 0;
  166. left: 0;
  167. right: 0;
  168. background-color: #fff;
  169. z-index: 999;
  170. max-height: 80%;
  171. overflow-y: scroll;
  172. padding-bottom: 250rpx;
  173. .top {
  174. position: -webkit-sticky;
  175. position: sticky;
  176. top: 0;
  177. display: flex;
  178. justify-content: space-between;
  179. align-items: center;
  180. padding: 30rpx;
  181. border-bottom: 1rpx solid rgba(180, 180, 180,0.3);;
  182. .selectAll {
  183. display: flex;
  184. align-items: center;
  185. }
  186. .clearCart {
  187. display: flex;
  188. align-items: center;
  189. gap: 10rpx;
  190. color: #b3b3b3;
  191. font-size: 28rpx;
  192. image {
  193. width: 42rpx;
  194. height: 43rpx;
  195. }
  196. }
  197. }
  198. .bottom {
  199. padding: 30rpx;
  200. .cart-item {
  201. display: flex;
  202. align-items: center;
  203. justify-content: space-between;
  204. margin-top: 20rpx;
  205. .cart-item-left {
  206. display: flex;
  207. align-items: center;
  208. gap: 30rpx;
  209. .cart-item-left-content {
  210. display: flex;
  211. gap: 10rpx;
  212. image {
  213. width: 126rpx;
  214. height: 126rpx;
  215. border: 1rpx solid #eeeeee;
  216. }
  217. .info {
  218. display: flex;
  219. flex-direction: column;
  220. justify-content: space-between;
  221. .name {
  222. font-size: 28rpx;
  223. color: #333333;
  224. }
  225. .remark {
  226. font-size: 24rpx;
  227. color: #767676;
  228. }
  229. .price {
  230. font-size: 28rpx;
  231. color: #B08657;
  232. }
  233. }
  234. }
  235. }
  236. }
  237. }
  238. }
  239. .van-stepper__minus,.van-stepper__plus {
  240. border-radius: 100% !important;
  241. width: 45rpx !important;
  242. height: 45rpx !important;
  243. }
  244. .van-stepper__minus {
  245. border: 1rpx solid #d8d8d8 !important;
  246. color: #d8d8d8 !important;
  247. font-weight: bold !important;
  248. }
  249. .van-stepper__plus {
  250. background-color: var(--themeColor) !important;
  251. color: #fff !important;
  252. }
  253. .van-stepper__input {
  254. background-color: #fff !important;
  255. color: #353535 !important;
  256. font-weight: bold !important;
  257. }
  258. /* 多选框 */
  259. .wx-checkbox-input {
  260. width: 40rpx !important;
  261. height: 40rpx !important;
  262. border-radius: 100% !important;
  263. background-color: #fff !important;
  264. }
  265. .wx-checkbox-input.wx-checkbox-input-checked {
  266. width: 40rpx !important;
  267. height: 40rpx !important;
  268. background-color: var(--themeColor) !important;
  269. }

(3)js文件如下:

  1. Page({
  2. /**
  3. * 页面的初始数据
  4. */
  5. data: {
  6. menuList:[],
  7. productList: [],
  8. cartList: [],
  9. currentIndex: 0,
  10. currentGroupId: "",
  11. baseUrl: "",
  12. scrollTop: 0,
  13. hideModal: true,
  14. ani: '',
  15. totalNum: 0, // 已选商品数量
  16. totalPrice: 0, // 已选商品总金额
  17. allChecked: true,
  18. },
  19. /**
  20. * 生命周期函数--监听页面加载
  21. */
  22. onLoad(options) {
  23. this.getGoodsGroup()
  24. },
  25. // 获取商品分组
  26. getGoodsGroup() {
  27. ...
  28. goodsGroupFindAll(data).then(res => {
  29. if(res.data.code === 1) {
  30. this.setData({menuList: res.data.data.content}
  31. if(this.data.currentGroupId) {
  32. this.getProductList(this.data.currentGroupId)
  33. } else {
  34. this.getProductList(res.data.data.content[0].id)
  35. }
  36. } else {
  37. wx.showToast({
  38. title: res.data.msg,
  39. icon: 'none'
  40. })
  41. }
  42. })
  43. },
  44. // 获取商品列表
  45. getProductList(groupId) {
  46. let data = {}
  47. data.groupId = groupId
  48. goodsMallFindAll(data).then(res => {
  49. if(res.data.code === 1) {
  50. ...
  51. this.setData({productList: res.data.data})
  52. } else {
  53. wx.showToast({
  54. title: res.data.msg,
  55. icon: 'none'
  56. })
  57. }
  58. })
  59. },
  60. // 获取所有商品更新缓存购物车数据
  61. getAllProduct() {
  62. let data = {}
  63. data.shopId = wx.getStorageSync('shop').id
  64. goodsMallFindAll(data).then(res => {
  65. if(res.data.code === 1) {
  66. let allProduct = res.data.data
  67. let cart = wx.getStorageSync('cart') || []
  68. let allProductId = allProduct.map(e => e.id)
  69. cart = cart.filter(e => allProductId.includes(e.id))
  70. cart = cart.map(ele => {
  71. allProduct.map(ele2 => {
  72. if(ele.id == ele2.id) {
  73. ele = Object.assign(ele, ele2);
  74. }
  75. })
  76. return ele
  77. })
  78. wx.setStorageSync('cart', cart)
  79. this.setCart()
  80. } else {
  81. wx.showToast({
  82. title: res.data.msg,
  83. icon: 'none'
  84. })
  85. }
  86. })
  87. },
  88. // 购物车回填商品列表数据
  89. handleList() {
  90. let cart = wx.getStorageSync('cart') || []
  91. let productList = this.data.productList.map(item => {
  92. delete item.num
  93. return item
  94. })
  95. productList.map(item => {
  96. cart.map(v => {
  97. if(item.id === v.id) {
  98. item.num = v.num
  99. }
  100. })
  101. })
  102. this.setData({productList})
  103. },
  104. // 点击侧边栏
  105. handleMenuItemChange(e) {
  106. let {index,id} = e.currentTarget.dataset
  107. this.setData({
  108. currentIndex: index,
  109. currentGroupId: id,
  110. scrollTop: 0
  111. })
  112. this.getProductList(id)
  113. },
  114. // 点击购物车
  115. handleCart() {
  116. this.setData({
  117. cartList: wx.getStorageSync('cart'),
  118. })
  119. if(wx.getStorageSync('cart') && wx.getStorageSync('cart').length != 0) {
  120. this.setData({hideModal: false})
  121. } else {
  122. wx.showToast({
  123. title: '请添加商品',
  124. icon: 'none'
  125. })
  126. }
  127. },
  128. // 阻止事件冒泡
  129. preventBubbling() {},
  130. // 加入购物车
  131. doPlusNum(e) {
  132. console.log(e);
  133. let productInfo = e.currentTarget.dataset.item
  134. let cart = wx.getStorageSync('cart') || []
  135. let index = cart.findIndex(v => v.id === productInfo.id)
  136. if(index === -1) {
  137. cart.push({...productInfo,num: 1,checked: true})
  138. } else {
  139. cart[index].num = cart[index].num + 1
  140. }
  141. wx.setStorageSync('cart', cart)
  142. this.setData({cartList: cart})
  143. wx.showToast({
  144. title: '商品已放入购物车',
  145. icon: 'none'
  146. })
  147. this.cartWwing()
  148. this.setCart()
  149. },
  150. // 移除出购物车
  151. doMinusNum(e) {
  152. let that = this
  153. console.log(e);
  154. let productInfo = e.currentTarget.dataset.item
  155. let cart = wx.getStorageSync('cart') || []
  156. let index = cart.findIndex(v => v.id === productInfo.id)
  157. if(cart[index].num > 1) {
  158. cart[index].num--;
  159. this.setCart(cart)
  160. } else if(cart[index].num == 1) {
  161. cart[index].num = 0
  162. wx.showModal({
  163. content: '确定不要了吗?',
  164. success(res) {
  165. if(res.confirm) {
  166. cart.splice(index,1)
  167. } else if(res.cancel) {
  168. cart[index].num = 1
  169. }
  170. that.setCart(cart)
  171. }
  172. })
  173. }
  174. },
  175. // 设置购物车状态
  176. setCart(cart) {
  177. cart = cart ? cart : wx.getStorageSync('cart') || []
  178. if(cart.length === 0) {
  179. this.setData({hideModal: true})
  180. }
  181. let allChecked = true,totalNum = 0,totalPrice = 0
  182. cart.forEach(v => {
  183. if(v.checked) {
  184. totalPrice += getRoundeNumber(v.price * v.num) * 1
  185. totalNum += v.num
  186. } else {
  187. allChecked = false
  188. }
  189. })
  190. allChecked = cart.length != 0 ? allChecked : false
  191. wx.setStorageSync('cart', cart)
  192. this.setData({
  193. allChecked,
  194. totalNum,
  195. totalPrice,
  196. cartList: cart
  197. })
  198. this.handleList()
  199. },
  200. // 加入购物车动画
  201. cartWwing: function(){
  202. var animation = wx.createAnimation({
  203. duration: 100,
  204. timingFunction: 'ease-in'
  205. })
  206. animation.translateX(6).rotate(21).step()
  207. animation.translateX(-6).rotate(-21).step()
  208. animation.translateX(0).rotate(0).step()
  209. // 导出动画
  210. this.setData({
  211. ani: animation.export()
  212. })
  213. },
  214. // 购物车勾选
  215. checkboxChange(e) {
  216. console.log(e);
  217. let { id } = e.currentTarget.dataset
  218. let cartList = JSON.parse(JSON.stringify(this.data.cartList))
  219. let index = cartList.findIndex(v => v.id === id)
  220. cartList[index].checked = !cartList[index].checked
  221. this.setCart(cartList)
  222. },
  223. // 全选
  224. handleAllCheck() {
  225. let { cartList,allChecked } = this.data
  226. allChecked = !allChecked
  227. cartList.forEach(v => v.checked = allChecked)
  228. this.setCart(cartList)
  229. },
  230. // 清空购物车
  231. handleClearCart() {
  232. let that = this
  233. wx.showModal({
  234. content:'确定不要了吗?',
  235. success(res) {
  236. if(res.confirm) {
  237. that.setCart([])
  238. } else if(res.cancel) {
  239. console.log('用户点击取消');
  240. }
  241. }
  242. })
  243. },
  244. // 支付跳转
  245. placeTheOrder() {
  246. let data = {}
  247. ...
  248. orderGoodsInsert(data).then(res => {
  249. if(res.data.code === 1) {
  250. ...
  251. // 删除缓存中已经下单的商品
  252. let newCart = wx.getStorageSync('cart').filter(v => !v.checked)
  253. this.setCart(newCart)
  254. } else {
  255. wx.showToast({
  256. title: res.data.msg,
  257. icon: 'none'
  258. })
  259. }
  260. })
  261. },
  262. /**
  263. * 生命周期函数--监听页面显示
  264. */
  265. onShow() {
  266. this.setCart()
  267. }
  268. })

效果图如下:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/517102
推荐阅读
相关标签
  

闽ICP备14008679号