cart.vue 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. <template>
  2. <view class="cart" :style="{ paddingTop: CustomBar + 'px' }">
  3. <header-cart
  4. :systeminfo="systeminfo"
  5. :navbar-data="nvabarData"
  6. :headerBtnPosi="headerBtnPosi"
  7. :page="1"
  8. ></header-cart>
  9. <template v-if="!isEmpty">
  10. <template v-if="listType !== 'delete'">
  11. <!-- 商品统计 / 批量删除商品 -->
  12. <view class="cart-top">
  13. <view class="count">共{{ kindCount }}件商品</view>
  14. <view class="btn" @click="showDelManager">删除</view>
  15. </view>
  16. <!-- TODO -->
  17. <view class="receive-coupon">
  18. <view class="tip-text">还差¥200可用“满200元减50元”优惠券</view>
  19. <view class="btn" @click="couponVisible = true">领券</view>
  20. </view>
  21. </template>
  22. <!-- 购物车列表 -->
  23. <cm-cart-product
  24. v-for="(cart, index) in goodsList"
  25. class="product-list"
  26. :listType="listType"
  27. :key="index"
  28. :data="cart"
  29. vkey="productList"
  30. @chooseAll="chooseAll"
  31. @chooseOne="chooseOne"
  32. ></cm-cart-product>
  33. <!-- 失效商品列表 -->
  34. <cm-cart-product class="product-list" :key="index" :data="failureList" listType="expired"></cm-cart-product>
  35. <!-- 底部按钮 -->
  36. <view class="footer" :style="[footerBottom]">
  37. <view class="radio" @click="checkAll">
  38. <text class="iconfont" :class="[isCheckAll ? 'icon-xuanze' : 'icon-weixuanze']"></text>
  39. <text class="tip">全选</text>
  40. </view>
  41. <template v-if="!isshowDelbtn">
  42. <view class="center">
  43. <view class="row">
  44. <text>总价:</text> <text class="total-price">¥{{ allPrice | NumFormat }}</text>
  45. </view>
  46. <!-- TODO -->
  47. <view class="row">
  48. <text>共减</text> <text class="discounted-price">¥200.00</text>
  49. <text @click="showDiscountedDetail">优惠明细</text>
  50. <text
  51. class="iconfont"
  52. :class="showDitail ? 'tui-icon-arrowdown' : 'tui-icon-arrowup'"
  53. ></text>
  54. </view>
  55. </view>
  56. <view class="submit" @click="toConfirmation">去结算({{ allCount }})</view>
  57. </template>
  58. <view v-else class="footer-del">
  59. <view class="btn btn-cancel" @tap.stop="hideDelManage">取消</view>
  60. <view class="btn btn-confirm" @tap.stop="deleteList">删除</view>
  61. </view>
  62. </view>
  63. </template>
  64. <view class="empty" v-else>
  65. <cm-empty :image="StaticUrl + 'icon-empty-cart.png'" message="购物车空空的,快去逛逛吧~"></cm-empty>
  66. </view>
  67. <!-- 优惠券列表 TODO-->
  68. <cm-coupon-list
  69. title="优惠券"
  70. listType="search"
  71. :visible="couponVisible"
  72. @close="closeCouponList"
  73. ></cm-coupon-list>
  74. <!-- 优惠明细 -->
  75. <cm-drawer
  76. title="优惠明细"
  77. :visible="showDitail"
  78. position="bottom"
  79. :offset="100"
  80. @close="showDitail = false"
  81. zIndex="99"
  82. >
  83. <template>
  84. <view class="discounted-ditail">
  85. <view class="row"> <text>商品总额</text> <text>¥450.00</text> </view>
  86. <view class="row"> <text>促销满减</text> <text class="red">-¥180.00</text> </view>
  87. <view class="row"> <text>优惠券</text> <text class="red">-¥20.00</text> </view>
  88. <view class="row total"> <text>总计</text> <text>¥250.00</text> </view>
  89. <view class="tip"> 实际订单金额以结算页为准 </view>
  90. </view>
  91. </template>
  92. </cm-drawer>
  93. <!-- 操作弹窗 -->
  94. <tui-modal
  95. :show="modal"
  96. @click="handleClick"
  97. @cancel="hideModal"
  98. :content="contentModalText"
  99. color="#333"
  100. :size="32"
  101. shape="circle"
  102. :maskClosable="false"
  103. ></tui-modal>
  104. </view>
  105. </template>
  106. <script>
  107. import authorize from '@/common/authorize.js'
  108. import CmCouponList from '@/components/cm-module/cm-coupon-list/cm-coupon-list'
  109. import CmDrawer from '@/components/cm-module/cm-drawer/cm-drawer.vue'
  110. import CmCartProduct from '@/components/cm-module/cm-cart-product/cm-cart-product.vue'
  111. import HeaderCart from '@/components/cm-module/headerNavbar/header-cart.vue'
  112. import CmEmpty from '@/components/cm-module/cm-empty/cm-empty.vue'
  113. import { mapGetters, mapActions, mapMutations } from 'vuex'
  114. export default {
  115. components: {
  116. CmCouponList,
  117. CmDrawer,
  118. CmCartProduct,
  119. HeaderCart,
  120. CmEmpty
  121. },
  122. data() {
  123. return {
  124. nvabarData: {
  125. //顶部自定义导航
  126. showCapsule: 1, // 是否显示左上角图标 1表示显示 0表示不显示,
  127. showSearch: 0,
  128. title: '购物车' // 导航栏 中间的标题
  129. },
  130. headerBtnPosi: this.setHeaderBtnPosi(), //获取设备顶部胶囊高度
  131. systeminfo: this.setSysteminfo(),
  132. StaticUrl: this.$Static,
  133. CustomBar: this.CustomBar, // 顶部导航栏高度
  134. popupShow: false,
  135. handlerPros: {}, //监听单挑促销商品
  136. isCheckAll: false, //是否全选
  137. allPrice: 0, //所有价格
  138. totalOriginalPrice: 0, //所有原价价
  139. reducedPrice: 0, //满减
  140. skeletonShow: true,
  141. isshowDelbtn: false,
  142. scrollHeight: 'auto',
  143. hasNextPage: false,
  144. modal: false,
  145. contentModalText: '',
  146. deleteType: 0,
  147. couponVisible: false,
  148. showDitail: false,
  149. listType: 'list'
  150. }
  151. },
  152. onLoad() {
  153. this.setScrollHeight()
  154. },
  155. computed: {
  156. ...mapGetters([
  157. 'hasLogin',
  158. 'isIphoneX',
  159. 'isEmpty',
  160. 'goodsList',
  161. 'failureList',
  162. 'kindCount',
  163. 'userId',
  164. 'cartIds'
  165. ]),
  166. //被选中的产品数量
  167. allCount() {
  168. return this.cartIds.length
  169. },
  170. footerBottom() {
  171. return {
  172. bottom: this.isIphoneX ? '0' : ''
  173. }
  174. }
  175. },
  176. filters: {
  177. NumFormat(value) {
  178. //处理金额
  179. return Number(value).toFixed(2)
  180. },
  181. totalprice(val, count) {
  182. //单件商品的价格 × 数量
  183. return (val * count).toFixed(2)
  184. }
  185. },
  186. watch: {
  187. //深度监听所有数据,每次改变重新计算总价和总数
  188. goodsList: {
  189. deep: true,
  190. handler(val, oldval) {
  191. this.totalPeice()
  192. }
  193. }
  194. },
  195. //下拉刷新
  196. onPullDownRefresh() {
  197. this.initCart().finally(() => {
  198. setTimeout(() => {
  199. uni.stopPullDownRefresh()
  200. }, 2000)
  201. })
  202. },
  203. onShow() {
  204. if (this.hasLogin) {
  205. this.initData()
  206. } else {
  207. this.$api.redirectTo('/pages/login/login')
  208. }
  209. },
  210. onHide() {
  211. this.saveCartIds([])
  212. },
  213. methods: {
  214. ...mapActions('cart', ['initCart', 'updateShoppogCount', 'removeFromCart', 'removeFailureFromCart']),
  215. ...mapMutations('cart', [
  216. 'selectProduct',
  217. 'selectAllShopProduct',
  218. 'selectAllProduct',
  219. 'saveCartIds',
  220. 'selectFailure',
  221. 'selectAllFailure'
  222. ]),
  223. initData() {
  224. this.initCart().finally(() => {
  225. this.skeletonShow = false
  226. })
  227. },
  228. // 关闭优惠券弹窗
  229. closeCouponList() {
  230. this.couponVisible = false
  231. },
  232. // 活动价弹窗
  233. clickPopupShow(pros, type) {
  234. if (pros.ladderList.length > 0) {
  235. this.popupShow = true
  236. this.handlerPros = pros
  237. }
  238. },
  239. // 优惠明细 TODO
  240. showDiscountedDetail() {
  241. this.showDitail = true
  242. },
  243. // 勾选单个失效商品
  244. ischeckFailure(failure) {
  245. this.selectFailure({
  246. productId: failure.productId,
  247. checked: !failure.productsChecked
  248. })
  249. this.getCheckedProductId()
  250. },
  251. // 勾选供应商下所有商品
  252. chooseAll() {
  253. this.isSelectAll()
  254. },
  255. // 勾选单核商品
  256. chooseOne() {
  257. this.isSelectAll()
  258. },
  259. // 勾选全部商品
  260. checkAll() {
  261. this.isCheckAll = !this.isCheckAll
  262. this.selectAllProduct(this.isCheckAll)
  263. // 删除商品的全选也要选中失效商品
  264. if (this.isshowDelbtn) {
  265. this.selectAllFailure(this.isCheckAll)
  266. }
  267. this.isSelectAll()
  268. },
  269. // 判断是否全选商品
  270. isSelectAll() {
  271. this.isCheckAll = this.goodsList.every(shop => shop.checked)
  272. this.getCheckedProductId()
  273. },
  274. // 获取勾选商品的id
  275. getCheckedProductId() {
  276. const cartIds = []
  277. this.goodsList.forEach(shop => {
  278. const ids = shop.productList.reduce((cartIds, prod) => {
  279. if (prod.productsChecked) cartIds.push(prod.cartId)
  280. return cartIds
  281. }, [])
  282. cartIds.push(...ids)
  283. })
  284. if (this.isshowDelbtn) {
  285. const ids = this.failureList.reduce((cartIds, prod) => {
  286. if (prod.productsChecked) cartIds.push(prod.cartId)
  287. return cartIds
  288. }, [])
  289. cartIds.push(...ids)
  290. }
  291. console.log(cartIds)
  292. this.saveCartIds(cartIds)
  293. },
  294. //计算总价格,每次调用此方法,将初始值为0,遍历价格并累加
  295. totalPeice() {
  296. const priceObj = this.goodsList.reduce(
  297. (priceObj, shop) => {
  298. shop.productList.forEach(prod => {
  299. if (prod.productsChecked) {
  300. priceObj.totalOriginalPrice += prod.price * prod.productCount
  301. // 单品满减
  302. if (prod.promotion && prod.promotion.type * 1 === 1 && prod.promotion.mode * 1 === 2) {
  303. // 单品满减-重新计算供应商总价/满减金额
  304. if (prod.price * prod.productCount >= prod.promotion.touchPrice) {
  305. priceObj.reducedPrice += prod.promotion.reducedPrice
  306. }
  307. }
  308. }
  309. })
  310. priceObj.allPrice = priceObj.totalOriginalPrice - priceObj.reducedPrice
  311. return priceObj
  312. },
  313. {
  314. totalOriginalPrice: 0,
  315. reducedPrice: 0,
  316. allPrice: 0
  317. }
  318. )
  319. this.allPrice = priceObj.allPrice
  320. },
  321. //商品数量加加
  322. changeCountAdd(item, pros) {
  323. const productCount = pros.productCount + 1
  324. this.updateShoppogCount({
  325. cartId: pros.cartId,
  326. productCount
  327. })
  328. },
  329. //商品数量减减
  330. changeCountSub(item, pros) {
  331. let productCount = pros.productCount
  332. if (productCount <= 1) {
  333. productCount = 1
  334. this.$util.msg('购买数量不能少于1', 2000)
  335. return
  336. } else {
  337. productCount--
  338. }
  339. this.updateShoppogCount({
  340. cartId: pros.cartId,
  341. productCount
  342. })
  343. },
  344. //输入商品数量更新
  345. changeInput(pros) {},
  346. //输入商品数量更新
  347. changeNumber(e, item, pros) {
  348. let _value = Math.abs(Number(e.detail.value))
  349. let productCount = pros.productCount
  350. if (_value <= 1) {
  351. this.$util.msg('购买数量不能少于1', 2000)
  352. productCount = 1
  353. } else {
  354. productCount = _value
  355. }
  356. this.updateShoppogCount({
  357. cartId: pros.cartId,
  358. productCount
  359. })
  360. },
  361. //显示删除商品管理
  362. showDelManager() {
  363. this.isshowDelbtn = true
  364. this.listType = 'delete'
  365. },
  366. //隐藏删除商品管理
  367. hideDelManage() {
  368. this.selectAllFailure(false)
  369. this.getCheckedProductId()
  370. this.isshowDelbtn = false
  371. this.listType = 'list'
  372. },
  373. //删除购物车商品
  374. deleteList() {
  375. if (this.cartIds.length <= 0) {
  376. return this.$util.msg('请选择要删除的商品~', 2000)
  377. } else {
  378. this.modal = true
  379. this.contentModalText = '确定删除选中的商品吗?'
  380. this.deleteType = 1
  381. }
  382. },
  383. // 清空失效商品
  384. deletefailureList() {
  385. this.modal = true
  386. this.contentModalText = '确定清除所有失效商品吗?'
  387. this.deleteType = 2
  388. },
  389. //从购物车移除商品
  390. handleClick(e) {
  391. if (e.index !== 1) return (this.modal = false)
  392. if (this.deleteType === 1) {
  393. // 删除所有勾选的商品
  394. this.removeFromCart().finally(() => {
  395. this.isshowDelbtn = false
  396. })
  397. } else {
  398. //一键删除失效商品
  399. this.removeFailureFromCart().finally(() => {
  400. this.isshowDelbtn = false
  401. this.listType = 'list'
  402. })
  403. }
  404. this.modal = false
  405. },
  406. //跳转确认订单页面
  407. toConfirmation() {
  408. if (this.cartIds.length <= 0) {
  409. return this.$util.msg('请先选择结算商品~', 2000)
  410. }
  411. let cartPramsData = {
  412. allPrice: this.allPrice,
  413. allCount: this.allCount,
  414. cartIds: this.cartIds.join(','),
  415. productCount: ''
  416. }
  417. this.$api.navigateTo(`/pages/user/order/create-order?data=${JSON.stringify({ data: cartPramsData })}`)
  418. },
  419. hideModal() {
  420. this.modal = false
  421. },
  422. navToListPage(item) {
  423. this.$api.navigateTo(`/pages/goods/product?productId=${item.productId}`)
  424. },
  425. // 窗口高度-footer高度
  426. setScrollHeight() {
  427. const { windowHeight, pixelRatio } = uni.getSystemInfoSync()
  428. setTimeout(() => {
  429. const query = uni.createSelectorQuery().in(this)
  430. query.selectAll('.footer').boundingClientRect()
  431. query.exec(res => {
  432. this.windowHeight = windowHeight
  433. if (res[0][0]) {
  434. this.scrollHeight = windowHeight - res[0][0].height
  435. }
  436. })
  437. }, 500)
  438. },
  439. setHeaderBtnPosi() {
  440. // 获得胶囊按钮位置信息
  441. let headerBtnPosi = uni.getMenuButtonBoundingClientRect()
  442. return headerBtnPosi
  443. },
  444. setSysteminfo() {
  445. let systeminfo
  446. uni.getSystemInfo({
  447. // 获取设备信息
  448. success: res => {
  449. systeminfo = res
  450. }
  451. })
  452. return systeminfo
  453. }
  454. }
  455. }
  456. </script>
  457. <style lang="scss" scoped>
  458. .cart {
  459. padding-bottom: 124rpx;
  460. background: #f7f7f7;
  461. min-height: 100%;
  462. box-sizing: border-box;
  463. .cart-top {
  464. display: flex;
  465. justify-content: space-between;
  466. align-items: center;
  467. padding: 0 24rpx;
  468. width: 750rpx;
  469. height: 80rpx;
  470. background: #f7f7f7;
  471. box-sizing: border-box;
  472. .count {
  473. font-size: 30rpx;
  474. color: #333333;
  475. }
  476. .btn {
  477. display: flex;
  478. justify-content: center;
  479. align-items: center;
  480. width: 88rpx;
  481. height: 42rpx;
  482. background: #fff8fd;
  483. border: 1rpx solid #ff457b;
  484. border-radius: 24rpx;
  485. font-size: 26rpx;
  486. color: #ff457b;
  487. }
  488. }
  489. .receive-coupon {
  490. display: flex;
  491. justify-content: space-between;
  492. align-items: center;
  493. padding: 24rpx;
  494. background: #fff;
  495. .tip-text {
  496. font-size: 26rpx;
  497. color: #ff457b;
  498. }
  499. .btn {
  500. width: 88rpx;
  501. height: 42rpx;
  502. background: linear-gradient(270deg, #f83c6c 0%, #fc32b4 100%);
  503. border-radius: 28rpx;
  504. font-size: 26rpx;
  505. color: #ffffff;
  506. text-align: center;
  507. line-height: 42rpx;
  508. }
  509. }
  510. .footer {
  511. position: fixed;
  512. bottom: -40rpx;
  513. left: 0;
  514. z-index: 899;
  515. display: flex;
  516. justify-content: space-between;
  517. align-items: center;
  518. width: 100%;
  519. height: 140rpx;
  520. box-sizing: border-box;
  521. padding: 0 24rpx 40rpx;
  522. border-top: 1rpx solid #eee;
  523. background: #ffffff;
  524. .radio {
  525. font-size: 36rpx;
  526. color: #b2b2b2;
  527. .icon-xuanze {
  528. color: #f83c6c;
  529. }
  530. .tip {
  531. font-size: 30rpx;
  532. color: #333333;
  533. margin-left: 6rpx;
  534. }
  535. }
  536. .row {
  537. &:nth-child(1) {
  538. font-size: 26rpx;
  539. color: #333333;
  540. .total-price {
  541. color: #ff457b;
  542. }
  543. }
  544. &:nth-child(2) {
  545. margin-top: 6rpx;
  546. font-size: 24rpx;
  547. color: #ff457b;
  548. }
  549. .discounted-price {
  550. margin-right: 32rpx;
  551. }
  552. }
  553. .submit {
  554. width: 210rpx;
  555. height: 80rpx;
  556. background: linear-gradient(90deg, #fc32b4 0%, #f83c6c 100%);
  557. border-radius: 40rpx;
  558. font-size: 30rpx;
  559. color: #ffffff;
  560. text-align: center;
  561. line-height: 80rpx;
  562. }
  563. .footer-del {
  564. width: 420rpx;
  565. height: 100rpx;
  566. position: absolute;
  567. padding-left: 200rpx;
  568. background: #ffffff;
  569. right: 0;
  570. top: 0;
  571. z-index: 1000;
  572. box-sizing: border-box;
  573. padding: 10rpx 0;
  574. display: flex;
  575. &.show {
  576. animation: showDelbtn 0s linear both;
  577. }
  578. &.none {
  579. animation: hideDelbtn 0s linear both;
  580. }
  581. .btn {
  582. flex: 1;
  583. margin: 0 8rpx;
  584. height: 100%;
  585. line-height: 80rpx;
  586. font-size: $font-size-28;
  587. color: #ffffff;
  588. text-align: center;
  589. float: left;
  590. border-radius: 40rpx;
  591. }
  592. .btn.btn-cancel {
  593. background: #f7f7f7;
  594. color: #b2b2b2;
  595. }
  596. .btn.btn-confirm {
  597. background: $btn-confirm;
  598. color: #ffffff;
  599. }
  600. @keyframes showDelbtn {
  601. 0% {
  602. transform: translateX(0);
  603. }
  604. 100% {
  605. transform: translateX(-100%);
  606. }
  607. }
  608. @keyframes hideDelbtn {
  609. 0% {
  610. transform: translateX(-100%);
  611. }
  612. 100% {
  613. transform: translateX(0);
  614. }
  615. }
  616. }
  617. }
  618. .discounted-ditail {
  619. padding: 52rpx 8rpx 0;
  620. .row {
  621. display: flex;
  622. justify-content: space-between;
  623. align-items: center;
  624. padding: 12rpx 0;
  625. text {
  626. font-size: 30rpx;
  627. color: #333333;
  628. &.red {
  629. color: #f94b4b;
  630. }
  631. }
  632. &.total {
  633. font-size: 30rpx;
  634. font-weight: 600;
  635. }
  636. }
  637. .tip {
  638. padding: 12rpx 0;
  639. font-size: 26rpx;
  640. color: #999999;
  641. }
  642. }
  643. .empty{
  644. height: 80vh;
  645. }
  646. }
  647. </style>