cart.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <template>
  2. <view class="container">
  3. <!-- 购物车为空 -->
  4. <view v-if="!hasLogin || cartList.length === 0">
  5. <view class="cart-empty">
  6. <u-empty text="去逛逛添点什么吧" width="500rpx" height="500rpx" icon="/static/images/empty/cart.png"></u-empty>
  7. </view>
  8. </view>
  9. <!-- 购物车列表 -->
  10. <scroll-view v-if="hasLogin && cartList.length > 0" class="cart-product" scroll-y="true">
  11. <yd-cart-product :product-list="cartList" @productCheckedChange="handleProductCheckedChange" @productCountChange="handleProductCountChange"></yd-cart-product>
  12. </scroll-view>
  13. <!-- 未登录 -->
  14. <view v-if="!hasLogin" class="login-tips-box">
  15. <view class="login-tips">
  16. <navigator url="/pages/login/social" open-type="navigate" hover-class="none">
  17. <view class="login-link">登录查看</view>
  18. </navigator>
  19. </view>
  20. </view>
  21. <!-- 底部菜单 -->
  22. <view class="cart-btn-container">
  23. <view class="btn-box">
  24. <view class="product-check-info">
  25. <view class="check-all-btn" @click.stop="handleCheckAllProduct">
  26. <u-icon v-if="isCheckAll" name="checkmark-circle-fill" color="#3c9cff" size="22"></u-icon>
  27. <view v-else class="un-check-box"></view>
  28. </view>
  29. <view class="info-text">合计:</view>
  30. <view>
  31. <yd-text-price color="red" size="15" intSize="20" :price="totalAmount"></yd-text-price>
  32. </view>
  33. </view>
  34. <view v-if="checkedProduct.length > 0" class="cart-btn-group">
  35. <u-button type="warning" shape="circle" size="small" text="移除" @click="handleRemoveProduct"></u-button>
  36. <view class="btn-gap"></view>
  37. <u-button style="margin-left: 10px" class="main-btn" type="primary" shape="circle" size="small" text="去结算" @click="handleCheckoutProduct"></u-button>
  38. </view>
  39. <view v-else class="cart-btn-group">
  40. <u-button type="warning" shape="circle" size="small" text="移除" disabled></u-button>
  41. <view class="btn-gap"></view>
  42. <u-button style="margin-left: 10px" class="main-btn" type="primary" shape="circle" size="small" text="去结算" disabled></u-button>
  43. </view>
  44. </view>
  45. </view>
  46. </view>
  47. </template>
  48. <script>
  49. export default {
  50. data() {
  51. return {
  52. title: '',
  53. cartList: [],
  54. checkedNumber: 0,
  55. totalAmount: 0
  56. }
  57. },
  58. computed: {
  59. checkedProduct() {
  60. return this.cartList.filter(item => {
  61. return item.checked
  62. })
  63. },
  64. isCheckAll() {
  65. if (this.cartList.length < 1) {
  66. return false
  67. }
  68. return this.cartList.every(item => {
  69. return item.checked
  70. })
  71. },
  72. hasLogin() {
  73. return this.$store.getters.hasLogin
  74. }
  75. },
  76. onShow() {
  77. if (this.hasLogin) {
  78. this.loadCartDetailData()
  79. } else {
  80. this.cartList =[]
  81. }
  82. },
  83. methods: {
  84. loadCartDetailData() {
  85. this.$store.dispatch('CartProductDetail').then(res => {
  86. this.cartList = res.data || []
  87. })
  88. },
  89. /** 商品全选/取消全选 */
  90. handleCheckAllProduct() {
  91. if (this.cartList.length < 1) {
  92. return
  93. }
  94. const productIds = this.cartList.map(item => {
  95. return item.productId
  96. })
  97. this.$store.dispatch('CartProductCheckChange', { productIds, checked: !this.isCheckAll }).then(res => {
  98. this.cartList = res.data || []
  99. })
  100. },
  101. /** 商品单选/取消单选 */
  102. handleProductCheckedChange(productId, checked) {
  103. this.$store.dispatch('CartProductCheckChange', { productIds: [productId], checked: checked }).then(res => {
  104. this.cartList = res.data || []
  105. })
  106. },
  107. /** 修改购物车商品数量 */
  108. handleProductCountChange(productId, number) {
  109. this.$store.dispatch('CartProductCountChange', { productIds: [productId], productCount: number }).then(res => {
  110. this.cartList = res.data || []
  111. })
  112. },
  113. /** 移除购物车商品 */
  114. handleRemoveProduct() {
  115. if (this.checkedProduct < 1) {
  116. return
  117. }
  118. const productIds = this.checkedProduct.map(item => {
  119. return item.productId
  120. })
  121. uni.showModal({
  122. title: '确定要移除选中的商品?',
  123. cancelText: '取消',
  124. confirmText: '移除',
  125. success: res => {
  126. if (res.confirm) {
  127. this.$store.dispatch('CartProductCountChange', { productIds: productIds, productCount: 0 }).then(res => {
  128. this.cartList = res.data || []
  129. })
  130. } else if (res.cancel) {
  131. //console.log('用户点击取消')
  132. }
  133. }
  134. })
  135. },
  136. /** 购物车提交结算 */
  137. handleCheckoutProduct() {
  138. if (this.checkedProduct < 1) {
  139. return
  140. }
  141. const checkedProduct = this.checkedProduct.map(item => {
  142. return { productId: item.productId, productCount: item.productCount, sellPrice: item.sellPrice }
  143. })
  144. uni.$u.route('/pages/checkout/checkout', {
  145. checkedProduct: JSON.stringify(checkedProduct)
  146. })
  147. }
  148. }
  149. }
  150. </script>
  151. <style lang="scss" scoped>
  152. .login-tips-box {
  153. padding-top: 100rpx;
  154. .login-tips {
  155. @include flex-center;
  156. color: #939393;
  157. font-size: 24rpx;
  158. letter-spacing: 5rpx;
  159. }
  160. .login-link {
  161. width: 160rpx;
  162. height: 50rpx;
  163. line-height: 50rpx;
  164. border-radius: 50rpx;
  165. border: 1px solid #777;
  166. color: #777;
  167. text-align: center;
  168. }
  169. }
  170. .cart-btn-container {
  171. position: fixed;
  172. bottom: 0;
  173. left: 0;
  174. .btn-box {
  175. background: $custom-bg-color;
  176. border-top: $custom-border-style;
  177. width: 750rpx;
  178. @include flex-space-between();
  179. height: 100rpx;
  180. .product-check-info {
  181. @include flex-left;
  182. .check-all-btn {
  183. padding: 20rpx;
  184. .un-check-box {
  185. width: 20px;
  186. height: 20px;
  187. border: 1px solid #939393;
  188. border-radius: 50%;
  189. }
  190. }
  191. .info-text {
  192. font-size: 26rpx;
  193. font-weight: bold;
  194. color: #666666;
  195. }
  196. }
  197. .cart-btn-group {
  198. @include flex-right();
  199. width: 360rpx;
  200. padding-right: 10px;
  201. .btn-gap {
  202. width: 20rpx;
  203. }
  204. }
  205. }
  206. }
  207. </style>